]> git.proxmox.com Git - mirror_qemu.git/blob - block/raw-posix.c
Merge remote-tracking branch 'origin/master' into staging
[mirror_qemu.git] / block / raw-posix.c
1 /*
2 * Block driver for RAW files (posix)
3 *
4 * Copyright (c) 2006 Fabrice Bellard
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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.
23 */
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"
31
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
43
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
60
61 #ifdef __OpenBSD__
62 #include <sys/ioctl.h>
63 #include <sys/disklabel.h>
64 #include <sys/dkio.h>
65 #endif
66
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
73
74 #ifdef __DragonFly__
75 #include <sys/ioctl.h>
76 #include <sys/diskslice.h>
77 #endif
78
79 #ifdef CONFIG_XFS
80 #include <xfs/xfs.h>
81 #endif
82
83 //#define DEBUG_FLOPPY
84
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
92
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
101
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
106
107 #define FTYPE_FILE 0
108 #define FTYPE_CD 1
109 #define FTYPE_FD 2
110
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)
114
115 #define MAX_BLOCKSIZE 4096
116
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;
138
139 static int fd_open(BlockDriverState *bs);
140 static int64_t raw_getlength(BlockDriverState *bs);
141
142 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
143 static int cdrom_reopen(BlockDriverState *bs);
144 #endif
145
146 #if defined(__NetBSD__)
147 static int raw_normalize_devicepath(const char **filename)
148 {
149 static char namebuf[PATH_MAX];
150 const char *dp, *fname;
151 struct stat sb;
152
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;
159 }
160
161 if (!S_ISBLK(sb.st_mode)) {
162 return 0;
163 }
164
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);
170 }
171 fprintf(stderr, "%s is a block device", fname);
172 *filename = namebuf;
173 fprintf(stderr, ", using %s\n", *filename);
174
175 return 0;
176 }
177 #else
178 static int raw_normalize_devicepath(const char **filename)
179 {
180 return 0;
181 }
182 #endif
183
184 static int raw_open_common(BlockDriverState *bs, const char *filename,
185 int bdrv_flags, int open_flags)
186 {
187 BDRVRawState *s = bs->opaque;
188 int fd, ret;
189
190 ret = raw_normalize_devicepath(&filename);
191 if (ret != 0) {
192 return ret;
193 }
194
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;
201 }
202
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;
209
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;
217 }
218 s->fd = fd;
219 s->aligned_buf = NULL;
220
221 if ((bdrv_flags & BDRV_O_NOCACHE)) {
222 /*
223 * Allocate a buffer for read/modify/write cycles. Chose the size
224 * pessimistically as we don't know the block size yet.
225 */
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;
230 }
231 }
232
233 /* We're falling back to POSIX AIO in some cases so init always */
234 if (paio_init() < 0) {
235 goto out_free_buf;
236 }
237
238 #ifdef CONFIG_LINUX_AIO
239 /*
240 * Currently Linux do AIO only for files opened with O_DIRECT
241 * specified so check NOCACHE flag too
242 */
243 if ((bdrv_flags & (BDRV_O_NOCACHE|BDRV_O_NATIVE_AIO)) ==
244 (BDRV_O_NOCACHE|BDRV_O_NATIVE_AIO)) {
245
246 s->aio_ctx = laio_init();
247 if (!s->aio_ctx) {
248 goto out_free_buf;
249 }
250 s->use_aio = 1;
251 } else
252 #endif
253 {
254 #ifdef CONFIG_LINUX_AIO
255 s->use_aio = 0;
256 #endif
257 }
258
259 #ifdef CONFIG_XFS
260 if (platform_test_xfs_fd(s->fd)) {
261 s->is_xfs = 1;
262 }
263 #endif
264
265 return 0;
266
267 out_free_buf:
268 qemu_vfree(s->aligned_buf);
269 out_close:
270 close(fd);
271 return -errno;
272 }
273
274 static int raw_open(BlockDriverState *bs, const char *filename, int flags)
275 {
276 BDRVRawState *s = bs->opaque;
277
278 s->type = FTYPE_FILE;
279 return raw_open_common(bs, filename, flags, 0);
280 }
281
282 /* XXX: use host sector size if necessary with:
283 #ifdef DIOCGSECTORSIZE
284 {
285 unsigned int sectorsize = 512;
286 if (!ioctl(fd, DIOCGSECTORSIZE, &sectorsize) &&
287 sectorsize > bufsize)
288 bufsize = sectorsize;
289 }
290 #endif
291 #ifdef CONFIG_COCOA
292 uint32_t blockSize = 512;
293 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
294 bufsize = blockSize;
295 }
296 #endif
297 */
298
299 /*
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.
302 *
303 * This function may be called without alignment if the caller ensures
304 * that O_DIRECT is not in effect.
305 */
306 static int raw_pread_aligned(BlockDriverState *bs, int64_t offset,
307 uint8_t *buf, int count)
308 {
309 BDRVRawState *s = bs->opaque;
310 int ret;
311
312 ret = fd_open(bs);
313 if (ret < 0)
314 return ret;
315
316 ret = pread(s->fd, buf, count, offset);
317 if (ret == count)
318 return ret;
319
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;
326 }
327 }
328
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));
333
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;
342
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));
347 }
348
349 return (ret < 0) ? -errno : ret;
350 }
351
352 /*
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.
356 *
357 * This function may be called without alignment if the caller ensures
358 * that O_DIRECT is not in effect.
359 */
360 static int raw_pwrite_aligned(BlockDriverState *bs, int64_t offset,
361 const uint8_t *buf, int count)
362 {
363 BDRVRawState *s = bs->opaque;
364 int ret;
365
366 ret = fd_open(bs);
367 if (ret < 0)
368 return -errno;
369
370 ret = pwrite(s->fd, buf, count, offset);
371 if (ret == count)
372 return ret;
373
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));
378
379 return (ret < 0) ? -errno : ret;
380 }
381
382
383 /*
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.
387 */
388 static int raw_pread(BlockDriverState *bs, int64_t offset,
389 uint8_t *buf, int count)
390 {
391 BDRVRawState *s = bs->opaque;
392 unsigned sector_mask = bs->buffer_alignment - 1;
393 int size, ret, shift, sum;
394
395 sum = 0;
396
397 if (s->aligned_buf != NULL) {
398
399 if (offset & sector_mask) {
400 /* align offset on a sector size bytes boundary */
401
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;
409
410 size = bs->buffer_alignment - shift;
411 if (size > count)
412 size = count;
413 memcpy(buf, s->aligned_buf + shift, size);
414
415 buf += size;
416 offset += size;
417 count -= size;
418 sum += size;
419
420 if (count == 0)
421 return sum;
422 }
423 if (count & sector_mask || (uintptr_t) buf & sector_mask) {
424
425 /* read on aligned buffer */
426
427 while (count) {
428
429 size = (count + sector_mask) & ~sector_mask;
430 if (size > s->aligned_buf_size)
431 size = s->aligned_buf_size;
432
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();
439 }
440
441 size = ret;
442 if (size > count)
443 size = count;
444
445 memcpy(buf, s->aligned_buf, size);
446
447 buf += size;
448 offset += size;
449 count -= size;
450 sum += size;
451 }
452
453 return sum;
454 }
455 }
456
457 return raw_pread_aligned(bs, offset, buf, count) + sum;
458 }
459
460 static int raw_read(BlockDriverState *bs, int64_t sector_num,
461 uint8_t *buf, int nb_sectors)
462 {
463 int ret;
464
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;
470 }
471
472 /*
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.
476 */
477 static int raw_pwrite(BlockDriverState *bs, int64_t offset,
478 const uint8_t *buf, int count)
479 {
480 BDRVRawState *s = bs->opaque;
481 unsigned sector_mask = bs->buffer_alignment - 1;
482 int size, ret, shift, sum;
483
484 sum = 0;
485
486 if (s->aligned_buf != NULL) {
487
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;
495
496 size = bs->buffer_alignment - shift;
497 if (size > count)
498 size = count;
499 memcpy(s->aligned_buf + shift, buf, size);
500
501 ret = raw_pwrite_aligned(bs, offset - shift, s->aligned_buf,
502 bs->buffer_alignment);
503 if (ret < 0)
504 return ret;
505
506 buf += size;
507 offset += size;
508 count -= size;
509 sum += size;
510
511 if (count == 0)
512 return sum;
513 }
514 if (count & sector_mask || (uintptr_t) buf & sector_mask) {
515
516 while ((size = (count & ~sector_mask)) != 0) {
517
518 if (size > s->aligned_buf_size)
519 size = s->aligned_buf_size;
520
521 memcpy(s->aligned_buf, buf, size);
522
523 ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, size);
524 if (ret < 0)
525 return ret;
526
527 buf += ret;
528 offset += ret;
529 count -= ret;
530 sum += ret;
531 }
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);
539
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;
546
547 sum += ret;
548 }
549 return sum;
550 }
551 }
552 return raw_pwrite_aligned(bs, offset, buf, count) + sum;
553 }
554
555 static int raw_write(BlockDriverState *bs, int64_t sector_num,
556 const uint8_t *buf, int nb_sectors)
557 {
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;
564 }
565
566 /*
567 * Check if all memory in this vector is sector aligned.
568 */
569 static int qiov_is_aligned(BlockDriverState *bs, QEMUIOVector *qiov)
570 {
571 int i;
572
573 for (i = 0; i < qiov->niov; i++) {
574 if ((uintptr_t) qiov->iov[i].iov_base % bs->buffer_alignment) {
575 return 0;
576 }
577 }
578
579 return 1;
580 }
581
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)
585 {
586 BDRVRawState *s = bs->opaque;
587
588 if (fd_open(bs) < 0)
589 return NULL;
590
591 /*
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.
595 */
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
604 }
605 }
606
607 return paio_submit(bs, s->fd, sector_num, qiov, nb_sectors,
608 cb, opaque, type);
609 }
610
611 static BlockDriverAIOCB *raw_aio_readv(BlockDriverState *bs,
612 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
613 BlockDriverCompletionFunc *cb, void *opaque)
614 {
615 return raw_aio_submit(bs, sector_num, qiov, nb_sectors,
616 cb, opaque, QEMU_AIO_READ);
617 }
618
619 static BlockDriverAIOCB *raw_aio_writev(BlockDriverState *bs,
620 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
621 BlockDriverCompletionFunc *cb, void *opaque)
622 {
623 return raw_aio_submit(bs, sector_num, qiov, nb_sectors,
624 cb, opaque, QEMU_AIO_WRITE);
625 }
626
627 static BlockDriverAIOCB *raw_aio_flush(BlockDriverState *bs,
628 BlockDriverCompletionFunc *cb, void *opaque)
629 {
630 BDRVRawState *s = bs->opaque;
631
632 if (fd_open(bs) < 0)
633 return NULL;
634
635 return paio_submit(bs, s->fd, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH);
636 }
637
638 static void raw_close(BlockDriverState *bs)
639 {
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);
646 }
647 }
648
649 static int raw_truncate(BlockDriverState *bs, int64_t offset)
650 {
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;
657 }
658
659 #ifdef __OpenBSD__
660 static int64_t raw_getlength(BlockDriverState *bs)
661 {
662 BDRVRawState *s = bs->opaque;
663 int fd = s->fd;
664 struct stat st;
665
666 if (fstat(fd, &st))
667 return -1;
668 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
669 struct disklabel dl;
670
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;
677 }
678 #elif defined(__NetBSD__)
679 static int64_t raw_getlength(BlockDriverState *bs)
680 {
681 BDRVRawState *s = bs->opaque;
682 int fd = s->fd;
683 struct stat st;
684
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;
689
690 if (ioctl(fd, DIOCGWEDGEINFO, &dkw) != -1) {
691 return dkw.dkw_size * 512;
692 } else {
693 struct disklabel dl;
694
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;
699 }
700 } else
701 return st.st_size;
702 }
703 #elif defined(__sun__)
704 static int64_t raw_getlength(BlockDriverState *bs)
705 {
706 BDRVRawState *s = bs->opaque;
707 struct dk_minfo minfo;
708 int ret;
709
710 ret = fd_open(bs);
711 if (ret < 0) {
712 return ret;
713 }
714
715 /*
716 * Use the DKIOCGMEDIAINFO ioctl to read the size.
717 */
718 ret = ioctl(s->fd, DKIOCGMEDIAINFO, &minfo);
719 if (ret != -1) {
720 return minfo.dki_lbsize * minfo.dki_capacity;
721 }
722
723 /*
724 * There are reports that lseek on some devices fails, but
725 * irc discussion said that contingency on contingency was overkill.
726 */
727 return lseek(s->fd, 0, SEEK_END);
728 }
729 #elif defined(CONFIG_BSD)
730 static int64_t raw_getlength(BlockDriverState *bs)
731 {
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;
740
741 ret = fd_open(bs);
742 if (ret < 0)
743 return ret;
744
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)
752 {
753 struct partinfo pi;
754 if (ioctl(fd, DIOCGPART, &pi) == 0)
755 size = pi.media_size;
756 else
757 size = 0;
758 }
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;
776 }
777 }
778 #endif
779 } else {
780 size = lseek(fd, 0, SEEK_END);
781 }
782 return size;
783 }
784 #else
785 static int64_t raw_getlength(BlockDriverState *bs)
786 {
787 BDRVRawState *s = bs->opaque;
788 int ret;
789
790 ret = fd_open(bs);
791 if (ret < 0) {
792 return ret;
793 }
794
795 return lseek(s->fd, 0, SEEK_END);
796 }
797 #endif
798
799 static int64_t raw_get_allocated_file_size(BlockDriverState *bs)
800 {
801 struct stat st;
802 BDRVRawState *s = bs->opaque;
803
804 if (fstat(s->fd, &st) < 0) {
805 return -errno;
806 }
807 return (int64_t)st.st_blocks * 512;
808 }
809
810 static int raw_create(const char *filename, QEMUOptionParameter *options)
811 {
812 int fd;
813 int result = 0;
814 int64_t total_size = 0;
815
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;
820 }
821 options++;
822 }
823
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;
831 }
832 if (close(fd) != 0) {
833 result = -errno;
834 }
835 }
836 return result;
837 }
838
839 static int raw_flush(BlockDriverState *bs)
840 {
841 BDRVRawState *s = bs->opaque;
842 return qemu_fdatasync(s->fd);
843 }
844
845 #ifdef CONFIG_XFS
846 static int xfs_discard(BDRVRawState *s, int64_t sector_num, int nb_sectors)
847 {
848 struct xfs_flock64 fl;
849
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;
854
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;
858 }
859
860 return 0;
861 }
862 #endif
863
864 static int raw_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors)
865 {
866 #ifdef CONFIG_XFS
867 BDRVRawState *s = bs->opaque;
868
869 if (s->is_xfs) {
870 return xfs_discard(s, sector_num, nb_sectors);
871 }
872 #endif
873
874 return 0;
875 }
876
877 static QEMUOptionParameter raw_create_options[] = {
878 {
879 .name = BLOCK_OPT_SIZE,
880 .type = OPT_SIZE,
881 .help = "Virtual disk size"
882 },
883 { NULL }
884 };
885
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,
898
899 .bdrv_aio_readv = raw_aio_readv,
900 .bdrv_aio_writev = raw_aio_writev,
901 .bdrv_aio_flush = raw_aio_flush,
902
903 .bdrv_truncate = raw_truncate,
904 .bdrv_getlength = raw_getlength,
905 .bdrv_get_allocated_file_size
906 = raw_get_allocated_file_size,
907
908 .create_options = raw_create_options,
909 };
910
911 /***********************************************/
912 /* host device */
913
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 );
917
918 kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
919 {
920 kern_return_t kernResult;
921 mach_port_t masterPort;
922 CFMutableDictionaryRef classesToMatch;
923
924 kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
925 if ( KERN_SUCCESS != kernResult ) {
926 printf( "IOMasterPort returned %d\n", kernResult );
927 }
928
929 classesToMatch = IOServiceMatching( kIOCDMediaClass );
930 if ( classesToMatch == NULL ) {
931 printf( "IOServiceMatching returned a NULL dictionary.\n" );
932 } else {
933 CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
934 }
935 kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
936 if ( KERN_SUCCESS != kernResult )
937 {
938 printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
939 }
940
941 return kernResult;
942 }
943
944 kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
945 {
946 io_object_t nextMedia;
947 kern_return_t kernResult = KERN_FAILURE;
948 *bsdPath = '\0';
949 nextMedia = IOIteratorNext( mediaIterator );
950 if ( nextMedia )
951 {
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;
961 }
962 CFRelease( bsdPathAsCFString );
963 }
964 IOObjectRelease( nextMedia );
965 }
966
967 return kernResult;
968 }
969
970 #endif
971
972 static int hdev_probe_device(const char *filename)
973 {
974 struct stat st;
975
976 /* allow a dedicated CD-ROM driver to match with a higher priority */
977 if (strstart(filename, "/dev/cdrom", NULL))
978 return 50;
979
980 if (stat(filename, &st) >= 0 &&
981 (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
982 return 100;
983 }
984
985 return 0;
986 }
987
988 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
989 {
990 BDRVRawState *s = bs->opaque;
991
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;
998
999 kernResult = FindEjectableCDMedia( &mediaIterator );
1000 kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
1001
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);
1010 }
1011 filename = bsdPath;
1012 }
1013
1014 if ( mediaIterator )
1015 IOObjectRelease( mediaIterator );
1016 }
1017 #endif
1018
1019 s->type = FTYPE_FILE;
1020 #if defined(__linux__)
1021 {
1022 char resolved_path[ MAXPATHLEN ], *temp;
1023
1024 temp = realpath(filename, resolved_path);
1025 if (temp && strstart(temp, "/dev/sg", NULL)) {
1026 bs->sg = 1;
1027 }
1028 }
1029 #endif
1030
1031 return raw_open_common(bs, filename, flags, 0);
1032 }
1033
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)
1039 {
1040 BDRVRawState *s = bs->opaque;
1041 int last_media_present;
1042
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
1053 }
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;
1061 }
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;
1072 }
1073 #ifdef DEBUG_FLOPPY
1074 printf("Floppy opened\n");
1075 #endif
1076 }
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;
1082 }
1083
1084 static int hdev_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1085 {
1086 BDRVRawState *s = bs->opaque;
1087
1088 return ioctl(s->fd, req, buf);
1089 }
1090
1091 static BlockDriverAIOCB *hdev_aio_ioctl(BlockDriverState *bs,
1092 unsigned long int req, void *buf,
1093 BlockDriverCompletionFunc *cb, void *opaque)
1094 {
1095 BDRVRawState *s = bs->opaque;
1096
1097 if (fd_open(bs) < 0)
1098 return NULL;
1099 return paio_ioctl(bs, s->fd, req, buf, cb, opaque);
1100 }
1101
1102 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1103 static int fd_open(BlockDriverState *bs)
1104 {
1105 BDRVRawState *s = bs->opaque;
1106
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;
1111 }
1112 #else /* !linux && !FreeBSD */
1113
1114 static int fd_open(BlockDriverState *bs)
1115 {
1116 return 0;
1117 }
1118
1119 #endif /* !linux && !FreeBSD */
1120
1121 static int hdev_create(const char *filename, QEMUOptionParameter *options)
1122 {
1123 int fd;
1124 int ret = 0;
1125 struct stat stat_buf;
1126 int64_t total_size = 0;
1127
1128 /* Read out options */
1129 while (options && options->name) {
1130 if (!strcmp(options->name, "size")) {
1131 total_size = options->value.n / BDRV_SECTOR_SIZE;
1132 }
1133 options++;
1134 }
1135
1136 fd = open(filename, O_WRONLY | O_BINARY);
1137 if (fd < 0)
1138 return -errno;
1139
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;
1146
1147 close(fd);
1148 return ret;
1149 }
1150
1151 static int hdev_has_zero_init(BlockDriverState *bs)
1152 {
1153 return 0;
1154 }
1155
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,
1167
1168 .bdrv_aio_readv = raw_aio_readv,
1169 .bdrv_aio_writev = raw_aio_writev,
1170 .bdrv_aio_flush = raw_aio_flush,
1171
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,
1177
1178 /* generic scsi device */
1179 #ifdef __linux__
1180 .bdrv_ioctl = hdev_ioctl,
1181 .bdrv_aio_ioctl = hdev_aio_ioctl,
1182 #endif
1183 };
1184
1185 #ifdef __linux__
1186 static int floppy_open(BlockDriverState *bs, const char *filename, int flags)
1187 {
1188 BDRVRawState *s = bs->opaque;
1189 int ret;
1190
1191 s->type = FTYPE_FD;
1192
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;
1197
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;
1202
1203 return 0;
1204 }
1205
1206 static int floppy_probe_device(const char *filename)
1207 {
1208 int fd, ret;
1209 int prio = 0;
1210 struct floppy_struct fdparam;
1211 struct stat st;
1212
1213 if (strstart(filename, "/dev/fd", NULL))
1214 prio = 50;
1215
1216 fd = open(filename, O_RDONLY | O_NONBLOCK);
1217 if (fd < 0) {
1218 goto out;
1219 }
1220 ret = fstat(fd, &st);
1221 if (ret == -1 || !S_ISBLK(st.st_mode)) {
1222 goto outc;
1223 }
1224
1225 /* Attempt to detect via a floppy specific ioctl */
1226 ret = ioctl(fd, FDGETPRM, &fdparam);
1227 if (ret >= 0)
1228 prio = 100;
1229
1230 outc:
1231 close(fd);
1232 out:
1233 return prio;
1234 }
1235
1236
1237 static int floppy_is_inserted(BlockDriverState *bs)
1238 {
1239 return fd_open(bs) >= 0;
1240 }
1241
1242 static int floppy_media_changed(BlockDriverState *bs)
1243 {
1244 BDRVRawState *s = bs->opaque;
1245 int ret;
1246
1247 /*
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.
1250 */
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;
1258 }
1259
1260 static void floppy_eject(BlockDriverState *bs, int eject_flag)
1261 {
1262 BDRVRawState *s = bs->opaque;
1263 int fd;
1264
1265 if (s->fd >= 0) {
1266 close(s->fd);
1267 s->fd = -1;
1268 }
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);
1274 }
1275 }
1276
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,
1288
1289 .bdrv_aio_readv = raw_aio_readv,
1290 .bdrv_aio_writev = raw_aio_writev,
1291 .bdrv_aio_flush = raw_aio_flush,
1292
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,
1298
1299 /* removable device support */
1300 .bdrv_is_inserted = floppy_is_inserted,
1301 .bdrv_media_changed = floppy_media_changed,
1302 .bdrv_eject = floppy_eject,
1303 };
1304
1305 static int cdrom_open(BlockDriverState *bs, const char *filename, int flags)
1306 {
1307 BDRVRawState *s = bs->opaque;
1308
1309 s->type = FTYPE_CD;
1310
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);
1313 }
1314
1315 static int cdrom_probe_device(const char *filename)
1316 {
1317 int fd, ret;
1318 int prio = 0;
1319 struct stat st;
1320
1321 fd = open(filename, O_RDONLY | O_NONBLOCK);
1322 if (fd < 0) {
1323 goto out;
1324 }
1325 ret = fstat(fd, &st);
1326 if (ret == -1 || !S_ISBLK(st.st_mode)) {
1327 goto outc;
1328 }
1329
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;
1334
1335 outc:
1336 close(fd);
1337 out:
1338 return prio;
1339 }
1340
1341 static int cdrom_is_inserted(BlockDriverState *bs)
1342 {
1343 BDRVRawState *s = bs->opaque;
1344 int ret;
1345
1346 ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
1347 if (ret == CDS_DISC_OK)
1348 return 1;
1349 return 0;
1350 }
1351
1352 static void cdrom_eject(BlockDriverState *bs, int eject_flag)
1353 {
1354 BDRVRawState *s = bs->opaque;
1355
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");
1362 }
1363 }
1364
1365 static void cdrom_set_locked(BlockDriverState *bs, int locked)
1366 {
1367 BDRVRawState *s = bs->opaque;
1368
1369 if (ioctl(s->fd, CDROM_LOCKDOOR, locked) < 0) {
1370 /*
1371 * Note: an error can happen if the distribution automatically
1372 * mounts the CD-ROM
1373 */
1374 /* perror("CDROM_LOCKDOOR"); */
1375 }
1376 }
1377
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,
1389
1390 .bdrv_aio_readv = raw_aio_readv,
1391 .bdrv_aio_writev = raw_aio_writev,
1392 .bdrv_aio_flush = raw_aio_flush,
1393
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,
1399
1400 /* removable device support */
1401 .bdrv_is_inserted = cdrom_is_inserted,
1402 .bdrv_eject = cdrom_eject,
1403 .bdrv_set_locked = cdrom_set_locked,
1404
1405 /* generic scsi device */
1406 .bdrv_ioctl = hdev_ioctl,
1407 .bdrv_aio_ioctl = hdev_aio_ioctl,
1408 };
1409 #endif /* __linux__ */
1410
1411 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
1412 static int cdrom_open(BlockDriverState *bs, const char *filename, int flags)
1413 {
1414 BDRVRawState *s = bs->opaque;
1415 int ret;
1416
1417 s->type = FTYPE_CD;
1418
1419 ret = raw_open_common(bs, filename, flags, 0);
1420 if (ret)
1421 return ret;
1422
1423 /* make sure the door isnt locked at this time */
1424 ioctl(s->fd, CDIOCALLOW);
1425 return 0;
1426 }
1427
1428 static int cdrom_probe_device(const char *filename)
1429 {
1430 if (strstart(filename, "/dev/cd", NULL) ||
1431 strstart(filename, "/dev/acd", NULL))
1432 return 100;
1433 return 0;
1434 }
1435
1436 static int cdrom_reopen(BlockDriverState *bs)
1437 {
1438 BDRVRawState *s = bs->opaque;
1439 int fd;
1440
1441 /*
1442 * Force reread of possibly changed/newly loaded disc,
1443 * FreeBSD seems to not notice sometimes...
1444 */
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;
1451 }
1452 s->fd = fd;
1453
1454 /* make sure the door isnt locked at this time */
1455 ioctl(s->fd, CDIOCALLOW);
1456 return 0;
1457 }
1458
1459 static int cdrom_is_inserted(BlockDriverState *bs)
1460 {
1461 return raw_getlength(bs) > 0;
1462 }
1463
1464 static void cdrom_eject(BlockDriverState *bs, int eject_flag)
1465 {
1466 BDRVRawState *s = bs->opaque;
1467
1468 if (s->fd < 0)
1469 return;
1470
1471 (void) ioctl(s->fd, CDIOCALLOW);
1472
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");
1479 }
1480
1481 cdrom_reopen(bs);
1482 }
1483
1484 static void cdrom_set_locked(BlockDriverState *bs, int locked)
1485 {
1486 BDRVRawState *s = bs->opaque;
1487
1488 if (s->fd < 0)
1489 return;
1490 if (ioctl(s->fd, (locked ? CDIOCPREVENT : CDIOCALLOW)) < 0) {
1491 /*
1492 * Note: an error can happen if the distribution automatically
1493 * mounts the CD-ROM
1494 */
1495 /* perror("CDROM_LOCKDOOR"); */
1496 }
1497 }
1498
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,
1510
1511 .bdrv_aio_readv = raw_aio_readv,
1512 .bdrv_aio_writev = raw_aio_writev,
1513 .bdrv_aio_flush = raw_aio_flush,
1514
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,
1520
1521 /* removable device support */
1522 .bdrv_is_inserted = cdrom_is_inserted,
1523 .bdrv_eject = cdrom_eject,
1524 .bdrv_set_locked = cdrom_set_locked,
1525 };
1526 #endif /* __FreeBSD__ */
1527
1528 static void bdrv_file_init(void)
1529 {
1530 /*
1531 * Register all the drivers. Note that order is important, the driver
1532 * registered last will get probed first.
1533 */
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
1543 }
1544
1545 block_init(bdrv_file_init);