fix event fallout in grlib_apbuart.c
[qemu/aliguori.git] / block / raw-posix.c
blobc5c99446c06908c66efc6ec77ac67e2e35d590af
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 "qemu-log.h"
28 #include "block_int.h"
29 #include "module.h"
30 #include "block/raw-posix-aio.h"
32 #ifdef CONFIG_COCOA
33 #include <paths.h>
34 #include <sys/param.h>
35 #include <IOKit/IOKitLib.h>
36 #include <IOKit/IOBSD.h>
37 #include <IOKit/storage/IOMediaBSDClient.h>
38 #include <IOKit/storage/IOMedia.h>
39 #include <IOKit/storage/IOCDMedia.h>
40 //#include <IOKit/storage/IOCDTypes.h>
41 #include <CoreFoundation/CoreFoundation.h>
42 #endif
44 #ifdef __sun__
45 #define _POSIX_PTHREAD_SEMANTICS 1
46 #include <sys/dkio.h>
47 #endif
48 #ifdef __linux__
49 #include <sys/types.h>
50 #include <sys/stat.h>
51 #include <sys/ioctl.h>
52 #include <sys/param.h>
53 #include <linux/cdrom.h>
54 #include <linux/fd.h>
55 #endif
56 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
57 #include <sys/disk.h>
58 #include <sys/cdio.h>
59 #endif
61 #ifdef __OpenBSD__
62 #include <sys/ioctl.h>
63 #include <sys/disklabel.h>
64 #include <sys/dkio.h>
65 #endif
67 #ifdef __NetBSD__
68 #include <sys/ioctl.h>
69 #include <sys/disklabel.h>
70 #include <sys/dkio.h>
71 #include <sys/disk.h>
72 #endif
74 #ifdef __DragonFly__
75 #include <sys/ioctl.h>
76 #include <sys/diskslice.h>
77 #endif
79 #ifdef CONFIG_XFS
80 #include <xfs/xfs.h>
81 #endif
83 //#define DEBUG_FLOPPY
85 //#define DEBUG_BLOCK
86 #if defined(DEBUG_BLOCK)
87 #define DEBUG_BLOCK_PRINT(formatCstr, ...) do { if (qemu_log_enabled()) \
88 { qemu_log(formatCstr, ## __VA_ARGS__); qemu_log_flush(); } } while (0)
89 #else
90 #define DEBUG_BLOCK_PRINT(formatCstr, ...)
91 #endif
93 /* OS X does not have O_DSYNC */
94 #ifndef O_DSYNC
95 #ifdef O_SYNC
96 #define O_DSYNC O_SYNC
97 #elif defined(O_FSYNC)
98 #define O_DSYNC O_FSYNC
99 #endif
100 #endif
102 /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */
103 #ifndef O_DIRECT
104 #define O_DIRECT O_DSYNC
105 #endif
107 #define FTYPE_FILE 0
108 #define FTYPE_CD 1
109 #define FTYPE_FD 2
111 /* if the FD is not accessed during that time (in ns), we try to
112 reopen it to see if the disk has been changed */
113 #define FD_OPEN_TIMEOUT (1000000000)
115 #define MAX_BLOCKSIZE 4096
117 typedef struct BDRVRawState {
118 int fd;
119 int type;
120 int open_flags;
121 #if defined(__linux__)
122 /* linux floppy specific */
123 int64_t fd_open_time;
124 int64_t fd_error_time;
125 int fd_got_error;
126 int fd_media_changed;
127 #endif
128 #ifdef CONFIG_LINUX_AIO
129 int use_aio;
130 void *aio_ctx;
131 #endif
132 uint8_t *aligned_buf;
133 unsigned aligned_buf_size;
134 #ifdef CONFIG_XFS
135 bool is_xfs : 1;
136 #endif
137 } BDRVRawState;
139 static int fd_open(BlockDriverState *bs);
140 static int64_t raw_getlength(BlockDriverState *bs);
142 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
143 static int cdrom_reopen(BlockDriverState *bs);
144 #endif
146 #if defined(__NetBSD__)
147 static int raw_normalize_devicepath(const char **filename)
149 static char namebuf[PATH_MAX];
150 const char *dp, *fname;
151 struct stat sb;
153 fname = *filename;
154 dp = strrchr(fname, '/');
155 if (lstat(fname, &sb) < 0) {
156 fprintf(stderr, "%s: stat failed: %s\n",
157 fname, strerror(errno));
158 return -errno;
161 if (!S_ISBLK(sb.st_mode)) {
162 return 0;
165 if (dp == NULL) {
166 snprintf(namebuf, PATH_MAX, "r%s", fname);
167 } else {
168 snprintf(namebuf, PATH_MAX, "%.*s/r%s",
169 (int)(dp - fname), fname, dp + 1);
171 fprintf(stderr, "%s is a block device", fname);
172 *filename = namebuf;
173 fprintf(stderr, ", using %s\n", *filename);
175 return 0;
177 #else
178 static int raw_normalize_devicepath(const char **filename)
180 return 0;
182 #endif
184 static int raw_open_common(BlockDriverState *bs, const char *filename,
185 int bdrv_flags, int open_flags)
187 BDRVRawState *s = bs->opaque;
188 int fd, ret;
190 ret = raw_normalize_devicepath(&filename);
191 if (ret != 0) {
192 return ret;
195 s->open_flags = open_flags | O_BINARY;
196 s->open_flags &= ~O_ACCMODE;
197 if (bdrv_flags & BDRV_O_RDWR) {
198 s->open_flags |= O_RDWR;
199 } else {
200 s->open_flags |= O_RDONLY;
203 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
204 * and O_DIRECT for no caching. */
205 if ((bdrv_flags & BDRV_O_NOCACHE))
206 s->open_flags |= O_DIRECT;
207 if (!(bdrv_flags & BDRV_O_CACHE_WB))
208 s->open_flags |= O_DSYNC;
210 s->fd = -1;
211 fd = qemu_open(filename, s->open_flags, 0644);
212 if (fd < 0) {
213 ret = -errno;
214 if (ret == -EROFS)
215 ret = -EACCES;
216 return ret;
218 s->fd = fd;
219 s->aligned_buf = NULL;
221 if ((bdrv_flags & BDRV_O_NOCACHE)) {
223 * Allocate a buffer for read/modify/write cycles. Chose the size
224 * pessimistically as we don't know the block size yet.
226 s->aligned_buf_size = 32 * MAX_BLOCKSIZE;
227 s->aligned_buf = qemu_memalign(MAX_BLOCKSIZE, s->aligned_buf_size);
228 if (s->aligned_buf == NULL) {
229 goto out_close;
233 /* We're falling back to POSIX AIO in some cases so init always */
234 if (paio_init() < 0) {
235 goto out_free_buf;
238 #ifdef CONFIG_LINUX_AIO
239 if ((bdrv_flags & (BDRV_O_NOCACHE|BDRV_O_NATIVE_AIO)) ==
240 (BDRV_O_NOCACHE|BDRV_O_NATIVE_AIO)) {
242 s->aio_ctx = laio_init();
243 if (!s->aio_ctx) {
244 goto out_free_buf;
246 s->use_aio = 1;
247 } else
248 #endif
250 #ifdef CONFIG_LINUX_AIO
251 s->use_aio = 0;
252 #endif
255 #ifdef CONFIG_XFS
256 if (platform_test_xfs_fd(s->fd)) {
257 s->is_xfs = 1;
259 #endif
261 return 0;
263 out_free_buf:
264 qemu_vfree(s->aligned_buf);
265 out_close:
266 close(fd);
267 return -errno;
270 static int raw_open(BlockDriverState *bs, const char *filename, int flags)
272 BDRVRawState *s = bs->opaque;
274 s->type = FTYPE_FILE;
275 return raw_open_common(bs, filename, flags, 0);
278 /* XXX: use host sector size if necessary with:
279 #ifdef DIOCGSECTORSIZE
281 unsigned int sectorsize = 512;
282 if (!ioctl(fd, DIOCGSECTORSIZE, &sectorsize) &&
283 sectorsize > bufsize)
284 bufsize = sectorsize;
286 #endif
287 #ifdef CONFIG_COCOA
288 uint32_t blockSize = 512;
289 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
290 bufsize = blockSize;
292 #endif
296 * offset and count are in bytes, but must be multiples of 512 for files
297 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
299 * This function may be called without alignment if the caller ensures
300 * that O_DIRECT is not in effect.
302 static int raw_pread_aligned(BlockDriverState *bs, int64_t offset,
303 uint8_t *buf, int count)
305 BDRVRawState *s = bs->opaque;
306 int ret;
308 ret = fd_open(bs);
309 if (ret < 0)
310 return ret;
312 ret = pread(s->fd, buf, count, offset);
313 if (ret == count)
314 return ret;
316 /* Allow reads beyond the end (needed for pwrite) */
317 if ((ret == 0) && bs->growable) {
318 int64_t size = raw_getlength(bs);
319 if (offset >= size) {
320 memset(buf, 0, count);
321 return count;
325 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
326 "] read failed %d : %d = %s\n",
327 s->fd, bs->filename, offset, buf, count,
328 bs->total_sectors, ret, errno, strerror(errno));
330 /* Try harder for CDrom. */
331 if (s->type != FTYPE_FILE) {
332 ret = pread(s->fd, buf, count, offset);
333 if (ret == count)
334 return ret;
335 ret = pread(s->fd, buf, count, offset);
336 if (ret == count)
337 return ret;
339 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
340 "] retry read failed %d : %d = %s\n",
341 s->fd, bs->filename, offset, buf, count,
342 bs->total_sectors, ret, errno, strerror(errno));
345 return (ret < 0) ? -errno : ret;
349 * offset and count are in bytes, but must be multiples of the sector size
350 * for files opened with O_DIRECT. buf must be aligned to sector size bytes
351 * then.
353 * This function may be called without alignment if the caller ensures
354 * that O_DIRECT is not in effect.
356 static int raw_pwrite_aligned(BlockDriverState *bs, int64_t offset,
357 const uint8_t *buf, int count)
359 BDRVRawState *s = bs->opaque;
360 int ret;
362 ret = fd_open(bs);
363 if (ret < 0)
364 return -errno;
366 ret = pwrite(s->fd, buf, count, offset);
367 if (ret == count)
368 return ret;
370 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
371 "] write failed %d : %d = %s\n",
372 s->fd, bs->filename, offset, buf, count,
373 bs->total_sectors, ret, errno, strerror(errno));
375 return (ret < 0) ? -errno : ret;
380 * offset and count are in bytes and possibly not aligned. For files opened
381 * with O_DIRECT, necessary alignments are ensured before calling
382 * raw_pread_aligned to do the actual read.
384 static int raw_pread(BlockDriverState *bs, int64_t offset,
385 uint8_t *buf, int count)
387 BDRVRawState *s = bs->opaque;
388 unsigned sector_mask = bs->buffer_alignment - 1;
389 int size, ret, shift, sum;
391 sum = 0;
393 if (s->aligned_buf != NULL) {
395 if (offset & sector_mask) {
396 /* align offset on a sector size bytes boundary */
398 shift = offset & sector_mask;
399 size = (shift + count + sector_mask) & ~sector_mask;
400 if (size > s->aligned_buf_size)
401 size = s->aligned_buf_size;
402 ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, size);
403 if (ret < 0)
404 return ret;
406 size = bs->buffer_alignment - shift;
407 if (size > count)
408 size = count;
409 memcpy(buf, s->aligned_buf + shift, size);
411 buf += size;
412 offset += size;
413 count -= size;
414 sum += size;
416 if (count == 0)
417 return sum;
419 if (count & sector_mask || (uintptr_t) buf & sector_mask) {
421 /* read on aligned buffer */
423 while (count) {
425 size = (count + sector_mask) & ~sector_mask;
426 if (size > s->aligned_buf_size)
427 size = s->aligned_buf_size;
429 ret = raw_pread_aligned(bs, offset, s->aligned_buf, size);
430 if (ret < 0) {
431 return ret;
432 } else if (ret == 0) {
433 fprintf(stderr, "raw_pread: read beyond end of file\n");
434 abort();
437 size = ret;
438 if (size > count)
439 size = count;
441 memcpy(buf, s->aligned_buf, size);
443 buf += size;
444 offset += size;
445 count -= size;
446 sum += size;
449 return sum;
453 return raw_pread_aligned(bs, offset, buf, count) + sum;
456 static int raw_read(BlockDriverState *bs, int64_t sector_num,
457 uint8_t *buf, int nb_sectors)
459 int ret;
461 ret = raw_pread(bs, sector_num * BDRV_SECTOR_SIZE, buf,
462 nb_sectors * BDRV_SECTOR_SIZE);
463 if (ret == (nb_sectors * BDRV_SECTOR_SIZE))
464 ret = 0;
465 return ret;
469 * offset and count are in bytes and possibly not aligned. For files opened
470 * with O_DIRECT, necessary alignments are ensured before calling
471 * raw_pwrite_aligned to do the actual write.
473 static int raw_pwrite(BlockDriverState *bs, int64_t offset,
474 const uint8_t *buf, int count)
476 BDRVRawState *s = bs->opaque;
477 unsigned sector_mask = bs->buffer_alignment - 1;
478 int size, ret, shift, sum;
480 sum = 0;
482 if (s->aligned_buf != NULL) {
484 if (offset & sector_mask) {
485 /* align offset on a sector size bytes boundary */
486 shift = offset & sector_mask;
487 ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf,
488 bs->buffer_alignment);
489 if (ret < 0)
490 return ret;
492 size = bs->buffer_alignment - shift;
493 if (size > count)
494 size = count;
495 memcpy(s->aligned_buf + shift, buf, size);
497 ret = raw_pwrite_aligned(bs, offset - shift, s->aligned_buf,
498 bs->buffer_alignment);
499 if (ret < 0)
500 return ret;
502 buf += size;
503 offset += size;
504 count -= size;
505 sum += size;
507 if (count == 0)
508 return sum;
510 if (count & sector_mask || (uintptr_t) buf & sector_mask) {
512 while ((size = (count & ~sector_mask)) != 0) {
514 if (size > s->aligned_buf_size)
515 size = s->aligned_buf_size;
517 memcpy(s->aligned_buf, buf, size);
519 ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, size);
520 if (ret < 0)
521 return ret;
523 buf += ret;
524 offset += ret;
525 count -= ret;
526 sum += ret;
528 /* here, count < sector_size because (count & ~sector_mask) == 0 */
529 if (count) {
530 ret = raw_pread_aligned(bs, offset, s->aligned_buf,
531 bs->buffer_alignment);
532 if (ret < 0)
533 return ret;
534 memcpy(s->aligned_buf, buf, count);
536 ret = raw_pwrite_aligned(bs, offset, s->aligned_buf,
537 bs->buffer_alignment);
538 if (ret < 0)
539 return ret;
540 if (count < ret)
541 ret = count;
543 sum += ret;
545 return sum;
548 return raw_pwrite_aligned(bs, offset, buf, count) + sum;
551 static int raw_write(BlockDriverState *bs, int64_t sector_num,
552 const uint8_t *buf, int nb_sectors)
554 int ret;
555 ret = raw_pwrite(bs, sector_num * BDRV_SECTOR_SIZE, buf,
556 nb_sectors * BDRV_SECTOR_SIZE);
557 if (ret == (nb_sectors * BDRV_SECTOR_SIZE))
558 ret = 0;
559 return ret;
563 * Check if all memory in this vector is sector aligned.
565 static int qiov_is_aligned(BlockDriverState *bs, QEMUIOVector *qiov)
567 int i;
569 for (i = 0; i < qiov->niov; i++) {
570 if ((uintptr_t) qiov->iov[i].iov_base % bs->buffer_alignment) {
571 return 0;
575 return 1;
578 static BlockDriverAIOCB *raw_aio_submit(BlockDriverState *bs,
579 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
580 BlockDriverCompletionFunc *cb, void *opaque, int type)
582 BDRVRawState *s = bs->opaque;
584 if (fd_open(bs) < 0)
585 return NULL;
588 * If O_DIRECT is used the buffer needs to be aligned on a sector
589 * boundary. Check if this is the case or tell the low-level
590 * driver that it needs to copy the buffer.
592 if (s->aligned_buf) {
593 if (!qiov_is_aligned(bs, qiov)) {
594 type |= QEMU_AIO_MISALIGNED;
595 #ifdef CONFIG_LINUX_AIO
596 } else if (s->use_aio) {
597 return laio_submit(bs, s->aio_ctx, s->fd, sector_num, qiov,
598 nb_sectors, cb, opaque, type);
599 #endif
603 return paio_submit(bs, s->fd, sector_num, qiov, nb_sectors,
604 cb, opaque, type);
607 static BlockDriverAIOCB *raw_aio_readv(BlockDriverState *bs,
608 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
609 BlockDriverCompletionFunc *cb, void *opaque)
611 return raw_aio_submit(bs, sector_num, qiov, nb_sectors,
612 cb, opaque, QEMU_AIO_READ);
615 static BlockDriverAIOCB *raw_aio_writev(BlockDriverState *bs,
616 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
617 BlockDriverCompletionFunc *cb, void *opaque)
619 return raw_aio_submit(bs, sector_num, qiov, nb_sectors,
620 cb, opaque, QEMU_AIO_WRITE);
623 static BlockDriverAIOCB *raw_aio_flush(BlockDriverState *bs,
624 BlockDriverCompletionFunc *cb, void *opaque)
626 BDRVRawState *s = bs->opaque;
628 if (fd_open(bs) < 0)
629 return NULL;
631 return paio_submit(bs, s->fd, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH);
634 static void raw_close(BlockDriverState *bs)
636 BDRVRawState *s = bs->opaque;
637 if (s->fd >= 0) {
638 close(s->fd);
639 s->fd = -1;
640 if (s->aligned_buf != NULL)
641 qemu_vfree(s->aligned_buf);
645 static int raw_truncate(BlockDriverState *bs, int64_t offset)
647 BDRVRawState *s = bs->opaque;
648 if (s->type != FTYPE_FILE)
649 return -ENOTSUP;
650 if (ftruncate(s->fd, offset) < 0)
651 return -errno;
652 return 0;
655 #ifdef __OpenBSD__
656 static int64_t raw_getlength(BlockDriverState *bs)
658 BDRVRawState *s = bs->opaque;
659 int fd = s->fd;
660 struct stat st;
662 if (fstat(fd, &st))
663 return -1;
664 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
665 struct disklabel dl;
667 if (ioctl(fd, DIOCGDINFO, &dl))
668 return -1;
669 return (uint64_t)dl.d_secsize *
670 dl.d_partitions[DISKPART(st.st_rdev)].p_size;
671 } else
672 return st.st_size;
674 #elif defined(__NetBSD__)
675 static int64_t raw_getlength(BlockDriverState *bs)
677 BDRVRawState *s = bs->opaque;
678 int fd = s->fd;
679 struct stat st;
681 if (fstat(fd, &st))
682 return -1;
683 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
684 struct dkwedge_info dkw;
686 if (ioctl(fd, DIOCGWEDGEINFO, &dkw) != -1) {
687 return dkw.dkw_size * 512;
688 } else {
689 struct disklabel dl;
691 if (ioctl(fd, DIOCGDINFO, &dl))
692 return -1;
693 return (uint64_t)dl.d_secsize *
694 dl.d_partitions[DISKPART(st.st_rdev)].p_size;
696 } else
697 return st.st_size;
699 #elif defined(__sun__)
700 static int64_t raw_getlength(BlockDriverState *bs)
702 BDRVRawState *s = bs->opaque;
703 struct dk_minfo minfo;
704 int ret;
706 ret = fd_open(bs);
707 if (ret < 0) {
708 return ret;
712 * Use the DKIOCGMEDIAINFO ioctl to read the size.
714 ret = ioctl(s->fd, DKIOCGMEDIAINFO, &minfo);
715 if (ret != -1) {
716 return minfo.dki_lbsize * minfo.dki_capacity;
720 * There are reports that lseek on some devices fails, but
721 * irc discussion said that contingency on contingency was overkill.
723 return lseek(s->fd, 0, SEEK_END);
725 #elif defined(CONFIG_BSD)
726 static int64_t raw_getlength(BlockDriverState *bs)
728 BDRVRawState *s = bs->opaque;
729 int fd = s->fd;
730 int64_t size;
731 struct stat sb;
732 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
733 int reopened = 0;
734 #endif
735 int ret;
737 ret = fd_open(bs);
738 if (ret < 0)
739 return ret;
741 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
742 again:
743 #endif
744 if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
745 #ifdef DIOCGMEDIASIZE
746 if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
747 #elif defined(DIOCGPART)
749 struct partinfo pi;
750 if (ioctl(fd, DIOCGPART, &pi) == 0)
751 size = pi.media_size;
752 else
753 size = 0;
755 if (size == 0)
756 #endif
757 #ifdef CONFIG_COCOA
758 size = LONG_LONG_MAX;
759 #else
760 size = lseek(fd, 0LL, SEEK_END);
761 #endif
762 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
763 switch(s->type) {
764 case FTYPE_CD:
765 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
766 if (size == 2048LL * (unsigned)-1)
767 size = 0;
768 /* XXX no disc? maybe we need to reopen... */
769 if (size <= 0 && !reopened && cdrom_reopen(bs) >= 0) {
770 reopened = 1;
771 goto again;
774 #endif
775 } else {
776 size = lseek(fd, 0, SEEK_END);
778 return size;
780 #else
781 static int64_t raw_getlength(BlockDriverState *bs)
783 BDRVRawState *s = bs->opaque;
784 int ret;
786 ret = fd_open(bs);
787 if (ret < 0) {
788 return ret;
791 return lseek(s->fd, 0, SEEK_END);
793 #endif
795 static int64_t raw_get_allocated_file_size(BlockDriverState *bs)
797 struct stat st;
798 BDRVRawState *s = bs->opaque;
800 if (fstat(s->fd, &st) < 0) {
801 return -errno;
803 return (int64_t)st.st_blocks * 512;
806 static int raw_create(const char *filename, QEMUOptionParameter *options)
808 int fd;
809 int result = 0;
810 int64_t total_size = 0;
812 /* Read out options */
813 while (options && options->name) {
814 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
815 total_size = options->value.n / BDRV_SECTOR_SIZE;
817 options++;
820 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
821 0644);
822 if (fd < 0) {
823 result = -errno;
824 } else {
825 if (ftruncate(fd, total_size * BDRV_SECTOR_SIZE) != 0) {
826 result = -errno;
828 if (close(fd) != 0) {
829 result = -errno;
832 return result;
835 static int raw_flush(BlockDriverState *bs)
837 BDRVRawState *s = bs->opaque;
838 return qemu_fdatasync(s->fd);
841 #ifdef CONFIG_XFS
842 static int xfs_discard(BDRVRawState *s, int64_t sector_num, int nb_sectors)
844 struct xfs_flock64 fl;
846 memset(&fl, 0, sizeof(fl));
847 fl.l_whence = SEEK_SET;
848 fl.l_start = sector_num << 9;
849 fl.l_len = (int64_t)nb_sectors << 9;
851 if (xfsctl(NULL, s->fd, XFS_IOC_UNRESVSP64, &fl) < 0) {
852 DEBUG_BLOCK_PRINT("cannot punch hole (%s)\n", strerror(errno));
853 return -errno;
856 return 0;
858 #endif
860 static int raw_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors)
862 #ifdef CONFIG_XFS
863 BDRVRawState *s = bs->opaque;
865 if (s->is_xfs) {
866 return xfs_discard(s, sector_num, nb_sectors);
868 #endif
870 return 0;
873 static QEMUOptionParameter raw_create_options[] = {
875 .name = BLOCK_OPT_SIZE,
876 .type = OPT_SIZE,
877 .help = "Virtual disk size"
879 { NULL }
882 static BlockDriver bdrv_file = {
883 .format_name = "file",
884 .protocol_name = "file",
885 .instance_size = sizeof(BDRVRawState),
886 .bdrv_probe = NULL, /* no probe for protocols */
887 .bdrv_file_open = raw_open,
888 .bdrv_read = raw_read,
889 .bdrv_write = raw_write,
890 .bdrv_close = raw_close,
891 .bdrv_create = raw_create,
892 .bdrv_flush = raw_flush,
893 .bdrv_discard = raw_discard,
895 .bdrv_aio_readv = raw_aio_readv,
896 .bdrv_aio_writev = raw_aio_writev,
897 .bdrv_aio_flush = raw_aio_flush,
899 .bdrv_truncate = raw_truncate,
900 .bdrv_getlength = raw_getlength,
901 .bdrv_get_allocated_file_size
902 = raw_get_allocated_file_size,
904 .create_options = raw_create_options,
907 /***********************************************/
908 /* host device */
910 #ifdef CONFIG_COCOA
911 static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
912 static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
914 kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
916 kern_return_t kernResult;
917 mach_port_t masterPort;
918 CFMutableDictionaryRef classesToMatch;
920 kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
921 if ( KERN_SUCCESS != kernResult ) {
922 printf( "IOMasterPort returned %d\n", kernResult );
925 classesToMatch = IOServiceMatching( kIOCDMediaClass );
926 if ( classesToMatch == NULL ) {
927 printf( "IOServiceMatching returned a NULL dictionary.\n" );
928 } else {
929 CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
931 kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
932 if ( KERN_SUCCESS != kernResult )
934 printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
937 return kernResult;
940 kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
942 io_object_t nextMedia;
943 kern_return_t kernResult = KERN_FAILURE;
944 *bsdPath = '\0';
945 nextMedia = IOIteratorNext( mediaIterator );
946 if ( nextMedia )
948 CFTypeRef bsdPathAsCFString;
949 bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
950 if ( bsdPathAsCFString ) {
951 size_t devPathLength;
952 strcpy( bsdPath, _PATH_DEV );
953 strcat( bsdPath, "r" );
954 devPathLength = strlen( bsdPath );
955 if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
956 kernResult = KERN_SUCCESS;
958 CFRelease( bsdPathAsCFString );
960 IOObjectRelease( nextMedia );
963 return kernResult;
966 #endif
968 static int hdev_probe_device(const char *filename)
970 struct stat st;
972 /* allow a dedicated CD-ROM driver to match with a higher priority */
973 if (strstart(filename, "/dev/cdrom", NULL))
974 return 50;
976 if (stat(filename, &st) >= 0 &&
977 (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
978 return 100;
981 return 0;
984 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
986 BDRVRawState *s = bs->opaque;
988 #ifdef CONFIG_COCOA
989 if (strstart(filename, "/dev/cdrom", NULL)) {
990 kern_return_t kernResult;
991 io_iterator_t mediaIterator;
992 char bsdPath[ MAXPATHLEN ];
993 int fd;
995 kernResult = FindEjectableCDMedia( &mediaIterator );
996 kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
998 if ( bsdPath[ 0 ] != '\0' ) {
999 strcat(bsdPath,"s0");
1000 /* some CDs don't have a partition 0 */
1001 fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
1002 if (fd < 0) {
1003 bsdPath[strlen(bsdPath)-1] = '1';
1004 } else {
1005 close(fd);
1007 filename = bsdPath;
1010 if ( mediaIterator )
1011 IOObjectRelease( mediaIterator );
1013 #endif
1015 s->type = FTYPE_FILE;
1016 #if defined(__linux__)
1018 char resolved_path[ MAXPATHLEN ], *temp;
1020 temp = realpath(filename, resolved_path);
1021 if (temp && strstart(temp, "/dev/sg", NULL)) {
1022 bs->sg = 1;
1025 #endif
1027 return raw_open_common(bs, filename, flags, 0);
1030 #if defined(__linux__)
1031 /* Note: we do not have a reliable method to detect if the floppy is
1032 present. The current method is to try to open the floppy at every
1033 I/O and to keep it opened during a few hundreds of ms. */
1034 static int fd_open(BlockDriverState *bs)
1036 BDRVRawState *s = bs->opaque;
1037 int last_media_present;
1039 if (s->type != FTYPE_FD)
1040 return 0;
1041 last_media_present = (s->fd >= 0);
1042 if (s->fd >= 0 &&
1043 (get_clock() - s->fd_open_time) >= FD_OPEN_TIMEOUT) {
1044 close(s->fd);
1045 s->fd = -1;
1046 #ifdef DEBUG_FLOPPY
1047 printf("Floppy closed\n");
1048 #endif
1050 if (s->fd < 0) {
1051 if (s->fd_got_error &&
1052 (get_clock() - s->fd_error_time) < FD_OPEN_TIMEOUT) {
1053 #ifdef DEBUG_FLOPPY
1054 printf("No floppy (open delayed)\n");
1055 #endif
1056 return -EIO;
1058 s->fd = open(bs->filename, s->open_flags & ~O_NONBLOCK);
1059 if (s->fd < 0) {
1060 s->fd_error_time = get_clock();
1061 s->fd_got_error = 1;
1062 if (last_media_present)
1063 s->fd_media_changed = 1;
1064 #ifdef DEBUG_FLOPPY
1065 printf("No floppy\n");
1066 #endif
1067 return -EIO;
1069 #ifdef DEBUG_FLOPPY
1070 printf("Floppy opened\n");
1071 #endif
1073 if (!last_media_present)
1074 s->fd_media_changed = 1;
1075 s->fd_open_time = get_clock();
1076 s->fd_got_error = 0;
1077 return 0;
1080 static int hdev_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1082 BDRVRawState *s = bs->opaque;
1084 return ioctl(s->fd, req, buf);
1087 static BlockDriverAIOCB *hdev_aio_ioctl(BlockDriverState *bs,
1088 unsigned long int req, void *buf,
1089 BlockDriverCompletionFunc *cb, void *opaque)
1091 BDRVRawState *s = bs->opaque;
1093 if (fd_open(bs) < 0)
1094 return NULL;
1095 return paio_ioctl(bs, s->fd, req, buf, cb, opaque);
1098 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1099 static int fd_open(BlockDriverState *bs)
1101 BDRVRawState *s = bs->opaque;
1103 /* this is just to ensure s->fd is sane (its called by io ops) */
1104 if (s->fd >= 0)
1105 return 0;
1106 return -EIO;
1108 #else /* !linux && !FreeBSD */
1110 static int fd_open(BlockDriverState *bs)
1112 return 0;
1115 #endif /* !linux && !FreeBSD */
1117 static int hdev_create(const char *filename, QEMUOptionParameter *options)
1119 int fd;
1120 int ret = 0;
1121 struct stat stat_buf;
1122 int64_t total_size = 0;
1124 /* Read out options */
1125 while (options && options->name) {
1126 if (!strcmp(options->name, "size")) {
1127 total_size = options->value.n / BDRV_SECTOR_SIZE;
1129 options++;
1132 fd = open(filename, O_WRONLY | O_BINARY);
1133 if (fd < 0)
1134 return -errno;
1136 if (fstat(fd, &stat_buf) < 0)
1137 ret = -errno;
1138 else if (!S_ISBLK(stat_buf.st_mode) && !S_ISCHR(stat_buf.st_mode))
1139 ret = -ENODEV;
1140 else if (lseek(fd, 0, SEEK_END) < total_size * BDRV_SECTOR_SIZE)
1141 ret = -ENOSPC;
1143 close(fd);
1144 return ret;
1147 static int hdev_has_zero_init(BlockDriverState *bs)
1149 return 0;
1152 static BlockDriver bdrv_host_device = {
1153 .format_name = "host_device",
1154 .protocol_name = "host_device",
1155 .instance_size = sizeof(BDRVRawState),
1156 .bdrv_probe_device = hdev_probe_device,
1157 .bdrv_file_open = hdev_open,
1158 .bdrv_close = raw_close,
1159 .bdrv_create = hdev_create,
1160 .create_options = raw_create_options,
1161 .bdrv_has_zero_init = hdev_has_zero_init,
1162 .bdrv_flush = raw_flush,
1164 .bdrv_aio_readv = raw_aio_readv,
1165 .bdrv_aio_writev = raw_aio_writev,
1166 .bdrv_aio_flush = raw_aio_flush,
1168 .bdrv_read = raw_read,
1169 .bdrv_write = raw_write,
1170 .bdrv_getlength = raw_getlength,
1171 .bdrv_get_allocated_file_size
1172 = raw_get_allocated_file_size,
1174 /* generic scsi device */
1175 #ifdef __linux__
1176 .bdrv_ioctl = hdev_ioctl,
1177 .bdrv_aio_ioctl = hdev_aio_ioctl,
1178 #endif
1181 #ifdef __linux__
1182 static int floppy_open(BlockDriverState *bs, const char *filename, int flags)
1184 BDRVRawState *s = bs->opaque;
1185 int ret;
1187 s->type = FTYPE_FD;
1189 /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
1190 ret = raw_open_common(bs, filename, flags, O_NONBLOCK);
1191 if (ret)
1192 return ret;
1194 /* close fd so that we can reopen it as needed */
1195 close(s->fd);
1196 s->fd = -1;
1197 s->fd_media_changed = 1;
1199 return 0;
1202 static int floppy_probe_device(const char *filename)
1204 int fd, ret;
1205 int prio = 0;
1206 struct floppy_struct fdparam;
1207 struct stat st;
1209 if (strstart(filename, "/dev/fd", NULL))
1210 prio = 50;
1212 fd = open(filename, O_RDONLY | O_NONBLOCK);
1213 if (fd < 0) {
1214 goto out;
1216 ret = fstat(fd, &st);
1217 if (ret == -1 || !S_ISBLK(st.st_mode)) {
1218 goto outc;
1221 /* Attempt to detect via a floppy specific ioctl */
1222 ret = ioctl(fd, FDGETPRM, &fdparam);
1223 if (ret >= 0)
1224 prio = 100;
1226 outc:
1227 close(fd);
1228 out:
1229 return prio;
1233 static int floppy_is_inserted(BlockDriverState *bs)
1235 return fd_open(bs) >= 0;
1238 static int floppy_media_changed(BlockDriverState *bs)
1240 BDRVRawState *s = bs->opaque;
1241 int ret;
1244 * XXX: we do not have a true media changed indication.
1245 * It does not work if the floppy is changed without trying to read it.
1247 fd_open(bs);
1248 ret = s->fd_media_changed;
1249 s->fd_media_changed = 0;
1250 #ifdef DEBUG_FLOPPY
1251 printf("Floppy changed=%d\n", ret);
1252 #endif
1253 return ret;
1256 static void floppy_eject(BlockDriverState *bs, int eject_flag)
1258 BDRVRawState *s = bs->opaque;
1259 int fd;
1261 if (s->fd >= 0) {
1262 close(s->fd);
1263 s->fd = -1;
1265 fd = open(bs->filename, s->open_flags | O_NONBLOCK);
1266 if (fd >= 0) {
1267 if (ioctl(fd, FDEJECT, 0) < 0)
1268 perror("FDEJECT");
1269 close(fd);
1273 static BlockDriver bdrv_host_floppy = {
1274 .format_name = "host_floppy",
1275 .protocol_name = "host_floppy",
1276 .instance_size = sizeof(BDRVRawState),
1277 .bdrv_probe_device = floppy_probe_device,
1278 .bdrv_file_open = floppy_open,
1279 .bdrv_close = raw_close,
1280 .bdrv_create = hdev_create,
1281 .create_options = raw_create_options,
1282 .bdrv_has_zero_init = hdev_has_zero_init,
1283 .bdrv_flush = raw_flush,
1285 .bdrv_aio_readv = raw_aio_readv,
1286 .bdrv_aio_writev = raw_aio_writev,
1287 .bdrv_aio_flush = raw_aio_flush,
1289 .bdrv_read = raw_read,
1290 .bdrv_write = raw_write,
1291 .bdrv_getlength = raw_getlength,
1292 .bdrv_get_allocated_file_size
1293 = raw_get_allocated_file_size,
1295 /* removable device support */
1296 .bdrv_is_inserted = floppy_is_inserted,
1297 .bdrv_media_changed = floppy_media_changed,
1298 .bdrv_eject = floppy_eject,
1301 static int cdrom_open(BlockDriverState *bs, const char *filename, int flags)
1303 BDRVRawState *s = bs->opaque;
1305 s->type = FTYPE_CD;
1307 /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
1308 return raw_open_common(bs, filename, flags, O_NONBLOCK);
1311 static int cdrom_probe_device(const char *filename)
1313 int fd, ret;
1314 int prio = 0;
1315 struct stat st;
1317 fd = open(filename, O_RDONLY | O_NONBLOCK);
1318 if (fd < 0) {
1319 goto out;
1321 ret = fstat(fd, &st);
1322 if (ret == -1 || !S_ISBLK(st.st_mode)) {
1323 goto outc;
1326 /* Attempt to detect via a CDROM specific ioctl */
1327 ret = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
1328 if (ret >= 0)
1329 prio = 100;
1331 outc:
1332 close(fd);
1333 out:
1334 return prio;
1337 static int cdrom_is_inserted(BlockDriverState *bs)
1339 BDRVRawState *s = bs->opaque;
1340 int ret;
1342 ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
1343 if (ret == CDS_DISC_OK)
1344 return 1;
1345 return 0;
1348 static void cdrom_eject(BlockDriverState *bs, int eject_flag)
1350 BDRVRawState *s = bs->opaque;
1352 if (eject_flag) {
1353 if (ioctl(s->fd, CDROMEJECT, NULL) < 0)
1354 perror("CDROMEJECT");
1355 } else {
1356 if (ioctl(s->fd, CDROMCLOSETRAY, NULL) < 0)
1357 perror("CDROMEJECT");
1361 static void cdrom_set_locked(BlockDriverState *bs, int locked)
1363 BDRVRawState *s = bs->opaque;
1365 if (ioctl(s->fd, CDROM_LOCKDOOR, locked) < 0) {
1367 * Note: an error can happen if the distribution automatically
1368 * mounts the CD-ROM
1370 /* perror("CDROM_LOCKDOOR"); */
1374 static BlockDriver bdrv_host_cdrom = {
1375 .format_name = "host_cdrom",
1376 .protocol_name = "host_cdrom",
1377 .instance_size = sizeof(BDRVRawState),
1378 .bdrv_probe_device = cdrom_probe_device,
1379 .bdrv_file_open = cdrom_open,
1380 .bdrv_close = raw_close,
1381 .bdrv_create = hdev_create,
1382 .create_options = raw_create_options,
1383 .bdrv_has_zero_init = hdev_has_zero_init,
1384 .bdrv_flush = raw_flush,
1386 .bdrv_aio_readv = raw_aio_readv,
1387 .bdrv_aio_writev = raw_aio_writev,
1388 .bdrv_aio_flush = raw_aio_flush,
1390 .bdrv_read = raw_read,
1391 .bdrv_write = raw_write,
1392 .bdrv_getlength = raw_getlength,
1393 .bdrv_get_allocated_file_size
1394 = raw_get_allocated_file_size,
1396 /* removable device support */
1397 .bdrv_is_inserted = cdrom_is_inserted,
1398 .bdrv_eject = cdrom_eject,
1399 .bdrv_set_locked = cdrom_set_locked,
1401 /* generic scsi device */
1402 .bdrv_ioctl = hdev_ioctl,
1403 .bdrv_aio_ioctl = hdev_aio_ioctl,
1405 #endif /* __linux__ */
1407 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
1408 static int cdrom_open(BlockDriverState *bs, const char *filename, int flags)
1410 BDRVRawState *s = bs->opaque;
1411 int ret;
1413 s->type = FTYPE_CD;
1415 ret = raw_open_common(bs, filename, flags, 0);
1416 if (ret)
1417 return ret;
1419 /* make sure the door isnt locked at this time */
1420 ioctl(s->fd, CDIOCALLOW);
1421 return 0;
1424 static int cdrom_probe_device(const char *filename)
1426 if (strstart(filename, "/dev/cd", NULL) ||
1427 strstart(filename, "/dev/acd", NULL))
1428 return 100;
1429 return 0;
1432 static int cdrom_reopen(BlockDriverState *bs)
1434 BDRVRawState *s = bs->opaque;
1435 int fd;
1438 * Force reread of possibly changed/newly loaded disc,
1439 * FreeBSD seems to not notice sometimes...
1441 if (s->fd >= 0)
1442 close(s->fd);
1443 fd = open(bs->filename, s->open_flags, 0644);
1444 if (fd < 0) {
1445 s->fd = -1;
1446 return -EIO;
1448 s->fd = fd;
1450 /* make sure the door isnt locked at this time */
1451 ioctl(s->fd, CDIOCALLOW);
1452 return 0;
1455 static int cdrom_is_inserted(BlockDriverState *bs)
1457 return raw_getlength(bs) > 0;
1460 static void cdrom_eject(BlockDriverState *bs, int eject_flag)
1462 BDRVRawState *s = bs->opaque;
1464 if (s->fd < 0)
1465 return;
1467 (void) ioctl(s->fd, CDIOCALLOW);
1469 if (eject_flag) {
1470 if (ioctl(s->fd, CDIOCEJECT) < 0)
1471 perror("CDIOCEJECT");
1472 } else {
1473 if (ioctl(s->fd, CDIOCCLOSE) < 0)
1474 perror("CDIOCCLOSE");
1477 cdrom_reopen(bs);
1480 static void cdrom_set_locked(BlockDriverState *bs, int locked)
1482 BDRVRawState *s = bs->opaque;
1484 if (s->fd < 0)
1485 return;
1486 if (ioctl(s->fd, (locked ? CDIOCPREVENT : CDIOCALLOW)) < 0) {
1488 * Note: an error can happen if the distribution automatically
1489 * mounts the CD-ROM
1491 /* perror("CDROM_LOCKDOOR"); */
1495 static BlockDriver bdrv_host_cdrom = {
1496 .format_name = "host_cdrom",
1497 .protocol_name = "host_cdrom",
1498 .instance_size = sizeof(BDRVRawState),
1499 .bdrv_probe_device = cdrom_probe_device,
1500 .bdrv_file_open = cdrom_open,
1501 .bdrv_close = raw_close,
1502 .bdrv_create = hdev_create,
1503 .create_options = raw_create_options,
1504 .bdrv_has_zero_init = hdev_has_zero_init,
1505 .bdrv_flush = raw_flush,
1507 .bdrv_aio_readv = raw_aio_readv,
1508 .bdrv_aio_writev = raw_aio_writev,
1509 .bdrv_aio_flush = raw_aio_flush,
1511 .bdrv_read = raw_read,
1512 .bdrv_write = raw_write,
1513 .bdrv_getlength = raw_getlength,
1514 .bdrv_get_allocated_file_size
1515 = raw_get_allocated_file_size,
1517 /* removable device support */
1518 .bdrv_is_inserted = cdrom_is_inserted,
1519 .bdrv_eject = cdrom_eject,
1520 .bdrv_set_locked = cdrom_set_locked,
1522 #endif /* __FreeBSD__ */
1524 static void bdrv_file_init(void)
1527 * Register all the drivers. Note that order is important, the driver
1528 * registered last will get probed first.
1530 bdrv_register(&bdrv_file);
1531 bdrv_register(&bdrv_host_device);
1532 #ifdef __linux__
1533 bdrv_register(&bdrv_host_floppy);
1534 bdrv_register(&bdrv_host_cdrom);
1535 #endif
1536 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1537 bdrv_register(&bdrv_host_cdrom);
1538 #endif
1541 block_init(bdrv_file_init);