Get rid of sys/poll.h
[qemu/qemu-JZ.git] / block-raw-posix.c
blob83a358cd4dd45f2eda72049568b927d5753cc197
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 <assert.h>
29 #ifdef CONFIG_AIO
30 #include <aio.h>
31 #endif
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
45 #ifdef __sun__
46 #define _POSIX_PTHREAD_SEMANTICS 1
47 #include <signal.h>
48 #include <sys/dkio.h>
49 #endif
50 #ifdef __linux__
51 #include <sys/ioctl.h>
52 #include <linux/cdrom.h>
53 #include <linux/fd.h>
54 #endif
55 #ifdef __FreeBSD__
56 #include <signal.h>
57 #include <sys/disk.h>
58 #endif
60 #ifdef __OpenBSD__
61 #include <sys/ioctl.h>
62 #include <sys/disklabel.h>
63 #include <sys/dkio.h>
64 #endif
66 //#define DEBUG_FLOPPY
68 //#define DEBUG_BLOCK
69 #if defined(DEBUG_BLOCK)
70 #define DEBUG_BLOCK_PRINT(formatCstr, args...) do { if (loglevel != 0) \
71 { fprintf(logfile, formatCstr, ##args); fflush(logfile); } } while (0)
72 #else
73 #define DEBUG_BLOCK_PRINT(formatCstr, args...)
74 #endif
76 #define FTYPE_FILE 0
77 #define FTYPE_CD 1
78 #define FTYPE_FD 2
80 #define ALIGNED_BUFFER_SIZE (32 * 512)
82 /* if the FD is not accessed during that time (in ms), we try to
83 reopen it to see if the disk has been changed */
84 #define FD_OPEN_TIMEOUT 1000
86 /* posix-aio doesn't allow multiple outstanding requests to a single file
87 * descriptor. we implement a pool of dup()'d file descriptors to work
88 * around this */
89 #define RAW_FD_POOL_SIZE 64
91 typedef struct BDRVRawState {
92 int fd;
93 int type;
94 unsigned int lseek_err_cnt;
95 int fd_pool[RAW_FD_POOL_SIZE];
96 #if defined(__linux__)
97 /* linux floppy specific */
98 int fd_open_flags;
99 int64_t fd_open_time;
100 int64_t fd_error_time;
101 int fd_got_error;
102 int fd_media_changed;
103 #endif
104 #if defined(O_DIRECT)
105 uint8_t* aligned_buf;
106 #endif
107 } BDRVRawState;
109 static int posix_aio_init(void);
111 static int fd_open(BlockDriverState *bs);
113 static int raw_open(BlockDriverState *bs, const char *filename, int flags)
115 BDRVRawState *s = bs->opaque;
116 int fd, open_flags, ret;
117 int i;
119 posix_aio_init();
121 s->lseek_err_cnt = 0;
123 open_flags = O_BINARY;
124 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
125 open_flags |= O_RDWR;
126 } else {
127 open_flags |= O_RDONLY;
128 bs->read_only = 1;
130 if (flags & BDRV_O_CREAT)
131 open_flags |= O_CREAT | O_TRUNC;
132 #ifdef O_DIRECT
133 if (flags & BDRV_O_DIRECT)
134 open_flags |= O_DIRECT;
135 #endif
137 s->type = FTYPE_FILE;
139 fd = open(filename, open_flags, 0644);
140 if (fd < 0) {
141 ret = -errno;
142 if (ret == -EROFS)
143 ret = -EACCES;
144 return ret;
146 s->fd = fd;
147 for (i = 0; i < RAW_FD_POOL_SIZE; i++)
148 s->fd_pool[i] = -1;
149 #if defined(O_DIRECT)
150 s->aligned_buf = NULL;
151 if (flags & BDRV_O_DIRECT) {
152 s->aligned_buf = qemu_memalign(512, ALIGNED_BUFFER_SIZE);
153 if (s->aligned_buf == NULL) {
154 ret = -errno;
155 close(fd);
156 return ret;
159 #endif
160 return 0;
163 /* XXX: use host sector size if necessary with:
164 #ifdef DIOCGSECTORSIZE
166 unsigned int sectorsize = 512;
167 if (!ioctl(fd, DIOCGSECTORSIZE, &sectorsize) &&
168 sectorsize > bufsize)
169 bufsize = sectorsize;
171 #endif
172 #ifdef CONFIG_COCOA
173 u_int32_t blockSize = 512;
174 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
175 bufsize = blockSize;
177 #endif
181 * offset and count are in bytes, but must be multiples of 512 for files
182 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
184 * This function may be called without alignment if the caller ensures
185 * that O_DIRECT is not in effect.
187 static int raw_pread_aligned(BlockDriverState *bs, int64_t offset,
188 uint8_t *buf, int count)
190 BDRVRawState *s = bs->opaque;
191 int ret;
193 ret = fd_open(bs);
194 if (ret < 0)
195 return ret;
197 if (offset >= 0 && lseek(s->fd, offset, SEEK_SET) == (off_t)-1) {
198 ++(s->lseek_err_cnt);
199 if(s->lseek_err_cnt <= 10) {
200 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
201 "] lseek failed : %d = %s\n",
202 s->fd, bs->filename, offset, buf, count,
203 bs->total_sectors, errno, strerror(errno));
205 return -1;
207 s->lseek_err_cnt=0;
209 ret = read(s->fd, buf, count);
210 if (ret == count)
211 goto label__raw_read__success;
213 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
214 "] read failed %d : %d = %s\n",
215 s->fd, bs->filename, offset, buf, count,
216 bs->total_sectors, ret, errno, strerror(errno));
218 /* Try harder for CDrom. */
219 if (bs->type == BDRV_TYPE_CDROM) {
220 lseek(s->fd, offset, SEEK_SET);
221 ret = read(s->fd, buf, count);
222 if (ret == count)
223 goto label__raw_read__success;
224 lseek(s->fd, offset, SEEK_SET);
225 ret = read(s->fd, buf, count);
226 if (ret == count)
227 goto label__raw_read__success;
229 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
230 "] retry read failed %d : %d = %s\n",
231 s->fd, bs->filename, offset, buf, count,
232 bs->total_sectors, ret, errno, strerror(errno));
235 label__raw_read__success:
237 return ret;
241 * offset and count are in bytes, but must be multiples of 512 for files
242 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
244 * This function may be called without alignment if the caller ensures
245 * that O_DIRECT is not in effect.
247 static int raw_pwrite_aligned(BlockDriverState *bs, int64_t offset,
248 const uint8_t *buf, int count)
250 BDRVRawState *s = bs->opaque;
251 int ret;
253 ret = fd_open(bs);
254 if (ret < 0)
255 return ret;
257 if (offset >= 0 && lseek(s->fd, offset, SEEK_SET) == (off_t)-1) {
258 ++(s->lseek_err_cnt);
259 if(s->lseek_err_cnt) {
260 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%"
261 PRId64 "] lseek failed : %d = %s\n",
262 s->fd, bs->filename, offset, buf, count,
263 bs->total_sectors, errno, strerror(errno));
265 return -1;
267 s->lseek_err_cnt = 0;
269 ret = write(s->fd, buf, count);
270 if (ret == count)
271 goto label__raw_write__success;
273 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
274 "] write failed %d : %d = %s\n",
275 s->fd, bs->filename, offset, buf, count,
276 bs->total_sectors, ret, errno, strerror(errno));
278 label__raw_write__success:
280 return ret;
284 #if defined(O_DIRECT)
286 * offset and count are in bytes and possibly not aligned. For files opened
287 * with O_DIRECT, necessary alignments are ensured before calling
288 * raw_pread_aligned to do the actual read.
290 static int raw_pread(BlockDriverState *bs, int64_t offset,
291 uint8_t *buf, int count)
293 BDRVRawState *s = bs->opaque;
294 int size, ret, shift, sum;
296 sum = 0;
298 if (s->aligned_buf != NULL) {
300 if (offset & 0x1ff) {
301 /* align offset on a 512 bytes boundary */
303 shift = offset & 0x1ff;
304 size = (shift + count + 0x1ff) & ~0x1ff;
305 if (size > ALIGNED_BUFFER_SIZE)
306 size = ALIGNED_BUFFER_SIZE;
307 ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, size);
308 if (ret < 0)
309 return ret;
311 size = 512 - shift;
312 if (size > count)
313 size = count;
314 memcpy(buf, s->aligned_buf + shift, size);
316 buf += size;
317 offset += size;
318 count -= size;
319 sum += size;
321 if (count == 0)
322 return sum;
324 if (count & 0x1ff || (uintptr_t) buf & 0x1ff) {
326 /* read on aligned buffer */
328 while (count) {
330 size = (count + 0x1ff) & ~0x1ff;
331 if (size > ALIGNED_BUFFER_SIZE)
332 size = ALIGNED_BUFFER_SIZE;
334 ret = raw_pread_aligned(bs, offset, s->aligned_buf, size);
335 if (ret < 0)
336 return ret;
338 size = ret;
339 if (size > count)
340 size = count;
342 memcpy(buf, s->aligned_buf, size);
344 buf += size;
345 offset += size;
346 count -= size;
347 sum += size;
350 return sum;
354 return raw_pread_aligned(bs, offset, buf, count) + sum;
358 * offset and count are in bytes and possibly not aligned. For files opened
359 * with O_DIRECT, necessary alignments are ensured before calling
360 * raw_pwrite_aligned to do the actual write.
362 static int raw_pwrite(BlockDriverState *bs, int64_t offset,
363 const uint8_t *buf, int count)
365 BDRVRawState *s = bs->opaque;
366 int size, ret, shift, sum;
368 sum = 0;
370 if (s->aligned_buf != NULL) {
372 if (offset & 0x1ff) {
373 /* align offset on a 512 bytes boundary */
374 shift = offset & 0x1ff;
375 ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, 512);
376 if (ret < 0)
377 return ret;
379 size = 512 - shift;
380 if (size > count)
381 size = count;
382 memcpy(s->aligned_buf + shift, buf, size);
384 ret = raw_pwrite_aligned(bs, offset - shift, s->aligned_buf, 512);
385 if (ret < 0)
386 return ret;
388 buf += size;
389 offset += size;
390 count -= size;
391 sum += size;
393 if (count == 0)
394 return sum;
396 if (count & 0x1ff || (uintptr_t) buf & 0x1ff) {
398 while ((size = (count & ~0x1ff)) != 0) {
400 if (size > ALIGNED_BUFFER_SIZE)
401 size = ALIGNED_BUFFER_SIZE;
403 memcpy(s->aligned_buf, buf, size);
405 ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, size);
406 if (ret < 0)
407 return ret;
409 buf += ret;
410 offset += ret;
411 count -= ret;
412 sum += ret;
414 /* here, count < 512 because (count & ~0x1ff) == 0 */
415 if (count) {
416 ret = raw_pread_aligned(bs, offset, s->aligned_buf, 512);
417 if (ret < 0)
418 return ret;
419 memcpy(s->aligned_buf, buf, count);
421 ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, 512);
422 if (ret < 0)
423 return ret;
424 if (count < ret)
425 ret = count;
427 sum += ret;
429 return sum;
432 return raw_pwrite_aligned(bs, offset, buf, count) + sum;
435 #else
436 #define raw_pread raw_pread_aligned
437 #define raw_pwrite raw_pwrite_aligned
438 #endif
441 #ifdef CONFIG_AIO
442 /***********************************************************/
443 /* Unix AIO using POSIX AIO */
445 typedef struct RawAIOCB {
446 BlockDriverAIOCB common;
447 int fd;
448 struct aiocb aiocb;
449 struct RawAIOCB *next;
450 int ret;
451 } RawAIOCB;
453 typedef struct PosixAioState
455 int rfd, wfd;
456 RawAIOCB *first_aio;
457 } PosixAioState;
459 static int raw_fd_pool_get(BDRVRawState *s)
461 int i;
463 for (i = 0; i < RAW_FD_POOL_SIZE; i++) {
464 /* already in use */
465 if (s->fd_pool[i] != -1)
466 continue;
468 /* try to dup file descriptor */
469 s->fd_pool[i] = dup(s->fd);
470 if (s->fd_pool[i] != -1)
471 return s->fd_pool[i];
474 /* we couldn't dup the file descriptor so just use the main one */
475 return s->fd;
478 static void raw_fd_pool_put(RawAIOCB *acb)
480 BDRVRawState *s = acb->common.bs->opaque;
481 int i;
483 for (i = 0; i < RAW_FD_POOL_SIZE; i++) {
484 if (s->fd_pool[i] == acb->fd) {
485 close(s->fd_pool[i]);
486 s->fd_pool[i] = -1;
491 static void posix_aio_read(void *opaque)
493 PosixAioState *s = opaque;
494 RawAIOCB *acb, **pacb;
495 int ret;
496 ssize_t len;
498 do {
499 char byte;
501 len = read(s->rfd, &byte, 1);
502 if (len == -1 && errno == EINTR)
503 continue;
504 if (len == -1 && errno == EAGAIN)
505 break;
506 } while (len == -1);
508 for(;;) {
509 pacb = &s->first_aio;
510 for(;;) {
511 acb = *pacb;
512 if (!acb)
513 goto the_end;
514 ret = aio_error(&acb->aiocb);
515 if (ret == ECANCELED) {
516 /* remove the request */
517 *pacb = acb->next;
518 raw_fd_pool_put(acb);
519 qemu_aio_release(acb);
520 } else if (ret != EINPROGRESS) {
521 /* end of aio */
522 if (ret == 0) {
523 ret = aio_return(&acb->aiocb);
524 if (ret == acb->aiocb.aio_nbytes)
525 ret = 0;
526 else
527 ret = -EINVAL;
528 } else {
529 ret = -ret;
531 /* remove the request */
532 *pacb = acb->next;
533 /* call the callback */
534 acb->common.cb(acb->common.opaque, ret);
535 raw_fd_pool_put(acb);
536 qemu_aio_release(acb);
537 break;
538 } else {
539 pacb = &acb->next;
543 the_end: ;
546 static int posix_aio_flush(void *opaque)
548 PosixAioState *s = opaque;
549 return !!s->first_aio;
552 static PosixAioState *posix_aio_state;
554 static void aio_signal_handler(int signum)
556 if (posix_aio_state) {
557 char byte = 0;
559 write(posix_aio_state->wfd, &byte, sizeof(byte));
562 qemu_service_io();
565 static int posix_aio_init(void)
567 struct sigaction act;
568 PosixAioState *s;
569 int fds[2];
571 if (posix_aio_state)
572 return 0;
574 s = qemu_malloc(sizeof(PosixAioState));
575 if (s == NULL)
576 return -ENOMEM;
578 sigfillset(&act.sa_mask);
579 act.sa_flags = 0; /* do not restart syscalls to interrupt select() */
580 act.sa_handler = aio_signal_handler;
581 sigaction(SIGUSR2, &act, NULL);
583 s->first_aio = NULL;
584 if (pipe(fds) == -1) {
585 fprintf(stderr, "failed to create pipe\n");
586 return -errno;
589 s->rfd = fds[0];
590 s->wfd = fds[1];
592 fcntl(s->wfd, F_SETFL, O_NONBLOCK);
594 qemu_aio_set_fd_handler(s->rfd, posix_aio_read, NULL, posix_aio_flush, s);
596 #if defined(__linux__)
598 struct aioinit ai;
600 memset(&ai, 0, sizeof(ai));
601 #if defined(__GLIBC_PREREQ) && __GLIBC_PREREQ(2, 4)
602 ai.aio_threads = 64;
603 ai.aio_num = 64;
604 #else
605 /* XXX: aio thread exit seems to hang on RedHat 9 and this init
606 seems to fix the problem. */
607 ai.aio_threads = 1;
608 ai.aio_num = 1;
609 ai.aio_idle_time = 365 * 100000;
610 #endif
611 aio_init(&ai);
613 #endif
614 posix_aio_state = s;
616 return 0;
619 static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
620 int64_t sector_num, uint8_t *buf, int nb_sectors,
621 BlockDriverCompletionFunc *cb, void *opaque)
623 BDRVRawState *s = bs->opaque;
624 RawAIOCB *acb;
626 if (fd_open(bs) < 0)
627 return NULL;
629 acb = qemu_aio_get(bs, cb, opaque);
630 if (!acb)
631 return NULL;
632 acb->fd = raw_fd_pool_get(s);
633 acb->aiocb.aio_fildes = acb->fd;
634 acb->aiocb.aio_sigevent.sigev_signo = SIGUSR2;
635 acb->aiocb.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
636 acb->aiocb.aio_buf = buf;
637 if (nb_sectors < 0)
638 acb->aiocb.aio_nbytes = -nb_sectors;
639 else
640 acb->aiocb.aio_nbytes = nb_sectors * 512;
641 acb->aiocb.aio_offset = sector_num * 512;
642 acb->next = posix_aio_state->first_aio;
643 posix_aio_state->first_aio = acb;
644 return acb;
647 static void raw_aio_em_cb(void* opaque)
649 RawAIOCB *acb = opaque;
650 acb->common.cb(acb->common.opaque, acb->ret);
651 qemu_aio_release(acb);
654 static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
655 int64_t sector_num, uint8_t *buf, int nb_sectors,
656 BlockDriverCompletionFunc *cb, void *opaque)
658 RawAIOCB *acb;
661 * If O_DIRECT is used and the buffer is not aligned fall back
662 * to synchronous IO.
664 #if defined(O_DIRECT)
665 BDRVRawState *s = bs->opaque;
667 if (unlikely(s->aligned_buf != NULL && ((uintptr_t) buf % 512))) {
668 QEMUBH *bh;
669 acb = qemu_aio_get(bs, cb, opaque);
670 acb->ret = raw_pread(bs, 512 * sector_num, buf, 512 * nb_sectors);
671 bh = qemu_bh_new(raw_aio_em_cb, acb);
672 qemu_bh_schedule(bh);
673 return &acb->common;
675 #endif
677 acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
678 if (!acb)
679 return NULL;
680 if (aio_read(&acb->aiocb) < 0) {
681 qemu_aio_release(acb);
682 return NULL;
684 return &acb->common;
687 static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
688 int64_t sector_num, const uint8_t *buf, int nb_sectors,
689 BlockDriverCompletionFunc *cb, void *opaque)
691 RawAIOCB *acb;
694 * If O_DIRECT is used and the buffer is not aligned fall back
695 * to synchronous IO.
697 #if defined(O_DIRECT)
698 BDRVRawState *s = bs->opaque;
700 if (unlikely(s->aligned_buf != NULL && ((uintptr_t) buf % 512))) {
701 QEMUBH *bh;
702 acb = qemu_aio_get(bs, cb, opaque);
703 acb->ret = raw_pwrite(bs, 512 * sector_num, buf, 512 * nb_sectors);
704 bh = qemu_bh_new(raw_aio_em_cb, acb);
705 qemu_bh_schedule(bh);
706 return &acb->common;
708 #endif
710 acb = raw_aio_setup(bs, sector_num, (uint8_t*)buf, nb_sectors, cb, opaque);
711 if (!acb)
712 return NULL;
713 if (aio_write(&acb->aiocb) < 0) {
714 qemu_aio_release(acb);
715 return NULL;
717 return &acb->common;
720 static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
722 int ret;
723 RawAIOCB *acb = (RawAIOCB *)blockacb;
724 RawAIOCB **pacb;
726 ret = aio_cancel(acb->aiocb.aio_fildes, &acb->aiocb);
727 if (ret == AIO_NOTCANCELED) {
728 /* fail safe: if the aio could not be canceled, we wait for
729 it */
730 while (aio_error(&acb->aiocb) == EINPROGRESS);
733 /* remove the callback from the queue */
734 pacb = &posix_aio_state->first_aio;
735 for(;;) {
736 if (*pacb == NULL) {
737 break;
738 } else if (*pacb == acb) {
739 *pacb = acb->next;
740 raw_fd_pool_put(acb);
741 qemu_aio_release(acb);
742 break;
744 pacb = &acb->next;
748 #else /* CONFIG_AIO */
749 static int posix_aio_init(void)
751 return 0;
753 #endif /* CONFIG_AIO */
755 static void raw_close_fd_pool(BDRVRawState *s)
757 int i;
759 for (i = 0; i < RAW_FD_POOL_SIZE; i++) {
760 if (s->fd_pool[i] != -1) {
761 close(s->fd_pool[i]);
762 s->fd_pool[i] = -1;
767 static void raw_close(BlockDriverState *bs)
769 BDRVRawState *s = bs->opaque;
770 if (s->fd >= 0) {
771 close(s->fd);
772 s->fd = -1;
773 #if defined(O_DIRECT)
774 if (s->aligned_buf != NULL)
775 qemu_free(s->aligned_buf);
776 #endif
778 raw_close_fd_pool(s);
781 static int raw_truncate(BlockDriverState *bs, int64_t offset)
783 BDRVRawState *s = bs->opaque;
784 if (s->type != FTYPE_FILE)
785 return -ENOTSUP;
786 if (ftruncate(s->fd, offset) < 0)
787 return -errno;
788 return 0;
791 #ifdef __OpenBSD__
792 static int64_t raw_getlength(BlockDriverState *bs)
794 BDRVRawState *s = bs->opaque;
795 int fd = s->fd;
796 struct stat st;
798 if (fstat(fd, &st))
799 return -1;
800 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
801 struct disklabel dl;
803 if (ioctl(fd, DIOCGDINFO, &dl))
804 return -1;
805 return (uint64_t)dl.d_secsize *
806 dl.d_partitions[DISKPART(st.st_rdev)].p_size;
807 } else
808 return st.st_size;
810 #else /* !__OpenBSD__ */
811 static int64_t raw_getlength(BlockDriverState *bs)
813 BDRVRawState *s = bs->opaque;
814 int fd = s->fd;
815 int64_t size;
816 #ifdef _BSD
817 struct stat sb;
818 #endif
819 #ifdef __sun__
820 struct dk_minfo minfo;
821 int rv;
822 #endif
823 int ret;
825 ret = fd_open(bs);
826 if (ret < 0)
827 return ret;
829 #ifdef _BSD
830 if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
831 #ifdef DIOCGMEDIASIZE
832 if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
833 #endif
834 #ifdef CONFIG_COCOA
835 size = LONG_LONG_MAX;
836 #else
837 size = lseek(fd, 0LL, SEEK_END);
838 #endif
839 } else
840 #endif
841 #ifdef __sun__
843 * use the DKIOCGMEDIAINFO ioctl to read the size.
845 rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo );
846 if ( rv != -1 ) {
847 size = minfo.dki_lbsize * minfo.dki_capacity;
848 } else /* there are reports that lseek on some devices
849 fails, but irc discussion said that contingency
850 on contingency was overkill */
851 #endif
853 size = lseek(fd, 0, SEEK_END);
855 return size;
857 #endif
859 static int raw_create(const char *filename, int64_t total_size,
860 const char *backing_file, int flags)
862 int fd;
864 if (flags || backing_file)
865 return -ENOTSUP;
867 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
868 0644);
869 if (fd < 0)
870 return -EIO;
871 ftruncate(fd, total_size * 512);
872 close(fd);
873 return 0;
876 static void raw_flush(BlockDriverState *bs)
878 BDRVRawState *s = bs->opaque;
879 fsync(s->fd);
882 BlockDriver bdrv_raw = {
883 "raw",
884 sizeof(BDRVRawState),
885 NULL, /* no probe for protocols */
886 raw_open,
887 NULL,
888 NULL,
889 raw_close,
890 raw_create,
891 raw_flush,
893 #ifdef CONFIG_AIO
894 .bdrv_aio_read = raw_aio_read,
895 .bdrv_aio_write = raw_aio_write,
896 .bdrv_aio_cancel = raw_aio_cancel,
897 .aiocb_size = sizeof(RawAIOCB),
898 #endif
899 .bdrv_pread = raw_pread,
900 .bdrv_pwrite = raw_pwrite,
901 .bdrv_truncate = raw_truncate,
902 .bdrv_getlength = raw_getlength,
905 /***********************************************/
906 /* host device */
908 #ifdef CONFIG_COCOA
909 static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
910 static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
912 kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
914 kern_return_t kernResult;
915 mach_port_t masterPort;
916 CFMutableDictionaryRef classesToMatch;
918 kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
919 if ( KERN_SUCCESS != kernResult ) {
920 printf( "IOMasterPort returned %d\n", kernResult );
923 classesToMatch = IOServiceMatching( kIOCDMediaClass );
924 if ( classesToMatch == NULL ) {
925 printf( "IOServiceMatching returned a NULL dictionary.\n" );
926 } else {
927 CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
929 kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
930 if ( KERN_SUCCESS != kernResult )
932 printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
935 return kernResult;
938 kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
940 io_object_t nextMedia;
941 kern_return_t kernResult = KERN_FAILURE;
942 *bsdPath = '\0';
943 nextMedia = IOIteratorNext( mediaIterator );
944 if ( nextMedia )
946 CFTypeRef bsdPathAsCFString;
947 bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
948 if ( bsdPathAsCFString ) {
949 size_t devPathLength;
950 strcpy( bsdPath, _PATH_DEV );
951 strcat( bsdPath, "r" );
952 devPathLength = strlen( bsdPath );
953 if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
954 kernResult = KERN_SUCCESS;
956 CFRelease( bsdPathAsCFString );
958 IOObjectRelease( nextMedia );
961 return kernResult;
964 #endif
966 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
968 BDRVRawState *s = bs->opaque;
969 int fd, open_flags, ret, i;
971 posix_aio_init();
973 #ifdef CONFIG_COCOA
974 if (strstart(filename, "/dev/cdrom", NULL)) {
975 kern_return_t kernResult;
976 io_iterator_t mediaIterator;
977 char bsdPath[ MAXPATHLEN ];
978 int fd;
980 kernResult = FindEjectableCDMedia( &mediaIterator );
981 kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
983 if ( bsdPath[ 0 ] != '\0' ) {
984 strcat(bsdPath,"s0");
985 /* some CDs don't have a partition 0 */
986 fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
987 if (fd < 0) {
988 bsdPath[strlen(bsdPath)-1] = '1';
989 } else {
990 close(fd);
992 filename = bsdPath;
995 if ( mediaIterator )
996 IOObjectRelease( mediaIterator );
998 #endif
999 open_flags = O_BINARY;
1000 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
1001 open_flags |= O_RDWR;
1002 } else {
1003 open_flags |= O_RDONLY;
1004 bs->read_only = 1;
1006 #ifdef O_DIRECT
1007 if (flags & BDRV_O_DIRECT)
1008 open_flags |= O_DIRECT;
1009 #endif
1011 s->type = FTYPE_FILE;
1012 #if defined(__linux__)
1013 if (strstart(filename, "/dev/cd", NULL)) {
1014 /* open will not fail even if no CD is inserted */
1015 open_flags |= O_NONBLOCK;
1016 s->type = FTYPE_CD;
1017 } else if (strstart(filename, "/dev/fd", NULL)) {
1018 s->type = FTYPE_FD;
1019 s->fd_open_flags = open_flags;
1020 /* open will not fail even if no floppy is inserted */
1021 open_flags |= O_NONBLOCK;
1022 } else if (strstart(filename, "/dev/sg", NULL)) {
1023 bs->sg = 1;
1025 #endif
1026 fd = open(filename, open_flags, 0644);
1027 if (fd < 0) {
1028 ret = -errno;
1029 if (ret == -EROFS)
1030 ret = -EACCES;
1031 return ret;
1033 s->fd = fd;
1034 for (i = 0; i < RAW_FD_POOL_SIZE; i++)
1035 s->fd_pool[i] = -1;
1036 #if defined(__linux__)
1037 /* close fd so that we can reopen it as needed */
1038 if (s->type == FTYPE_FD) {
1039 close(s->fd);
1040 s->fd = -1;
1041 s->fd_media_changed = 1;
1043 #endif
1044 return 0;
1047 #if defined(__linux__)
1048 /* Note: we do not have a reliable method to detect if the floppy is
1049 present. The current method is to try to open the floppy at every
1050 I/O and to keep it opened during a few hundreds of ms. */
1051 static int fd_open(BlockDriverState *bs)
1053 BDRVRawState *s = bs->opaque;
1054 int last_media_present;
1056 if (s->type != FTYPE_FD)
1057 return 0;
1058 last_media_present = (s->fd >= 0);
1059 if (s->fd >= 0 &&
1060 (qemu_get_clock(rt_clock) - s->fd_open_time) >= FD_OPEN_TIMEOUT) {
1061 close(s->fd);
1062 s->fd = -1;
1063 raw_close_fd_pool(s);
1064 #ifdef DEBUG_FLOPPY
1065 printf("Floppy closed\n");
1066 #endif
1068 if (s->fd < 0) {
1069 if (s->fd_got_error &&
1070 (qemu_get_clock(rt_clock) - s->fd_error_time) < FD_OPEN_TIMEOUT) {
1071 #ifdef DEBUG_FLOPPY
1072 printf("No floppy (open delayed)\n");
1073 #endif
1074 return -EIO;
1076 s->fd = open(bs->filename, s->fd_open_flags);
1077 if (s->fd < 0) {
1078 s->fd_error_time = qemu_get_clock(rt_clock);
1079 s->fd_got_error = 1;
1080 if (last_media_present)
1081 s->fd_media_changed = 1;
1082 #ifdef DEBUG_FLOPPY
1083 printf("No floppy\n");
1084 #endif
1085 return -EIO;
1087 #ifdef DEBUG_FLOPPY
1088 printf("Floppy opened\n");
1089 #endif
1091 if (!last_media_present)
1092 s->fd_media_changed = 1;
1093 s->fd_open_time = qemu_get_clock(rt_clock);
1094 s->fd_got_error = 0;
1095 return 0;
1098 static int raw_is_inserted(BlockDriverState *bs)
1100 BDRVRawState *s = bs->opaque;
1101 int ret;
1103 switch(s->type) {
1104 case FTYPE_CD:
1105 ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
1106 if (ret == CDS_DISC_OK)
1107 return 1;
1108 else
1109 return 0;
1110 break;
1111 case FTYPE_FD:
1112 ret = fd_open(bs);
1113 return (ret >= 0);
1114 default:
1115 return 1;
1119 /* currently only used by fdc.c, but a CD version would be good too */
1120 static int raw_media_changed(BlockDriverState *bs)
1122 BDRVRawState *s = bs->opaque;
1124 switch(s->type) {
1125 case FTYPE_FD:
1127 int ret;
1128 /* XXX: we do not have a true media changed indication. It
1129 does not work if the floppy is changed without trying
1130 to read it */
1131 fd_open(bs);
1132 ret = s->fd_media_changed;
1133 s->fd_media_changed = 0;
1134 #ifdef DEBUG_FLOPPY
1135 printf("Floppy changed=%d\n", ret);
1136 #endif
1137 return ret;
1139 default:
1140 return -ENOTSUP;
1144 static int raw_eject(BlockDriverState *bs, int eject_flag)
1146 BDRVRawState *s = bs->opaque;
1148 switch(s->type) {
1149 case FTYPE_CD:
1150 if (eject_flag) {
1151 if (ioctl (s->fd, CDROMEJECT, NULL) < 0)
1152 perror("CDROMEJECT");
1153 } else {
1154 if (ioctl (s->fd, CDROMCLOSETRAY, NULL) < 0)
1155 perror("CDROMEJECT");
1157 break;
1158 case FTYPE_FD:
1160 int fd;
1161 if (s->fd >= 0) {
1162 close(s->fd);
1163 s->fd = -1;
1164 raw_close_fd_pool(s);
1166 fd = open(bs->filename, s->fd_open_flags | O_NONBLOCK);
1167 if (fd >= 0) {
1168 if (ioctl(fd, FDEJECT, 0) < 0)
1169 perror("FDEJECT");
1170 close(fd);
1173 break;
1174 default:
1175 return -ENOTSUP;
1177 return 0;
1180 static int raw_set_locked(BlockDriverState *bs, int locked)
1182 BDRVRawState *s = bs->opaque;
1184 switch(s->type) {
1185 case FTYPE_CD:
1186 if (ioctl (s->fd, CDROM_LOCKDOOR, locked) < 0) {
1187 /* Note: an error can happen if the distribution automatically
1188 mounts the CD-ROM */
1189 // perror("CDROM_LOCKDOOR");
1191 break;
1192 default:
1193 return -ENOTSUP;
1195 return 0;
1198 static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1200 BDRVRawState *s = bs->opaque;
1202 return ioctl(s->fd, req, buf);
1204 #else
1206 static int fd_open(BlockDriverState *bs)
1208 return 0;
1211 static int raw_is_inserted(BlockDriverState *bs)
1213 return 1;
1216 static int raw_media_changed(BlockDriverState *bs)
1218 return -ENOTSUP;
1221 static int raw_eject(BlockDriverState *bs, int eject_flag)
1223 return -ENOTSUP;
1226 static int raw_set_locked(BlockDriverState *bs, int locked)
1228 return -ENOTSUP;
1231 static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1233 return -ENOTSUP;
1235 #endif /* !linux */
1237 BlockDriver bdrv_host_device = {
1238 "host_device",
1239 sizeof(BDRVRawState),
1240 NULL, /* no probe for protocols */
1241 hdev_open,
1242 NULL,
1243 NULL,
1244 raw_close,
1245 NULL,
1246 raw_flush,
1248 #ifdef CONFIG_AIO
1249 .bdrv_aio_read = raw_aio_read,
1250 .bdrv_aio_write = raw_aio_write,
1251 .bdrv_aio_cancel = raw_aio_cancel,
1252 .aiocb_size = sizeof(RawAIOCB),
1253 #endif
1254 .bdrv_pread = raw_pread,
1255 .bdrv_pwrite = raw_pwrite,
1256 .bdrv_getlength = raw_getlength,
1258 /* removable device support */
1259 .bdrv_is_inserted = raw_is_inserted,
1260 .bdrv_media_changed = raw_media_changed,
1261 .bdrv_eject = raw_eject,
1262 .bdrv_set_locked = raw_set_locked,
1263 /* generic scsi device */
1264 .bdrv_ioctl = raw_ioctl,