1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
41#include <CoreFoundation/CoreFoundation.h>
42#endif
43
44#ifdef __sun__
45#define _POSIX_PTHREAD_SEMANTICS 1
46#include <signal.h>
47#include <sys/dkio.h>
48#endif
49#ifdef __linux__
50#include <sys/ioctl.h>
51#include <sys/param.h>
52#include <linux/cdrom.h>
53#include <linux/fd.h>
54#endif
55#if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
56#include <signal.h>
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 __DragonFly__
68#include <sys/ioctl.h>
69#include <sys/diskslice.h>
70#endif
71
72#ifdef CONFIG_XFS
73#include <xfs/xfs.h>
74#endif
75
76
77
78
79#if defined(DEBUG_BLOCK)
80#define DEBUG_BLOCK_PRINT(formatCstr, ...) do { if (qemu_log_enabled()) \
81 { qemu_log(formatCstr, ## __VA_ARGS__); qemu_log_flush(); } } while (0)
82#else
83#define DEBUG_BLOCK_PRINT(formatCstr, ...)
84#endif
85
86
87#ifndef O_DSYNC
88#ifdef O_SYNC
89#define O_DSYNC O_SYNC
90#elif defined(O_FSYNC)
91#define O_DSYNC O_FSYNC
92#endif
93#endif
94
95
96#ifndef O_DIRECT
97#define O_DIRECT O_DSYNC
98#endif
99
100#define FTYPE_FILE 0
101#define FTYPE_CD 1
102#define FTYPE_FD 2
103
104
105
106#define FD_OPEN_TIMEOUT (1000000000)
107
108#define MAX_BLOCKSIZE 4096
109
110typedef struct BDRVRawState {
111 int fd;
112 int type;
113 int open_flags;
114#if defined(__linux__)
115
116 int64_t fd_open_time;
117 int64_t fd_error_time;
118 int fd_got_error;
119 int fd_media_changed;
120#endif
121#ifdef CONFIG_LINUX_AIO
122 int use_aio;
123 void *aio_ctx;
124#endif
125 uint8_t *aligned_buf;
126 unsigned aligned_buf_size;
127#ifdef CONFIG_XFS
128 bool is_xfs : 1;
129#endif
130} BDRVRawState;
131
132static int fd_open(BlockDriverState *bs);
133static int64_t raw_getlength(BlockDriverState *bs);
134
135#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
136static int cdrom_reopen(BlockDriverState *bs);
137#endif
138
139static int raw_open_common(BlockDriverState *bs, const char *filename,
140 int bdrv_flags, int open_flags)
141{
142 BDRVRawState *s = bs->opaque;
143 int fd, ret;
144
145 s->open_flags = open_flags | O_BINARY;
146 s->open_flags &= ~O_ACCMODE;
147 if (bdrv_flags & BDRV_O_RDWR) {
148 s->open_flags |= O_RDWR;
149 } else {
150 s->open_flags |= O_RDONLY;
151 }
152
153
154
155 if ((bdrv_flags & BDRV_O_NOCACHE))
156 s->open_flags |= O_DIRECT;
157 else if (!(bdrv_flags & BDRV_O_CACHE_WB))
158 s->open_flags |= O_DSYNC;
159
160 s->fd = -1;
161 fd = qemu_open(filename, s->open_flags, 0644);
162 if (fd < 0) {
163 ret = -errno;
164 if (ret == -EROFS)
165 ret = -EACCES;
166 return ret;
167 }
168 s->fd = fd;
169 s->aligned_buf = NULL;
170
171 if ((bdrv_flags & BDRV_O_NOCACHE)) {
172
173
174
175
176 s->aligned_buf_size = 32 * MAX_BLOCKSIZE;
177 s->aligned_buf = qemu_memalign(MAX_BLOCKSIZE, s->aligned_buf_size);
178 if (s->aligned_buf == NULL) {
179 goto out_close;
180 }
181 }
182
183#ifdef CONFIG_LINUX_AIO
184 if ((bdrv_flags & (BDRV_O_NOCACHE|BDRV_O_NATIVE_AIO)) ==
185 (BDRV_O_NOCACHE|BDRV_O_NATIVE_AIO)) {
186
187
188 paio_init();
189
190 s->aio_ctx = laio_init();
191 if (!s->aio_ctx) {
192 goto out_free_buf;
193 }
194 s->use_aio = 1;
195 } else
196#endif
197 {
198 if (paio_init() < 0) {
199 goto out_free_buf;
200 }
201#ifdef CONFIG_LINUX_AIO
202 s->use_aio = 0;
203#endif
204 }
205
206#ifdef CONFIG_XFS
207 if (platform_test_xfs_fd(s->fd)) {
208 s->is_xfs = 1;
209 }
210#endif
211
212 return 0;
213
214out_free_buf:
215 qemu_vfree(s->aligned_buf);
216out_close:
217 close(fd);
218 return -errno;
219}
220
221static int raw_open(BlockDriverState *bs, const char *filename, int flags)
222{
223 BDRVRawState *s = bs->opaque;
224
225 s->type = FTYPE_FILE;
226 return raw_open_common(bs, filename, flags, 0);
227}
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253static int raw_pread_aligned(BlockDriverState *bs, int64_t offset,
254 uint8_t *buf, int count)
255{
256 BDRVRawState *s = bs->opaque;
257 int ret;
258
259 ret = fd_open(bs);
260 if (ret < 0)
261 return ret;
262
263 ret = pread(s->fd, buf, count, offset);
264 if (ret == count)
265 return ret;
266
267
268 if ((ret == 0) && bs->growable) {
269 int64_t size = raw_getlength(bs);
270 if (offset >= size) {
271 memset(buf, 0, count);
272 return count;
273 }
274 }
275
276 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
277 "] read failed %d : %d = %s\n",
278 s->fd, bs->filename, offset, buf, count,
279 bs->total_sectors, ret, errno, strerror(errno));
280
281
282 if (s->type != FTYPE_FILE) {
283 ret = pread(s->fd, buf, count, offset);
284 if (ret == count)
285 return ret;
286 ret = pread(s->fd, buf, count, offset);
287 if (ret == count)
288 return ret;
289
290 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
291 "] retry read failed %d : %d = %s\n",
292 s->fd, bs->filename, offset, buf, count,
293 bs->total_sectors, ret, errno, strerror(errno));
294 }
295
296 return (ret < 0) ? -errno : ret;
297}
298
299
300
301
302
303
304
305
306
307static int raw_pwrite_aligned(BlockDriverState *bs, int64_t offset,
308 const uint8_t *buf, int count)
309{
310 BDRVRawState *s = bs->opaque;
311 int ret;
312
313 ret = fd_open(bs);
314 if (ret < 0)
315 return -errno;
316
317 ret = pwrite(s->fd, buf, count, offset);
318 if (ret == count)
319 return ret;
320
321 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
322 "] write failed %d : %d = %s\n",
323 s->fd, bs->filename, offset, buf, count,
324 bs->total_sectors, ret, errno, strerror(errno));
325
326 return (ret < 0) ? -errno : ret;
327}
328
329
330
331
332
333
334
335static int raw_pread(BlockDriverState *bs, int64_t offset,
336 uint8_t *buf, int count)
337{
338 BDRVRawState *s = bs->opaque;
339 unsigned sector_mask = bs->buffer_alignment - 1;
340 int size, ret, shift, sum;
341
342 sum = 0;
343
344 if (s->aligned_buf != NULL) {
345
346 if (offset & sector_mask) {
347
348
349 shift = offset & sector_mask;
350 size = (shift + count + sector_mask) & ~sector_mask;
351 if (size > s->aligned_buf_size)
352 size = s->aligned_buf_size;
353 ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, size);
354 if (ret < 0)
355 return ret;
356
357 size = bs->buffer_alignment - shift;
358 if (size > count)
359 size = count;
360 memcpy(buf, s->aligned_buf + shift, size);
361
362 buf += size;
363 offset += size;
364 count -= size;
365 sum += size;
366
367 if (count == 0)
368 return sum;
369 }
370 if (count & sector_mask || (uintptr_t) buf & sector_mask) {
371
372
373
374 while (count) {
375
376 size = (count + sector_mask) & ~sector_mask;
377 if (size > s->aligned_buf_size)
378 size = s->aligned_buf_size;
379
380 ret = raw_pread_aligned(bs, offset, s->aligned_buf, size);
381 if (ret < 0) {
382 return ret;
383 } else if (ret == 0) {
384 fprintf(stderr, "raw_pread: read beyond end of file\n");
385 abort();
386 }
387
388 size = ret;
389 if (size > count)
390 size = count;
391
392 memcpy(buf, s->aligned_buf, size);
393
394 buf += size;
395 offset += size;
396 count -= size;
397 sum += size;
398 }
399
400 return sum;
401 }
402 }
403
404 return raw_pread_aligned(bs, offset, buf, count) + sum;
405}
406
407static int raw_read(BlockDriverState *bs, int64_t sector_num,
408 uint8_t *buf, int nb_sectors)
409{
410 int ret;
411
412 ret = raw_pread(bs, sector_num * BDRV_SECTOR_SIZE, buf,
413 nb_sectors * BDRV_SECTOR_SIZE);
414 if (ret == (nb_sectors * BDRV_SECTOR_SIZE))
415 ret = 0;
416 return ret;
417}
418
419
420
421
422
423
424static int raw_pwrite(BlockDriverState *bs, int64_t offset,
425 const uint8_t *buf, int count)
426{
427 BDRVRawState *s = bs->opaque;
428 unsigned sector_mask = bs->buffer_alignment - 1;
429 int size, ret, shift, sum;
430
431 sum = 0;
432
433 if (s->aligned_buf != NULL) {
434
435 if (offset & sector_mask) {
436
437 shift = offset & sector_mask;
438 ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf,
439 bs->buffer_alignment);
440 if (ret < 0)
441 return ret;
442
443 size = bs->buffer_alignment - shift;
444 if (size > count)
445 size = count;
446 memcpy(s->aligned_buf + shift, buf, size);
447
448 ret = raw_pwrite_aligned(bs, offset - shift, s->aligned_buf,
449 bs->buffer_alignment);
450 if (ret < 0)
451 return ret;
452
453 buf += size;
454 offset += size;
455 count -= size;
456 sum += size;
457
458 if (count == 0)
459 return sum;
460 }
461 if (count & sector_mask || (uintptr_t) buf & sector_mask) {
462
463 while ((size = (count & ~sector_mask)) != 0) {
464
465 if (size > s->aligned_buf_size)
466 size = s->aligned_buf_size;
467
468 memcpy(s->aligned_buf, buf, size);
469
470 ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, size);
471 if (ret < 0)
472 return ret;
473
474 buf += ret;
475 offset += ret;
476 count -= ret;
477 sum += ret;
478 }
479
480 if (count) {
481 ret = raw_pread_aligned(bs, offset, s->aligned_buf,
482 bs->buffer_alignment);
483 if (ret < 0)
484 return ret;
485 memcpy(s->aligned_buf, buf, count);
486
487 ret = raw_pwrite_aligned(bs, offset, s->aligned_buf,
488 bs->buffer_alignment);
489 if (ret < 0)
490 return ret;
491 if (count < ret)
492 ret = count;
493
494 sum += ret;
495 }
496 return sum;
497 }
498 }
499 return raw_pwrite_aligned(bs, offset, buf, count) + sum;
500}
501
502static int raw_write(BlockDriverState *bs, int64_t sector_num,
503 const uint8_t *buf, int nb_sectors)
504{
505 int ret;
506 ret = raw_pwrite(bs, sector_num * BDRV_SECTOR_SIZE, buf,
507 nb_sectors * BDRV_SECTOR_SIZE);
508 if (ret == (nb_sectors * BDRV_SECTOR_SIZE))
509 ret = 0;
510 return ret;
511}
512
513
514
515
516static int qiov_is_aligned(BlockDriverState *bs, QEMUIOVector *qiov)
517{
518 int i;
519
520 for (i = 0; i < qiov->niov; i++) {
521 if ((uintptr_t) qiov->iov[i].iov_base % bs->buffer_alignment) {
522 return 0;
523 }
524 }
525
526 return 1;
527}
528
529static BlockDriverAIOCB *raw_aio_submit(BlockDriverState *bs,
530 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
531 BlockDriverCompletionFunc *cb, void *opaque, int type)
532{
533 BDRVRawState *s = bs->opaque;
534
535 if (fd_open(bs) < 0)
536 return NULL;
537
538
539
540
541
542
543 if (s->aligned_buf) {
544 if (!qiov_is_aligned(bs, qiov)) {
545 type |= QEMU_AIO_MISALIGNED;
546#ifdef CONFIG_LINUX_AIO
547 } else if (s->use_aio) {
548 return laio_submit(bs, s->aio_ctx, s->fd, sector_num, qiov,
549 nb_sectors, cb, opaque, type);
550#endif
551 }
552 }
553
554 return paio_submit(bs, s->fd, sector_num, qiov, nb_sectors,
555 cb, opaque, type);
556}
557
558static BlockDriverAIOCB *raw_aio_readv(BlockDriverState *bs,
559 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
560 BlockDriverCompletionFunc *cb, void *opaque)
561{
562 return raw_aio_submit(bs, sector_num, qiov, nb_sectors,
563 cb, opaque, QEMU_AIO_READ);
564}
565
566static BlockDriverAIOCB *raw_aio_writev(BlockDriverState *bs,
567 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
568 BlockDriverCompletionFunc *cb, void *opaque)
569{
570 return raw_aio_submit(bs, sector_num, qiov, nb_sectors,
571 cb, opaque, QEMU_AIO_WRITE);
572}
573
574static BlockDriverAIOCB *raw_aio_flush(BlockDriverState *bs,
575 BlockDriverCompletionFunc *cb, void *opaque)
576{
577 BDRVRawState *s = bs->opaque;
578
579 if (fd_open(bs) < 0)
580 return NULL;
581
582 return paio_submit(bs, s->fd, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH);
583}
584
585static void raw_close(BlockDriverState *bs)
586{
587 BDRVRawState *s = bs->opaque;
588 if (s->fd >= 0) {
589 close(s->fd);
590 s->fd = -1;
591 if (s->aligned_buf != NULL)
592 qemu_vfree(s->aligned_buf);
593 }
594}
595
596static int raw_truncate(BlockDriverState *bs, int64_t offset)
597{
598 BDRVRawState *s = bs->opaque;
599 if (s->type != FTYPE_FILE)
600 return -ENOTSUP;
601 if (ftruncate(s->fd, offset) < 0)
602 return -errno;
603 return 0;
604}
605
606#ifdef __OpenBSD__
607static int64_t raw_getlength(BlockDriverState *bs)
608{
609 BDRVRawState *s = bs->opaque;
610 int fd = s->fd;
611 struct stat st;
612
613 if (fstat(fd, &st))
614 return -1;
615 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
616 struct disklabel dl;
617
618 if (ioctl(fd, DIOCGDINFO, &dl))
619 return -1;
620 return (uint64_t)dl.d_secsize *
621 dl.d_partitions[DISKPART(st.st_rdev)].p_size;
622 } else
623 return st.st_size;
624}
625#elif defined(__sun__)
626static int64_t raw_getlength(BlockDriverState *bs)
627{
628 BDRVRawState *s = bs->opaque;
629 struct dk_minfo minfo;
630 int ret;
631
632 ret = fd_open(bs);
633 if (ret < 0) {
634 return ret;
635 }
636
637
638
639
640 ret = ioctl(s->fd, DKIOCGMEDIAINFO, &minfo);
641 if (ret != -1) {
642 return minfo.dki_lbsize * minfo.dki_capacity;
643 }
644
645
646
647
648
649 return lseek(s->fd, 0, SEEK_END);
650}
651#elif defined(CONFIG_BSD)
652static int64_t raw_getlength(BlockDriverState *bs)
653{
654 BDRVRawState *s = bs->opaque;
655 int fd = s->fd;
656 int64_t size;
657 struct stat sb;
658#if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
659 int reopened = 0;
660#endif
661 int ret;
662
663 ret = fd_open(bs);
664 if (ret < 0)
665 return ret;
666
667#if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
668again:
669#endif
670 if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
671#ifdef DIOCGMEDIASIZE
672 if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
673#elif defined(DIOCGPART)
674 {
675 struct partinfo pi;
676 if (ioctl(fd, DIOCGPART, &pi) == 0)
677 size = pi.media_size;
678 else
679 size = 0;
680 }
681 if (size == 0)
682#endif
683#ifdef CONFIG_COCOA
684 size = LONG_LONG_MAX;
685#else
686 size = lseek(fd, 0LL, SEEK_END);
687#endif
688#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
689 switch(s->type) {
690 case FTYPE_CD:
691
692 if (size == 2048LL * (unsigned)-1)
693 size = 0;
694
695 if (size <= 0 && !reopened && cdrom_reopen(bs) >= 0) {
696 reopened = 1;
697 goto again;
698 }
699 }
700#endif
701 } else {
702 size = lseek(fd, 0, SEEK_END);
703 }
704 return size;
705}
706#else
707static int64_t raw_getlength(BlockDriverState *bs)
708{
709 BDRVRawState *s = bs->opaque;
710 int ret;
711
712 ret = fd_open(bs);
713 if (ret < 0) {
714 return ret;
715 }
716
717 return lseek(s->fd, 0, SEEK_END);
718}
719#endif
720
721static int raw_create(const char *filename, QEMUOptionParameter *options)
722{
723 int fd;
724 int result = 0;
725 int64_t total_size = 0;
726
727
728 while (options && options->name) {
729 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
730 total_size = options->value.n / BDRV_SECTOR_SIZE;
731 }
732 options++;
733 }
734
735 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
736 0644);
737 if (fd < 0) {
738 result = -errno;
739 } else {
740 if (ftruncate(fd, total_size * BDRV_SECTOR_SIZE) != 0) {
741 result = -errno;
742 }
743 if (close(fd) != 0) {
744 result = -errno;
745 }
746 }
747 return result;
748}
749
750static int raw_flush(BlockDriverState *bs)
751{
752 BDRVRawState *s = bs->opaque;
753 return qemu_fdatasync(s->fd);
754}
755
756#ifdef CONFIG_XFS
757static int xfs_discard(BDRVRawState *s, int64_t sector_num, int nb_sectors)
758{
759 struct xfs_flock64 fl;
760
761 memset(&fl, 0, sizeof(fl));
762 fl.l_whence = SEEK_SET;
763 fl.l_start = sector_num << 9;
764 fl.l_len = (int64_t)nb_sectors << 9;
765
766 if (xfsctl(NULL, s->fd, XFS_IOC_UNRESVSP64, &fl) < 0) {
767 DEBUG_BLOCK_PRINT("cannot punch hole (%s)\n", strerror(errno));
768 return -errno;
769 }
770
771 return 0;
772}
773#endif
774
775static int raw_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors)
776{
777#ifdef CONFIG_XFS
778 BDRVRawState *s = bs->opaque;
779
780 if (s->is_xfs) {
781 return xfs_discard(s, sector_num, nb_sectors);
782 }
783#endif
784
785 return 0;
786}
787
788static QEMUOptionParameter raw_create_options[] = {
789 {
790 .name = BLOCK_OPT_SIZE,
791 .type = OPT_SIZE,
792 .help = "Virtual disk size"
793 },
794 { NULL }
795};
796
797static BlockDriver bdrv_file = {
798 .format_name = "file",
799 .protocol_name = "file",
800 .instance_size = sizeof(BDRVRawState),
801 .bdrv_probe = NULL,
802 .bdrv_file_open = raw_open,
803 .bdrv_read = raw_read,
804 .bdrv_write = raw_write,
805 .bdrv_close = raw_close,
806 .bdrv_create = raw_create,
807 .bdrv_flush = raw_flush,
808 .bdrv_discard = raw_discard,
809
810 .bdrv_aio_readv = raw_aio_readv,
811 .bdrv_aio_writev = raw_aio_writev,
812 .bdrv_aio_flush = raw_aio_flush,
813
814 .bdrv_truncate = raw_truncate,
815 .bdrv_getlength = raw_getlength,
816
817 .create_options = raw_create_options,
818};
819
820
821
822
823#ifdef CONFIG_COCOA
824static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
825static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
826
827kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
828{
829 kern_return_t kernResult;
830 mach_port_t masterPort;
831 CFMutableDictionaryRef classesToMatch;
832
833 kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
834 if ( KERN_SUCCESS != kernResult ) {
835 printf( "IOMasterPort returned %d\n", kernResult );
836 }
837
838 classesToMatch = IOServiceMatching( kIOCDMediaClass );
839 if ( classesToMatch == NULL ) {
840 printf( "IOServiceMatching returned a NULL dictionary.\n" );
841 } else {
842 CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
843 }
844 kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
845 if ( KERN_SUCCESS != kernResult )
846 {
847 printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
848 }
849
850 return kernResult;
851}
852
853kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
854{
855 io_object_t nextMedia;
856 kern_return_t kernResult = KERN_FAILURE;
857 *bsdPath = '\0';
858 nextMedia = IOIteratorNext( mediaIterator );
859 if ( nextMedia )
860 {
861 CFTypeRef bsdPathAsCFString;
862 bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
863 if ( bsdPathAsCFString ) {
864 size_t devPathLength;
865 strcpy( bsdPath, _PATH_DEV );
866 strcat( bsdPath, "r" );
867 devPathLength = strlen( bsdPath );
868 if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
869 kernResult = KERN_SUCCESS;
870 }
871 CFRelease( bsdPathAsCFString );
872 }
873 IOObjectRelease( nextMedia );
874 }
875
876 return kernResult;
877}
878
879#endif
880
881static int hdev_probe_device(const char *filename)
882{
883 struct stat st;
884
885
886 if (strstart(filename, "/dev/cdrom", NULL))
887 return 50;
888
889 if (stat(filename, &st) >= 0 &&
890 (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
891 return 100;
892 }
893
894 return 0;
895}
896
897static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
898{
899 BDRVRawState *s = bs->opaque;
900
901#ifdef CONFIG_COCOA
902 if (strstart(filename, "/dev/cdrom", NULL)) {
903 kern_return_t kernResult;
904 io_iterator_t mediaIterator;
905 char bsdPath[ MAXPATHLEN ];
906 int fd;
907
908 kernResult = FindEjectableCDMedia( &mediaIterator );
909 kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
910
911 if ( bsdPath[ 0 ] != '\0' ) {
912 strcat(bsdPath,"s0");
913
914 fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
915 if (fd < 0) {
916 bsdPath[strlen(bsdPath)-1] = '1';
917 } else {
918 close(fd);
919 }
920 filename = bsdPath;
921 }
922
923 if ( mediaIterator )
924 IOObjectRelease( mediaIterator );
925 }
926#endif
927
928 s->type = FTYPE_FILE;
929#if defined(__linux__)
930 {
931 char resolved_path[ MAXPATHLEN ], *temp;
932
933 temp = realpath(filename, resolved_path);
934 if (temp && strstart(temp, "/dev/sg", NULL)) {
935 bs->sg = 1;
936 }
937 }
938#endif
939
940 return raw_open_common(bs, filename, flags, 0);
941}
942
943#if defined(__linux__)
944
945
946
947static int fd_open(BlockDriverState *bs)
948{
949 BDRVRawState *s = bs->opaque;
950 int last_media_present;
951
952 if (s->type != FTYPE_FD)
953 return 0;
954 last_media_present = (s->fd >= 0);
955 if (s->fd >= 0 &&
956 (get_clock() - s->fd_open_time) >= FD_OPEN_TIMEOUT) {
957 close(s->fd);
958 s->fd = -1;
959#ifdef DEBUG_FLOPPY
960 printf("Floppy closed\n");
961#endif
962 }
963 if (s->fd < 0) {
964 if (s->fd_got_error &&
965 (get_clock() - s->fd_error_time) < FD_OPEN_TIMEOUT) {
966#ifdef DEBUG_FLOPPY
967 printf("No floppy (open delayed)\n");
968#endif
969 return -EIO;
970 }
971 s->fd = open(bs->filename, s->open_flags & ~O_NONBLOCK);
972 if (s->fd < 0) {
973 s->fd_error_time = get_clock();
974 s->fd_got_error = 1;
975 if (last_media_present)
976 s->fd_media_changed = 1;
977#ifdef DEBUG_FLOPPY
978 printf("No floppy\n");
979#endif
980 return -EIO;
981 }
982#ifdef DEBUG_FLOPPY
983 printf("Floppy opened\n");
984#endif
985 }
986 if (!last_media_present)
987 s->fd_media_changed = 1;
988 s->fd_open_time = get_clock();
989 s->fd_got_error = 0;
990 return 0;
991}
992
993static int hdev_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
994{
995 BDRVRawState *s = bs->opaque;
996
997 return ioctl(s->fd, req, buf);
998}
999
1000static BlockDriverAIOCB *hdev_aio_ioctl(BlockDriverState *bs,
1001 unsigned long int req, void *buf,
1002 BlockDriverCompletionFunc *cb, void *opaque)
1003{
1004 BDRVRawState *s = bs->opaque;
1005
1006 if (fd_open(bs) < 0)
1007 return NULL;
1008 return paio_ioctl(bs, s->fd, req, buf, cb, opaque);
1009}
1010
1011#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1012static int fd_open(BlockDriverState *bs)
1013{
1014 BDRVRawState *s = bs->opaque;
1015
1016
1017 if (s->fd >= 0)
1018 return 0;
1019 return -EIO;
1020}
1021#else
1022
1023static int fd_open(BlockDriverState *bs)
1024{
1025 return 0;
1026}
1027
1028#endif
1029
1030static int hdev_create(const char *filename, QEMUOptionParameter *options)
1031{
1032 int fd;
1033 int ret = 0;
1034 struct stat stat_buf;
1035 int64_t total_size = 0;
1036
1037
1038 while (options && options->name) {
1039 if (!strcmp(options->name, "size")) {
1040 total_size = options->value.n / BDRV_SECTOR_SIZE;
1041 }
1042 options++;
1043 }
1044
1045 fd = open(filename, O_WRONLY | O_BINARY);
1046 if (fd < 0)
1047 return -errno;
1048
1049 if (fstat(fd, &stat_buf) < 0)
1050 ret = -errno;
1051 else if (!S_ISBLK(stat_buf.st_mode) && !S_ISCHR(stat_buf.st_mode))
1052 ret = -ENODEV;
1053 else if (lseek(fd, 0, SEEK_END) < total_size * BDRV_SECTOR_SIZE)
1054 ret = -ENOSPC;
1055
1056 close(fd);
1057 return ret;
1058}
1059
1060static int hdev_has_zero_init(BlockDriverState *bs)
1061{
1062 return 0;
1063}
1064
1065static BlockDriver bdrv_host_device = {
1066 .format_name = "host_device",
1067 .protocol_name = "host_device",
1068 .instance_size = sizeof(BDRVRawState),
1069 .bdrv_probe_device = hdev_probe_device,
1070 .bdrv_file_open = hdev_open,
1071 .bdrv_close = raw_close,
1072 .bdrv_create = hdev_create,
1073 .create_options = raw_create_options,
1074 .bdrv_has_zero_init = hdev_has_zero_init,
1075 .bdrv_flush = raw_flush,
1076
1077 .bdrv_aio_readv = raw_aio_readv,
1078 .bdrv_aio_writev = raw_aio_writev,
1079 .bdrv_aio_flush = raw_aio_flush,
1080
1081 .bdrv_read = raw_read,
1082 .bdrv_write = raw_write,
1083 .bdrv_getlength = raw_getlength,
1084
1085
1086#ifdef __linux__
1087 .bdrv_ioctl = hdev_ioctl,
1088 .bdrv_aio_ioctl = hdev_aio_ioctl,
1089#endif
1090};
1091
1092#ifdef __linux__
1093static int floppy_open(BlockDriverState *bs, const char *filename, int flags)
1094{
1095 BDRVRawState *s = bs->opaque;
1096 int ret;
1097
1098 s->type = FTYPE_FD;
1099
1100
1101 ret = raw_open_common(bs, filename, flags, O_NONBLOCK);
1102 if (ret)
1103 return ret;
1104
1105
1106 close(s->fd);
1107 s->fd = -1;
1108 s->fd_media_changed = 1;
1109
1110 return 0;
1111}
1112
1113static int floppy_probe_device(const char *filename)
1114{
1115 int fd, ret;
1116 int prio = 0;
1117 struct floppy_struct fdparam;
1118
1119 if (strstart(filename, "/dev/fd", NULL))
1120 prio = 50;
1121
1122 fd = open(filename, O_RDONLY | O_NONBLOCK);
1123 if (fd < 0) {
1124 goto out;
1125 }
1126
1127
1128 ret = ioctl(fd, FDGETPRM, &fdparam);
1129 if (ret >= 0)
1130 prio = 100;
1131
1132 close(fd);
1133out:
1134 return prio;
1135}
1136
1137
1138static int floppy_is_inserted(BlockDriverState *bs)
1139{
1140 return fd_open(bs) >= 0;
1141}
1142
1143static int floppy_media_changed(BlockDriverState *bs)
1144{
1145 BDRVRawState *s = bs->opaque;
1146 int ret;
1147
1148
1149
1150
1151
1152 fd_open(bs);
1153 ret = s->fd_media_changed;
1154 s->fd_media_changed = 0;
1155#ifdef DEBUG_FLOPPY
1156 printf("Floppy changed=%d\n", ret);
1157#endif
1158 return ret;
1159}
1160
1161static int floppy_eject(BlockDriverState *bs, int eject_flag)
1162{
1163 BDRVRawState *s = bs->opaque;
1164 int fd;
1165
1166 if (s->fd >= 0) {
1167 close(s->fd);
1168 s->fd = -1;
1169 }
1170 fd = open(bs->filename, s->open_flags | O_NONBLOCK);
1171 if (fd >= 0) {
1172 if (ioctl(fd, FDEJECT, 0) < 0)
1173 perror("FDEJECT");
1174 close(fd);
1175 }
1176
1177 return 0;
1178}
1179
1180static BlockDriver bdrv_host_floppy = {
1181 .format_name = "host_floppy",
1182 .protocol_name = "host_floppy",
1183 .instance_size = sizeof(BDRVRawState),
1184 .bdrv_probe_device = floppy_probe_device,
1185 .bdrv_file_open = floppy_open,
1186 .bdrv_close = raw_close,
1187 .bdrv_create = hdev_create,
1188 .create_options = raw_create_options,
1189 .bdrv_has_zero_init = hdev_has_zero_init,
1190 .bdrv_flush = raw_flush,
1191
1192 .bdrv_aio_readv = raw_aio_readv,
1193 .bdrv_aio_writev = raw_aio_writev,
1194 .bdrv_aio_flush = raw_aio_flush,
1195
1196 .bdrv_read = raw_read,
1197 .bdrv_write = raw_write,
1198 .bdrv_getlength = raw_getlength,
1199
1200
1201 .bdrv_is_inserted = floppy_is_inserted,
1202 .bdrv_media_changed = floppy_media_changed,
1203 .bdrv_eject = floppy_eject,
1204};
1205
1206static int cdrom_open(BlockDriverState *bs, const char *filename, int flags)
1207{
1208 BDRVRawState *s = bs->opaque;
1209
1210 s->type = FTYPE_CD;
1211
1212
1213 return raw_open_common(bs, filename, flags, O_NONBLOCK);
1214}
1215
1216static int cdrom_probe_device(const char *filename)
1217{
1218 int fd, ret;
1219 int prio = 0;
1220
1221 fd = open(filename, O_RDONLY | O_NONBLOCK);
1222 if (fd < 0) {
1223 goto out;
1224 }
1225
1226
1227 ret = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
1228 if (ret >= 0)
1229 prio = 100;
1230
1231 close(fd);
1232out:
1233 return prio;
1234}
1235
1236static int cdrom_is_inserted(BlockDriverState *bs)
1237{
1238 BDRVRawState *s = bs->opaque;
1239 int ret;
1240
1241 ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
1242 if (ret == CDS_DISC_OK)
1243 return 1;
1244 return 0;
1245}
1246
1247static int cdrom_eject(BlockDriverState *bs, int eject_flag)
1248{
1249 BDRVRawState *s = bs->opaque;
1250
1251 if (eject_flag) {
1252 if (ioctl(s->fd, CDROMEJECT, NULL) < 0)
1253 perror("CDROMEJECT");
1254 } else {
1255 if (ioctl(s->fd, CDROMCLOSETRAY, NULL) < 0)
1256 perror("CDROMEJECT");
1257 }
1258
1259 return 0;
1260}
1261
1262static int cdrom_set_locked(BlockDriverState *bs, int locked)
1263{
1264 BDRVRawState *s = bs->opaque;
1265
1266 if (ioctl(s->fd, CDROM_LOCKDOOR, locked) < 0) {
1267
1268
1269
1270
1271
1272 }
1273
1274 return 0;
1275}
1276
1277static BlockDriver bdrv_host_cdrom = {
1278 .format_name = "host_cdrom",
1279 .protocol_name = "host_cdrom",
1280 .instance_size = sizeof(BDRVRawState),
1281 .bdrv_probe_device = cdrom_probe_device,
1282 .bdrv_file_open = cdrom_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
1297
1298 .bdrv_is_inserted = cdrom_is_inserted,
1299 .bdrv_eject = cdrom_eject,
1300 .bdrv_set_locked = cdrom_set_locked,
1301
1302
1303 .bdrv_ioctl = hdev_ioctl,
1304 .bdrv_aio_ioctl = hdev_aio_ioctl,
1305};
1306#endif
1307
1308#if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
1309static int cdrom_open(BlockDriverState *bs, const char *filename, int flags)
1310{
1311 BDRVRawState *s = bs->opaque;
1312 int ret;
1313
1314 s->type = FTYPE_CD;
1315
1316 ret = raw_open_common(bs, filename, flags, 0);
1317 if (ret)
1318 return ret;
1319
1320
1321 ioctl(s->fd, CDIOCALLOW);
1322 return 0;
1323}
1324
1325static int cdrom_probe_device(const char *filename)
1326{
1327 if (strstart(filename, "/dev/cd", NULL) ||
1328 strstart(filename, "/dev/acd", NULL))
1329 return 100;
1330 return 0;
1331}
1332
1333static int cdrom_reopen(BlockDriverState *bs)
1334{
1335 BDRVRawState *s = bs->opaque;
1336 int fd;
1337
1338
1339
1340
1341
1342 if (s->fd >= 0)
1343 close(s->fd);
1344 fd = open(bs->filename, s->open_flags, 0644);
1345 if (fd < 0) {
1346 s->fd = -1;
1347 return -EIO;
1348 }
1349 s->fd = fd;
1350
1351
1352 ioctl(s->fd, CDIOCALLOW);
1353 return 0;
1354}
1355
1356static int cdrom_is_inserted(BlockDriverState *bs)
1357{
1358 return raw_getlength(bs) > 0;
1359}
1360
1361static int cdrom_eject(BlockDriverState *bs, int eject_flag)
1362{
1363 BDRVRawState *s = bs->opaque;
1364
1365 if (s->fd < 0)
1366 return -ENOTSUP;
1367
1368 (void) ioctl(s->fd, CDIOCALLOW);
1369
1370 if (eject_flag) {
1371 if (ioctl(s->fd, CDIOCEJECT) < 0)
1372 perror("CDIOCEJECT");
1373 } else {
1374 if (ioctl(s->fd, CDIOCCLOSE) < 0)
1375 perror("CDIOCCLOSE");
1376 }
1377
1378 if (cdrom_reopen(bs) < 0)
1379 return -ENOTSUP;
1380 return 0;
1381}
1382
1383static int cdrom_set_locked(BlockDriverState *bs, int locked)
1384{
1385 BDRVRawState *s = bs->opaque;
1386
1387 if (s->fd < 0)
1388 return -ENOTSUP;
1389 if (ioctl(s->fd, (locked ? CDIOCPREVENT : CDIOCALLOW)) < 0) {
1390
1391
1392
1393
1394
1395 }
1396
1397 return 0;
1398}
1399
1400static BlockDriver bdrv_host_cdrom = {
1401 .format_name = "host_cdrom",
1402 .protocol_name = "host_cdrom",
1403 .instance_size = sizeof(BDRVRawState),
1404 .bdrv_probe_device = cdrom_probe_device,
1405 .bdrv_file_open = cdrom_open,
1406 .bdrv_close = raw_close,
1407 .bdrv_create = hdev_create,
1408 .create_options = raw_create_options,
1409 .bdrv_has_zero_init = hdev_has_zero_init,
1410 .bdrv_flush = raw_flush,
1411
1412 .bdrv_aio_readv = raw_aio_readv,
1413 .bdrv_aio_writev = raw_aio_writev,
1414 .bdrv_aio_flush = raw_aio_flush,
1415
1416 .bdrv_read = raw_read,
1417 .bdrv_write = raw_write,
1418 .bdrv_getlength = raw_getlength,
1419
1420
1421 .bdrv_is_inserted = cdrom_is_inserted,
1422 .bdrv_eject = cdrom_eject,
1423 .bdrv_set_locked = cdrom_set_locked,
1424};
1425#endif
1426
1427static void bdrv_file_init(void)
1428{
1429
1430
1431
1432
1433 bdrv_register(&bdrv_file);
1434 bdrv_register(&bdrv_host_device);
1435#ifdef __linux__
1436 bdrv_register(&bdrv_host_floppy);
1437 bdrv_register(&bdrv_host_cdrom);
1438#endif
1439#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1440 bdrv_register(&bdrv_host_cdrom);
1441#endif
1442}
1443
1444block_init(bdrv_file_init);
1445