kvm: testsuite: Add mov r, imm instructions to the real mode test harness
[qemu-kvm/amd-iommu.git] / block-raw-posix.c
blobc7d1fa8070d6fe2b3c62b7bf313ae8a482fcdd22
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 #if !defined(QEMU_IMG) && !defined(QEMU_NBD)
26 #include "qemu-kvm.h"
27 #include "qemu-timer.h"
28 #include "exec-all.h"
29 #else
30 #define kvm_enabled() 0
31 #define qemu_kvm_aio_start() ((void)0)
32 #define qemu_kvm_aio_end() ((void)0)
33 #define qemu_kvm_aio_wait() ((void)0)
34 #define qemu_kvm_aio_poll() ((void)0)
35 #endif
36 #include "block_int.h"
37 #include <assert.h>
38 #ifdef CONFIG_AIO
39 #include <aio.h>
40 #endif
42 #ifdef CONFIG_COCOA
43 #include <paths.h>
44 #include <sys/param.h>
45 #include <IOKit/IOKitLib.h>
46 #include <IOKit/IOBSD.h>
47 #include <IOKit/storage/IOMediaBSDClient.h>
48 #include <IOKit/storage/IOMedia.h>
49 #include <IOKit/storage/IOCDMedia.h>
50 //#include <IOKit/storage/IOCDTypes.h>
51 #include <CoreFoundation/CoreFoundation.h>
52 #endif
54 #ifdef __sun__
55 #define _POSIX_PTHREAD_SEMANTICS 1
56 #include <signal.h>
57 #include <sys/dkio.h>
58 #endif
59 #ifdef __linux__
60 #include <sys/ioctl.h>
61 #include <linux/cdrom.h>
62 #include <linux/fd.h>
63 #endif
64 #ifdef __FreeBSD__
65 #include <sys/disk.h>
66 #endif
68 #ifdef __OpenBSD__
69 #include <sys/ioctl.h>
70 #include <sys/disklabel.h>
71 #include <sys/dkio.h>
72 #endif
74 //#define DEBUG_FLOPPY
76 //#define DEBUG_BLOCK
77 #if defined(DEBUG_BLOCK) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
78 #define DEBUG_BLOCK_PRINT(formatCstr, args...) do { if (loglevel != 0) \
79 { fprintf(logfile, formatCstr, ##args); fflush(logfile); } } while (0)
80 #else
81 #define DEBUG_BLOCK_PRINT(formatCstr, args...)
82 #endif
84 #define FTYPE_FILE 0
85 #define FTYPE_CD 1
86 #define FTYPE_FD 2
88 #define ALIGNED_BUFFER_SIZE (32 * 512)
90 /* if the FD is not accessed during that time (in ms), we try to
91 reopen it to see if the disk has been changed */
92 #define FD_OPEN_TIMEOUT 1000
94 typedef struct BDRVRawState {
95 int fd;
96 int type;
97 unsigned int lseek_err_cnt;
98 #if defined(__linux__)
99 /* linux floppy specific */
100 int fd_open_flags;
101 int64_t fd_open_time;
102 int64_t fd_error_time;
103 int fd_got_error;
104 int fd_media_changed;
105 #endif
106 #if defined(O_DIRECT) && !defined(QEMU_IMG)
107 uint8_t* aligned_buf;
108 #endif
109 } BDRVRawState;
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;
118 s->lseek_err_cnt = 0;
120 open_flags = O_BINARY;
121 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
122 open_flags |= O_RDWR;
123 } else {
124 open_flags |= O_RDONLY;
125 bs->read_only = 1;
127 if (flags & BDRV_O_CREAT)
128 open_flags |= O_CREAT | O_TRUNC;
129 #ifdef O_DIRECT
130 if (flags & BDRV_O_DIRECT)
131 open_flags |= O_DIRECT;
132 #endif
134 s->type = FTYPE_FILE;
136 fd = open(filename, open_flags, 0644);
137 if (fd < 0) {
138 ret = -errno;
139 if (ret == -EROFS)
140 ret = -EACCES;
141 return ret;
143 s->fd = fd;
144 #if defined(O_DIRECT) && !defined(QEMU_IMG)
145 s->aligned_buf = NULL;
146 if (flags & BDRV_O_DIRECT) {
147 s->aligned_buf = qemu_memalign(512, ALIGNED_BUFFER_SIZE);
148 if (s->aligned_buf == NULL) {
149 ret = -errno;
150 close(fd);
151 return ret;
154 #endif
155 return 0;
158 /* XXX: use host sector size if necessary with:
159 #ifdef DIOCGSECTORSIZE
161 unsigned int sectorsize = 512;
162 if (!ioctl(fd, DIOCGSECTORSIZE, &sectorsize) &&
163 sectorsize > bufsize)
164 bufsize = sectorsize;
166 #endif
167 #ifdef CONFIG_COCOA
168 u_int32_t blockSize = 512;
169 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
170 bufsize = blockSize;
172 #endif
176 * offset and count are in bytes, but must be multiples of 512 for files
177 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
179 * This function may be called without alignment if the caller ensures
180 * that O_DIRECT is not in effect.
182 static int raw_pread_aligned(BlockDriverState *bs, int64_t offset,
183 uint8_t *buf, int count)
185 BDRVRawState *s = bs->opaque;
186 int ret;
188 ret = fd_open(bs);
189 if (ret < 0)
190 return ret;
192 if (offset >= 0 && lseek(s->fd, offset, SEEK_SET) == (off_t)-1) {
193 ++(s->lseek_err_cnt);
194 if(s->lseek_err_cnt <= 10) {
195 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
196 "] lseek failed : %d = %s\n",
197 s->fd, bs->filename, offset, buf, count,
198 bs->total_sectors, errno, strerror(errno));
200 return -1;
202 s->lseek_err_cnt=0;
204 ret = read(s->fd, buf, count);
205 if (ret == count)
206 goto label__raw_read__success;
208 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
209 "] read failed %d : %d = %s\n",
210 s->fd, bs->filename, offset, buf, count,
211 bs->total_sectors, ret, errno, strerror(errno));
213 /* Try harder for CDrom. */
214 if (bs->type == BDRV_TYPE_CDROM) {
215 lseek(s->fd, offset, SEEK_SET);
216 ret = read(s->fd, buf, count);
217 if (ret == count)
218 goto label__raw_read__success;
219 lseek(s->fd, offset, SEEK_SET);
220 ret = read(s->fd, buf, count);
221 if (ret == count)
222 goto label__raw_read__success;
224 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
225 "] retry read failed %d : %d = %s\n",
226 s->fd, bs->filename, offset, buf, count,
227 bs->total_sectors, ret, errno, strerror(errno));
230 label__raw_read__success:
232 return ret;
236 * offset and count are in bytes, but must be multiples of 512 for files
237 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
239 * This function may be called without alignment if the caller ensures
240 * that O_DIRECT is not in effect.
242 static int raw_pwrite_aligned(BlockDriverState *bs, int64_t offset,
243 const uint8_t *buf, int count)
245 BDRVRawState *s = bs->opaque;
246 int ret;
248 ret = fd_open(bs);
249 if (ret < 0)
250 return ret;
252 if (offset >= 0 && lseek(s->fd, offset, SEEK_SET) == (off_t)-1) {
253 ++(s->lseek_err_cnt);
254 if(s->lseek_err_cnt) {
255 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%"
256 PRId64 "] lseek failed : %d = %s\n",
257 s->fd, bs->filename, offset, buf, count,
258 bs->total_sectors, errno, strerror(errno));
260 return -1;
262 s->lseek_err_cnt = 0;
264 ret = write(s->fd, buf, count);
265 if (ret == count)
266 goto label__raw_write__success;
268 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
269 "] write failed %d : %d = %s\n",
270 s->fd, bs->filename, offset, buf, count,
271 bs->total_sectors, ret, errno, strerror(errno));
273 label__raw_write__success:
275 return ret;
279 #if defined(O_DIRECT) && !defined(QEMU_IMG)
281 * offset and count are in bytes and possibly not aligned. For files opened
282 * with O_DIRECT, necessary alignments are ensured before calling
283 * raw_pread_aligned to do the actual read.
285 static int raw_pread(BlockDriverState *bs, int64_t offset,
286 uint8_t *buf, int count)
288 BDRVRawState *s = bs->opaque;
289 int size, ret, shift, sum;
291 sum = 0;
293 if (s->aligned_buf != NULL) {
295 if (offset & 0x1ff) {
296 /* align offset on a 512 bytes boundary */
298 shift = offset & 0x1ff;
299 size = (shift + count + 0x1ff) & ~0x1ff;
300 if (size > ALIGNED_BUFFER_SIZE)
301 size = ALIGNED_BUFFER_SIZE;
302 ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, size);
303 if (ret < 0)
304 return ret;
306 size = 512 - shift;
307 if (size > count)
308 size = count;
309 memcpy(buf, s->aligned_buf + shift, size);
311 buf += size;
312 offset += size;
313 count -= size;
314 sum += size;
316 if (count == 0)
317 return sum;
319 if (count & 0x1ff || (uintptr_t) buf & 0x1ff) {
321 /* read on aligned buffer */
323 while (count) {
325 size = (count + 0x1ff) & ~0x1ff;
326 if (size > ALIGNED_BUFFER_SIZE)
327 size = ALIGNED_BUFFER_SIZE;
329 ret = raw_pread_aligned(bs, offset, s->aligned_buf, size);
330 if (ret < 0)
331 return ret;
333 size = ret;
334 if (size > count)
335 size = count;
337 memcpy(buf, s->aligned_buf, size);
339 buf += size;
340 offset += size;
341 count -= size;
342 sum += size;
345 return sum;
349 return raw_pread_aligned(bs, offset, buf, count) + sum;
353 * offset and count are in bytes and possibly not aligned. For files opened
354 * with O_DIRECT, necessary alignments are ensured before calling
355 * raw_pwrite_aligned to do the actual write.
357 static int raw_pwrite(BlockDriverState *bs, int64_t offset,
358 const uint8_t *buf, int count)
360 BDRVRawState *s = bs->opaque;
361 int size, ret, shift, sum;
363 sum = 0;
365 if (s->aligned_buf != NULL) {
367 if (offset & 0x1ff) {
368 /* align offset on a 512 bytes boundary */
369 shift = offset & 0x1ff;
370 ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, 512);
371 if (ret < 0)
372 return ret;
374 size = 512 - shift;
375 if (size > count)
376 size = count;
377 memcpy(s->aligned_buf + shift, buf, size);
379 ret = raw_pwrite_aligned(bs, offset - shift, s->aligned_buf, 512);
380 if (ret < 0)
381 return ret;
383 buf += size;
384 offset += size;
385 count -= size;
386 sum += size;
388 if (count == 0)
389 return sum;
391 if (count & 0x1ff || (uintptr_t) buf & 0x1ff) {
393 while ((size = (count & ~0x1ff)) != 0) {
395 if (size > ALIGNED_BUFFER_SIZE)
396 size = ALIGNED_BUFFER_SIZE;
398 memcpy(s->aligned_buf, buf, size);
400 ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, size);
401 if (ret < 0)
402 return ret;
404 buf += ret;
405 offset += ret;
406 count -= ret;
407 sum += ret;
409 /* here, count < 512 because (count & ~0x1ff) == 0 */
410 if (count) {
411 ret = raw_pread_aligned(bs, offset, s->aligned_buf, 512);
412 if (ret < 0)
413 return ret;
414 memcpy(s->aligned_buf, buf, count);
416 ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, 512);
417 if (ret < 0)
418 return ret;
419 if (count < ret)
420 ret = count;
422 sum += ret;
424 return sum;
427 return raw_pwrite_aligned(bs, offset, buf, count) + sum;
430 #else
431 #define raw_pread raw_pread_aligned
432 #define raw_pwrite raw_pwrite_aligned
433 #endif
436 #ifdef CONFIG_AIO
437 /***********************************************************/
438 /* Unix AIO using POSIX AIO */
440 typedef struct RawAIOCB {
441 BlockDriverAIOCB common;
442 struct aiocb aiocb;
443 struct RawAIOCB *next;
444 int ret;
445 } RawAIOCB;
447 static int aio_sig_num = SIGUSR2;
448 static RawAIOCB *first_aio; /* AIO issued */
449 static int aio_initialized = 0;
451 static void aio_signal_handler(int signum)
453 #if !defined(QEMU_IMG) && !defined(QEMU_NBD)
454 CPUState *env = cpu_single_env;
455 if (env) {
456 /* stop the currently executing cpu because a timer occured */
457 cpu_interrupt(env, CPU_INTERRUPT_EXIT);
458 #ifdef USE_KQEMU
459 if (env->kqemu_enabled) {
460 kqemu_cpu_interrupt(env);
462 #endif
464 #endif
467 void qemu_aio_init(void)
469 struct sigaction act;
471 aio_initialized = 1;
473 sigfillset(&act.sa_mask);
474 act.sa_flags = 0; /* do not restart syscalls to interrupt select() */
475 act.sa_handler = aio_signal_handler;
476 sigaction(aio_sig_num, &act, NULL);
478 #if defined(__GLIBC__) && defined(__linux__)
480 /* XXX: aio thread exit seems to hang on RedHat 9 and this init
481 seems to fix the problem. */
482 struct aioinit ai;
483 memset(&ai, 0, sizeof(ai));
484 ai.aio_threads = 1;
485 ai.aio_num = 1;
486 ai.aio_idle_time = 365 * 100000;
487 aio_init(&ai);
489 #endif
492 void qemu_aio_poll(void)
494 RawAIOCB *acb, **pacb;
495 int ret;
497 for(;;) {
498 pacb = &first_aio;
499 for(;;) {
500 acb = *pacb;
501 if (!acb)
502 goto the_end;
503 ret = aio_error(&acb->aiocb);
504 if (ret == ECANCELED) {
505 /* remove the request */
506 *pacb = acb->next;
507 qemu_aio_release(acb);
508 } else if (ret != EINPROGRESS) {
509 /* end of aio */
510 if (ret == 0) {
511 ret = aio_return(&acb->aiocb);
512 if (ret == acb->aiocb.aio_nbytes)
513 ret = 0;
514 else
515 ret = -EINVAL;
516 } else {
517 ret = -ret;
519 /* remove the request */
520 *pacb = acb->next;
521 /* call the callback */
522 acb->common.cb(acb->common.opaque, ret);
523 qemu_aio_release(acb);
524 break;
525 } else {
526 pacb = &acb->next;
530 the_end: ;
533 /* Wait for all IO requests to complete. */
534 void qemu_aio_flush(void)
536 qemu_aio_wait_start();
537 qemu_aio_poll();
538 while (first_aio) {
539 qemu_aio_wait();
541 qemu_aio_wait_end();
544 /* wait until at least one AIO was handled */
545 static sigset_t wait_oset;
547 void qemu_aio_wait_start(void)
549 sigset_t set;
551 if (!aio_initialized)
552 qemu_aio_init();
553 #ifndef QEMU_IMG
554 if (kvm_enabled()) {
555 qemu_kvm_aio_wait_start();
556 return;
558 #endif
559 sigemptyset(&set);
560 sigaddset(&set, aio_sig_num);
561 sigprocmask(SIG_BLOCK, &set, &wait_oset);
564 void qemu_aio_wait(void)
566 sigset_t set;
567 int nb_sigs;
569 #if !defined(QEMU_IMG) && !defined(QEMU_NBD)
570 if (qemu_bh_poll())
571 return;
572 if (kvm_enabled()) {
573 qemu_kvm_aio_wait();
574 qemu_aio_poll();
575 return;
577 #endif
578 sigemptyset(&set);
579 sigaddset(&set, aio_sig_num);
580 sigwait(&set, &nb_sigs);
581 qemu_aio_poll();
584 void qemu_aio_wait_end(void)
586 #ifndef QEMU_IMG
587 if (kvm_enabled()) {
588 qemu_kvm_aio_wait_end();
589 return;
591 #endif
592 sigprocmask(SIG_SETMASK, &wait_oset, NULL);
595 static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
596 int64_t sector_num, uint8_t *buf, int nb_sectors,
597 BlockDriverCompletionFunc *cb, void *opaque)
599 BDRVRawState *s = bs->opaque;
600 RawAIOCB *acb;
602 if (fd_open(bs) < 0)
603 return NULL;
605 acb = qemu_aio_get(bs, cb, opaque);
606 if (!acb)
607 return NULL;
608 acb->aiocb.aio_fildes = s->fd;
609 acb->aiocb.aio_sigevent.sigev_signo = aio_sig_num;
610 acb->aiocb.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
611 acb->aiocb.aio_buf = buf;
612 if (nb_sectors < 0)
613 acb->aiocb.aio_nbytes = -nb_sectors;
614 else
615 acb->aiocb.aio_nbytes = nb_sectors * 512;
616 acb->aiocb.aio_offset = sector_num * 512;
617 acb->next = first_aio;
618 first_aio = acb;
619 return acb;
622 #if !defined(QEMU_IMG) && !defined(QEMU_NBD)
623 static void raw_aio_em_cb(void* opaque)
625 RawAIOCB *acb = opaque;
626 acb->common.cb(acb->common.opaque, acb->ret);
627 qemu_aio_release(acb);
629 #endif
631 static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
632 int64_t sector_num, uint8_t *buf, int nb_sectors,
633 BlockDriverCompletionFunc *cb, void *opaque)
635 RawAIOCB *acb;
638 * If O_DIRECT is used and the buffer is not aligned fall back
639 * to synchronous IO.
641 #if defined(O_DIRECT) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
642 BDRVRawState *s = bs->opaque;
644 if (unlikely(s->aligned_buf != NULL && ((uintptr_t) buf % 512))) {
645 QEMUBH *bh;
646 acb = qemu_aio_get(bs, cb, opaque);
647 acb->ret = raw_pread(bs, 512 * sector_num, buf, 512 * nb_sectors);
648 bh = qemu_bh_new(raw_aio_em_cb, acb);
649 qemu_bh_schedule(bh);
650 return &acb->common;
652 #endif
654 acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
655 if (!acb)
656 return NULL;
657 if (aio_read(&acb->aiocb) < 0) {
658 qemu_aio_release(acb);
659 return NULL;
661 return &acb->common;
664 static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
665 int64_t sector_num, const uint8_t *buf, int nb_sectors,
666 BlockDriverCompletionFunc *cb, void *opaque)
668 RawAIOCB *acb;
671 * If O_DIRECT is used and the buffer is not aligned fall back
672 * to synchronous IO.
674 #if defined(O_DIRECT) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
675 BDRVRawState *s = bs->opaque;
677 if (unlikely(s->aligned_buf != NULL && ((uintptr_t) buf % 512))) {
678 QEMUBH *bh;
679 acb = qemu_aio_get(bs, cb, opaque);
680 acb->ret = raw_pwrite(bs, 512 * sector_num, buf, 512 * nb_sectors);
681 bh = qemu_bh_new(raw_aio_em_cb, acb);
682 qemu_bh_schedule(bh);
683 return &acb->common;
685 #endif
687 acb = raw_aio_setup(bs, sector_num, (uint8_t*)buf, nb_sectors, cb, opaque);
688 if (!acb)
689 return NULL;
690 if (aio_write(&acb->aiocb) < 0) {
691 qemu_aio_release(acb);
692 return NULL;
694 return &acb->common;
697 static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
699 int ret;
700 RawAIOCB *acb = (RawAIOCB *)blockacb;
701 RawAIOCB **pacb;
703 ret = aio_cancel(acb->aiocb.aio_fildes, &acb->aiocb);
704 if (ret == AIO_NOTCANCELED) {
705 /* fail safe: if the aio could not be canceled, we wait for
706 it */
707 while (aio_error(&acb->aiocb) == EINPROGRESS);
710 /* remove the callback from the queue */
711 pacb = &first_aio;
712 for(;;) {
713 if (*pacb == NULL) {
714 break;
715 } else if (*pacb == acb) {
716 *pacb = acb->next;
717 qemu_aio_release(acb);
718 break;
720 pacb = &acb->next;
724 # else /* CONFIG_AIO */
726 void qemu_aio_init(void)
730 void qemu_aio_poll(void)
734 void qemu_aio_flush(void)
738 void qemu_aio_wait_start(void)
742 void qemu_aio_wait(void)
744 #if !defined(QEMU_IMG) && !defined(QEMU_NBD)
745 qemu_bh_poll();
746 #endif
749 void qemu_aio_wait_end(void)
753 #endif /* CONFIG_AIO */
755 static void raw_close(BlockDriverState *bs)
757 BDRVRawState *s = bs->opaque;
758 if (s->fd >= 0) {
759 close(s->fd);
760 s->fd = -1;
761 #if defined(O_DIRECT) && !defined(QEMU_IMG)
762 if (s->aligned_buf != NULL)
763 qemu_free(s->aligned_buf);
764 #endif
768 static int raw_truncate(BlockDriverState *bs, int64_t offset)
770 BDRVRawState *s = bs->opaque;
771 if (s->type != FTYPE_FILE)
772 return -ENOTSUP;
773 if (ftruncate(s->fd, offset) < 0)
774 return -errno;
775 return 0;
778 #ifdef __OpenBSD__
779 static int64_t raw_getlength(BlockDriverState *bs)
781 BDRVRawState *s = bs->opaque;
782 int fd = s->fd;
783 struct stat st;
785 if (fstat(fd, &st))
786 return -1;
787 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
788 struct disklabel dl;
790 if (ioctl(fd, DIOCGDINFO, &dl))
791 return -1;
792 return (uint64_t)dl.d_secsize *
793 dl.d_partitions[DISKPART(st.st_rdev)].p_size;
794 } else
795 return st.st_size;
797 #else /* !__OpenBSD__ */
798 static int64_t raw_getlength(BlockDriverState *bs)
800 BDRVRawState *s = bs->opaque;
801 int fd = s->fd;
802 int64_t size;
803 #ifdef _BSD
804 struct stat sb;
805 #endif
806 #ifdef __sun__
807 struct dk_minfo minfo;
808 int rv;
809 #endif
810 int ret;
812 ret = fd_open(bs);
813 if (ret < 0)
814 return ret;
816 #ifdef _BSD
817 if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
818 #ifdef DIOCGMEDIASIZE
819 if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
820 #endif
821 #ifdef CONFIG_COCOA
822 size = LONG_LONG_MAX;
823 #else
824 size = lseek(fd, 0LL, SEEK_END);
825 #endif
826 } else
827 #endif
828 #ifdef __sun__
830 * use the DKIOCGMEDIAINFO ioctl to read the size.
832 rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo );
833 if ( rv != -1 ) {
834 size = minfo.dki_lbsize * minfo.dki_capacity;
835 } else /* there are reports that lseek on some devices
836 fails, but irc discussion said that contingency
837 on contingency was overkill */
838 #endif
840 size = lseek(fd, 0, SEEK_END);
842 return size;
844 #endif
846 static int raw_create(const char *filename, int64_t total_size,
847 const char *backing_file, int flags)
849 int fd;
851 if (flags || backing_file)
852 return -ENOTSUP;
854 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
855 0644);
856 if (fd < 0)
857 return -EIO;
858 ftruncate(fd, total_size * 512);
859 close(fd);
860 return 0;
863 static void raw_flush(BlockDriverState *bs)
865 BDRVRawState *s = bs->opaque;
866 fsync(s->fd);
869 BlockDriver bdrv_raw = {
870 "raw",
871 sizeof(BDRVRawState),
872 NULL, /* no probe for protocols */
873 raw_open,
874 NULL,
875 NULL,
876 raw_close,
877 raw_create,
878 raw_flush,
880 #ifdef CONFIG_AIO
881 .bdrv_aio_read = raw_aio_read,
882 .bdrv_aio_write = raw_aio_write,
883 .bdrv_aio_cancel = raw_aio_cancel,
884 .aiocb_size = sizeof(RawAIOCB),
885 #endif
886 .protocol_name = "file",
887 .bdrv_pread = raw_pread,
888 .bdrv_pwrite = raw_pwrite,
889 .bdrv_truncate = raw_truncate,
890 .bdrv_getlength = raw_getlength,
893 /***********************************************/
894 /* host device */
896 #ifdef CONFIG_COCOA
897 static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
898 static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
900 kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
902 kern_return_t kernResult;
903 mach_port_t masterPort;
904 CFMutableDictionaryRef classesToMatch;
906 kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
907 if ( KERN_SUCCESS != kernResult ) {
908 printf( "IOMasterPort returned %d\n", kernResult );
911 classesToMatch = IOServiceMatching( kIOCDMediaClass );
912 if ( classesToMatch == NULL ) {
913 printf( "IOServiceMatching returned a NULL dictionary.\n" );
914 } else {
915 CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
917 kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
918 if ( KERN_SUCCESS != kernResult )
920 printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
923 return kernResult;
926 kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
928 io_object_t nextMedia;
929 kern_return_t kernResult = KERN_FAILURE;
930 *bsdPath = '\0';
931 nextMedia = IOIteratorNext( mediaIterator );
932 if ( nextMedia )
934 CFTypeRef bsdPathAsCFString;
935 bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
936 if ( bsdPathAsCFString ) {
937 size_t devPathLength;
938 strcpy( bsdPath, _PATH_DEV );
939 strcat( bsdPath, "r" );
940 devPathLength = strlen( bsdPath );
941 if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
942 kernResult = KERN_SUCCESS;
944 CFRelease( bsdPathAsCFString );
946 IOObjectRelease( nextMedia );
949 return kernResult;
952 #endif
954 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
956 BDRVRawState *s = bs->opaque;
957 int fd, open_flags, ret;
959 #ifdef CONFIG_COCOA
960 if (strstart(filename, "/dev/cdrom", NULL)) {
961 kern_return_t kernResult;
962 io_iterator_t mediaIterator;
963 char bsdPath[ MAXPATHLEN ];
964 int fd;
966 kernResult = FindEjectableCDMedia( &mediaIterator );
967 kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
969 if ( bsdPath[ 0 ] != '\0' ) {
970 strcat(bsdPath,"s0");
971 /* some CDs don't have a partition 0 */
972 fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
973 if (fd < 0) {
974 bsdPath[strlen(bsdPath)-1] = '1';
975 } else {
976 close(fd);
978 filename = bsdPath;
981 if ( mediaIterator )
982 IOObjectRelease( mediaIterator );
984 #endif
985 open_flags = O_BINARY;
986 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
987 open_flags |= O_RDWR;
988 } else {
989 open_flags |= O_RDONLY;
990 bs->read_only = 1;
992 #ifdef O_DIRECT
993 if (flags & BDRV_O_DIRECT)
994 open_flags |= O_DIRECT;
995 #endif
997 s->type = FTYPE_FILE;
998 #if defined(__linux__)
999 if (strstart(filename, "/dev/cd", NULL)) {
1000 /* open will not fail even if no CD is inserted */
1001 open_flags |= O_NONBLOCK;
1002 s->type = FTYPE_CD;
1003 } else if (strstart(filename, "/dev/fd", NULL)) {
1004 s->type = FTYPE_FD;
1005 s->fd_open_flags = open_flags;
1006 /* open will not fail even if no floppy is inserted */
1007 open_flags |= O_NONBLOCK;
1008 } else if (strstart(filename, "/dev/sg", NULL)) {
1009 bs->sg = 1;
1011 #endif
1012 fd = open(filename, open_flags, 0644);
1013 if (fd < 0) {
1014 ret = -errno;
1015 if (ret == -EROFS)
1016 ret = -EACCES;
1017 return ret;
1019 s->fd = fd;
1020 #if defined(__linux__)
1021 /* close fd so that we can reopen it as needed */
1022 if (s->type == FTYPE_FD) {
1023 close(s->fd);
1024 s->fd = -1;
1025 s->fd_media_changed = 1;
1027 #endif
1028 return 0;
1031 #if defined(__linux__) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
1033 /* Note: we do not have a reliable method to detect if the floppy is
1034 present. The current method is to try to open the floppy at every
1035 I/O and to keep it opened during a few hundreds of ms. */
1036 static int fd_open(BlockDriverState *bs)
1038 BDRVRawState *s = bs->opaque;
1039 int last_media_present;
1041 if (s->type != FTYPE_FD)
1042 return 0;
1043 last_media_present = (s->fd >= 0);
1044 if (s->fd >= 0 &&
1045 (qemu_get_clock(rt_clock) - s->fd_open_time) >= FD_OPEN_TIMEOUT) {
1046 close(s->fd);
1047 s->fd = -1;
1048 #ifdef DEBUG_FLOPPY
1049 printf("Floppy closed\n");
1050 #endif
1052 if (s->fd < 0) {
1053 if (s->fd_got_error &&
1054 (qemu_get_clock(rt_clock) - s->fd_error_time) < FD_OPEN_TIMEOUT) {
1055 #ifdef DEBUG_FLOPPY
1056 printf("No floppy (open delayed)\n");
1057 #endif
1058 return -EIO;
1060 s->fd = open(bs->filename, s->fd_open_flags);
1061 if (s->fd < 0) {
1062 s->fd_error_time = qemu_get_clock(rt_clock);
1063 s->fd_got_error = 1;
1064 if (last_media_present)
1065 s->fd_media_changed = 1;
1066 #ifdef DEBUG_FLOPPY
1067 printf("No floppy\n");
1068 #endif
1069 return -EIO;
1071 #ifdef DEBUG_FLOPPY
1072 printf("Floppy opened\n");
1073 #endif
1075 if (!last_media_present)
1076 s->fd_media_changed = 1;
1077 s->fd_open_time = qemu_get_clock(rt_clock);
1078 s->fd_got_error = 0;
1079 return 0;
1081 #else
1082 static int fd_open(BlockDriverState *bs)
1084 return 0;
1086 #endif
1088 #if defined(__linux__)
1090 static int raw_is_inserted(BlockDriverState *bs)
1092 BDRVRawState *s = bs->opaque;
1093 int ret;
1095 switch(s->type) {
1096 case FTYPE_CD:
1097 ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
1098 if (ret == CDS_DISC_OK)
1099 return 1;
1100 else
1101 return 0;
1102 break;
1103 case FTYPE_FD:
1104 ret = fd_open(bs);
1105 return (ret >= 0);
1106 default:
1107 return 1;
1111 /* currently only used by fdc.c, but a CD version would be good too */
1112 static int raw_media_changed(BlockDriverState *bs)
1114 BDRVRawState *s = bs->opaque;
1116 switch(s->type) {
1117 case FTYPE_FD:
1119 int ret;
1120 /* XXX: we do not have a true media changed indication. It
1121 does not work if the floppy is changed without trying
1122 to read it */
1123 fd_open(bs);
1124 ret = s->fd_media_changed;
1125 s->fd_media_changed = 0;
1126 #ifdef DEBUG_FLOPPY
1127 printf("Floppy changed=%d\n", ret);
1128 #endif
1129 return ret;
1131 default:
1132 return -ENOTSUP;
1136 static int raw_eject(BlockDriverState *bs, int eject_flag)
1138 BDRVRawState *s = bs->opaque;
1140 switch(s->type) {
1141 case FTYPE_CD:
1142 if (eject_flag) {
1143 if (ioctl (s->fd, CDROMEJECT, NULL) < 0)
1144 perror("CDROMEJECT");
1145 } else {
1146 if (ioctl (s->fd, CDROMCLOSETRAY, NULL) < 0)
1147 perror("CDROMEJECT");
1149 break;
1150 case FTYPE_FD:
1152 int fd;
1153 if (s->fd >= 0) {
1154 close(s->fd);
1155 s->fd = -1;
1157 fd = open(bs->filename, s->fd_open_flags | O_NONBLOCK);
1158 if (fd >= 0) {
1159 if (ioctl(fd, FDEJECT, 0) < 0)
1160 perror("FDEJECT");
1161 close(fd);
1164 break;
1165 default:
1166 return -ENOTSUP;
1168 return 0;
1171 static int raw_set_locked(BlockDriverState *bs, int locked)
1173 BDRVRawState *s = bs->opaque;
1175 switch(s->type) {
1176 case FTYPE_CD:
1177 if (ioctl (s->fd, CDROM_LOCKDOOR, locked) < 0) {
1178 /* Note: an error can happen if the distribution automatically
1179 mounts the CD-ROM */
1180 // perror("CDROM_LOCKDOOR");
1182 break;
1183 default:
1184 return -ENOTSUP;
1186 return 0;
1189 static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1191 BDRVRawState *s = bs->opaque;
1193 return ioctl(s->fd, req, buf);
1195 #else
1197 static int raw_is_inserted(BlockDriverState *bs)
1199 return 1;
1202 static int raw_media_changed(BlockDriverState *bs)
1204 return -ENOTSUP;
1207 static int raw_eject(BlockDriverState *bs, int eject_flag)
1209 return -ENOTSUP;
1212 static int raw_set_locked(BlockDriverState *bs, int locked)
1214 return -ENOTSUP;
1217 static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1219 return -ENOTSUP;
1221 #endif /* !linux */
1223 BlockDriver bdrv_host_device = {
1224 "host_device",
1225 sizeof(BDRVRawState),
1226 NULL, /* no probe for protocols */
1227 hdev_open,
1228 NULL,
1229 NULL,
1230 raw_close,
1231 NULL,
1232 raw_flush,
1234 #ifdef CONFIG_AIO
1235 .bdrv_aio_read = raw_aio_read,
1236 .bdrv_aio_write = raw_aio_write,
1237 .bdrv_aio_cancel = raw_aio_cancel,
1238 .aiocb_size = sizeof(RawAIOCB),
1239 #endif
1240 .bdrv_pread = raw_pread,
1241 .bdrv_pwrite = raw_pwrite,
1242 .bdrv_getlength = raw_getlength,
1244 /* removable device support */
1245 .bdrv_is_inserted = raw_is_inserted,
1246 .bdrv_media_changed = raw_media_changed,
1247 .bdrv_eject = raw_eject,
1248 .bdrv_set_locked = raw_set_locked,
1249 /* generic scsi device */
1250 .bdrv_ioctl = raw_ioctl,