kvm: configure: run kernel configure even with --with-patched-kernel
[qemu-kvm/fedora.git] / block-raw-posix.c
blob6d617bb3afc3767a7c2e8d77f854b7f17fee092e
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,
600 int64_t sector_num, uint8_t *buf, 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_buf = buf;
615 if (nb_sectors < 0)
616 acb->aiocb.aio_nbytes = -nb_sectors;
617 else
618 acb->aiocb.aio_nbytes = nb_sectors * 512;
619 acb->aiocb.aio_offset = sector_num * 512;
620 acb->next = posix_aio_state->first_aio;
621 posix_aio_state->first_aio = acb;
622 return acb;
625 static void raw_aio_em_cb(void* opaque)
627 RawAIOCB *acb = opaque;
628 acb->common.cb(acb->common.opaque, acb->ret);
629 qemu_aio_release(acb);
632 static void raw_aio_remove(RawAIOCB *acb)
634 RawAIOCB **pacb;
636 /* remove the callback from the queue */
637 pacb = &posix_aio_state->first_aio;
638 for(;;) {
639 if (*pacb == NULL) {
640 fprintf(stderr, "raw_aio_remove: aio request not found!\n");
641 break;
642 } else if (*pacb == acb) {
643 *pacb = acb->next;
644 qemu_aio_release(acb);
645 break;
647 pacb = &(*pacb)->next;
651 static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
652 int64_t sector_num, uint8_t *buf, int nb_sectors,
653 BlockDriverCompletionFunc *cb, void *opaque)
655 RawAIOCB *acb;
658 * If O_DIRECT is used and the buffer is not aligned fall back
659 * to synchronous IO.
661 BDRVRawState *s = bs->opaque;
663 if (unlikely(s->aligned_buf != NULL && ((uintptr_t) buf % 512))) {
664 QEMUBH *bh;
665 acb = qemu_aio_get(bs, cb, opaque);
666 acb->ret = raw_pread(bs, 512 * sector_num, buf, 512 * nb_sectors);
667 bh = qemu_bh_new(raw_aio_em_cb, acb);
668 qemu_bh_schedule(bh);
669 return &acb->common;
672 acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
673 if (!acb)
674 return NULL;
675 if (qemu_paio_read(&acb->aiocb) < 0) {
676 raw_aio_remove(acb);
677 return NULL;
679 return &acb->common;
682 static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
683 int64_t sector_num, const uint8_t *buf, int nb_sectors,
684 BlockDriverCompletionFunc *cb, void *opaque)
686 RawAIOCB *acb;
689 * If O_DIRECT is used and the buffer is not aligned fall back
690 * to synchronous IO.
692 BDRVRawState *s = bs->opaque;
694 if (unlikely(s->aligned_buf != NULL && ((uintptr_t) buf % 512))) {
695 QEMUBH *bh;
696 acb = qemu_aio_get(bs, cb, opaque);
697 acb->ret = raw_pwrite(bs, 512 * sector_num, buf, 512 * nb_sectors);
698 bh = qemu_bh_new(raw_aio_em_cb, acb);
699 qemu_bh_schedule(bh);
700 return &acb->common;
703 acb = raw_aio_setup(bs, sector_num, (uint8_t*)buf, nb_sectors, cb, opaque);
704 if (!acb)
705 return NULL;
706 if (qemu_paio_write(&acb->aiocb) < 0) {
707 raw_aio_remove(acb);
708 return NULL;
710 return &acb->common;
713 static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
715 int ret;
716 RawAIOCB *acb = (RawAIOCB *)blockacb;
718 ret = qemu_paio_cancel(acb->aiocb.aio_fildes, &acb->aiocb);
719 if (ret == QEMU_PAIO_NOTCANCELED) {
720 /* fail safe: if the aio could not be canceled, we wait for
721 it */
722 while (qemu_paio_error(&acb->aiocb) == EINPROGRESS);
725 raw_aio_remove(acb);
727 #else /* CONFIG_AIO */
728 static int posix_aio_init(void)
730 return 0;
732 #endif /* CONFIG_AIO */
735 static void raw_close(BlockDriverState *bs)
737 BDRVRawState *s = bs->opaque;
738 if (s->fd >= 0) {
739 close(s->fd);
740 s->fd = -1;
741 if (s->aligned_buf != NULL)
742 qemu_free(s->aligned_buf);
746 static int raw_truncate(BlockDriverState *bs, int64_t offset)
748 BDRVRawState *s = bs->opaque;
749 if (s->type != FTYPE_FILE)
750 return -ENOTSUP;
751 if (ftruncate(s->fd, offset) < 0)
752 return -errno;
753 return 0;
756 #ifdef __OpenBSD__
757 static int64_t raw_getlength(BlockDriverState *bs)
759 BDRVRawState *s = bs->opaque;
760 int fd = s->fd;
761 struct stat st;
763 if (fstat(fd, &st))
764 return -1;
765 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
766 struct disklabel dl;
768 if (ioctl(fd, DIOCGDINFO, &dl))
769 return -1;
770 return (uint64_t)dl.d_secsize *
771 dl.d_partitions[DISKPART(st.st_rdev)].p_size;
772 } else
773 return st.st_size;
775 #else /* !__OpenBSD__ */
776 static int64_t raw_getlength(BlockDriverState *bs)
778 BDRVRawState *s = bs->opaque;
779 int fd = s->fd;
780 int64_t size;
781 #ifdef HOST_BSD
782 struct stat sb;
783 #ifdef __FreeBSD__
784 int reopened = 0;
785 #endif
786 #endif
787 #ifdef __sun__
788 struct dk_minfo minfo;
789 int rv;
790 #endif
791 int ret;
793 ret = fd_open(bs);
794 if (ret < 0)
795 return ret;
797 #ifdef HOST_BSD
798 #ifdef __FreeBSD__
799 again:
800 #endif
801 if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
802 #ifdef DIOCGMEDIASIZE
803 if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
804 #elif defined(DIOCGPART)
806 struct partinfo pi;
807 if (ioctl(fd, DIOCGPART, &pi) == 0)
808 size = pi.media_size;
809 else
810 size = 0;
812 if (size == 0)
813 #endif
814 #ifdef CONFIG_COCOA
815 size = LONG_LONG_MAX;
816 #else
817 size = lseek(fd, 0LL, SEEK_END);
818 #endif
819 #ifdef __FreeBSD__
820 switch(s->type) {
821 case FTYPE_CD:
822 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
823 if (size == 2048LL * (unsigned)-1)
824 size = 0;
825 /* XXX no disc? maybe we need to reopen... */
826 if (size <= 0 && !reopened && cd_open(bs) >= 0) {
827 reopened = 1;
828 goto again;
831 #endif
832 } else
833 #endif
834 #ifdef __sun__
836 * use the DKIOCGMEDIAINFO ioctl to read the size.
838 rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo );
839 if ( rv != -1 ) {
840 size = minfo.dki_lbsize * minfo.dki_capacity;
841 } else /* there are reports that lseek on some devices
842 fails, but irc discussion said that contingency
843 on contingency was overkill */
844 #endif
846 size = lseek(fd, 0, SEEK_END);
848 return size;
850 #endif
852 static int raw_create(const char *filename, int64_t total_size,
853 const char *backing_file, int flags)
855 int fd;
857 if (flags || backing_file)
858 return -ENOTSUP;
860 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
861 0644);
862 if (fd < 0)
863 return -EIO;
864 ftruncate(fd, total_size * 512);
865 close(fd);
866 return 0;
869 static void raw_flush(BlockDriverState *bs)
871 BDRVRawState *s = bs->opaque;
872 fsync(s->fd);
875 BlockDriver bdrv_raw = {
876 "raw",
877 sizeof(BDRVRawState),
878 NULL, /* no probe for protocols */
879 raw_open,
880 NULL,
881 NULL,
882 raw_close,
883 raw_create,
884 raw_flush,
886 #ifdef CONFIG_AIO
887 .bdrv_aio_read = raw_aio_read,
888 .bdrv_aio_write = raw_aio_write,
889 .bdrv_aio_cancel = raw_aio_cancel,
890 .aiocb_size = sizeof(RawAIOCB),
891 #endif
893 .bdrv_read = raw_read,
894 .bdrv_write = raw_write,
895 .bdrv_truncate = raw_truncate,
896 .bdrv_getlength = raw_getlength,
899 /***********************************************/
900 /* host device */
902 #ifdef CONFIG_COCOA
903 static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
904 static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
906 kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
908 kern_return_t kernResult;
909 mach_port_t masterPort;
910 CFMutableDictionaryRef classesToMatch;
912 kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
913 if ( KERN_SUCCESS != kernResult ) {
914 printf( "IOMasterPort returned %d\n", kernResult );
917 classesToMatch = IOServiceMatching( kIOCDMediaClass );
918 if ( classesToMatch == NULL ) {
919 printf( "IOServiceMatching returned a NULL dictionary.\n" );
920 } else {
921 CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
923 kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
924 if ( KERN_SUCCESS != kernResult )
926 printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
929 return kernResult;
932 kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
934 io_object_t nextMedia;
935 kern_return_t kernResult = KERN_FAILURE;
936 *bsdPath = '\0';
937 nextMedia = IOIteratorNext( mediaIterator );
938 if ( nextMedia )
940 CFTypeRef bsdPathAsCFString;
941 bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
942 if ( bsdPathAsCFString ) {
943 size_t devPathLength;
944 strcpy( bsdPath, _PATH_DEV );
945 strcat( bsdPath, "r" );
946 devPathLength = strlen( bsdPath );
947 if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
948 kernResult = KERN_SUCCESS;
950 CFRelease( bsdPathAsCFString );
952 IOObjectRelease( nextMedia );
955 return kernResult;
958 #endif
960 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
962 BDRVRawState *s = bs->opaque;
963 int fd, open_flags, ret;
965 posix_aio_init();
967 #ifdef CONFIG_COCOA
968 if (strstart(filename, "/dev/cdrom", NULL)) {
969 kern_return_t kernResult;
970 io_iterator_t mediaIterator;
971 char bsdPath[ MAXPATHLEN ];
972 int fd;
974 kernResult = FindEjectableCDMedia( &mediaIterator );
975 kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
977 if ( bsdPath[ 0 ] != '\0' ) {
978 strcat(bsdPath,"s0");
979 /* some CDs don't have a partition 0 */
980 fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
981 if (fd < 0) {
982 bsdPath[strlen(bsdPath)-1] = '1';
983 } else {
984 close(fd);
986 filename = bsdPath;
989 if ( mediaIterator )
990 IOObjectRelease( mediaIterator );
992 #endif
993 open_flags = O_BINARY;
994 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
995 open_flags |= O_RDWR;
996 } else {
997 open_flags |= O_RDONLY;
998 bs->read_only = 1;
1000 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
1001 * and O_DIRECT for no caching. */
1002 if ((flags & BDRV_O_NOCACHE))
1003 open_flags |= O_DIRECT;
1004 else if (!(flags & BDRV_O_CACHE_WB))
1005 open_flags |= O_DSYNC;
1007 s->type = FTYPE_FILE;
1008 #if defined(__linux__)
1009 if (strstart(filename, "/dev/cd", NULL)) {
1010 /* open will not fail even if no CD is inserted */
1011 open_flags |= O_NONBLOCK;
1012 s->type = FTYPE_CD;
1013 } else if (strstart(filename, "/dev/fd", NULL)) {
1014 s->type = FTYPE_FD;
1015 s->fd_open_flags = open_flags;
1016 /* open will not fail even if no floppy is inserted */
1017 open_flags |= O_NONBLOCK;
1018 } else if (strstart(filename, "/dev/sg", NULL)) {
1019 bs->sg = 1;
1021 #endif
1022 #if defined(__FreeBSD__)
1023 if (strstart(filename, "/dev/cd", NULL) ||
1024 strstart(filename, "/dev/acd", NULL)) {
1025 s->type = FTYPE_CD;
1026 s->cd_open_flags = open_flags;
1028 #endif
1029 s->fd = -1;
1030 fd = open(filename, open_flags, 0644);
1031 if (fd < 0) {
1032 ret = -errno;
1033 if (ret == -EROFS)
1034 ret = -EACCES;
1035 return ret;
1037 s->fd = fd;
1038 #if defined(__FreeBSD__)
1039 /* make sure the door isnt locked at this time */
1040 if (s->type == FTYPE_CD)
1041 ioctl (s->fd, CDIOCALLOW);
1042 #endif
1043 #if defined(__linux__)
1044 /* close fd so that we can reopen it as needed */
1045 if (s->type == FTYPE_FD) {
1046 close(s->fd);
1047 s->fd = -1;
1048 s->fd_media_changed = 1;
1050 #endif
1051 return 0;
1054 #if defined(__linux__)
1055 /* Note: we do not have a reliable method to detect if the floppy is
1056 present. The current method is to try to open the floppy at every
1057 I/O and to keep it opened during a few hundreds of ms. */
1058 static int fd_open(BlockDriverState *bs)
1060 BDRVRawState *s = bs->opaque;
1061 int last_media_present;
1063 if (s->type != FTYPE_FD)
1064 return 0;
1065 last_media_present = (s->fd >= 0);
1066 if (s->fd >= 0 &&
1067 (qemu_get_clock(rt_clock) - s->fd_open_time) >= FD_OPEN_TIMEOUT) {
1068 close(s->fd);
1069 s->fd = -1;
1070 #ifdef DEBUG_FLOPPY
1071 printf("Floppy closed\n");
1072 #endif
1074 if (s->fd < 0) {
1075 if (s->fd_got_error &&
1076 (qemu_get_clock(rt_clock) - s->fd_error_time) < FD_OPEN_TIMEOUT) {
1077 #ifdef DEBUG_FLOPPY
1078 printf("No floppy (open delayed)\n");
1079 #endif
1080 return -EIO;
1082 s->fd = open(bs->filename, s->fd_open_flags);
1083 if (s->fd < 0) {
1084 s->fd_error_time = qemu_get_clock(rt_clock);
1085 s->fd_got_error = 1;
1086 if (last_media_present)
1087 s->fd_media_changed = 1;
1088 #ifdef DEBUG_FLOPPY
1089 printf("No floppy\n");
1090 #endif
1091 return -EIO;
1093 #ifdef DEBUG_FLOPPY
1094 printf("Floppy opened\n");
1095 #endif
1097 if (!last_media_present)
1098 s->fd_media_changed = 1;
1099 s->fd_open_time = qemu_get_clock(rt_clock);
1100 s->fd_got_error = 0;
1101 return 0;
1104 static int raw_is_inserted(BlockDriverState *bs)
1106 BDRVRawState *s = bs->opaque;
1107 int ret;
1109 switch(s->type) {
1110 case FTYPE_CD:
1111 ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
1112 if (ret == CDS_DISC_OK)
1113 return 1;
1114 else
1115 return 0;
1116 break;
1117 case FTYPE_FD:
1118 ret = fd_open(bs);
1119 return (ret >= 0);
1120 default:
1121 return 1;
1125 /* currently only used by fdc.c, but a CD version would be good too */
1126 static int raw_media_changed(BlockDriverState *bs)
1128 BDRVRawState *s = bs->opaque;
1130 switch(s->type) {
1131 case FTYPE_FD:
1133 int ret;
1134 /* XXX: we do not have a true media changed indication. It
1135 does not work if the floppy is changed without trying
1136 to read it */
1137 fd_open(bs);
1138 ret = s->fd_media_changed;
1139 s->fd_media_changed = 0;
1140 #ifdef DEBUG_FLOPPY
1141 printf("Floppy changed=%d\n", ret);
1142 #endif
1143 return ret;
1145 default:
1146 return -ENOTSUP;
1150 static int raw_eject(BlockDriverState *bs, int eject_flag)
1152 BDRVRawState *s = bs->opaque;
1154 switch(s->type) {
1155 case FTYPE_CD:
1156 if (eject_flag) {
1157 if (ioctl (s->fd, CDROMEJECT, NULL) < 0)
1158 perror("CDROMEJECT");
1159 } else {
1160 if (ioctl (s->fd, CDROMCLOSETRAY, NULL) < 0)
1161 perror("CDROMEJECT");
1163 break;
1164 case FTYPE_FD:
1166 int fd;
1167 if (s->fd >= 0) {
1168 close(s->fd);
1169 s->fd = -1;
1171 fd = open(bs->filename, s->fd_open_flags | O_NONBLOCK);
1172 if (fd >= 0) {
1173 if (ioctl(fd, FDEJECT, 0) < 0)
1174 perror("FDEJECT");
1175 close(fd);
1178 break;
1179 default:
1180 return -ENOTSUP;
1182 return 0;
1185 static int raw_set_locked(BlockDriverState *bs, int locked)
1187 BDRVRawState *s = bs->opaque;
1189 switch(s->type) {
1190 case FTYPE_CD:
1191 if (ioctl (s->fd, CDROM_LOCKDOOR, locked) < 0) {
1192 /* Note: an error can happen if the distribution automatically
1193 mounts the CD-ROM */
1194 // perror("CDROM_LOCKDOOR");
1196 break;
1197 default:
1198 return -ENOTSUP;
1200 return 0;
1203 static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1205 BDRVRawState *s = bs->opaque;
1207 return ioctl(s->fd, req, buf);
1210 static BlockDriverAIOCB *raw_aio_ioctl(BlockDriverState *bs,
1211 unsigned long int req, void *buf,
1212 BlockDriverCompletionFunc *cb, void *opaque)
1214 RawAIOCB *acb;
1216 acb = raw_aio_setup(bs, 0, buf, 0, cb, opaque);
1217 if (!acb)
1218 return NULL;
1220 acb->aiocb.aio_ioctl_cmd = req;
1221 if (qemu_paio_ioctl(&acb->aiocb) < 0) {
1222 raw_aio_remove(acb);
1223 return NULL;
1226 return &acb->common;
1229 #elif defined(__FreeBSD__)
1231 static int fd_open(BlockDriverState *bs)
1233 BDRVRawState *s = bs->opaque;
1235 /* this is just to ensure s->fd is sane (its called by io ops) */
1236 if (s->fd >= 0)
1237 return 0;
1238 return -EIO;
1241 static int cd_open(BlockDriverState *bs)
1243 #if defined(__FreeBSD__)
1244 BDRVRawState *s = bs->opaque;
1245 int fd;
1247 switch(s->type) {
1248 case FTYPE_CD:
1249 /* XXX force reread of possibly changed/newly loaded disc,
1250 * FreeBSD seems to not notice sometimes... */
1251 if (s->fd >= 0)
1252 close (s->fd);
1253 fd = open(bs->filename, s->cd_open_flags, 0644);
1254 if (fd < 0) {
1255 s->fd = -1;
1256 return -EIO;
1258 s->fd = fd;
1259 /* make sure the door isnt locked at this time */
1260 ioctl (s->fd, CDIOCALLOW);
1262 #endif
1263 return 0;
1266 static int raw_is_inserted(BlockDriverState *bs)
1268 BDRVRawState *s = bs->opaque;
1270 switch(s->type) {
1271 case FTYPE_CD:
1272 return (raw_getlength(bs) > 0);
1273 case FTYPE_FD:
1274 /* XXX handle this */
1275 /* FALLTHRU */
1276 default:
1277 return 1;
1281 static int raw_media_changed(BlockDriverState *bs)
1283 return -ENOTSUP;
1286 static int raw_eject(BlockDriverState *bs, int eject_flag)
1288 BDRVRawState *s = bs->opaque;
1290 switch(s->type) {
1291 case FTYPE_CD:
1292 if (s->fd < 0)
1293 return -ENOTSUP;
1294 (void) ioctl (s->fd, CDIOCALLOW);
1295 if (eject_flag) {
1296 if (ioctl (s->fd, CDIOCEJECT) < 0)
1297 perror("CDIOCEJECT");
1298 } else {
1299 if (ioctl (s->fd, CDIOCCLOSE) < 0)
1300 perror("CDIOCCLOSE");
1302 if (cd_open(bs) < 0)
1303 return -ENOTSUP;
1304 break;
1305 case FTYPE_FD:
1306 /* XXX handle this */
1307 /* FALLTHRU */
1308 default:
1309 return -ENOTSUP;
1311 return 0;
1314 static int raw_set_locked(BlockDriverState *bs, int locked)
1316 BDRVRawState *s = bs->opaque;
1318 switch(s->type) {
1319 case FTYPE_CD:
1320 if (s->fd < 0)
1321 return -ENOTSUP;
1322 if (ioctl (s->fd, (locked ? CDIOCPREVENT : CDIOCALLOW)) < 0) {
1323 /* Note: an error can happen if the distribution automatically
1324 mounts the CD-ROM */
1325 // perror("CDROM_LOCKDOOR");
1327 break;
1328 default:
1329 return -ENOTSUP;
1331 return 0;
1334 static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1336 return -ENOTSUP;
1338 #else /* !linux && !FreeBSD */
1340 static int fd_open(BlockDriverState *bs)
1342 return 0;
1345 static int raw_is_inserted(BlockDriverState *bs)
1347 return 1;
1350 static int raw_media_changed(BlockDriverState *bs)
1352 return -ENOTSUP;
1355 static int raw_eject(BlockDriverState *bs, int eject_flag)
1357 return -ENOTSUP;
1360 static int raw_set_locked(BlockDriverState *bs, int locked)
1362 return -ENOTSUP;
1365 static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1367 return -ENOTSUP;
1370 static BlockDriverAIOCB *raw_aio_ioctl(BlockDriverState *bs,
1371 unsigned long int req, void *buf,
1372 BlockDriverCompletionFunc *cb, void *opaque)
1374 return NULL;
1376 #endif /* !linux && !FreeBSD */
1378 BlockDriver bdrv_host_device = {
1379 .format_name = "host_device",
1380 .instance_size = sizeof(BDRVRawState),
1381 .bdrv_open = hdev_open,
1382 .bdrv_close = raw_close,
1383 .bdrv_flush = raw_flush,
1385 #ifdef CONFIG_AIO
1386 .bdrv_aio_read = raw_aio_read,
1387 .bdrv_aio_write = raw_aio_write,
1388 .bdrv_aio_cancel = raw_aio_cancel,
1389 .aiocb_size = sizeof(RawAIOCB),
1390 #endif
1392 .bdrv_read = raw_read,
1393 .bdrv_write = raw_write,
1394 .bdrv_getlength = raw_getlength,
1396 /* removable device support */
1397 .bdrv_is_inserted = raw_is_inserted,
1398 .bdrv_media_changed = raw_media_changed,
1399 .bdrv_eject = raw_eject,
1400 .bdrv_set_locked = raw_set_locked,
1401 /* generic scsi device */
1402 .bdrv_ioctl = raw_ioctl,
1403 .bdrv_aio_ioctl = raw_aio_ioctl,