something wrong.
[qemu/umeq.git] / block / raw-posix.c
blob96a6012f3881c1b03eb6ea44e2aa997b6f829da4
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 #include "block/add-cow.h"
85 //#define DEBUG_FLOPPY
87 //#define DEBUG_BLOCK
88 #if defined(DEBUG_BLOCK)
89 #define DEBUG_BLOCK_PRINT(formatCstr, ...) do { if (qemu_log_enabled()) \
90 { qemu_log(formatCstr, ## __VA_ARGS__); qemu_log_flush(); } } while (0)
91 #else
92 #define DEBUG_BLOCK_PRINT(formatCstr, ...)
93 #endif
95 /* OS X does not have O_DSYNC */
96 #ifndef O_DSYNC
97 #ifdef O_SYNC
98 #define O_DSYNC O_SYNC
99 #elif defined(O_FSYNC)
100 #define O_DSYNC O_FSYNC
101 #endif
102 #endif
104 /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */
105 #ifndef O_DIRECT
106 #define O_DIRECT O_DSYNC
107 #endif
109 #define FTYPE_FILE 0
110 #define FTYPE_CD 1
111 #define FTYPE_FD 2
113 /* if the FD is not accessed during that time (in ns), we try to
114 reopen it to see if the disk has been changed */
115 #define FD_OPEN_TIMEOUT (1000000000)
117 #define MAX_BLOCKSIZE 4096
119 typedef struct BDRVRawState {
120 int fd;
121 int type;
122 int open_flags;
123 #if defined(__linux__)
124 /* linux floppy specific */
125 int64_t fd_open_time;
126 int64_t fd_error_time;
127 int fd_got_error;
128 int fd_media_changed;
129 #endif
130 #ifdef CONFIG_LINUX_AIO
131 int use_aio;
132 void *aio_ctx;
133 #endif
134 uint8_t *aligned_buf;
135 unsigned aligned_buf_size;
136 #ifdef CONFIG_XFS
137 bool is_xfs : 1;
138 #endif
139 } BDRVRawState;
141 static int fd_open(BlockDriverState *bs);
142 static int64_t raw_getlength(BlockDriverState *bs);
144 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
145 static int cdrom_reopen(BlockDriverState *bs);
146 #endif
148 #if defined(__NetBSD__)
149 static int raw_normalize_devicepath(const char **filename)
151 static char namebuf[PATH_MAX];
152 const char *dp, *fname;
153 struct stat sb;
155 fname = *filename;
156 dp = strrchr(fname, '/');
157 if (lstat(fname, &sb) < 0) {
158 fprintf(stderr, "%s: stat failed: %s\n",
159 fname, strerror(errno));
160 return -errno;
163 if (!S_ISBLK(sb.st_mode)) {
164 return 0;
167 if (dp == NULL) {
168 snprintf(namebuf, PATH_MAX, "r%s", fname);
169 } else {
170 snprintf(namebuf, PATH_MAX, "%.*s/r%s",
171 (int)(dp - fname), fname, dp + 1);
173 fprintf(stderr, "%s is a block device", fname);
174 *filename = namebuf;
175 fprintf(stderr, ", using %s\n", *filename);
177 return 0;
179 #else
180 static int raw_normalize_devicepath(const char **filename)
182 return 0;
184 #endif
186 static int raw_open_common(BlockDriverState *bs, const char *filename,
187 int bdrv_flags, int open_flags)
189 BDRVRawState *s = bs->opaque;
190 int fd, ret;
192 ret = raw_normalize_devicepath(&filename);
193 if (ret != 0) {
194 return ret;
197 s->open_flags = open_flags | O_BINARY;
198 s->open_flags &= ~O_ACCMODE;
199 if (bdrv_flags & BDRV_O_RDWR) {
200 s->open_flags |= O_RDWR;
201 } else {
202 s->open_flags |= O_RDONLY;
205 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
206 * and O_DIRECT for no caching. */
207 if ((bdrv_flags & BDRV_O_NOCACHE))
208 s->open_flags |= O_DIRECT;
209 if (!(bdrv_flags & BDRV_O_CACHE_WB))
210 s->open_flags |= O_DSYNC;
212 s->fd = -1;
213 fd = qemu_open(filename, s->open_flags, 0644);
214 if (fd < 0) {
215 ret = -errno;
216 if (ret == -EROFS)
217 ret = -EACCES;
218 return ret;
220 s->fd = fd;
221 s->aligned_buf = NULL;
223 if ((bdrv_flags & BDRV_O_NOCACHE)) {
225 * Allocate a buffer for read/modify/write cycles. Chose the size
226 * pessimistically as we don't know the block size yet.
228 s->aligned_buf_size = 32 * MAX_BLOCKSIZE;
229 s->aligned_buf = qemu_memalign(MAX_BLOCKSIZE, s->aligned_buf_size);
230 if (s->aligned_buf == NULL) {
231 goto out_close;
235 /* We're falling back to POSIX AIO in some cases so init always */
236 if (paio_init() < 0) {
237 goto out_free_buf;
240 #ifdef CONFIG_LINUX_AIO
241 if ((bdrv_flags & (BDRV_O_NOCACHE|BDRV_O_NATIVE_AIO)) ==
242 (BDRV_O_NOCACHE|BDRV_O_NATIVE_AIO)) {
244 s->aio_ctx = laio_init();
245 if (!s->aio_ctx) {
246 goto out_free_buf;
248 s->use_aio = 1;
249 } else
250 #endif
252 #ifdef CONFIG_LINUX_AIO
253 s->use_aio = 0;
254 #endif
257 #ifdef CONFIG_XFS
258 if (platform_test_xfs_fd(s->fd)) {
259 s->is_xfs = 1;
261 #endif
263 return 0;
265 out_free_buf:
266 qemu_vfree(s->aligned_buf);
267 out_close:
268 close(fd);
269 return -errno;
272 static int raw_open(BlockDriverState *bs, const char *filename, int flags)
274 BDRVRawState *s = bs->opaque;
275 struct add_cow_head cow_header;
277 if(bs->cow_file[0] && (get_add_cow_head(bs->cow_file,&cow_header) <0 )) {
278 DEBUG_BLOCK_PRINT("get_add_cow_head() failed: %d = %s\n",
279 errno, strerror(errno));
282 s->type = FTYPE_FILE;
283 return raw_open_common(bs, filename, flags, 0);
286 /* XXX: use host sector size if necessary with:
287 #ifdef DIOCGSECTORSIZE
289 unsigned int sectorsize = 512;
290 if (!ioctl(fd, DIOCGSECTORSIZE, &sectorsize) &&
291 sectorsize > bufsize)
292 bufsize = sectorsize;
294 #endif
295 #ifdef CONFIG_COCOA
296 uint32_t blockSize = 512;
297 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
298 bufsize = blockSize;
300 #endif
304 * offset and count are in bytes, but must be multiples of 512 for files
305 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
307 * This function may be called without alignment if the caller ensures
308 * that O_DIRECT is not in effect.
310 static int raw_pread_aligned(BlockDriverState *bs, int64_t offset,
311 uint8_t *buf, int count)
313 BDRVRawState *s = bs->opaque;
314 int ret;
316 ret = fd_open(bs);
317 if (ret < 0)
318 return ret;
320 ret = pread(s->fd, buf, count, offset);
321 if (ret == count)
322 return ret;
324 /* Allow reads beyond the end (needed for pwrite) */
325 if ((ret == 0) && bs->growable) {
326 int64_t size = raw_getlength(bs);
327 if (offset >= size) {
328 memset(buf, 0, count);
329 return count;
333 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
334 "] read failed %d : %d = %s\n",
335 s->fd, bs->filename, offset, buf, count,
336 bs->total_sectors, ret, errno, strerror(errno));
338 /* Try harder for CDrom. */
339 if (s->type != FTYPE_FILE) {
340 ret = pread(s->fd, buf, count, offset);
341 if (ret == count)
342 return ret;
343 ret = pread(s->fd, buf, count, offset);
344 if (ret == count)
345 return ret;
347 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
348 "] retry read failed %d : %d = %s\n",
349 s->fd, bs->filename, offset, buf, count,
350 bs->total_sectors, ret, errno, strerror(errno));
353 return (ret < 0) ? -errno : ret;
357 * offset and count are in bytes, but must be multiples of the sector size
358 * for files opened with O_DIRECT. buf must be aligned to sector size bytes
359 * then.
361 * This function may be called without alignment if the caller ensures
362 * that O_DIRECT is not in effect.
364 static int raw_pwrite_aligned(BlockDriverState *bs, int64_t offset,
365 const uint8_t *buf, int count)
367 BDRVRawState *s = bs->opaque;
368 int ret;
370 ret = fd_open(bs);
371 if (ret < 0)
372 return -errno;
374 ret = pwrite(s->fd, buf, count, offset);
375 if (ret == count)
376 return ret;
378 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
379 "] write failed %d : %d = %s\n",
380 s->fd, bs->filename, offset, buf, count,
381 bs->total_sectors, ret, errno, strerror(errno));
383 return (ret < 0) ? -errno : ret;
388 * offset and count are in bytes and possibly not aligned. For files opened
389 * with O_DIRECT, necessary alignments are ensured before calling
390 * raw_pread_aligned to do the actual read.
392 static int raw_pread(BlockDriverState *bs, int64_t offset,
393 uint8_t *buf, int count)
395 BDRVRawState *s = bs->opaque;
396 unsigned sector_mask = bs->buffer_alignment - 1;
397 int size, ret, shift, sum;
399 sum = 0;
401 if (s->aligned_buf != NULL) {
403 if (offset & sector_mask) {
404 /* align offset on a sector size bytes boundary */
406 shift = offset & sector_mask;
407 size = (shift + count + sector_mask) & ~sector_mask;
408 if (size > s->aligned_buf_size)
409 size = s->aligned_buf_size;
410 ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, size);
411 if (ret < 0)
412 return ret;
414 size = bs->buffer_alignment - shift;
415 if (size > count)
416 size = count;
417 memcpy(buf, s->aligned_buf + shift, size);
419 buf += size;
420 offset += size;
421 count -= size;
422 sum += size;
424 if (count == 0)
425 return sum;
427 if (count & sector_mask || (uintptr_t) buf & sector_mask) {
429 /* read on aligned buffer */
431 while (count) {
433 size = (count + sector_mask) & ~sector_mask;
434 if (size > s->aligned_buf_size)
435 size = s->aligned_buf_size;
437 ret = raw_pread_aligned(bs, offset, s->aligned_buf, size);
438 if (ret < 0) {
439 return ret;
440 } else if (ret == 0) {
441 fprintf(stderr, "raw_pread: read beyond end of file\n");
442 abort();
445 size = ret;
446 if (size > count)
447 size = count;
449 memcpy(buf, s->aligned_buf, size);
451 buf += size;
452 offset += size;
453 count -= size;
454 sum += size;
457 return sum;
461 return raw_pread_aligned(bs, offset, buf, count) + sum;
464 static int raw_read(BlockDriverState *bs, int64_t sector_num,
465 uint8_t *buf, int nb_sectors)
467 int ret;
469 ret = raw_pread(bs, sector_num * BDRV_SECTOR_SIZE, buf,
470 nb_sectors * BDRV_SECTOR_SIZE);
471 if (ret == (nb_sectors * BDRV_SECTOR_SIZE))
472 ret = 0;
473 return ret;
477 * offset and count are in bytes and possibly not aligned. For files opened
478 * with O_DIRECT, necessary alignments are ensured before calling
479 * raw_pwrite_aligned to do the actual write.
481 static int raw_pwrite(BlockDriverState *bs, int64_t offset,
482 const uint8_t *buf, int count)
484 BDRVRawState *s = bs->opaque;
485 unsigned sector_mask = bs->buffer_alignment - 1;
486 int size, ret, shift, sum;
488 sum = 0;
490 if (s->aligned_buf != NULL) {
492 if (offset & sector_mask) {
493 /* align offset on a sector size bytes boundary */
494 shift = offset & sector_mask;
495 ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf,
496 bs->buffer_alignment);
497 if (ret < 0)
498 return ret;
500 size = bs->buffer_alignment - shift;
501 if (size > count)
502 size = count;
503 memcpy(s->aligned_buf + shift, buf, size);
505 ret = raw_pwrite_aligned(bs, offset - shift, s->aligned_buf,
506 bs->buffer_alignment);
507 if (ret < 0)
508 return ret;
510 buf += size;
511 offset += size;
512 count -= size;
513 sum += size;
515 if (count == 0)
516 return sum;
518 if (count & sector_mask || (uintptr_t) buf & sector_mask) {
520 while ((size = (count & ~sector_mask)) != 0) {
522 if (size > s->aligned_buf_size)
523 size = s->aligned_buf_size;
525 memcpy(s->aligned_buf, buf, size);
527 ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, size);
528 if (ret < 0)
529 return ret;
531 buf += ret;
532 offset += ret;
533 count -= ret;
534 sum += ret;
536 /* here, count < sector_size because (count & ~sector_mask) == 0 */
537 if (count) {
538 ret = raw_pread_aligned(bs, offset, s->aligned_buf,
539 bs->buffer_alignment);
540 if (ret < 0)
541 return ret;
542 memcpy(s->aligned_buf, buf, count);
544 ret = raw_pwrite_aligned(bs, offset, s->aligned_buf,
545 bs->buffer_alignment);
546 if (ret < 0)
547 return ret;
548 if (count < ret)
549 ret = count;
551 sum += ret;
553 return sum;
556 return raw_pwrite_aligned(bs, offset, buf, count) + sum;
559 static int raw_write(BlockDriverState *bs, int64_t sector_num,
560 const uint8_t *buf, int nb_sectors)
562 int ret;
563 ret = raw_pwrite(bs, sector_num * BDRV_SECTOR_SIZE, buf,
564 nb_sectors * BDRV_SECTOR_SIZE);
565 if (ret == (nb_sectors * BDRV_SECTOR_SIZE))
566 ret = 0;
567 return ret;
571 * Check if all memory in this vector is sector aligned.
573 static int qiov_is_aligned(BlockDriverState *bs, QEMUIOVector *qiov)
575 int i;
577 for (i = 0; i < qiov->niov; i++) {
578 if ((uintptr_t) qiov->iov[i].iov_base % bs->buffer_alignment) {
579 return 0;
583 return 1;
586 static BlockDriverAIOCB *raw_aio_submit(BlockDriverState *bs,
587 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
588 BlockDriverCompletionFunc *cb, void *opaque, int type)
590 BDRVRawState *s = bs->opaque;
592 if (fd_open(bs) < 0)
593 return NULL;
596 * If O_DIRECT is used the buffer needs to be aligned on a sector
597 * boundary. Check if this is the case or tell the low-level
598 * driver that it needs to copy the buffer.
600 if (s->aligned_buf) {
601 if (!qiov_is_aligned(bs, qiov)) {
602 type |= QEMU_AIO_MISALIGNED;
603 #ifdef CONFIG_LINUX_AIO
604 } else if (s->use_aio) {
605 return laio_submit(bs, s->aio_ctx, s->fd, sector_num, qiov,
606 nb_sectors, cb, opaque, type);
607 #endif
611 return paio_submit(bs, s->fd, sector_num, qiov, nb_sectors,
612 cb, opaque, type);
615 static BlockDriverAIOCB *raw_aio_readv(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_READ);
623 static BlockDriverAIOCB *raw_aio_writev(BlockDriverState *bs,
624 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
625 BlockDriverCompletionFunc *cb, void *opaque)
627 return raw_aio_submit(bs, sector_num, qiov, nb_sectors,
628 cb, opaque, QEMU_AIO_WRITE);
631 static BlockDriverAIOCB *raw_aio_flush(BlockDriverState *bs,
632 BlockDriverCompletionFunc *cb, void *opaque)
634 BDRVRawState *s = bs->opaque;
636 if (fd_open(bs) < 0)
637 return NULL;
639 return paio_submit(bs, s->fd, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH);
642 static void raw_close(BlockDriverState *bs)
644 BDRVRawState *s = bs->opaque;
645 if (s->fd >= 0) {
646 close(s->fd);
647 s->fd = -1;
648 if (s->aligned_buf != NULL)
649 qemu_vfree(s->aligned_buf);
653 static int raw_truncate(BlockDriverState *bs, int64_t offset)
655 BDRVRawState *s = bs->opaque;
656 if (s->type != FTYPE_FILE)
657 return -ENOTSUP;
658 if (ftruncate(s->fd, offset) < 0)
659 return -errno;
660 return 0;
663 #ifdef __OpenBSD__
664 static int64_t raw_getlength(BlockDriverState *bs)
666 BDRVRawState *s = bs->opaque;
667 int fd = s->fd;
668 struct stat st;
670 if (fstat(fd, &st))
671 return -1;
672 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
673 struct disklabel dl;
675 if (ioctl(fd, DIOCGDINFO, &dl))
676 return -1;
677 return (uint64_t)dl.d_secsize *
678 dl.d_partitions[DISKPART(st.st_rdev)].p_size;
679 } else
680 return st.st_size;
682 #elif defined(__NetBSD__)
683 static int64_t raw_getlength(BlockDriverState *bs)
685 BDRVRawState *s = bs->opaque;
686 int fd = s->fd;
687 struct stat st;
689 if (fstat(fd, &st))
690 return -1;
691 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
692 struct dkwedge_info dkw;
694 if (ioctl(fd, DIOCGWEDGEINFO, &dkw) != -1) {
695 return dkw.dkw_size * 512;
696 } else {
697 struct disklabel dl;
699 if (ioctl(fd, DIOCGDINFO, &dl))
700 return -1;
701 return (uint64_t)dl.d_secsize *
702 dl.d_partitions[DISKPART(st.st_rdev)].p_size;
704 } else
705 return st.st_size;
707 #elif defined(__sun__)
708 static int64_t raw_getlength(BlockDriverState *bs)
710 BDRVRawState *s = bs->opaque;
711 struct dk_minfo minfo;
712 int ret;
714 ret = fd_open(bs);
715 if (ret < 0) {
716 return ret;
720 * Use the DKIOCGMEDIAINFO ioctl to read the size.
722 ret = ioctl(s->fd, DKIOCGMEDIAINFO, &minfo);
723 if (ret != -1) {
724 return minfo.dki_lbsize * minfo.dki_capacity;
728 * There are reports that lseek on some devices fails, but
729 * irc discussion said that contingency on contingency was overkill.
731 return lseek(s->fd, 0, SEEK_END);
733 #elif defined(CONFIG_BSD)
734 static int64_t raw_getlength(BlockDriverState *bs)
736 BDRVRawState *s = bs->opaque;
737 int fd = s->fd;
738 int64_t size;
739 struct stat sb;
740 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
741 int reopened = 0;
742 #endif
743 int ret;
745 ret = fd_open(bs);
746 if (ret < 0)
747 return ret;
749 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
750 again:
751 #endif
752 if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
753 #ifdef DIOCGMEDIASIZE
754 if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
755 #elif defined(DIOCGPART)
757 struct partinfo pi;
758 if (ioctl(fd, DIOCGPART, &pi) == 0)
759 size = pi.media_size;
760 else
761 size = 0;
763 if (size == 0)
764 #endif
765 #ifdef CONFIG_COCOA
766 size = LONG_LONG_MAX;
767 #else
768 size = lseek(fd, 0LL, SEEK_END);
769 #endif
770 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
771 switch(s->type) {
772 case FTYPE_CD:
773 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
774 if (size == 2048LL * (unsigned)-1)
775 size = 0;
776 /* XXX no disc? maybe we need to reopen... */
777 if (size <= 0 && !reopened && cdrom_reopen(bs) >= 0) {
778 reopened = 1;
779 goto again;
782 #endif
783 } else {
784 size = lseek(fd, 0, SEEK_END);
786 return size;
788 #else
789 static int64_t raw_getlength(BlockDriverState *bs)
791 BDRVRawState *s = bs->opaque;
792 int ret;
794 ret = fd_open(bs);
795 if (ret < 0) {
796 return ret;
799 return lseek(s->fd, 0, SEEK_END);
801 #endif
803 static int64_t raw_get_allocated_file_size(BlockDriverState *bs)
805 struct stat st;
806 BDRVRawState *s = bs->opaque;
808 if (fstat(s->fd, &st) < 0) {
809 return -errno;
811 return (int64_t)st.st_blocks * 512;
814 static int raw_create(const char *filename, QEMUOptionParameter *options)
816 int fd;
817 int result = 0;
818 int64_t total_size = 0;
819 char *backing_file = NULL;
820 char *backing_fmt = NULL;
821 char *cow_file = NULL;
823 /* Read out options */
824 while (options && options->name) {
825 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
826 total_size = options->value.n / BDRV_SECTOR_SIZE;
828 else if(!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {
829 backing_file = options->value.s;
831 else if(!strcmp(options->name, BLOCK_OPT_BACKING_FMT)) {
832 backing_fmt = options->value.s;
834 else if(!strcmp(options->name, BLOCK_OPT_COW_FILE)) {
835 cow_file = options->value.s;
838 options++;
841 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
842 0644);
843 if (fd < 0) {
844 result = -errno;
845 } else {
846 if (ftruncate(fd, total_size * BDRV_SECTOR_SIZE) != 0) {
847 result = -errno;
849 if (close(fd) != 0) {
850 result = -errno;
854 create_cow_file(backing_file,backing_fmt,cow_file,total_size);
855 return result;
858 static int raw_flush(BlockDriverState *bs)
860 BDRVRawState *s = bs->opaque;
861 return qemu_fdatasync(s->fd);
864 #ifdef CONFIG_XFS
865 static int xfs_discard(BDRVRawState *s, int64_t sector_num, int nb_sectors)
867 struct xfs_flock64 fl;
869 memset(&fl, 0, sizeof(fl));
870 fl.l_whence = SEEK_SET;
871 fl.l_start = sector_num << 9;
872 fl.l_len = (int64_t)nb_sectors << 9;
874 if (xfsctl(NULL, s->fd, XFS_IOC_UNRESVSP64, &fl) < 0) {
875 DEBUG_BLOCK_PRINT("cannot punch hole (%s)\n", strerror(errno));
876 return -errno;
879 return 0;
881 #endif
883 static int raw_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors)
885 #ifdef CONFIG_XFS
886 BDRVRawState *s = bs->opaque;
888 if (s->is_xfs) {
889 return xfs_discard(s, sector_num, nb_sectors);
891 #endif
893 return 0;
896 static QEMUOptionParameter raw_create_options[] = {
898 .name = BLOCK_OPT_SIZE,
899 .type = OPT_SIZE,
900 .help = "Virtual disk size"
902 { NULL }
905 static BlockDriver bdrv_file = {
906 .format_name = "file",
907 .protocol_name = "file",
908 .instance_size = sizeof(BDRVRawState),
909 .bdrv_probe = NULL, /* no probe for protocols */
910 .bdrv_file_open = raw_open,
911 .bdrv_read = raw_read,
912 .bdrv_write = raw_write,
913 .bdrv_close = raw_close,
914 .bdrv_create = raw_create,
915 .bdrv_flush = raw_flush,
916 .bdrv_discard = raw_discard,
918 .bdrv_aio_readv = raw_aio_readv,
919 .bdrv_aio_writev = raw_aio_writev,
920 .bdrv_aio_flush = raw_aio_flush,
922 .bdrv_truncate = raw_truncate,
923 .bdrv_getlength = raw_getlength,
924 .bdrv_get_allocated_file_size
925 = raw_get_allocated_file_size,
927 .create_options = raw_create_options,
930 /***********************************************/
931 /* host device */
933 #ifdef CONFIG_COCOA
934 static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
935 static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
937 kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
939 kern_return_t kernResult;
940 mach_port_t masterPort;
941 CFMutableDictionaryRef classesToMatch;
943 kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
944 if ( KERN_SUCCESS != kernResult ) {
945 printf( "IOMasterPort returned %d\n", kernResult );
948 classesToMatch = IOServiceMatching( kIOCDMediaClass );
949 if ( classesToMatch == NULL ) {
950 printf( "IOServiceMatching returned a NULL dictionary.\n" );
951 } else {
952 CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
954 kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
955 if ( KERN_SUCCESS != kernResult )
957 printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
960 return kernResult;
963 kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
965 io_object_t nextMedia;
966 kern_return_t kernResult = KERN_FAILURE;
967 *bsdPath = '\0';
968 nextMedia = IOIteratorNext( mediaIterator );
969 if ( nextMedia )
971 CFTypeRef bsdPathAsCFString;
972 bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
973 if ( bsdPathAsCFString ) {
974 size_t devPathLength;
975 strcpy( bsdPath, _PATH_DEV );
976 strcat( bsdPath, "r" );
977 devPathLength = strlen( bsdPath );
978 if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
979 kernResult = KERN_SUCCESS;
981 CFRelease( bsdPathAsCFString );
983 IOObjectRelease( nextMedia );
986 return kernResult;
989 #endif
991 static int hdev_probe_device(const char *filename)
993 struct stat st;
995 /* allow a dedicated CD-ROM driver to match with a higher priority */
996 if (strstart(filename, "/dev/cdrom", NULL))
997 return 50;
999 if (stat(filename, &st) >= 0 &&
1000 (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
1001 return 100;
1004 return 0;
1007 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
1009 BDRVRawState *s = bs->opaque;
1011 #ifdef CONFIG_COCOA
1012 if (strstart(filename, "/dev/cdrom", NULL)) {
1013 kern_return_t kernResult;
1014 io_iterator_t mediaIterator;
1015 char bsdPath[ MAXPATHLEN ];
1016 int fd;
1018 kernResult = FindEjectableCDMedia( &mediaIterator );
1019 kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
1021 if ( bsdPath[ 0 ] != '\0' ) {
1022 strcat(bsdPath,"s0");
1023 /* some CDs don't have a partition 0 */
1024 fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
1025 if (fd < 0) {
1026 bsdPath[strlen(bsdPath)-1] = '1';
1027 } else {
1028 close(fd);
1030 filename = bsdPath;
1033 if ( mediaIterator )
1034 IOObjectRelease( mediaIterator );
1036 #endif
1038 s->type = FTYPE_FILE;
1039 #if defined(__linux__)
1041 char resolved_path[ MAXPATHLEN ], *temp;
1043 temp = realpath(filename, resolved_path);
1044 if (temp && strstart(temp, "/dev/sg", NULL)) {
1045 bs->sg = 1;
1048 #endif
1050 return raw_open_common(bs, filename, flags, 0);
1053 #if defined(__linux__)
1054 /* Note: we do not have a reliable method to detect if the floppy is
1055 present. The current method is to try to open the floppy at every
1056 I/O and to keep it opened during a few hundreds of ms. */
1057 static int fd_open(BlockDriverState *bs)
1059 BDRVRawState *s = bs->opaque;
1060 int last_media_present;
1062 if (s->type != FTYPE_FD)
1063 return 0;
1064 last_media_present = (s->fd >= 0);
1065 if (s->fd >= 0 &&
1066 (get_clock() - s->fd_open_time) >= FD_OPEN_TIMEOUT) {
1067 close(s->fd);
1068 s->fd = -1;
1069 #ifdef DEBUG_FLOPPY
1070 printf("Floppy closed\n");
1071 #endif
1073 if (s->fd < 0) {
1074 if (s->fd_got_error &&
1075 (get_clock() - s->fd_error_time) < FD_OPEN_TIMEOUT) {
1076 #ifdef DEBUG_FLOPPY
1077 printf("No floppy (open delayed)\n");
1078 #endif
1079 return -EIO;
1081 s->fd = open(bs->filename, s->open_flags & ~O_NONBLOCK);
1082 if (s->fd < 0) {
1083 s->fd_error_time = get_clock();
1084 s->fd_got_error = 1;
1085 if (last_media_present)
1086 s->fd_media_changed = 1;
1087 #ifdef DEBUG_FLOPPY
1088 printf("No floppy\n");
1089 #endif
1090 return -EIO;
1092 #ifdef DEBUG_FLOPPY
1093 printf("Floppy opened\n");
1094 #endif
1096 if (!last_media_present)
1097 s->fd_media_changed = 1;
1098 s->fd_open_time = get_clock();
1099 s->fd_got_error = 0;
1100 return 0;
1103 static int hdev_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1105 BDRVRawState *s = bs->opaque;
1107 return ioctl(s->fd, req, buf);
1110 static BlockDriverAIOCB *hdev_aio_ioctl(BlockDriverState *bs,
1111 unsigned long int req, void *buf,
1112 BlockDriverCompletionFunc *cb, void *opaque)
1114 BDRVRawState *s = bs->opaque;
1116 if (fd_open(bs) < 0)
1117 return NULL;
1118 return paio_ioctl(bs, s->fd, req, buf, cb, opaque);
1121 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1122 static int fd_open(BlockDriverState *bs)
1124 BDRVRawState *s = bs->opaque;
1126 /* this is just to ensure s->fd is sane (its called by io ops) */
1127 if (s->fd >= 0)
1128 return 0;
1129 return -EIO;
1131 #else /* !linux && !FreeBSD */
1133 static int fd_open(BlockDriverState *bs)
1135 return 0;
1138 #endif /* !linux && !FreeBSD */
1140 static int hdev_create(const char *filename, QEMUOptionParameter *options)
1142 int fd;
1143 int ret = 0;
1144 struct stat stat_buf;
1145 int64_t total_size = 0;
1147 /* Read out options */
1148 while (options && options->name) {
1149 if (!strcmp(options->name, "size")) {
1150 total_size = options->value.n / BDRV_SECTOR_SIZE;
1152 options++;
1155 fd = open(filename, O_WRONLY | O_BINARY);
1156 if (fd < 0)
1157 return -errno;
1159 if (fstat(fd, &stat_buf) < 0)
1160 ret = -errno;
1161 else if (!S_ISBLK(stat_buf.st_mode) && !S_ISCHR(stat_buf.st_mode))
1162 ret = -ENODEV;
1163 else if (lseek(fd, 0, SEEK_END) < total_size * BDRV_SECTOR_SIZE)
1164 ret = -ENOSPC;
1166 close(fd);
1167 return ret;
1170 static int hdev_has_zero_init(BlockDriverState *bs)
1172 return 0;
1175 static BlockDriver bdrv_host_device = {
1176 .format_name = "host_device",
1177 .protocol_name = "host_device",
1178 .instance_size = sizeof(BDRVRawState),
1179 .bdrv_probe_device = hdev_probe_device,
1180 .bdrv_file_open = hdev_open,
1181 .bdrv_close = raw_close,
1182 .bdrv_create = hdev_create,
1183 .create_options = raw_create_options,
1184 .bdrv_has_zero_init = hdev_has_zero_init,
1185 .bdrv_flush = raw_flush,
1187 .bdrv_aio_readv = raw_aio_readv,
1188 .bdrv_aio_writev = raw_aio_writev,
1189 .bdrv_aio_flush = raw_aio_flush,
1191 .bdrv_read = raw_read,
1192 .bdrv_write = raw_write,
1193 .bdrv_getlength = raw_getlength,
1194 .bdrv_get_allocated_file_size
1195 = raw_get_allocated_file_size,
1197 /* generic scsi device */
1198 #ifdef __linux__
1199 .bdrv_ioctl = hdev_ioctl,
1200 .bdrv_aio_ioctl = hdev_aio_ioctl,
1201 #endif
1204 #ifdef __linux__
1205 static int floppy_open(BlockDriverState *bs, const char *filename, int flags)
1207 BDRVRawState *s = bs->opaque;
1208 int ret;
1210 s->type = FTYPE_FD;
1212 /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
1213 ret = raw_open_common(bs, filename, flags, O_NONBLOCK);
1214 if (ret)
1215 return ret;
1217 /* close fd so that we can reopen it as needed */
1218 close(s->fd);
1219 s->fd = -1;
1220 s->fd_media_changed = 1;
1222 return 0;
1225 static int floppy_probe_device(const char *filename)
1227 int fd, ret;
1228 int prio = 0;
1229 struct floppy_struct fdparam;
1230 struct stat st;
1232 if (strstart(filename, "/dev/fd", NULL))
1233 prio = 50;
1235 fd = open(filename, O_RDONLY | O_NONBLOCK);
1236 if (fd < 0) {
1237 goto out;
1239 ret = fstat(fd, &st);
1240 if (ret == -1 || !S_ISBLK(st.st_mode)) {
1241 goto outc;
1244 /* Attempt to detect via a floppy specific ioctl */
1245 ret = ioctl(fd, FDGETPRM, &fdparam);
1246 if (ret >= 0)
1247 prio = 100;
1249 outc:
1250 close(fd);
1251 out:
1252 return prio;
1256 static int floppy_is_inserted(BlockDriverState *bs)
1258 return fd_open(bs) >= 0;
1261 static int floppy_media_changed(BlockDriverState *bs)
1263 BDRVRawState *s = bs->opaque;
1264 int ret;
1267 * XXX: we do not have a true media changed indication.
1268 * It does not work if the floppy is changed without trying to read it.
1270 fd_open(bs);
1271 ret = s->fd_media_changed;
1272 s->fd_media_changed = 0;
1273 #ifdef DEBUG_FLOPPY
1274 printf("Floppy changed=%d\n", ret);
1275 #endif
1276 return ret;
1279 static void floppy_eject(BlockDriverState *bs, int eject_flag)
1281 BDRVRawState *s = bs->opaque;
1282 int fd;
1284 if (s->fd >= 0) {
1285 close(s->fd);
1286 s->fd = -1;
1288 fd = open(bs->filename, s->open_flags | O_NONBLOCK);
1289 if (fd >= 0) {
1290 if (ioctl(fd, FDEJECT, 0) < 0)
1291 perror("FDEJECT");
1292 close(fd);
1296 static BlockDriver bdrv_host_floppy = {
1297 .format_name = "host_floppy",
1298 .protocol_name = "host_floppy",
1299 .instance_size = sizeof(BDRVRawState),
1300 .bdrv_probe_device = floppy_probe_device,
1301 .bdrv_file_open = floppy_open,
1302 .bdrv_close = raw_close,
1303 .bdrv_create = hdev_create,
1304 .create_options = raw_create_options,
1305 .bdrv_has_zero_init = hdev_has_zero_init,
1306 .bdrv_flush = raw_flush,
1308 .bdrv_aio_readv = raw_aio_readv,
1309 .bdrv_aio_writev = raw_aio_writev,
1310 .bdrv_aio_flush = raw_aio_flush,
1312 .bdrv_read = raw_read,
1313 .bdrv_write = raw_write,
1314 .bdrv_getlength = raw_getlength,
1315 .bdrv_get_allocated_file_size
1316 = raw_get_allocated_file_size,
1318 /* removable device support */
1319 .bdrv_is_inserted = floppy_is_inserted,
1320 .bdrv_media_changed = floppy_media_changed,
1321 .bdrv_eject = floppy_eject,
1324 static int cdrom_open(BlockDriverState *bs, const char *filename, int flags)
1326 BDRVRawState *s = bs->opaque;
1328 s->type = FTYPE_CD;
1330 /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
1331 return raw_open_common(bs, filename, flags, O_NONBLOCK);
1334 static int cdrom_probe_device(const char *filename)
1336 int fd, ret;
1337 int prio = 0;
1338 struct stat st;
1340 fd = open(filename, O_RDONLY | O_NONBLOCK);
1341 if (fd < 0) {
1342 goto out;
1344 ret = fstat(fd, &st);
1345 if (ret == -1 || !S_ISBLK(st.st_mode)) {
1346 goto outc;
1349 /* Attempt to detect via a CDROM specific ioctl */
1350 ret = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
1351 if (ret >= 0)
1352 prio = 100;
1354 outc:
1355 close(fd);
1356 out:
1357 return prio;
1360 static int cdrom_is_inserted(BlockDriverState *bs)
1362 BDRVRawState *s = bs->opaque;
1363 int ret;
1365 ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
1366 if (ret == CDS_DISC_OK)
1367 return 1;
1368 return 0;
1371 static void cdrom_eject(BlockDriverState *bs, int eject_flag)
1373 BDRVRawState *s = bs->opaque;
1375 if (eject_flag) {
1376 if (ioctl(s->fd, CDROMEJECT, NULL) < 0)
1377 perror("CDROMEJECT");
1378 } else {
1379 if (ioctl(s->fd, CDROMCLOSETRAY, NULL) < 0)
1380 perror("CDROMEJECT");
1384 static void cdrom_set_locked(BlockDriverState *bs, int locked)
1386 BDRVRawState *s = bs->opaque;
1388 if (ioctl(s->fd, CDROM_LOCKDOOR, locked) < 0) {
1390 * Note: an error can happen if the distribution automatically
1391 * mounts the CD-ROM
1393 /* perror("CDROM_LOCKDOOR"); */
1397 static BlockDriver bdrv_host_cdrom = {
1398 .format_name = "host_cdrom",
1399 .protocol_name = "host_cdrom",
1400 .instance_size = sizeof(BDRVRawState),
1401 .bdrv_probe_device = cdrom_probe_device,
1402 .bdrv_file_open = cdrom_open,
1403 .bdrv_close = raw_close,
1404 .bdrv_create = hdev_create,
1405 .create_options = raw_create_options,
1406 .bdrv_has_zero_init = hdev_has_zero_init,
1407 .bdrv_flush = raw_flush,
1409 .bdrv_aio_readv = raw_aio_readv,
1410 .bdrv_aio_writev = raw_aio_writev,
1411 .bdrv_aio_flush = raw_aio_flush,
1413 .bdrv_read = raw_read,
1414 .bdrv_write = raw_write,
1415 .bdrv_getlength = raw_getlength,
1416 .bdrv_get_allocated_file_size
1417 = raw_get_allocated_file_size,
1419 /* removable device support */
1420 .bdrv_is_inserted = cdrom_is_inserted,
1421 .bdrv_eject = cdrom_eject,
1422 .bdrv_set_locked = cdrom_set_locked,
1424 /* generic scsi device */
1425 .bdrv_ioctl = hdev_ioctl,
1426 .bdrv_aio_ioctl = hdev_aio_ioctl,
1428 #endif /* __linux__ */
1430 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
1431 static int cdrom_open(BlockDriverState *bs, const char *filename, int flags)
1433 BDRVRawState *s = bs->opaque;
1434 int ret;
1436 s->type = FTYPE_CD;
1438 ret = raw_open_common(bs, filename, flags, 0);
1439 if (ret)
1440 return ret;
1442 /* make sure the door isnt locked at this time */
1443 ioctl(s->fd, CDIOCALLOW);
1444 return 0;
1447 static int cdrom_probe_device(const char *filename)
1449 if (strstart(filename, "/dev/cd", NULL) ||
1450 strstart(filename, "/dev/acd", NULL))
1451 return 100;
1452 return 0;
1455 static int cdrom_reopen(BlockDriverState *bs)
1457 BDRVRawState *s = bs->opaque;
1458 int fd;
1461 * Force reread of possibly changed/newly loaded disc,
1462 * FreeBSD seems to not notice sometimes...
1464 if (s->fd >= 0)
1465 close(s->fd);
1466 fd = open(bs->filename, s->open_flags, 0644);
1467 if (fd < 0) {
1468 s->fd = -1;
1469 return -EIO;
1471 s->fd = fd;
1473 /* make sure the door isnt locked at this time */
1474 ioctl(s->fd, CDIOCALLOW);
1475 return 0;
1478 static int cdrom_is_inserted(BlockDriverState *bs)
1480 return raw_getlength(bs) > 0;
1483 static void cdrom_eject(BlockDriverState *bs, int eject_flag)
1485 BDRVRawState *s = bs->opaque;
1487 if (s->fd < 0)
1488 return;
1490 (void) ioctl(s->fd, CDIOCALLOW);
1492 if (eject_flag) {
1493 if (ioctl(s->fd, CDIOCEJECT) < 0)
1494 perror("CDIOCEJECT");
1495 } else {
1496 if (ioctl(s->fd, CDIOCCLOSE) < 0)
1497 perror("CDIOCCLOSE");
1500 cdrom_reopen(bs);
1503 static void cdrom_set_locked(BlockDriverState *bs, int locked)
1505 BDRVRawState *s = bs->opaque;
1507 if (s->fd < 0)
1508 return;
1509 if (ioctl(s->fd, (locked ? CDIOCPREVENT : CDIOCALLOW)) < 0) {
1511 * Note: an error can happen if the distribution automatically
1512 * mounts the CD-ROM
1514 /* perror("CDROM_LOCKDOOR"); */
1518 static BlockDriver bdrv_host_cdrom = {
1519 .format_name = "host_cdrom",
1520 .protocol_name = "host_cdrom",
1521 .instance_size = sizeof(BDRVRawState),
1522 .bdrv_probe_device = cdrom_probe_device,
1523 .bdrv_file_open = cdrom_open,
1524 .bdrv_close = raw_close,
1525 .bdrv_create = hdev_create,
1526 .create_options = raw_create_options,
1527 .bdrv_has_zero_init = hdev_has_zero_init,
1528 .bdrv_flush = raw_flush,
1530 .bdrv_aio_readv = raw_aio_readv,
1531 .bdrv_aio_writev = raw_aio_writev,
1532 .bdrv_aio_flush = raw_aio_flush,
1534 .bdrv_read = raw_read,
1535 .bdrv_write = raw_write,
1536 .bdrv_getlength = raw_getlength,
1537 .bdrv_get_allocated_file_size
1538 = raw_get_allocated_file_size,
1540 /* removable device support */
1541 .bdrv_is_inserted = cdrom_is_inserted,
1542 .bdrv_eject = cdrom_eject,
1543 .bdrv_set_locked = cdrom_set_locked,
1545 #endif /* __FreeBSD__ */
1547 static void bdrv_file_init(void)
1550 * Register all the drivers. Note that order is important, the driver
1551 * registered last will get probed first.
1553 bdrv_register(&bdrv_file);
1554 bdrv_register(&bdrv_host_device);
1555 #ifdef __linux__
1556 bdrv_register(&bdrv_host_floppy);
1557 bdrv_register(&bdrv_host_cdrom);
1558 #endif
1559 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1560 bdrv_register(&bdrv_host_cdrom);
1561 #endif
1564 block_init(bdrv_file_init);