block: Drop tray status tracking, no longer used
[qemu.git] / block / raw-posix.c
blobbcf50b2cf76ce2b8c7e8f67aaa4789dc8501c994
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
240 * Currently Linux do AIO only for files opened with O_DIRECT
241 * specified so check NOCACHE flag too
243 if ((bdrv_flags & (BDRV_O_NOCACHE|BDRV_O_NATIVE_AIO)) ==
244 (BDRV_O_NOCACHE|BDRV_O_NATIVE_AIO)) {
246 s->aio_ctx = laio_init();
247 if (!s->aio_ctx) {
248 goto out_free_buf;
250 s->use_aio = 1;
251 } else
252 #endif
254 #ifdef CONFIG_LINUX_AIO
255 s->use_aio = 0;
256 #endif
259 #ifdef CONFIG_XFS
260 if (platform_test_xfs_fd(s->fd)) {
261 s->is_xfs = 1;
263 #endif
265 return 0;
267 out_free_buf:
268 qemu_vfree(s->aligned_buf);
269 out_close:
270 close(fd);
271 return -errno;
274 static int raw_open(BlockDriverState *bs, const char *filename, int flags)
276 BDRVRawState *s = bs->opaque;
278 s->type = FTYPE_FILE;
279 return raw_open_common(bs, filename, flags, 0);
282 /* XXX: use host sector size if necessary with:
283 #ifdef DIOCGSECTORSIZE
285 unsigned int sectorsize = 512;
286 if (!ioctl(fd, DIOCGSECTORSIZE, &sectorsize) &&
287 sectorsize > bufsize)
288 bufsize = sectorsize;
290 #endif
291 #ifdef CONFIG_COCOA
292 uint32_t blockSize = 512;
293 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
294 bufsize = blockSize;
296 #endif
300 * offset and count are in bytes, but must be multiples of 512 for files
301 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
303 * This function may be called without alignment if the caller ensures
304 * that O_DIRECT is not in effect.
306 static int raw_pread_aligned(BlockDriverState *bs, int64_t offset,
307 uint8_t *buf, int count)
309 BDRVRawState *s = bs->opaque;
310 int ret;
312 ret = fd_open(bs);
313 if (ret < 0)
314 return ret;
316 ret = pread(s->fd, buf, count, offset);
317 if (ret == count)
318 return ret;
320 /* Allow reads beyond the end (needed for pwrite) */
321 if ((ret == 0) && bs->growable) {
322 int64_t size = raw_getlength(bs);
323 if (offset >= size) {
324 memset(buf, 0, count);
325 return count;
329 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
330 "] read failed %d : %d = %s\n",
331 s->fd, bs->filename, offset, buf, count,
332 bs->total_sectors, ret, errno, strerror(errno));
334 /* Try harder for CDrom. */
335 if (s->type != FTYPE_FILE) {
336 ret = pread(s->fd, buf, count, offset);
337 if (ret == count)
338 return ret;
339 ret = pread(s->fd, buf, count, offset);
340 if (ret == count)
341 return ret;
343 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
344 "] retry read failed %d : %d = %s\n",
345 s->fd, bs->filename, offset, buf, count,
346 bs->total_sectors, ret, errno, strerror(errno));
349 return (ret < 0) ? -errno : ret;
353 * offset and count are in bytes, but must be multiples of the sector size
354 * for files opened with O_DIRECT. buf must be aligned to sector size bytes
355 * then.
357 * This function may be called without alignment if the caller ensures
358 * that O_DIRECT is not in effect.
360 static int raw_pwrite_aligned(BlockDriverState *bs, int64_t offset,
361 const uint8_t *buf, int count)
363 BDRVRawState *s = bs->opaque;
364 int ret;
366 ret = fd_open(bs);
367 if (ret < 0)
368 return -errno;
370 ret = pwrite(s->fd, buf, count, offset);
371 if (ret == count)
372 return ret;
374 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
375 "] write failed %d : %d = %s\n",
376 s->fd, bs->filename, offset, buf, count,
377 bs->total_sectors, ret, errno, strerror(errno));
379 return (ret < 0) ? -errno : ret;
384 * offset and count are in bytes and possibly not aligned. For files opened
385 * with O_DIRECT, necessary alignments are ensured before calling
386 * raw_pread_aligned to do the actual read.
388 static int raw_pread(BlockDriverState *bs, int64_t offset,
389 uint8_t *buf, int count)
391 BDRVRawState *s = bs->opaque;
392 unsigned sector_mask = bs->buffer_alignment - 1;
393 int size, ret, shift, sum;
395 sum = 0;
397 if (s->aligned_buf != NULL) {
399 if (offset & sector_mask) {
400 /* align offset on a sector size bytes boundary */
402 shift = offset & sector_mask;
403 size = (shift + count + sector_mask) & ~sector_mask;
404 if (size > s->aligned_buf_size)
405 size = s->aligned_buf_size;
406 ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, size);
407 if (ret < 0)
408 return ret;
410 size = bs->buffer_alignment - shift;
411 if (size > count)
412 size = count;
413 memcpy(buf, s->aligned_buf + shift, size);
415 buf += size;
416 offset += size;
417 count -= size;
418 sum += size;
420 if (count == 0)
421 return sum;
423 if (count & sector_mask || (uintptr_t) buf & sector_mask) {
425 /* read on aligned buffer */
427 while (count) {
429 size = (count + sector_mask) & ~sector_mask;
430 if (size > s->aligned_buf_size)
431 size = s->aligned_buf_size;
433 ret = raw_pread_aligned(bs, offset, s->aligned_buf, size);
434 if (ret < 0) {
435 return ret;
436 } else if (ret == 0) {
437 fprintf(stderr, "raw_pread: read beyond end of file\n");
438 abort();
441 size = ret;
442 if (size > count)
443 size = count;
445 memcpy(buf, s->aligned_buf, size);
447 buf += size;
448 offset += size;
449 count -= size;
450 sum += size;
453 return sum;
457 return raw_pread_aligned(bs, offset, buf, count) + sum;
460 static int raw_read(BlockDriverState *bs, int64_t sector_num,
461 uint8_t *buf, int nb_sectors)
463 int ret;
465 ret = raw_pread(bs, sector_num * BDRV_SECTOR_SIZE, buf,
466 nb_sectors * BDRV_SECTOR_SIZE);
467 if (ret == (nb_sectors * BDRV_SECTOR_SIZE))
468 ret = 0;
469 return ret;
473 * offset and count are in bytes and possibly not aligned. For files opened
474 * with O_DIRECT, necessary alignments are ensured before calling
475 * raw_pwrite_aligned to do the actual write.
477 static int raw_pwrite(BlockDriverState *bs, int64_t offset,
478 const uint8_t *buf, int count)
480 BDRVRawState *s = bs->opaque;
481 unsigned sector_mask = bs->buffer_alignment - 1;
482 int size, ret, shift, sum;
484 sum = 0;
486 if (s->aligned_buf != NULL) {
488 if (offset & sector_mask) {
489 /* align offset on a sector size bytes boundary */
490 shift = offset & sector_mask;
491 ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf,
492 bs->buffer_alignment);
493 if (ret < 0)
494 return ret;
496 size = bs->buffer_alignment - shift;
497 if (size > count)
498 size = count;
499 memcpy(s->aligned_buf + shift, buf, size);
501 ret = raw_pwrite_aligned(bs, offset - shift, s->aligned_buf,
502 bs->buffer_alignment);
503 if (ret < 0)
504 return ret;
506 buf += size;
507 offset += size;
508 count -= size;
509 sum += size;
511 if (count == 0)
512 return sum;
514 if (count & sector_mask || (uintptr_t) buf & sector_mask) {
516 while ((size = (count & ~sector_mask)) != 0) {
518 if (size > s->aligned_buf_size)
519 size = s->aligned_buf_size;
521 memcpy(s->aligned_buf, buf, size);
523 ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, size);
524 if (ret < 0)
525 return ret;
527 buf += ret;
528 offset += ret;
529 count -= ret;
530 sum += ret;
532 /* here, count < sector_size because (count & ~sector_mask) == 0 */
533 if (count) {
534 ret = raw_pread_aligned(bs, offset, s->aligned_buf,
535 bs->buffer_alignment);
536 if (ret < 0)
537 return ret;
538 memcpy(s->aligned_buf, buf, count);
540 ret = raw_pwrite_aligned(bs, offset, s->aligned_buf,
541 bs->buffer_alignment);
542 if (ret < 0)
543 return ret;
544 if (count < ret)
545 ret = count;
547 sum += ret;
549 return sum;
552 return raw_pwrite_aligned(bs, offset, buf, count) + sum;
555 static int raw_write(BlockDriverState *bs, int64_t sector_num,
556 const uint8_t *buf, int nb_sectors)
558 int ret;
559 ret = raw_pwrite(bs, sector_num * BDRV_SECTOR_SIZE, buf,
560 nb_sectors * BDRV_SECTOR_SIZE);
561 if (ret == (nb_sectors * BDRV_SECTOR_SIZE))
562 ret = 0;
563 return ret;
567 * Check if all memory in this vector is sector aligned.
569 static int qiov_is_aligned(BlockDriverState *bs, QEMUIOVector *qiov)
571 int i;
573 for (i = 0; i < qiov->niov; i++) {
574 if ((uintptr_t) qiov->iov[i].iov_base % bs->buffer_alignment) {
575 return 0;
579 return 1;
582 static BlockDriverAIOCB *raw_aio_submit(BlockDriverState *bs,
583 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
584 BlockDriverCompletionFunc *cb, void *opaque, int type)
586 BDRVRawState *s = bs->opaque;
588 if (fd_open(bs) < 0)
589 return NULL;
592 * If O_DIRECT is used the buffer needs to be aligned on a sector
593 * boundary. Check if this is the case or tell the low-level
594 * driver that it needs to copy the buffer.
596 if (s->aligned_buf) {
597 if (!qiov_is_aligned(bs, qiov)) {
598 type |= QEMU_AIO_MISALIGNED;
599 #ifdef CONFIG_LINUX_AIO
600 } else if (s->use_aio) {
601 return laio_submit(bs, s->aio_ctx, s->fd, sector_num, qiov,
602 nb_sectors, cb, opaque, type);
603 #endif
607 return paio_submit(bs, s->fd, sector_num, qiov, nb_sectors,
608 cb, opaque, type);
611 static BlockDriverAIOCB *raw_aio_readv(BlockDriverState *bs,
612 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
613 BlockDriverCompletionFunc *cb, void *opaque)
615 return raw_aio_submit(bs, sector_num, qiov, nb_sectors,
616 cb, opaque, QEMU_AIO_READ);
619 static BlockDriverAIOCB *raw_aio_writev(BlockDriverState *bs,
620 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
621 BlockDriverCompletionFunc *cb, void *opaque)
623 return raw_aio_submit(bs, sector_num, qiov, nb_sectors,
624 cb, opaque, QEMU_AIO_WRITE);
627 static BlockDriverAIOCB *raw_aio_flush(BlockDriverState *bs,
628 BlockDriverCompletionFunc *cb, void *opaque)
630 BDRVRawState *s = bs->opaque;
632 if (fd_open(bs) < 0)
633 return NULL;
635 return paio_submit(bs, s->fd, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH);
638 static void raw_close(BlockDriverState *bs)
640 BDRVRawState *s = bs->opaque;
641 if (s->fd >= 0) {
642 close(s->fd);
643 s->fd = -1;
644 if (s->aligned_buf != NULL)
645 qemu_vfree(s->aligned_buf);
649 static int raw_truncate(BlockDriverState *bs, int64_t offset)
651 BDRVRawState *s = bs->opaque;
652 if (s->type != FTYPE_FILE)
653 return -ENOTSUP;
654 if (ftruncate(s->fd, offset) < 0)
655 return -errno;
656 return 0;
659 #ifdef __OpenBSD__
660 static int64_t raw_getlength(BlockDriverState *bs)
662 BDRVRawState *s = bs->opaque;
663 int fd = s->fd;
664 struct stat st;
666 if (fstat(fd, &st))
667 return -1;
668 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
669 struct disklabel dl;
671 if (ioctl(fd, DIOCGDINFO, &dl))
672 return -1;
673 return (uint64_t)dl.d_secsize *
674 dl.d_partitions[DISKPART(st.st_rdev)].p_size;
675 } else
676 return st.st_size;
678 #elif defined(__NetBSD__)
679 static int64_t raw_getlength(BlockDriverState *bs)
681 BDRVRawState *s = bs->opaque;
682 int fd = s->fd;
683 struct stat st;
685 if (fstat(fd, &st))
686 return -1;
687 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
688 struct dkwedge_info dkw;
690 if (ioctl(fd, DIOCGWEDGEINFO, &dkw) != -1) {
691 return dkw.dkw_size * 512;
692 } else {
693 struct disklabel dl;
695 if (ioctl(fd, DIOCGDINFO, &dl))
696 return -1;
697 return (uint64_t)dl.d_secsize *
698 dl.d_partitions[DISKPART(st.st_rdev)].p_size;
700 } else
701 return st.st_size;
703 #elif defined(__sun__)
704 static int64_t raw_getlength(BlockDriverState *bs)
706 BDRVRawState *s = bs->opaque;
707 struct dk_minfo minfo;
708 int ret;
710 ret = fd_open(bs);
711 if (ret < 0) {
712 return ret;
716 * Use the DKIOCGMEDIAINFO ioctl to read the size.
718 ret = ioctl(s->fd, DKIOCGMEDIAINFO, &minfo);
719 if (ret != -1) {
720 return minfo.dki_lbsize * minfo.dki_capacity;
724 * There are reports that lseek on some devices fails, but
725 * irc discussion said that contingency on contingency was overkill.
727 return lseek(s->fd, 0, SEEK_END);
729 #elif defined(CONFIG_BSD)
730 static int64_t raw_getlength(BlockDriverState *bs)
732 BDRVRawState *s = bs->opaque;
733 int fd = s->fd;
734 int64_t size;
735 struct stat sb;
736 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
737 int reopened = 0;
738 #endif
739 int ret;
741 ret = fd_open(bs);
742 if (ret < 0)
743 return ret;
745 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
746 again:
747 #endif
748 if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
749 #ifdef DIOCGMEDIASIZE
750 if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
751 #elif defined(DIOCGPART)
753 struct partinfo pi;
754 if (ioctl(fd, DIOCGPART, &pi) == 0)
755 size = pi.media_size;
756 else
757 size = 0;
759 if (size == 0)
760 #endif
761 #ifdef CONFIG_COCOA
762 size = LONG_LONG_MAX;
763 #else
764 size = lseek(fd, 0LL, SEEK_END);
765 #endif
766 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
767 switch(s->type) {
768 case FTYPE_CD:
769 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
770 if (size == 2048LL * (unsigned)-1)
771 size = 0;
772 /* XXX no disc? maybe we need to reopen... */
773 if (size <= 0 && !reopened && cdrom_reopen(bs) >= 0) {
774 reopened = 1;
775 goto again;
778 #endif
779 } else {
780 size = lseek(fd, 0, SEEK_END);
782 return size;
784 #else
785 static int64_t raw_getlength(BlockDriverState *bs)
787 BDRVRawState *s = bs->opaque;
788 int ret;
790 ret = fd_open(bs);
791 if (ret < 0) {
792 return ret;
795 return lseek(s->fd, 0, SEEK_END);
797 #endif
799 static int64_t raw_get_allocated_file_size(BlockDriverState *bs)
801 struct stat st;
802 BDRVRawState *s = bs->opaque;
804 if (fstat(s->fd, &st) < 0) {
805 return -errno;
807 return (int64_t)st.st_blocks * 512;
810 static int raw_create(const char *filename, QEMUOptionParameter *options)
812 int fd;
813 int result = 0;
814 int64_t total_size = 0;
816 /* Read out options */
817 while (options && options->name) {
818 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
819 total_size = options->value.n / BDRV_SECTOR_SIZE;
821 options++;
824 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
825 0644);
826 if (fd < 0) {
827 result = -errno;
828 } else {
829 if (ftruncate(fd, total_size * BDRV_SECTOR_SIZE) != 0) {
830 result = -errno;
832 if (close(fd) != 0) {
833 result = -errno;
836 return result;
839 static int raw_flush(BlockDriverState *bs)
841 BDRVRawState *s = bs->opaque;
842 return qemu_fdatasync(s->fd);
845 #ifdef CONFIG_XFS
846 static int xfs_discard(BDRVRawState *s, int64_t sector_num, int nb_sectors)
848 struct xfs_flock64 fl;
850 memset(&fl, 0, sizeof(fl));
851 fl.l_whence = SEEK_SET;
852 fl.l_start = sector_num << 9;
853 fl.l_len = (int64_t)nb_sectors << 9;
855 if (xfsctl(NULL, s->fd, XFS_IOC_UNRESVSP64, &fl) < 0) {
856 DEBUG_BLOCK_PRINT("cannot punch hole (%s)\n", strerror(errno));
857 return -errno;
860 return 0;
862 #endif
864 static int raw_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors)
866 #ifdef CONFIG_XFS
867 BDRVRawState *s = bs->opaque;
869 if (s->is_xfs) {
870 return xfs_discard(s, sector_num, nb_sectors);
872 #endif
874 return 0;
877 static QEMUOptionParameter raw_create_options[] = {
879 .name = BLOCK_OPT_SIZE,
880 .type = OPT_SIZE,
881 .help = "Virtual disk size"
883 { NULL }
886 static BlockDriver bdrv_file = {
887 .format_name = "file",
888 .protocol_name = "file",
889 .instance_size = sizeof(BDRVRawState),
890 .bdrv_probe = NULL, /* no probe for protocols */
891 .bdrv_file_open = raw_open,
892 .bdrv_read = raw_read,
893 .bdrv_write = raw_write,
894 .bdrv_close = raw_close,
895 .bdrv_create = raw_create,
896 .bdrv_flush = raw_flush,
897 .bdrv_discard = raw_discard,
899 .bdrv_aio_readv = raw_aio_readv,
900 .bdrv_aio_writev = raw_aio_writev,
901 .bdrv_aio_flush = raw_aio_flush,
903 .bdrv_truncate = raw_truncate,
904 .bdrv_getlength = raw_getlength,
905 .bdrv_get_allocated_file_size
906 = raw_get_allocated_file_size,
908 .create_options = raw_create_options,
911 /***********************************************/
912 /* host device */
914 #ifdef CONFIG_COCOA
915 static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
916 static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
918 kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
920 kern_return_t kernResult;
921 mach_port_t masterPort;
922 CFMutableDictionaryRef classesToMatch;
924 kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
925 if ( KERN_SUCCESS != kernResult ) {
926 printf( "IOMasterPort returned %d\n", kernResult );
929 classesToMatch = IOServiceMatching( kIOCDMediaClass );
930 if ( classesToMatch == NULL ) {
931 printf( "IOServiceMatching returned a NULL dictionary.\n" );
932 } else {
933 CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
935 kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
936 if ( KERN_SUCCESS != kernResult )
938 printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
941 return kernResult;
944 kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
946 io_object_t nextMedia;
947 kern_return_t kernResult = KERN_FAILURE;
948 *bsdPath = '\0';
949 nextMedia = IOIteratorNext( mediaIterator );
950 if ( nextMedia )
952 CFTypeRef bsdPathAsCFString;
953 bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
954 if ( bsdPathAsCFString ) {
955 size_t devPathLength;
956 strcpy( bsdPath, _PATH_DEV );
957 strcat( bsdPath, "r" );
958 devPathLength = strlen( bsdPath );
959 if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
960 kernResult = KERN_SUCCESS;
962 CFRelease( bsdPathAsCFString );
964 IOObjectRelease( nextMedia );
967 return kernResult;
970 #endif
972 static int hdev_probe_device(const char *filename)
974 struct stat st;
976 /* allow a dedicated CD-ROM driver to match with a higher priority */
977 if (strstart(filename, "/dev/cdrom", NULL))
978 return 50;
980 if (stat(filename, &st) >= 0 &&
981 (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
982 return 100;
985 return 0;
988 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
990 BDRVRawState *s = bs->opaque;
992 #ifdef CONFIG_COCOA
993 if (strstart(filename, "/dev/cdrom", NULL)) {
994 kern_return_t kernResult;
995 io_iterator_t mediaIterator;
996 char bsdPath[ MAXPATHLEN ];
997 int fd;
999 kernResult = FindEjectableCDMedia( &mediaIterator );
1000 kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
1002 if ( bsdPath[ 0 ] != '\0' ) {
1003 strcat(bsdPath,"s0");
1004 /* some CDs don't have a partition 0 */
1005 fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
1006 if (fd < 0) {
1007 bsdPath[strlen(bsdPath)-1] = '1';
1008 } else {
1009 close(fd);
1011 filename = bsdPath;
1014 if ( mediaIterator )
1015 IOObjectRelease( mediaIterator );
1017 #endif
1019 s->type = FTYPE_FILE;
1020 #if defined(__linux__)
1022 char resolved_path[ MAXPATHLEN ], *temp;
1024 temp = realpath(filename, resolved_path);
1025 if (temp && strstart(temp, "/dev/sg", NULL)) {
1026 bs->sg = 1;
1029 #endif
1031 return raw_open_common(bs, filename, flags, 0);
1034 #if defined(__linux__)
1035 /* Note: we do not have a reliable method to detect if the floppy is
1036 present. The current method is to try to open the floppy at every
1037 I/O and to keep it opened during a few hundreds of ms. */
1038 static int fd_open(BlockDriverState *bs)
1040 BDRVRawState *s = bs->opaque;
1041 int last_media_present;
1043 if (s->type != FTYPE_FD)
1044 return 0;
1045 last_media_present = (s->fd >= 0);
1046 if (s->fd >= 0 &&
1047 (get_clock() - s->fd_open_time) >= FD_OPEN_TIMEOUT) {
1048 close(s->fd);
1049 s->fd = -1;
1050 #ifdef DEBUG_FLOPPY
1051 printf("Floppy closed\n");
1052 #endif
1054 if (s->fd < 0) {
1055 if (s->fd_got_error &&
1056 (get_clock() - s->fd_error_time) < FD_OPEN_TIMEOUT) {
1057 #ifdef DEBUG_FLOPPY
1058 printf("No floppy (open delayed)\n");
1059 #endif
1060 return -EIO;
1062 s->fd = open(bs->filename, s->open_flags & ~O_NONBLOCK);
1063 if (s->fd < 0) {
1064 s->fd_error_time = get_clock();
1065 s->fd_got_error = 1;
1066 if (last_media_present)
1067 s->fd_media_changed = 1;
1068 #ifdef DEBUG_FLOPPY
1069 printf("No floppy\n");
1070 #endif
1071 return -EIO;
1073 #ifdef DEBUG_FLOPPY
1074 printf("Floppy opened\n");
1075 #endif
1077 if (!last_media_present)
1078 s->fd_media_changed = 1;
1079 s->fd_open_time = get_clock();
1080 s->fd_got_error = 0;
1081 return 0;
1084 static int hdev_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1086 BDRVRawState *s = bs->opaque;
1088 return ioctl(s->fd, req, buf);
1091 static BlockDriverAIOCB *hdev_aio_ioctl(BlockDriverState *bs,
1092 unsigned long int req, void *buf,
1093 BlockDriverCompletionFunc *cb, void *opaque)
1095 BDRVRawState *s = bs->opaque;
1097 if (fd_open(bs) < 0)
1098 return NULL;
1099 return paio_ioctl(bs, s->fd, req, buf, cb, opaque);
1102 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1103 static int fd_open(BlockDriverState *bs)
1105 BDRVRawState *s = bs->opaque;
1107 /* this is just to ensure s->fd is sane (its called by io ops) */
1108 if (s->fd >= 0)
1109 return 0;
1110 return -EIO;
1112 #else /* !linux && !FreeBSD */
1114 static int fd_open(BlockDriverState *bs)
1116 return 0;
1119 #endif /* !linux && !FreeBSD */
1121 static int hdev_create(const char *filename, QEMUOptionParameter *options)
1123 int fd;
1124 int ret = 0;
1125 struct stat stat_buf;
1126 int64_t total_size = 0;
1128 /* Read out options */
1129 while (options && options->name) {
1130 if (!strcmp(options->name, "size")) {
1131 total_size = options->value.n / BDRV_SECTOR_SIZE;
1133 options++;
1136 fd = open(filename, O_WRONLY | O_BINARY);
1137 if (fd < 0)
1138 return -errno;
1140 if (fstat(fd, &stat_buf) < 0)
1141 ret = -errno;
1142 else if (!S_ISBLK(stat_buf.st_mode) && !S_ISCHR(stat_buf.st_mode))
1143 ret = -ENODEV;
1144 else if (lseek(fd, 0, SEEK_END) < total_size * BDRV_SECTOR_SIZE)
1145 ret = -ENOSPC;
1147 close(fd);
1148 return ret;
1151 static int hdev_has_zero_init(BlockDriverState *bs)
1153 return 0;
1156 static BlockDriver bdrv_host_device = {
1157 .format_name = "host_device",
1158 .protocol_name = "host_device",
1159 .instance_size = sizeof(BDRVRawState),
1160 .bdrv_probe_device = hdev_probe_device,
1161 .bdrv_file_open = hdev_open,
1162 .bdrv_close = raw_close,
1163 .bdrv_create = hdev_create,
1164 .create_options = raw_create_options,
1165 .bdrv_has_zero_init = hdev_has_zero_init,
1166 .bdrv_flush = raw_flush,
1168 .bdrv_aio_readv = raw_aio_readv,
1169 .bdrv_aio_writev = raw_aio_writev,
1170 .bdrv_aio_flush = raw_aio_flush,
1172 .bdrv_read = raw_read,
1173 .bdrv_write = raw_write,
1174 .bdrv_getlength = raw_getlength,
1175 .bdrv_get_allocated_file_size
1176 = raw_get_allocated_file_size,
1178 /* generic scsi device */
1179 #ifdef __linux__
1180 .bdrv_ioctl = hdev_ioctl,
1181 .bdrv_aio_ioctl = hdev_aio_ioctl,
1182 #endif
1185 #ifdef __linux__
1186 static int floppy_open(BlockDriverState *bs, const char *filename, int flags)
1188 BDRVRawState *s = bs->opaque;
1189 int ret;
1191 s->type = FTYPE_FD;
1193 /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
1194 ret = raw_open_common(bs, filename, flags, O_NONBLOCK);
1195 if (ret)
1196 return ret;
1198 /* close fd so that we can reopen it as needed */
1199 close(s->fd);
1200 s->fd = -1;
1201 s->fd_media_changed = 1;
1203 return 0;
1206 static int floppy_probe_device(const char *filename)
1208 int fd, ret;
1209 int prio = 0;
1210 struct floppy_struct fdparam;
1211 struct stat st;
1213 if (strstart(filename, "/dev/fd", NULL))
1214 prio = 50;
1216 fd = open(filename, O_RDONLY | O_NONBLOCK);
1217 if (fd < 0) {
1218 goto out;
1220 ret = fstat(fd, &st);
1221 if (ret == -1 || !S_ISBLK(st.st_mode)) {
1222 goto outc;
1225 /* Attempt to detect via a floppy specific ioctl */
1226 ret = ioctl(fd, FDGETPRM, &fdparam);
1227 if (ret >= 0)
1228 prio = 100;
1230 outc:
1231 close(fd);
1232 out:
1233 return prio;
1237 static int floppy_is_inserted(BlockDriverState *bs)
1239 return fd_open(bs) >= 0;
1242 static int floppy_media_changed(BlockDriverState *bs)
1244 BDRVRawState *s = bs->opaque;
1245 int ret;
1248 * XXX: we do not have a true media changed indication.
1249 * It does not work if the floppy is changed without trying to read it.
1251 fd_open(bs);
1252 ret = s->fd_media_changed;
1253 s->fd_media_changed = 0;
1254 #ifdef DEBUG_FLOPPY
1255 printf("Floppy changed=%d\n", ret);
1256 #endif
1257 return ret;
1260 static void floppy_eject(BlockDriverState *bs, int eject_flag)
1262 BDRVRawState *s = bs->opaque;
1263 int fd;
1265 if (s->fd >= 0) {
1266 close(s->fd);
1267 s->fd = -1;
1269 fd = open(bs->filename, s->open_flags | O_NONBLOCK);
1270 if (fd >= 0) {
1271 if (ioctl(fd, FDEJECT, 0) < 0)
1272 perror("FDEJECT");
1273 close(fd);
1277 static BlockDriver bdrv_host_floppy = {
1278 .format_name = "host_floppy",
1279 .protocol_name = "host_floppy",
1280 .instance_size = sizeof(BDRVRawState),
1281 .bdrv_probe_device = floppy_probe_device,
1282 .bdrv_file_open = floppy_open,
1283 .bdrv_close = raw_close,
1284 .bdrv_create = hdev_create,
1285 .create_options = raw_create_options,
1286 .bdrv_has_zero_init = hdev_has_zero_init,
1287 .bdrv_flush = raw_flush,
1289 .bdrv_aio_readv = raw_aio_readv,
1290 .bdrv_aio_writev = raw_aio_writev,
1291 .bdrv_aio_flush = raw_aio_flush,
1293 .bdrv_read = raw_read,
1294 .bdrv_write = raw_write,
1295 .bdrv_getlength = raw_getlength,
1296 .bdrv_get_allocated_file_size
1297 = raw_get_allocated_file_size,
1299 /* removable device support */
1300 .bdrv_is_inserted = floppy_is_inserted,
1301 .bdrv_media_changed = floppy_media_changed,
1302 .bdrv_eject = floppy_eject,
1305 static int cdrom_open(BlockDriverState *bs, const char *filename, int flags)
1307 BDRVRawState *s = bs->opaque;
1309 s->type = FTYPE_CD;
1311 /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
1312 return raw_open_common(bs, filename, flags, O_NONBLOCK);
1315 static int cdrom_probe_device(const char *filename)
1317 int fd, ret;
1318 int prio = 0;
1319 struct stat st;
1321 fd = open(filename, O_RDONLY | O_NONBLOCK);
1322 if (fd < 0) {
1323 goto out;
1325 ret = fstat(fd, &st);
1326 if (ret == -1 || !S_ISBLK(st.st_mode)) {
1327 goto outc;
1330 /* Attempt to detect via a CDROM specific ioctl */
1331 ret = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
1332 if (ret >= 0)
1333 prio = 100;
1335 outc:
1336 close(fd);
1337 out:
1338 return prio;
1341 static int cdrom_is_inserted(BlockDriverState *bs)
1343 BDRVRawState *s = bs->opaque;
1344 int ret;
1346 ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
1347 if (ret == CDS_DISC_OK)
1348 return 1;
1349 return 0;
1352 static void cdrom_eject(BlockDriverState *bs, int eject_flag)
1354 BDRVRawState *s = bs->opaque;
1356 if (eject_flag) {
1357 if (ioctl(s->fd, CDROMEJECT, NULL) < 0)
1358 perror("CDROMEJECT");
1359 } else {
1360 if (ioctl(s->fd, CDROMCLOSETRAY, NULL) < 0)
1361 perror("CDROMEJECT");
1365 static void cdrom_set_locked(BlockDriverState *bs, int locked)
1367 BDRVRawState *s = bs->opaque;
1369 if (ioctl(s->fd, CDROM_LOCKDOOR, locked) < 0) {
1371 * Note: an error can happen if the distribution automatically
1372 * mounts the CD-ROM
1374 /* perror("CDROM_LOCKDOOR"); */
1378 static BlockDriver bdrv_host_cdrom = {
1379 .format_name = "host_cdrom",
1380 .protocol_name = "host_cdrom",
1381 .instance_size = sizeof(BDRVRawState),
1382 .bdrv_probe_device = cdrom_probe_device,
1383 .bdrv_file_open = cdrom_open,
1384 .bdrv_close = raw_close,
1385 .bdrv_create = hdev_create,
1386 .create_options = raw_create_options,
1387 .bdrv_has_zero_init = hdev_has_zero_init,
1388 .bdrv_flush = raw_flush,
1390 .bdrv_aio_readv = raw_aio_readv,
1391 .bdrv_aio_writev = raw_aio_writev,
1392 .bdrv_aio_flush = raw_aio_flush,
1394 .bdrv_read = raw_read,
1395 .bdrv_write = raw_write,
1396 .bdrv_getlength = raw_getlength,
1397 .bdrv_get_allocated_file_size
1398 = raw_get_allocated_file_size,
1400 /* removable device support */
1401 .bdrv_is_inserted = cdrom_is_inserted,
1402 .bdrv_eject = cdrom_eject,
1403 .bdrv_set_locked = cdrom_set_locked,
1405 /* generic scsi device */
1406 .bdrv_ioctl = hdev_ioctl,
1407 .bdrv_aio_ioctl = hdev_aio_ioctl,
1409 #endif /* __linux__ */
1411 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
1412 static int cdrom_open(BlockDriverState *bs, const char *filename, int flags)
1414 BDRVRawState *s = bs->opaque;
1415 int ret;
1417 s->type = FTYPE_CD;
1419 ret = raw_open_common(bs, filename, flags, 0);
1420 if (ret)
1421 return ret;
1423 /* make sure the door isnt locked at this time */
1424 ioctl(s->fd, CDIOCALLOW);
1425 return 0;
1428 static int cdrom_probe_device(const char *filename)
1430 if (strstart(filename, "/dev/cd", NULL) ||
1431 strstart(filename, "/dev/acd", NULL))
1432 return 100;
1433 return 0;
1436 static int cdrom_reopen(BlockDriverState *bs)
1438 BDRVRawState *s = bs->opaque;
1439 int fd;
1442 * Force reread of possibly changed/newly loaded disc,
1443 * FreeBSD seems to not notice sometimes...
1445 if (s->fd >= 0)
1446 close(s->fd);
1447 fd = open(bs->filename, s->open_flags, 0644);
1448 if (fd < 0) {
1449 s->fd = -1;
1450 return -EIO;
1452 s->fd = fd;
1454 /* make sure the door isnt locked at this time */
1455 ioctl(s->fd, CDIOCALLOW);
1456 return 0;
1459 static int cdrom_is_inserted(BlockDriverState *bs)
1461 return raw_getlength(bs) > 0;
1464 static void cdrom_eject(BlockDriverState *bs, int eject_flag)
1466 BDRVRawState *s = bs->opaque;
1468 if (s->fd < 0)
1469 return;
1471 (void) ioctl(s->fd, CDIOCALLOW);
1473 if (eject_flag) {
1474 if (ioctl(s->fd, CDIOCEJECT) < 0)
1475 perror("CDIOCEJECT");
1476 } else {
1477 if (ioctl(s->fd, CDIOCCLOSE) < 0)
1478 perror("CDIOCCLOSE");
1481 cdrom_reopen(bs);
1484 static void cdrom_set_locked(BlockDriverState *bs, int locked)
1486 BDRVRawState *s = bs->opaque;
1488 if (s->fd < 0)
1489 return;
1490 if (ioctl(s->fd, (locked ? CDIOCPREVENT : CDIOCALLOW)) < 0) {
1492 * Note: an error can happen if the distribution automatically
1493 * mounts the CD-ROM
1495 /* perror("CDROM_LOCKDOOR"); */
1499 static BlockDriver bdrv_host_cdrom = {
1500 .format_name = "host_cdrom",
1501 .protocol_name = "host_cdrom",
1502 .instance_size = sizeof(BDRVRawState),
1503 .bdrv_probe_device = cdrom_probe_device,
1504 .bdrv_file_open = cdrom_open,
1505 .bdrv_close = raw_close,
1506 .bdrv_create = hdev_create,
1507 .create_options = raw_create_options,
1508 .bdrv_has_zero_init = hdev_has_zero_init,
1509 .bdrv_flush = raw_flush,
1511 .bdrv_aio_readv = raw_aio_readv,
1512 .bdrv_aio_writev = raw_aio_writev,
1513 .bdrv_aio_flush = raw_aio_flush,
1515 .bdrv_read = raw_read,
1516 .bdrv_write = raw_write,
1517 .bdrv_getlength = raw_getlength,
1518 .bdrv_get_allocated_file_size
1519 = raw_get_allocated_file_size,
1521 /* removable device support */
1522 .bdrv_is_inserted = cdrom_is_inserted,
1523 .bdrv_eject = cdrom_eject,
1524 .bdrv_set_locked = cdrom_set_locked,
1526 #endif /* __FreeBSD__ */
1528 static void bdrv_file_init(void)
1531 * Register all the drivers. Note that order is important, the driver
1532 * registered last will get probed first.
1534 bdrv_register(&bdrv_file);
1535 bdrv_register(&bdrv_host_device);
1536 #ifdef __linux__
1537 bdrv_register(&bdrv_host_floppy);
1538 bdrv_register(&bdrv_host_cdrom);
1539 #endif
1540 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1541 bdrv_register(&bdrv_host_cdrom);
1542 #endif
1545 block_init(bdrv_file_init);