kvm: external module: dynamic unifdef
[kvm-userspace.git] / qemu / block-raw-posix.c
blob5e3e0bfbd87cbf8a963ceb3951920434555f2c90
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 <aio.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 #endif
61 #ifdef __OpenBSD__
62 #include <sys/ioctl.h>
63 #include <sys/disklabel.h>
64 #include <sys/dkio.h>
65 #endif
67 //#define DEBUG_FLOPPY
69 //#define DEBUG_BLOCK
70 #if defined(DEBUG_BLOCK)
71 #define DEBUG_BLOCK_PRINT(formatCstr, args...) do { if (loglevel != 0) \
72 { fprintf(logfile, formatCstr, ##args); fflush(logfile); } } while (0)
73 #else
74 #define DEBUG_BLOCK_PRINT(formatCstr, args...)
75 #endif
77 /* OS X does not have O_DSYNC */
78 #ifndef O_DSYNC
79 #define O_DSYNC O_SYNC
80 #endif
82 /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */
83 #ifndef O_DIRECT
84 #define O_DIRECT O_DSYNC
85 #endif
87 #define FTYPE_FILE 0
88 #define FTYPE_CD 1
89 #define FTYPE_FD 2
91 #define ALIGNED_BUFFER_SIZE (32 * 512)
93 /* if the FD is not accessed during that time (in ms), we try to
94 reopen it to see if the disk has been changed */
95 #define FD_OPEN_TIMEOUT 1000
97 /* posix-aio doesn't allow multiple outstanding requests to a single file
98 * descriptor. we implement a pool of dup()'d file descriptors to work
99 * around this */
100 #define RAW_FD_POOL_SIZE 64
102 typedef struct BDRVRawState {
103 int fd;
104 int type;
105 unsigned int lseek_err_cnt;
106 int fd_pool[RAW_FD_POOL_SIZE];
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 uint8_t* aligned_buf;
116 } BDRVRawState;
118 static int posix_aio_init(void);
120 static int fd_open(BlockDriverState *bs);
122 static int raw_open(BlockDriverState *bs, const char *filename, int flags)
124 BDRVRawState *s = bs->opaque;
125 int fd, open_flags, ret;
126 int i;
128 posix_aio_init();
130 s->lseek_err_cnt = 0;
132 open_flags = O_BINARY;
133 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
134 open_flags |= O_RDWR;
135 } else {
136 open_flags |= O_RDONLY;
137 bs->read_only = 1;
139 if (flags & BDRV_O_CREAT)
140 open_flags |= O_CREAT | O_TRUNC;
142 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
143 * and O_DIRECT for no caching. */
144 if ((flags & BDRV_O_NOCACHE))
145 open_flags |= O_DIRECT;
146 else if (!(flags & BDRV_O_CACHE_WB))
147 open_flags |= O_DSYNC;
149 s->type = FTYPE_FILE;
151 fd = open(filename, open_flags, 0644);
152 if (fd < 0) {
153 ret = -errno;
154 if (ret == -EROFS)
155 ret = -EACCES;
156 return ret;
158 s->fd = fd;
159 for (i = 0; i < RAW_FD_POOL_SIZE; i++)
160 s->fd_pool[i] = -1;
161 s->aligned_buf = NULL;
162 if ((flags & BDRV_O_NOCACHE)) {
163 s->aligned_buf = qemu_memalign(512, ALIGNED_BUFFER_SIZE);
164 if (s->aligned_buf == NULL) {
165 ret = -errno;
166 close(fd);
167 return ret;
170 return 0;
173 /* XXX: use host sector size if necessary with:
174 #ifdef DIOCGSECTORSIZE
176 unsigned int sectorsize = 512;
177 if (!ioctl(fd, DIOCGSECTORSIZE, &sectorsize) &&
178 sectorsize > bufsize)
179 bufsize = sectorsize;
181 #endif
182 #ifdef CONFIG_COCOA
183 u_int32_t blockSize = 512;
184 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
185 bufsize = blockSize;
187 #endif
191 * offset and count are in bytes, but must be multiples of 512 for files
192 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
194 * This function may be called without alignment if the caller ensures
195 * that O_DIRECT is not in effect.
197 static int raw_pread_aligned(BlockDriverState *bs, int64_t offset,
198 uint8_t *buf, int count)
200 BDRVRawState *s = bs->opaque;
201 int ret;
203 ret = fd_open(bs);
204 if (ret < 0)
205 return ret;
207 if (offset >= 0 && lseek(s->fd, offset, SEEK_SET) == (off_t)-1) {
208 ++(s->lseek_err_cnt);
209 if(s->lseek_err_cnt <= 10) {
210 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
211 "] lseek failed : %d = %s\n",
212 s->fd, bs->filename, offset, buf, count,
213 bs->total_sectors, errno, strerror(errno));
215 return -1;
217 s->lseek_err_cnt=0;
219 ret = read(s->fd, buf, count);
220 if (ret == count)
221 goto label__raw_read__success;
223 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
224 "] read failed %d : %d = %s\n",
225 s->fd, bs->filename, offset, buf, count,
226 bs->total_sectors, ret, errno, strerror(errno));
228 /* Try harder for CDrom. */
229 if (bs->type == BDRV_TYPE_CDROM) {
230 lseek(s->fd, offset, SEEK_SET);
231 ret = read(s->fd, buf, count);
232 if (ret == count)
233 goto label__raw_read__success;
234 lseek(s->fd, offset, SEEK_SET);
235 ret = read(s->fd, buf, count);
236 if (ret == count)
237 goto label__raw_read__success;
239 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
240 "] retry read failed %d : %d = %s\n",
241 s->fd, bs->filename, offset, buf, count,
242 bs->total_sectors, ret, errno, strerror(errno));
245 label__raw_read__success:
247 return ret;
251 * offset and count are in bytes, but must be multiples of 512 for files
252 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
254 * This function may be called without alignment if the caller ensures
255 * that O_DIRECT is not in effect.
257 static int raw_pwrite_aligned(BlockDriverState *bs, int64_t offset,
258 const uint8_t *buf, int count)
260 BDRVRawState *s = bs->opaque;
261 int ret;
263 ret = fd_open(bs);
264 if (ret < 0)
265 return ret;
267 if (offset >= 0 && lseek(s->fd, offset, SEEK_SET) == (off_t)-1) {
268 ++(s->lseek_err_cnt);
269 if(s->lseek_err_cnt) {
270 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%"
271 PRId64 "] lseek failed : %d = %s\n",
272 s->fd, bs->filename, offset, buf, count,
273 bs->total_sectors, errno, strerror(errno));
275 return -1;
277 s->lseek_err_cnt = 0;
279 ret = write(s->fd, buf, count);
280 if (ret == count)
281 goto label__raw_write__success;
283 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
284 "] write failed %d : %d = %s\n",
285 s->fd, bs->filename, offset, buf, count,
286 bs->total_sectors, ret, errno, strerror(errno));
288 label__raw_write__success:
290 return ret;
295 * offset and count are in bytes and possibly not aligned. For files opened
296 * with O_DIRECT, necessary alignments are ensured before calling
297 * raw_pread_aligned to do the actual read.
299 static int raw_pread(BlockDriverState *bs, int64_t offset,
300 uint8_t *buf, int count)
302 BDRVRawState *s = bs->opaque;
303 int size, ret, shift, sum;
305 sum = 0;
307 if (s->aligned_buf != NULL) {
309 if (offset & 0x1ff) {
310 /* align offset on a 512 bytes boundary */
312 shift = offset & 0x1ff;
313 size = (shift + count + 0x1ff) & ~0x1ff;
314 if (size > ALIGNED_BUFFER_SIZE)
315 size = ALIGNED_BUFFER_SIZE;
316 ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, size);
317 if (ret < 0)
318 return ret;
320 size = 512 - shift;
321 if (size > count)
322 size = count;
323 memcpy(buf, s->aligned_buf + shift, size);
325 buf += size;
326 offset += size;
327 count -= size;
328 sum += size;
330 if (count == 0)
331 return sum;
333 if (count & 0x1ff || (uintptr_t) buf & 0x1ff) {
335 /* read on aligned buffer */
337 while (count) {
339 size = (count + 0x1ff) & ~0x1ff;
340 if (size > ALIGNED_BUFFER_SIZE)
341 size = ALIGNED_BUFFER_SIZE;
343 ret = raw_pread_aligned(bs, offset, s->aligned_buf, size);
344 if (ret < 0)
345 return ret;
347 size = ret;
348 if (size > count)
349 size = count;
351 memcpy(buf, s->aligned_buf, size);
353 buf += size;
354 offset += size;
355 count -= size;
356 sum += size;
359 return sum;
363 return raw_pread_aligned(bs, offset, buf, count) + sum;
367 * offset and count are in bytes and possibly not aligned. For files opened
368 * with O_DIRECT, necessary alignments are ensured before calling
369 * raw_pwrite_aligned to do the actual write.
371 static int raw_pwrite(BlockDriverState *bs, int64_t offset,
372 const uint8_t *buf, int count)
374 BDRVRawState *s = bs->opaque;
375 int size, ret, shift, sum;
377 sum = 0;
379 if (s->aligned_buf != NULL) {
381 if (offset & 0x1ff) {
382 /* align offset on a 512 bytes boundary */
383 shift = offset & 0x1ff;
384 ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, 512);
385 if (ret < 0)
386 return ret;
388 size = 512 - shift;
389 if (size > count)
390 size = count;
391 memcpy(s->aligned_buf + shift, buf, size);
393 ret = raw_pwrite_aligned(bs, offset - shift, s->aligned_buf, 512);
394 if (ret < 0)
395 return ret;
397 buf += size;
398 offset += size;
399 count -= size;
400 sum += size;
402 if (count == 0)
403 return sum;
405 if (count & 0x1ff || (uintptr_t) buf & 0x1ff) {
407 while ((size = (count & ~0x1ff)) != 0) {
409 if (size > ALIGNED_BUFFER_SIZE)
410 size = ALIGNED_BUFFER_SIZE;
412 memcpy(s->aligned_buf, buf, size);
414 ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, size);
415 if (ret < 0)
416 return ret;
418 buf += ret;
419 offset += ret;
420 count -= ret;
421 sum += ret;
423 /* here, count < 512 because (count & ~0x1ff) == 0 */
424 if (count) {
425 ret = raw_pread_aligned(bs, offset, s->aligned_buf, 512);
426 if (ret < 0)
427 return ret;
428 memcpy(s->aligned_buf, buf, count);
430 ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, 512);
431 if (ret < 0)
432 return ret;
433 if (count < ret)
434 ret = count;
436 sum += ret;
438 return sum;
441 return raw_pwrite_aligned(bs, offset, buf, count) + sum;
444 #ifdef CONFIG_AIO
445 /***********************************************************/
446 /* Unix AIO using POSIX AIO */
448 typedef struct RawAIOCB {
449 BlockDriverAIOCB common;
450 int fd;
451 struct aiocb aiocb;
452 struct RawAIOCB *next;
453 int ret;
454 } RawAIOCB;
456 typedef struct PosixAioState
458 int fd;
459 RawAIOCB *first_aio;
460 } PosixAioState;
462 static int raw_fd_pool_get(BDRVRawState *s)
464 int i;
466 for (i = 0; i < RAW_FD_POOL_SIZE; i++) {
467 /* already in use */
468 if (s->fd_pool[i] != -1)
469 continue;
471 /* try to dup file descriptor */
472 s->fd_pool[i] = dup(s->fd);
473 if (s->fd_pool[i] != -1)
474 return s->fd_pool[i];
477 /* we couldn't dup the file descriptor so just use the main one */
478 return s->fd;
481 static void raw_fd_pool_put(RawAIOCB *acb)
483 BDRVRawState *s = acb->common.bs->opaque;
484 int i;
486 for (i = 0; i < RAW_FD_POOL_SIZE; i++) {
487 if (s->fd_pool[i] == acb->fd) {
488 close(s->fd_pool[i]);
489 s->fd_pool[i] = -1;
494 static void posix_aio_read(void *opaque)
496 PosixAioState *s = opaque;
497 RawAIOCB *acb, **pacb;
498 int ret;
499 size_t offset;
500 union {
501 struct qemu_signalfd_siginfo siginfo;
502 char buf[128];
503 } sig;
505 /* try to read from signalfd, don't freak out if we can't read anything */
506 offset = 0;
507 while (offset < 128) {
508 ssize_t len;
510 len = read(s->fd, sig.buf + offset, 128 - offset);
511 if (len == -1 && errno == EINTR)
512 continue;
513 if (len == -1 && errno == EAGAIN) {
514 /* there is no natural reason for this to happen,
515 * so we'll spin hard until we get everything just
516 * to be on the safe side. */
517 if (offset > 0)
518 continue;
521 offset += len;
524 for(;;) {
525 pacb = &s->first_aio;
526 for(;;) {
527 acb = *pacb;
528 if (!acb)
529 goto the_end;
530 ret = aio_error(&acb->aiocb);
531 if (ret == ECANCELED) {
532 /* remove the request */
533 *pacb = acb->next;
534 raw_fd_pool_put(acb);
535 qemu_aio_release(acb);
536 } else if (ret != EINPROGRESS) {
537 /* end of aio */
538 if (ret == 0) {
539 ret = aio_return(&acb->aiocb);
540 if (ret == acb->aiocb.aio_nbytes)
541 ret = 0;
542 else
543 ret = -EINVAL;
544 } else {
545 ret = -ret;
547 /* remove the request */
548 *pacb = acb->next;
549 /* call the callback */
550 acb->common.cb(acb->common.opaque, ret);
551 raw_fd_pool_put(acb);
552 qemu_aio_release(acb);
553 break;
554 } else {
555 pacb = &acb->next;
559 the_end: ;
562 static int posix_aio_flush(void *opaque)
564 PosixAioState *s = opaque;
565 return !!s->first_aio;
568 static PosixAioState *posix_aio_state;
570 static int posix_aio_init(void)
572 sigset_t mask;
573 PosixAioState *s;
575 if (posix_aio_state)
576 return 0;
578 s = qemu_malloc(sizeof(PosixAioState));
579 if (s == NULL)
580 return -ENOMEM;
582 /* Make sure to block AIO signal */
583 sigemptyset(&mask);
584 sigaddset(&mask, SIGUSR2);
585 sigprocmask(SIG_BLOCK, &mask, NULL);
587 s->first_aio = NULL;
588 s->fd = qemu_signalfd(&mask);
589 if (s->fd == -1) {
590 fprintf(stderr, "failed to create signalfd\n");
591 return -errno;
594 fcntl(s->fd, F_SETFL, O_NONBLOCK);
596 qemu_aio_set_fd_handler(s->fd, posix_aio_read, NULL, posix_aio_flush, s);
598 #if defined(__linux__)
600 struct aioinit ai;
602 memset(&ai, 0, sizeof(ai));
603 #if defined(__GLIBC_PREREQ) && __GLIBC_PREREQ(2, 4)
604 ai.aio_threads = 64;
605 ai.aio_num = 64;
606 #else
607 /* XXX: aio thread exit seems to hang on RedHat 9 and this init
608 seems to fix the problem. */
609 ai.aio_threads = 1;
610 ai.aio_num = 1;
611 ai.aio_idle_time = 365 * 100000;
612 #endif
613 aio_init(&ai);
615 #endif
616 posix_aio_state = s;
618 return 0;
621 static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
622 int64_t sector_num, uint8_t *buf, int nb_sectors,
623 BlockDriverCompletionFunc *cb, void *opaque)
625 BDRVRawState *s = bs->opaque;
626 RawAIOCB *acb;
628 if (fd_open(bs) < 0)
629 return NULL;
631 acb = qemu_aio_get(bs, cb, opaque);
632 if (!acb)
633 return NULL;
634 acb->fd = raw_fd_pool_get(s);
635 acb->aiocb.aio_fildes = acb->fd;
636 acb->aiocb.aio_sigevent.sigev_signo = SIGUSR2;
637 acb->aiocb.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
638 acb->aiocb.aio_buf = buf;
639 if (nb_sectors < 0)
640 acb->aiocb.aio_nbytes = -nb_sectors;
641 else
642 acb->aiocb.aio_nbytes = nb_sectors * 512;
643 acb->aiocb.aio_offset = sector_num * 512;
644 acb->next = posix_aio_state->first_aio;
645 posix_aio_state->first_aio = acb;
646 return acb;
649 static void raw_aio_em_cb(void* opaque)
651 RawAIOCB *acb = opaque;
652 acb->common.cb(acb->common.opaque, acb->ret);
653 qemu_aio_release(acb);
656 static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
657 int64_t sector_num, uint8_t *buf, int nb_sectors,
658 BlockDriverCompletionFunc *cb, void *opaque)
660 RawAIOCB *acb;
663 * If O_DIRECT is used and the buffer is not aligned fall back
664 * to synchronous IO.
666 BDRVRawState *s = bs->opaque;
668 if (unlikely(s->aligned_buf != NULL && ((uintptr_t) buf % 512))) {
669 QEMUBH *bh;
670 acb = qemu_aio_get(bs, cb, opaque);
671 acb->ret = raw_pread(bs, 512 * sector_num, buf, 512 * nb_sectors);
672 bh = qemu_bh_new(raw_aio_em_cb, acb);
673 qemu_bh_schedule(bh);
674 return &acb->common;
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 BDRVRawState *s = bs->opaque;
699 if (unlikely(s->aligned_buf != NULL && ((uintptr_t) buf % 512))) {
700 QEMUBH *bh;
701 acb = qemu_aio_get(bs, cb, opaque);
702 acb->ret = raw_pwrite(bs, 512 * sector_num, buf, 512 * nb_sectors);
703 bh = qemu_bh_new(raw_aio_em_cb, acb);
704 qemu_bh_schedule(bh);
705 return &acb->common;
708 acb = raw_aio_setup(bs, sector_num, (uint8_t*)buf, nb_sectors, cb, opaque);
709 if (!acb)
710 return NULL;
711 if (aio_write(&acb->aiocb) < 0) {
712 qemu_aio_release(acb);
713 return NULL;
715 return &acb->common;
718 static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
720 int ret;
721 RawAIOCB *acb = (RawAIOCB *)blockacb;
722 RawAIOCB **pacb;
724 ret = aio_cancel(acb->aiocb.aio_fildes, &acb->aiocb);
725 if (ret == AIO_NOTCANCELED) {
726 /* fail safe: if the aio could not be canceled, we wait for
727 it */
728 while (aio_error(&acb->aiocb) == EINPROGRESS);
731 /* remove the callback from the queue */
732 pacb = &posix_aio_state->first_aio;
733 for(;;) {
734 if (*pacb == NULL) {
735 break;
736 } else if (*pacb == acb) {
737 *pacb = acb->next;
738 raw_fd_pool_put(acb);
739 qemu_aio_release(acb);
740 break;
742 pacb = &acb->next;
746 #else /* CONFIG_AIO */
747 static int posix_aio_init(void)
749 return 0;
751 #endif /* CONFIG_AIO */
753 static void raw_close_fd_pool(BDRVRawState *s)
755 int i;
757 for (i = 0; i < RAW_FD_POOL_SIZE; i++) {
758 if (s->fd_pool[i] != -1) {
759 close(s->fd_pool[i]);
760 s->fd_pool[i] = -1;
765 static void raw_close(BlockDriverState *bs)
767 BDRVRawState *s = bs->opaque;
768 if (s->fd >= 0) {
769 close(s->fd);
770 s->fd = -1;
771 if (s->aligned_buf != NULL)
772 qemu_free(s->aligned_buf);
774 raw_close_fd_pool(s);
777 static int raw_truncate(BlockDriverState *bs, int64_t offset)
779 BDRVRawState *s = bs->opaque;
780 if (s->type != FTYPE_FILE)
781 return -ENOTSUP;
782 if (ftruncate(s->fd, offset) < 0)
783 return -errno;
784 return 0;
787 #ifdef __OpenBSD__
788 static int64_t raw_getlength(BlockDriverState *bs)
790 BDRVRawState *s = bs->opaque;
791 int fd = s->fd;
792 struct stat st;
794 if (fstat(fd, &st))
795 return -1;
796 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
797 struct disklabel dl;
799 if (ioctl(fd, DIOCGDINFO, &dl))
800 return -1;
801 return (uint64_t)dl.d_secsize *
802 dl.d_partitions[DISKPART(st.st_rdev)].p_size;
803 } else
804 return st.st_size;
806 #else /* !__OpenBSD__ */
807 static int64_t raw_getlength(BlockDriverState *bs)
809 BDRVRawState *s = bs->opaque;
810 int fd = s->fd;
811 int64_t size;
812 #ifdef _BSD
813 struct stat sb;
814 #endif
815 #ifdef __sun__
816 struct dk_minfo minfo;
817 int rv;
818 #endif
819 int ret;
821 ret = fd_open(bs);
822 if (ret < 0)
823 return ret;
825 #ifdef _BSD
826 if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
827 #ifdef DIOCGMEDIASIZE
828 if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
829 #endif
830 #ifdef CONFIG_COCOA
831 size = LONG_LONG_MAX;
832 #else
833 size = lseek(fd, 0LL, SEEK_END);
834 #endif
835 } else
836 #endif
837 #ifdef __sun__
839 * use the DKIOCGMEDIAINFO ioctl to read the size.
841 rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo );
842 if ( rv != -1 ) {
843 size = minfo.dki_lbsize * minfo.dki_capacity;
844 } else /* there are reports that lseek on some devices
845 fails, but irc discussion said that contingency
846 on contingency was overkill */
847 #endif
849 size = lseek(fd, 0, SEEK_END);
851 return size;
853 #endif
855 static int raw_create(const char *filename, int64_t total_size,
856 const char *backing_file, int flags)
858 int fd;
860 if (flags || backing_file)
861 return -ENOTSUP;
863 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
864 0644);
865 if (fd < 0)
866 return -EIO;
867 ftruncate(fd, total_size * 512);
868 close(fd);
869 return 0;
872 static void raw_flush(BlockDriverState *bs)
874 BDRVRawState *s = bs->opaque;
875 fsync(s->fd);
878 BlockDriver bdrv_raw = {
879 "raw",
880 sizeof(BDRVRawState),
881 NULL, /* no probe for protocols */
882 raw_open,
883 NULL,
884 NULL,
885 raw_close,
886 raw_create,
887 raw_flush,
889 #ifdef CONFIG_AIO
890 .bdrv_aio_read = raw_aio_read,
891 .bdrv_aio_write = raw_aio_write,
892 .bdrv_aio_cancel = raw_aio_cancel,
893 .aiocb_size = sizeof(RawAIOCB),
894 #endif
895 .bdrv_pread = raw_pread,
896 .bdrv_pwrite = raw_pwrite,
897 .bdrv_truncate = raw_truncate,
898 .bdrv_getlength = raw_getlength,
901 /***********************************************/
902 /* host device */
904 #ifdef CONFIG_COCOA
905 static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
906 static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
908 kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
910 kern_return_t kernResult;
911 mach_port_t masterPort;
912 CFMutableDictionaryRef classesToMatch;
914 kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
915 if ( KERN_SUCCESS != kernResult ) {
916 printf( "IOMasterPort returned %d\n", kernResult );
919 classesToMatch = IOServiceMatching( kIOCDMediaClass );
920 if ( classesToMatch == NULL ) {
921 printf( "IOServiceMatching returned a NULL dictionary.\n" );
922 } else {
923 CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
925 kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
926 if ( KERN_SUCCESS != kernResult )
928 printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
931 return kernResult;
934 kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
936 io_object_t nextMedia;
937 kern_return_t kernResult = KERN_FAILURE;
938 *bsdPath = '\0';
939 nextMedia = IOIteratorNext( mediaIterator );
940 if ( nextMedia )
942 CFTypeRef bsdPathAsCFString;
943 bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
944 if ( bsdPathAsCFString ) {
945 size_t devPathLength;
946 strcpy( bsdPath, _PATH_DEV );
947 strcat( bsdPath, "r" );
948 devPathLength = strlen( bsdPath );
949 if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
950 kernResult = KERN_SUCCESS;
952 CFRelease( bsdPathAsCFString );
954 IOObjectRelease( nextMedia );
957 return kernResult;
960 #endif
962 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
964 BDRVRawState *s = bs->opaque;
965 int fd, open_flags, ret, i;
967 posix_aio_init();
969 #ifdef CONFIG_COCOA
970 if (strstart(filename, "/dev/cdrom", NULL)) {
971 kern_return_t kernResult;
972 io_iterator_t mediaIterator;
973 char bsdPath[ MAXPATHLEN ];
974 int fd;
976 kernResult = FindEjectableCDMedia( &mediaIterator );
977 kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
979 if ( bsdPath[ 0 ] != '\0' ) {
980 strcat(bsdPath,"s0");
981 /* some CDs don't have a partition 0 */
982 fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
983 if (fd < 0) {
984 bsdPath[strlen(bsdPath)-1] = '1';
985 } else {
986 close(fd);
988 filename = bsdPath;
991 if ( mediaIterator )
992 IOObjectRelease( mediaIterator );
994 #endif
995 open_flags = O_BINARY;
996 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
997 open_flags |= O_RDWR;
998 } else {
999 open_flags |= O_RDONLY;
1000 bs->read_only = 1;
1002 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
1003 * and O_DIRECT for no caching. */
1004 if ((flags & BDRV_O_NOCACHE))
1005 open_flags |= O_DIRECT;
1006 else if (!(flags & BDRV_O_CACHE_WB))
1007 open_flags |= O_DSYNC;
1009 s->type = FTYPE_FILE;
1010 #if defined(__linux__)
1011 if (strstart(filename, "/dev/cd", NULL)) {
1012 /* open will not fail even if no CD is inserted */
1013 open_flags |= O_NONBLOCK;
1014 s->type = FTYPE_CD;
1015 } else if (strstart(filename, "/dev/fd", NULL)) {
1016 s->type = FTYPE_FD;
1017 s->fd_open_flags = open_flags;
1018 /* open will not fail even if no floppy is inserted */
1019 open_flags |= O_NONBLOCK;
1020 } else if (strstart(filename, "/dev/sg", NULL)) {
1021 bs->sg = 1;
1023 #endif
1024 fd = open(filename, open_flags, 0644);
1025 if (fd < 0) {
1026 ret = -errno;
1027 if (ret == -EROFS)
1028 ret = -EACCES;
1029 return ret;
1031 s->fd = fd;
1032 for (i = 0; i < RAW_FD_POOL_SIZE; i++)
1033 s->fd_pool[i] = -1;
1034 #if defined(__linux__)
1035 /* close fd so that we can reopen it as needed */
1036 if (s->type == FTYPE_FD) {
1037 close(s->fd);
1038 s->fd = -1;
1039 s->fd_media_changed = 1;
1041 #endif
1042 return 0;
1045 #if defined(__linux__)
1046 /* Note: we do not have a reliable method to detect if the floppy is
1047 present. The current method is to try to open the floppy at every
1048 I/O and to keep it opened during a few hundreds of ms. */
1049 static int fd_open(BlockDriverState *bs)
1051 BDRVRawState *s = bs->opaque;
1052 int last_media_present;
1054 if (s->type != FTYPE_FD)
1055 return 0;
1056 last_media_present = (s->fd >= 0);
1057 if (s->fd >= 0 &&
1058 (qemu_get_clock(rt_clock) - s->fd_open_time) >= FD_OPEN_TIMEOUT) {
1059 close(s->fd);
1060 s->fd = -1;
1061 raw_close_fd_pool(s);
1062 #ifdef DEBUG_FLOPPY
1063 printf("Floppy closed\n");
1064 #endif
1066 if (s->fd < 0) {
1067 if (s->fd_got_error &&
1068 (qemu_get_clock(rt_clock) - s->fd_error_time) < FD_OPEN_TIMEOUT) {
1069 #ifdef DEBUG_FLOPPY
1070 printf("No floppy (open delayed)\n");
1071 #endif
1072 return -EIO;
1074 s->fd = open(bs->filename, s->fd_open_flags);
1075 if (s->fd < 0) {
1076 s->fd_error_time = qemu_get_clock(rt_clock);
1077 s->fd_got_error = 1;
1078 if (last_media_present)
1079 s->fd_media_changed = 1;
1080 #ifdef DEBUG_FLOPPY
1081 printf("No floppy\n");
1082 #endif
1083 return -EIO;
1085 #ifdef DEBUG_FLOPPY
1086 printf("Floppy opened\n");
1087 #endif
1089 if (!last_media_present)
1090 s->fd_media_changed = 1;
1091 s->fd_open_time = qemu_get_clock(rt_clock);
1092 s->fd_got_error = 0;
1093 return 0;
1096 static int raw_is_inserted(BlockDriverState *bs)
1098 BDRVRawState *s = bs->opaque;
1099 int ret;
1101 switch(s->type) {
1102 case FTYPE_CD:
1103 ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
1104 if (ret == CDS_DISC_OK)
1105 return 1;
1106 else
1107 return 0;
1108 break;
1109 case FTYPE_FD:
1110 ret = fd_open(bs);
1111 return (ret >= 0);
1112 default:
1113 return 1;
1117 /* currently only used by fdc.c, but a CD version would be good too */
1118 static int raw_media_changed(BlockDriverState *bs)
1120 BDRVRawState *s = bs->opaque;
1122 switch(s->type) {
1123 case FTYPE_FD:
1125 int ret;
1126 /* XXX: we do not have a true media changed indication. It
1127 does not work if the floppy is changed without trying
1128 to read it */
1129 fd_open(bs);
1130 ret = s->fd_media_changed;
1131 s->fd_media_changed = 0;
1132 #ifdef DEBUG_FLOPPY
1133 printf("Floppy changed=%d\n", ret);
1134 #endif
1135 return ret;
1137 default:
1138 return -ENOTSUP;
1142 static int raw_eject(BlockDriverState *bs, int eject_flag)
1144 BDRVRawState *s = bs->opaque;
1146 switch(s->type) {
1147 case FTYPE_CD:
1148 if (eject_flag) {
1149 if (ioctl (s->fd, CDROMEJECT, NULL) < 0)
1150 perror("CDROMEJECT");
1151 } else {
1152 if (ioctl (s->fd, CDROMCLOSETRAY, NULL) < 0)
1153 perror("CDROMEJECT");
1155 break;
1156 case FTYPE_FD:
1158 int fd;
1159 if (s->fd >= 0) {
1160 close(s->fd);
1161 s->fd = -1;
1162 raw_close_fd_pool(s);
1164 fd = open(bs->filename, s->fd_open_flags | O_NONBLOCK);
1165 if (fd >= 0) {
1166 if (ioctl(fd, FDEJECT, 0) < 0)
1167 perror("FDEJECT");
1168 close(fd);
1171 break;
1172 default:
1173 return -ENOTSUP;
1175 return 0;
1178 static int raw_set_locked(BlockDriverState *bs, int locked)
1180 BDRVRawState *s = bs->opaque;
1182 switch(s->type) {
1183 case FTYPE_CD:
1184 if (ioctl (s->fd, CDROM_LOCKDOOR, locked) < 0) {
1185 /* Note: an error can happen if the distribution automatically
1186 mounts the CD-ROM */
1187 // perror("CDROM_LOCKDOOR");
1189 break;
1190 default:
1191 return -ENOTSUP;
1193 return 0;
1196 static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1198 BDRVRawState *s = bs->opaque;
1200 return ioctl(s->fd, req, buf);
1202 #else
1204 static int fd_open(BlockDriverState *bs)
1206 return 0;
1209 static int raw_is_inserted(BlockDriverState *bs)
1211 return 1;
1214 static int raw_media_changed(BlockDriverState *bs)
1216 return -ENOTSUP;
1219 static int raw_eject(BlockDriverState *bs, int eject_flag)
1221 return -ENOTSUP;
1224 static int raw_set_locked(BlockDriverState *bs, int locked)
1226 return -ENOTSUP;
1229 static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1231 return -ENOTSUP;
1233 #endif /* !linux */
1235 BlockDriver bdrv_host_device = {
1236 "host_device",
1237 sizeof(BDRVRawState),
1238 NULL, /* no probe for protocols */
1239 hdev_open,
1240 NULL,
1241 NULL,
1242 raw_close,
1243 NULL,
1244 raw_flush,
1246 #ifdef CONFIG_AIO
1247 .bdrv_aio_read = raw_aio_read,
1248 .bdrv_aio_write = raw_aio_write,
1249 .bdrv_aio_cancel = raw_aio_cancel,
1250 .aiocb_size = sizeof(RawAIOCB),
1251 #endif
1252 .bdrv_pread = raw_pread,
1253 .bdrv_pwrite = raw_pwrite,
1254 .bdrv_getlength = raw_getlength,
1256 /* removable device support */
1257 .bdrv_is_inserted = raw_is_inserted,
1258 .bdrv_media_changed = raw_media_changed,
1259 .bdrv_eject = raw_eject,
1260 .bdrv_set_locked = raw_set_locked,
1261 /* generic scsi device */
1262 .bdrv_ioctl = raw_ioctl,