kvm: libkvm: remove user_alloc field from slot representation
[qemu-kvm/fedora.git] / block-raw-posix.c
blob6bc6ed00b297f3449a6553f4cbb0febf31ded903
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 <signal.h>
66 #include <sys/disk.h>
67 #endif
69 #ifdef __OpenBSD__
70 #include <sys/ioctl.h>
71 #include <sys/disklabel.h>
72 #include <sys/dkio.h>
73 #endif
75 //#define DEBUG_FLOPPY
77 //#define DEBUG_BLOCK
78 #if defined(DEBUG_BLOCK) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
79 #define DEBUG_BLOCK_PRINT(formatCstr, args...) do { if (loglevel != 0) \
80 { fprintf(logfile, formatCstr, ##args); fflush(logfile); } } while (0)
81 #else
82 #define DEBUG_BLOCK_PRINT(formatCstr, args...)
83 #endif
85 #define FTYPE_FILE 0
86 #define FTYPE_CD 1
87 #define FTYPE_FD 2
89 #define ALIGNED_BUFFER_SIZE (32 * 512)
91 /* if the FD is not accessed during that time (in ms), we try to
92 reopen it to see if the disk has been changed */
93 #define FD_OPEN_TIMEOUT 1000
95 typedef struct BDRVRawState {
96 int fd;
97 int type;
98 unsigned int lseek_err_cnt;
99 #if defined(__linux__)
100 /* linux floppy specific */
101 int fd_open_flags;
102 int64_t fd_open_time;
103 int64_t fd_error_time;
104 int fd_got_error;
105 int fd_media_changed;
106 #endif
107 #if defined(O_DIRECT) && !defined(QEMU_IMG)
108 uint8_t* aligned_buf;
109 #endif
110 } BDRVRawState;
112 static int fd_open(BlockDriverState *bs);
114 static int raw_open(BlockDriverState *bs, const char *filename, int flags)
116 BDRVRawState *s = bs->opaque;
117 int fd, open_flags, ret;
119 s->lseek_err_cnt = 0;
121 open_flags = O_BINARY;
122 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
123 open_flags |= O_RDWR;
124 } else {
125 open_flags |= O_RDONLY;
126 bs->read_only = 1;
128 if (flags & BDRV_O_CREAT)
129 open_flags |= O_CREAT | O_TRUNC;
130 #ifdef O_DIRECT
131 if (flags & BDRV_O_DIRECT)
132 open_flags |= O_DIRECT;
133 #endif
135 s->type = FTYPE_FILE;
137 fd = open(filename, open_flags, 0644);
138 if (fd < 0) {
139 ret = -errno;
140 if (ret == -EROFS)
141 ret = -EACCES;
142 return ret;
144 s->fd = fd;
145 #if defined(O_DIRECT) && !defined(QEMU_IMG)
146 s->aligned_buf = NULL;
147 if (flags & BDRV_O_DIRECT) {
148 s->aligned_buf = qemu_memalign(512, ALIGNED_BUFFER_SIZE);
149 if (s->aligned_buf == NULL) {
150 ret = -errno;
151 close(fd);
152 return ret;
155 #endif
156 return 0;
159 /* XXX: use host sector size if necessary with:
160 #ifdef DIOCGSECTORSIZE
162 unsigned int sectorsize = 512;
163 if (!ioctl(fd, DIOCGSECTORSIZE, &sectorsize) &&
164 sectorsize > bufsize)
165 bufsize = sectorsize;
167 #endif
168 #ifdef CONFIG_COCOA
169 u_int32_t blockSize = 512;
170 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
171 bufsize = blockSize;
173 #endif
177 * offset and count are in bytes, but must be multiples of 512 for files
178 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
180 * This function may be called without alignment if the caller ensures
181 * that O_DIRECT is not in effect.
183 static int raw_pread_aligned(BlockDriverState *bs, int64_t offset,
184 uint8_t *buf, int count)
186 BDRVRawState *s = bs->opaque;
187 int ret;
189 ret = fd_open(bs);
190 if (ret < 0)
191 return ret;
193 if (offset >= 0 && lseek(s->fd, offset, SEEK_SET) == (off_t)-1) {
194 ++(s->lseek_err_cnt);
195 if(s->lseek_err_cnt <= 10) {
196 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
197 "] lseek failed : %d = %s\n",
198 s->fd, bs->filename, offset, buf, count,
199 bs->total_sectors, errno, strerror(errno));
201 return -1;
203 s->lseek_err_cnt=0;
205 ret = read(s->fd, buf, count);
206 if (ret == count)
207 goto label__raw_read__success;
209 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
210 "] read failed %d : %d = %s\n",
211 s->fd, bs->filename, offset, buf, count,
212 bs->total_sectors, ret, errno, strerror(errno));
214 /* Try harder for CDrom. */
215 if (bs->type == BDRV_TYPE_CDROM) {
216 lseek(s->fd, offset, SEEK_SET);
217 ret = read(s->fd, buf, count);
218 if (ret == count)
219 goto label__raw_read__success;
220 lseek(s->fd, offset, SEEK_SET);
221 ret = read(s->fd, buf, count);
222 if (ret == count)
223 goto label__raw_read__success;
225 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
226 "] retry read failed %d : %d = %s\n",
227 s->fd, bs->filename, offset, buf, count,
228 bs->total_sectors, ret, errno, strerror(errno));
231 label__raw_read__success:
233 return ret;
237 * offset and count are in bytes, but must be multiples of 512 for files
238 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
240 * This function may be called without alignment if the caller ensures
241 * that O_DIRECT is not in effect.
243 static int raw_pwrite_aligned(BlockDriverState *bs, int64_t offset,
244 const uint8_t *buf, int count)
246 BDRVRawState *s = bs->opaque;
247 int ret;
249 ret = fd_open(bs);
250 if (ret < 0)
251 return ret;
253 if (offset >= 0 && lseek(s->fd, offset, SEEK_SET) == (off_t)-1) {
254 ++(s->lseek_err_cnt);
255 if(s->lseek_err_cnt) {
256 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%"
257 PRId64 "] lseek failed : %d = %s\n",
258 s->fd, bs->filename, offset, buf, count,
259 bs->total_sectors, errno, strerror(errno));
261 return -1;
263 s->lseek_err_cnt = 0;
265 ret = write(s->fd, buf, count);
266 if (ret == count)
267 goto label__raw_write__success;
269 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
270 "] write failed %d : %d = %s\n",
271 s->fd, bs->filename, offset, buf, count,
272 bs->total_sectors, ret, errno, strerror(errno));
274 label__raw_write__success:
276 return ret;
280 #if defined(O_DIRECT) && !defined(QEMU_IMG)
282 * offset and count are in bytes and possibly not aligned. For files opened
283 * with O_DIRECT, necessary alignments are ensured before calling
284 * raw_pread_aligned to do the actual read.
286 static int raw_pread(BlockDriverState *bs, int64_t offset,
287 uint8_t *buf, int count)
289 BDRVRawState *s = bs->opaque;
290 int size, ret, shift, sum;
292 sum = 0;
294 if (s->aligned_buf != NULL) {
296 if (offset & 0x1ff) {
297 /* align offset on a 512 bytes boundary */
299 shift = offset & 0x1ff;
300 size = (shift + count + 0x1ff) & ~0x1ff;
301 if (size > ALIGNED_BUFFER_SIZE)
302 size = ALIGNED_BUFFER_SIZE;
303 ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, size);
304 if (ret < 0)
305 return ret;
307 size = 512 - shift;
308 if (size > count)
309 size = count;
310 memcpy(buf, s->aligned_buf + shift, size);
312 buf += size;
313 offset += size;
314 count -= size;
315 sum += size;
317 if (count == 0)
318 return sum;
320 if (count & 0x1ff || (uintptr_t) buf & 0x1ff) {
322 /* read on aligned buffer */
324 while (count) {
326 size = (count + 0x1ff) & ~0x1ff;
327 if (size > ALIGNED_BUFFER_SIZE)
328 size = ALIGNED_BUFFER_SIZE;
330 ret = raw_pread_aligned(bs, offset, s->aligned_buf, size);
331 if (ret < 0)
332 return ret;
334 size = ret;
335 if (size > count)
336 size = count;
338 memcpy(buf, s->aligned_buf, size);
340 buf += size;
341 offset += size;
342 count -= size;
343 sum += size;
346 return sum;
350 return raw_pread_aligned(bs, offset, buf, count) + sum;
354 * offset and count are in bytes and possibly not aligned. For files opened
355 * with O_DIRECT, necessary alignments are ensured before calling
356 * raw_pwrite_aligned to do the actual write.
358 static int raw_pwrite(BlockDriverState *bs, int64_t offset,
359 const uint8_t *buf, int count)
361 BDRVRawState *s = bs->opaque;
362 int size, ret, shift, sum;
364 sum = 0;
366 if (s->aligned_buf != NULL) {
368 if (offset & 0x1ff) {
369 /* align offset on a 512 bytes boundary */
370 shift = offset & 0x1ff;
371 ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, 512);
372 if (ret < 0)
373 return ret;
375 size = 512 - shift;
376 if (size > count)
377 size = count;
378 memcpy(s->aligned_buf + shift, buf, size);
380 ret = raw_pwrite_aligned(bs, offset - shift, s->aligned_buf, 512);
381 if (ret < 0)
382 return ret;
384 buf += size;
385 offset += size;
386 count -= size;
387 sum += size;
389 if (count == 0)
390 return sum;
392 if (count & 0x1ff || (uintptr_t) buf & 0x1ff) {
394 while ((size = (count & ~0x1ff)) != 0) {
396 if (size > ALIGNED_BUFFER_SIZE)
397 size = ALIGNED_BUFFER_SIZE;
399 memcpy(s->aligned_buf, buf, size);
401 ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, size);
402 if (ret < 0)
403 return ret;
405 buf += ret;
406 offset += ret;
407 count -= ret;
408 sum += ret;
410 /* here, count < 512 because (count & ~0x1ff) == 0 */
411 if (count) {
412 ret = raw_pread_aligned(bs, offset, s->aligned_buf, 512);
413 if (ret < 0)
414 return ret;
415 memcpy(s->aligned_buf, buf, count);
417 ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, 512);
418 if (ret < 0)
419 return ret;
420 if (count < ret)
421 ret = count;
423 sum += ret;
425 return sum;
428 return raw_pwrite_aligned(bs, offset, buf, count) + sum;
431 #else
432 #define raw_pread raw_pread_aligned
433 #define raw_pwrite raw_pwrite_aligned
434 #endif
437 #ifdef CONFIG_AIO
438 /***********************************************************/
439 /* Unix AIO using POSIX AIO */
441 typedef struct RawAIOCB {
442 BlockDriverAIOCB common;
443 struct aiocb aiocb;
444 struct RawAIOCB *next;
445 int ret;
446 } RawAIOCB;
448 static int aio_sig_num = SIGUSR2;
449 static RawAIOCB *first_aio; /* AIO issued */
450 static int aio_initialized = 0;
452 static void aio_signal_handler(int signum)
454 #if !defined(QEMU_IMG) && !defined(QEMU_NBD)
455 CPUState *env = cpu_single_env;
456 if (env) {
457 /* stop the currently executing cpu because a timer occured */
458 cpu_interrupt(env, CPU_INTERRUPT_EXIT);
459 #ifdef USE_KQEMU
460 if (env->kqemu_enabled) {
461 kqemu_cpu_interrupt(env);
463 #endif
465 #endif
468 void qemu_aio_init(void)
470 struct sigaction act;
472 aio_initialized = 1;
474 sigfillset(&act.sa_mask);
475 act.sa_flags = 0; /* do not restart syscalls to interrupt select() */
476 act.sa_handler = aio_signal_handler;
477 sigaction(aio_sig_num, &act, NULL);
479 #if defined(__GLIBC__) && defined(__linux__)
481 /* XXX: aio thread exit seems to hang on RedHat 9 and this init
482 seems to fix the problem. */
483 struct aioinit ai;
484 memset(&ai, 0, sizeof(ai));
485 ai.aio_threads = 1;
486 ai.aio_num = 1;
487 ai.aio_idle_time = 365 * 100000;
488 aio_init(&ai);
490 #endif
493 void qemu_aio_poll(void)
495 RawAIOCB *acb, **pacb;
496 int ret;
498 for(;;) {
499 pacb = &first_aio;
500 for(;;) {
501 acb = *pacb;
502 if (!acb)
503 goto the_end;
504 ret = aio_error(&acb->aiocb);
505 if (ret == ECANCELED) {
506 /* remove the request */
507 *pacb = acb->next;
508 qemu_aio_release(acb);
509 } else if (ret != EINPROGRESS) {
510 /* end of aio */
511 if (ret == 0) {
512 ret = aio_return(&acb->aiocb);
513 if (ret == acb->aiocb.aio_nbytes)
514 ret = 0;
515 else
516 ret = -EINVAL;
517 } else {
518 ret = -ret;
520 /* remove the request */
521 *pacb = acb->next;
522 /* call the callback */
523 acb->common.cb(acb->common.opaque, ret);
524 qemu_aio_release(acb);
525 break;
526 } else {
527 pacb = &acb->next;
531 the_end: ;
534 /* Wait for all IO requests to complete. */
535 void qemu_aio_flush(void)
537 qemu_aio_wait_start();
538 qemu_aio_poll();
539 while (first_aio) {
540 qemu_aio_wait();
542 qemu_aio_wait_end();
545 /* wait until at least one AIO was handled */
546 static sigset_t wait_oset;
548 void qemu_aio_wait_start(void)
550 sigset_t set;
552 if (!aio_initialized)
553 qemu_aio_init();
554 #ifndef QEMU_IMG
555 if (kvm_enabled()) {
556 qemu_kvm_aio_wait_start();
557 return;
559 #endif
560 sigemptyset(&set);
561 sigaddset(&set, aio_sig_num);
562 sigprocmask(SIG_BLOCK, &set, &wait_oset);
565 void qemu_aio_wait(void)
567 sigset_t set;
568 int nb_sigs;
570 #if !defined(QEMU_IMG) && !defined(QEMU_NBD)
571 if (qemu_bh_poll())
572 return;
573 if (kvm_enabled()) {
574 qemu_kvm_aio_wait();
575 qemu_aio_poll();
576 return;
578 #endif
579 sigemptyset(&set);
580 sigaddset(&set, aio_sig_num);
581 sigwait(&set, &nb_sigs);
582 qemu_aio_poll();
585 void qemu_aio_wait_end(void)
587 #ifndef QEMU_IMG
588 if (kvm_enabled()) {
589 qemu_kvm_aio_wait_end();
590 return;
592 #endif
593 sigprocmask(SIG_SETMASK, &wait_oset, NULL);
596 static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
597 int64_t sector_num, uint8_t *buf, int nb_sectors,
598 BlockDriverCompletionFunc *cb, void *opaque)
600 BDRVRawState *s = bs->opaque;
601 RawAIOCB *acb;
603 if (fd_open(bs) < 0)
604 return NULL;
606 acb = qemu_aio_get(bs, cb, opaque);
607 if (!acb)
608 return NULL;
609 acb->aiocb.aio_fildes = s->fd;
610 acb->aiocb.aio_sigevent.sigev_signo = aio_sig_num;
611 acb->aiocb.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
612 acb->aiocb.aio_buf = buf;
613 if (nb_sectors < 0)
614 acb->aiocb.aio_nbytes = -nb_sectors;
615 else
616 acb->aiocb.aio_nbytes = nb_sectors * 512;
617 acb->aiocb.aio_offset = sector_num * 512;
618 acb->next = first_aio;
619 first_aio = acb;
620 return acb;
623 #if !defined(QEMU_IMG) && !defined(QEMU_NBD)
624 static void raw_aio_em_cb(void* opaque)
626 RawAIOCB *acb = opaque;
627 acb->common.cb(acb->common.opaque, acb->ret);
628 qemu_aio_release(acb);
630 #endif
632 static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
633 int64_t sector_num, uint8_t *buf, int nb_sectors,
634 BlockDriverCompletionFunc *cb, void *opaque)
636 RawAIOCB *acb;
639 * If O_DIRECT is used and the buffer is not aligned fall back
640 * to synchronous IO.
642 #if defined(O_DIRECT) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
643 BDRVRawState *s = bs->opaque;
645 if (unlikely(s->aligned_buf != NULL && ((uintptr_t) buf % 512))) {
646 QEMUBH *bh;
647 acb = qemu_aio_get(bs, cb, opaque);
648 acb->ret = raw_pread(bs, 512 * sector_num, buf, 512 * nb_sectors);
649 bh = qemu_bh_new(raw_aio_em_cb, acb);
650 qemu_bh_schedule(bh);
651 return &acb->common;
653 #endif
655 acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
656 if (!acb)
657 return NULL;
658 if (aio_read(&acb->aiocb) < 0) {
659 qemu_aio_release(acb);
660 return NULL;
662 return &acb->common;
665 static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
666 int64_t sector_num, const uint8_t *buf, int nb_sectors,
667 BlockDriverCompletionFunc *cb, void *opaque)
669 RawAIOCB *acb;
672 * If O_DIRECT is used and the buffer is not aligned fall back
673 * to synchronous IO.
675 #if defined(O_DIRECT) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
676 BDRVRawState *s = bs->opaque;
678 if (unlikely(s->aligned_buf != NULL && ((uintptr_t) buf % 512))) {
679 QEMUBH *bh;
680 acb = qemu_aio_get(bs, cb, opaque);
681 acb->ret = raw_pwrite(bs, 512 * sector_num, buf, 512 * nb_sectors);
682 bh = qemu_bh_new(raw_aio_em_cb, acb);
683 qemu_bh_schedule(bh);
684 return &acb->common;
686 #endif
688 acb = raw_aio_setup(bs, sector_num, (uint8_t*)buf, nb_sectors, cb, opaque);
689 if (!acb)
690 return NULL;
691 if (aio_write(&acb->aiocb) < 0) {
692 qemu_aio_release(acb);
693 return NULL;
695 return &acb->common;
698 static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
700 int ret;
701 RawAIOCB *acb = (RawAIOCB *)blockacb;
702 RawAIOCB **pacb;
704 ret = aio_cancel(acb->aiocb.aio_fildes, &acb->aiocb);
705 if (ret == AIO_NOTCANCELED) {
706 /* fail safe: if the aio could not be canceled, we wait for
707 it */
708 while (aio_error(&acb->aiocb) == EINPROGRESS);
711 /* remove the callback from the queue */
712 pacb = &first_aio;
713 for(;;) {
714 if (*pacb == NULL) {
715 break;
716 } else if (*pacb == acb) {
717 *pacb = acb->next;
718 qemu_aio_release(acb);
719 break;
721 pacb = &acb->next;
725 # else /* CONFIG_AIO */
727 void qemu_aio_init(void)
731 void qemu_aio_poll(void)
735 void qemu_aio_flush(void)
739 void qemu_aio_wait_start(void)
743 void qemu_aio_wait(void)
745 #if !defined(QEMU_IMG) && !defined(QEMU_NBD)
746 qemu_bh_poll();
747 #endif
750 void qemu_aio_wait_end(void)
754 #endif /* CONFIG_AIO */
756 static void raw_close(BlockDriverState *bs)
758 BDRVRawState *s = bs->opaque;
759 if (s->fd >= 0) {
760 close(s->fd);
761 s->fd = -1;
762 #if defined(O_DIRECT) && !defined(QEMU_IMG)
763 if (s->aligned_buf != NULL)
764 qemu_free(s->aligned_buf);
765 #endif
769 static int raw_truncate(BlockDriverState *bs, int64_t offset)
771 BDRVRawState *s = bs->opaque;
772 if (s->type != FTYPE_FILE)
773 return -ENOTSUP;
774 if (ftruncate(s->fd, offset) < 0)
775 return -errno;
776 return 0;
779 #ifdef __OpenBSD__
780 static int64_t raw_getlength(BlockDriverState *bs)
782 BDRVRawState *s = bs->opaque;
783 int fd = s->fd;
784 struct stat st;
786 if (fstat(fd, &st))
787 return -1;
788 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
789 struct disklabel dl;
791 if (ioctl(fd, DIOCGDINFO, &dl))
792 return -1;
793 return (uint64_t)dl.d_secsize *
794 dl.d_partitions[DISKPART(st.st_rdev)].p_size;
795 } else
796 return st.st_size;
798 #else /* !__OpenBSD__ */
799 static int64_t raw_getlength(BlockDriverState *bs)
801 BDRVRawState *s = bs->opaque;
802 int fd = s->fd;
803 int64_t size;
804 #ifdef _BSD
805 struct stat sb;
806 #endif
807 #ifdef __sun__
808 struct dk_minfo minfo;
809 int rv;
810 #endif
811 int ret;
813 ret = fd_open(bs);
814 if (ret < 0)
815 return ret;
817 #ifdef _BSD
818 if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
819 #ifdef DIOCGMEDIASIZE
820 if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
821 #endif
822 #ifdef CONFIG_COCOA
823 size = LONG_LONG_MAX;
824 #else
825 size = lseek(fd, 0LL, SEEK_END);
826 #endif
827 } else
828 #endif
829 #ifdef __sun__
831 * use the DKIOCGMEDIAINFO ioctl to read the size.
833 rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo );
834 if ( rv != -1 ) {
835 size = minfo.dki_lbsize * minfo.dki_capacity;
836 } else /* there are reports that lseek on some devices
837 fails, but irc discussion said that contingency
838 on contingency was overkill */
839 #endif
841 size = lseek(fd, 0, SEEK_END);
843 return size;
845 #endif
847 static int raw_create(const char *filename, int64_t total_size,
848 const char *backing_file, int flags)
850 int fd;
852 if (flags || backing_file)
853 return -ENOTSUP;
855 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
856 0644);
857 if (fd < 0)
858 return -EIO;
859 ftruncate(fd, total_size * 512);
860 close(fd);
861 return 0;
864 static void raw_flush(BlockDriverState *bs)
866 BDRVRawState *s = bs->opaque;
867 fsync(s->fd);
870 BlockDriver bdrv_raw = {
871 "raw",
872 sizeof(BDRVRawState),
873 NULL, /* no probe for protocols */
874 raw_open,
875 NULL,
876 NULL,
877 raw_close,
878 raw_create,
879 raw_flush,
881 #ifdef CONFIG_AIO
882 .bdrv_aio_read = raw_aio_read,
883 .bdrv_aio_write = raw_aio_write,
884 .bdrv_aio_cancel = raw_aio_cancel,
885 .aiocb_size = sizeof(RawAIOCB),
886 #endif
887 .protocol_name = "file",
888 .bdrv_pread = raw_pread,
889 .bdrv_pwrite = raw_pwrite,
890 .bdrv_truncate = raw_truncate,
891 .bdrv_getlength = raw_getlength,
894 /***********************************************/
895 /* host device */
897 #ifdef CONFIG_COCOA
898 static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
899 static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
901 kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
903 kern_return_t kernResult;
904 mach_port_t masterPort;
905 CFMutableDictionaryRef classesToMatch;
907 kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
908 if ( KERN_SUCCESS != kernResult ) {
909 printf( "IOMasterPort returned %d\n", kernResult );
912 classesToMatch = IOServiceMatching( kIOCDMediaClass );
913 if ( classesToMatch == NULL ) {
914 printf( "IOServiceMatching returned a NULL dictionary.\n" );
915 } else {
916 CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
918 kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
919 if ( KERN_SUCCESS != kernResult )
921 printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
924 return kernResult;
927 kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
929 io_object_t nextMedia;
930 kern_return_t kernResult = KERN_FAILURE;
931 *bsdPath = '\0';
932 nextMedia = IOIteratorNext( mediaIterator );
933 if ( nextMedia )
935 CFTypeRef bsdPathAsCFString;
936 bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
937 if ( bsdPathAsCFString ) {
938 size_t devPathLength;
939 strcpy( bsdPath, _PATH_DEV );
940 strcat( bsdPath, "r" );
941 devPathLength = strlen( bsdPath );
942 if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
943 kernResult = KERN_SUCCESS;
945 CFRelease( bsdPathAsCFString );
947 IOObjectRelease( nextMedia );
950 return kernResult;
953 #endif
955 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
957 BDRVRawState *s = bs->opaque;
958 int fd, open_flags, ret;
960 #ifdef CONFIG_COCOA
961 if (strstart(filename, "/dev/cdrom", NULL)) {
962 kern_return_t kernResult;
963 io_iterator_t mediaIterator;
964 char bsdPath[ MAXPATHLEN ];
965 int fd;
967 kernResult = FindEjectableCDMedia( &mediaIterator );
968 kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
970 if ( bsdPath[ 0 ] != '\0' ) {
971 strcat(bsdPath,"s0");
972 /* some CDs don't have a partition 0 */
973 fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
974 if (fd < 0) {
975 bsdPath[strlen(bsdPath)-1] = '1';
976 } else {
977 close(fd);
979 filename = bsdPath;
982 if ( mediaIterator )
983 IOObjectRelease( mediaIterator );
985 #endif
986 open_flags = O_BINARY;
987 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
988 open_flags |= O_RDWR;
989 } else {
990 open_flags |= O_RDONLY;
991 bs->read_only = 1;
993 #ifdef O_DIRECT
994 if (flags & BDRV_O_DIRECT)
995 open_flags |= O_DIRECT;
996 #endif
998 s->type = FTYPE_FILE;
999 #if defined(__linux__)
1000 if (strstart(filename, "/dev/cd", NULL)) {
1001 /* open will not fail even if no CD is inserted */
1002 open_flags |= O_NONBLOCK;
1003 s->type = FTYPE_CD;
1004 } else if (strstart(filename, "/dev/fd", NULL)) {
1005 s->type = FTYPE_FD;
1006 s->fd_open_flags = open_flags;
1007 /* open will not fail even if no floppy is inserted */
1008 open_flags |= O_NONBLOCK;
1009 } else if (strstart(filename, "/dev/sg", NULL)) {
1010 bs->sg = 1;
1012 #endif
1013 fd = open(filename, open_flags, 0644);
1014 if (fd < 0) {
1015 ret = -errno;
1016 if (ret == -EROFS)
1017 ret = -EACCES;
1018 return ret;
1020 s->fd = fd;
1021 #if defined(__linux__)
1022 /* close fd so that we can reopen it as needed */
1023 if (s->type == FTYPE_FD) {
1024 close(s->fd);
1025 s->fd = -1;
1026 s->fd_media_changed = 1;
1028 #endif
1029 return 0;
1032 #if defined(__linux__) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
1034 /* Note: we do not have a reliable method to detect if the floppy is
1035 present. The current method is to try to open the floppy at every
1036 I/O and to keep it opened during a few hundreds of ms. */
1037 static int fd_open(BlockDriverState *bs)
1039 BDRVRawState *s = bs->opaque;
1040 int last_media_present;
1042 if (s->type != FTYPE_FD)
1043 return 0;
1044 last_media_present = (s->fd >= 0);
1045 if (s->fd >= 0 &&
1046 (qemu_get_clock(rt_clock) - s->fd_open_time) >= FD_OPEN_TIMEOUT) {
1047 close(s->fd);
1048 s->fd = -1;
1049 #ifdef DEBUG_FLOPPY
1050 printf("Floppy closed\n");
1051 #endif
1053 if (s->fd < 0) {
1054 if (s->fd_got_error &&
1055 (qemu_get_clock(rt_clock) - s->fd_error_time) < FD_OPEN_TIMEOUT) {
1056 #ifdef DEBUG_FLOPPY
1057 printf("No floppy (open delayed)\n");
1058 #endif
1059 return -EIO;
1061 s->fd = open(bs->filename, s->fd_open_flags);
1062 if (s->fd < 0) {
1063 s->fd_error_time = qemu_get_clock(rt_clock);
1064 s->fd_got_error = 1;
1065 if (last_media_present)
1066 s->fd_media_changed = 1;
1067 #ifdef DEBUG_FLOPPY
1068 printf("No floppy\n");
1069 #endif
1070 return -EIO;
1072 #ifdef DEBUG_FLOPPY
1073 printf("Floppy opened\n");
1074 #endif
1076 if (!last_media_present)
1077 s->fd_media_changed = 1;
1078 s->fd_open_time = qemu_get_clock(rt_clock);
1079 s->fd_got_error = 0;
1080 return 0;
1082 #else
1083 static int fd_open(BlockDriverState *bs)
1085 return 0;
1087 #endif
1089 #if defined(__linux__)
1091 static int raw_is_inserted(BlockDriverState *bs)
1093 BDRVRawState *s = bs->opaque;
1094 int ret;
1096 switch(s->type) {
1097 case FTYPE_CD:
1098 ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
1099 if (ret == CDS_DISC_OK)
1100 return 1;
1101 else
1102 return 0;
1103 break;
1104 case FTYPE_FD:
1105 ret = fd_open(bs);
1106 return (ret >= 0);
1107 default:
1108 return 1;
1112 /* currently only used by fdc.c, but a CD version would be good too */
1113 static int raw_media_changed(BlockDriverState *bs)
1115 BDRVRawState *s = bs->opaque;
1117 switch(s->type) {
1118 case FTYPE_FD:
1120 int ret;
1121 /* XXX: we do not have a true media changed indication. It
1122 does not work if the floppy is changed without trying
1123 to read it */
1124 fd_open(bs);
1125 ret = s->fd_media_changed;
1126 s->fd_media_changed = 0;
1127 #ifdef DEBUG_FLOPPY
1128 printf("Floppy changed=%d\n", ret);
1129 #endif
1130 return ret;
1132 default:
1133 return -ENOTSUP;
1137 static int raw_eject(BlockDriverState *bs, int eject_flag)
1139 BDRVRawState *s = bs->opaque;
1141 switch(s->type) {
1142 case FTYPE_CD:
1143 if (eject_flag) {
1144 if (ioctl (s->fd, CDROMEJECT, NULL) < 0)
1145 perror("CDROMEJECT");
1146 } else {
1147 if (ioctl (s->fd, CDROMCLOSETRAY, NULL) < 0)
1148 perror("CDROMEJECT");
1150 break;
1151 case FTYPE_FD:
1153 int fd;
1154 if (s->fd >= 0) {
1155 close(s->fd);
1156 s->fd = -1;
1158 fd = open(bs->filename, s->fd_open_flags | O_NONBLOCK);
1159 if (fd >= 0) {
1160 if (ioctl(fd, FDEJECT, 0) < 0)
1161 perror("FDEJECT");
1162 close(fd);
1165 break;
1166 default:
1167 return -ENOTSUP;
1169 return 0;
1172 static int raw_set_locked(BlockDriverState *bs, int locked)
1174 BDRVRawState *s = bs->opaque;
1176 switch(s->type) {
1177 case FTYPE_CD:
1178 if (ioctl (s->fd, CDROM_LOCKDOOR, locked) < 0) {
1179 /* Note: an error can happen if the distribution automatically
1180 mounts the CD-ROM */
1181 // perror("CDROM_LOCKDOOR");
1183 break;
1184 default:
1185 return -ENOTSUP;
1187 return 0;
1190 static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1192 BDRVRawState *s = bs->opaque;
1194 return ioctl(s->fd, req, buf);
1196 #else
1198 static int raw_is_inserted(BlockDriverState *bs)
1200 return 1;
1203 static int raw_media_changed(BlockDriverState *bs)
1205 return -ENOTSUP;
1208 static int raw_eject(BlockDriverState *bs, int eject_flag)
1210 return -ENOTSUP;
1213 static int raw_set_locked(BlockDriverState *bs, int locked)
1215 return -ENOTSUP;
1218 static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1220 return -ENOTSUP;
1222 #endif /* !linux */
1224 BlockDriver bdrv_host_device = {
1225 "host_device",
1226 sizeof(BDRVRawState),
1227 NULL, /* no probe for protocols */
1228 hdev_open,
1229 NULL,
1230 NULL,
1231 raw_close,
1232 NULL,
1233 raw_flush,
1235 #ifdef CONFIG_AIO
1236 .bdrv_aio_read = raw_aio_read,
1237 .bdrv_aio_write = raw_aio_write,
1238 .bdrv_aio_cancel = raw_aio_cancel,
1239 .aiocb_size = sizeof(RawAIOCB),
1240 #endif
1241 .bdrv_pread = raw_pread,
1242 .bdrv_pwrite = raw_pwrite,
1243 .bdrv_getlength = raw_getlength,
1245 /* removable device support */
1246 .bdrv_is_inserted = raw_is_inserted,
1247 .bdrv_media_changed = raw_media_changed,
1248 .bdrv_eject = raw_eject,
1249 .bdrv_set_locked = raw_set_locked,
1250 /* generic scsi device */
1251 .bdrv_ioctl = raw_ioctl,