]> git.proxmox.com Git - mirror_qemu.git/blob - block/raw-posix.c
raw-posix: add Linux native AIO support
[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 <signal.h>
47 #include <sys/dkio.h>
48 #endif
49 #ifdef __linux__
50 #include <sys/ioctl.h>
51 #include <linux/cdrom.h>
52 #include <linux/fd.h>
53 #endif
54 #ifdef __FreeBSD__
55 #include <signal.h>
56 #include <sys/disk.h>
57 #include <sys/cdio.h>
58 #endif
59
60 #ifdef __OpenBSD__
61 #include <sys/ioctl.h>
62 #include <sys/disklabel.h>
63 #include <sys/dkio.h>
64 #endif
65
66 #ifdef __DragonFly__
67 #include <sys/ioctl.h>
68 #include <sys/diskslice.h>
69 #endif
70
71 //#define DEBUG_FLOPPY
72
73 //#define DEBUG_BLOCK
74 #if defined(DEBUG_BLOCK)
75 #define DEBUG_BLOCK_PRINT(formatCstr, ...) do { if (qemu_log_enabled()) \
76 { qemu_log(formatCstr, ## __VA_ARGS__); qemu_log_flush(); } } while (0)
77 #else
78 #define DEBUG_BLOCK_PRINT(formatCstr, ...)
79 #endif
80
81 /* OS X does not have O_DSYNC */
82 #ifndef O_DSYNC
83 #ifdef O_SYNC
84 #define O_DSYNC O_SYNC
85 #elif defined(O_FSYNC)
86 #define O_DSYNC O_FSYNC
87 #endif
88 #endif
89
90 /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */
91 #ifndef O_DIRECT
92 #define O_DIRECT O_DSYNC
93 #endif
94
95 #define FTYPE_FILE 0
96 #define FTYPE_CD 1
97 #define FTYPE_FD 2
98
99 #define ALIGNED_BUFFER_SIZE (32 * 512)
100
101 /* if the FD is not accessed during that time (in ms), we try to
102 reopen it to see if the disk has been changed */
103 #define FD_OPEN_TIMEOUT 1000
104
105 typedef struct BDRVRawState {
106 int fd;
107 int type;
108 unsigned int lseek_err_cnt;
109 int open_flags;
110 void *aio_ctx;
111 #if defined(__linux__)
112 /* linux floppy specific */
113 int64_t fd_open_time;
114 int64_t fd_error_time;
115 int fd_got_error;
116 int fd_media_changed;
117 #endif
118 int use_aio;
119 uint8_t* aligned_buf;
120 } BDRVRawState;
121
122 static int fd_open(BlockDriverState *bs);
123 static int64_t raw_getlength(BlockDriverState *bs);
124
125 #if defined(__FreeBSD__)
126 static int cdrom_reopen(BlockDriverState *bs);
127 #endif
128
129 static int raw_open_common(BlockDriverState *bs, const char *filename,
130 int bdrv_flags, int open_flags)
131 {
132 BDRVRawState *s = bs->opaque;
133 int fd, ret;
134
135 s->lseek_err_cnt = 0;
136
137 s->open_flags = open_flags | O_BINARY;
138 s->open_flags &= ~O_ACCMODE;
139 if ((bdrv_flags & BDRV_O_ACCESS) == BDRV_O_RDWR) {
140 s->open_flags |= O_RDWR;
141 } else {
142 s->open_flags |= O_RDONLY;
143 bs->read_only = 1;
144 }
145
146 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
147 * and O_DIRECT for no caching. */
148 if ((bdrv_flags & BDRV_O_NOCACHE))
149 s->open_flags |= O_DIRECT;
150 else if (!(bdrv_flags & BDRV_O_CACHE_WB))
151 s->open_flags |= O_DSYNC;
152
153 s->fd = -1;
154 fd = open(filename, s->open_flags, 0644);
155 if (fd < 0) {
156 ret = -errno;
157 if (ret == -EROFS)
158 ret = -EACCES;
159 return ret;
160 }
161 s->fd = fd;
162 s->aligned_buf = NULL;
163
164 if ((bdrv_flags & BDRV_O_NOCACHE)) {
165 s->aligned_buf = qemu_blockalign(bs, ALIGNED_BUFFER_SIZE);
166 if (s->aligned_buf == NULL) {
167 goto out_close;
168 }
169 }
170
171 #ifdef CONFIG_LINUX_AIO
172 if ((bdrv_flags & (BDRV_O_NOCACHE|BDRV_O_NATIVE_AIO)) ==
173 (BDRV_O_NOCACHE|BDRV_O_NATIVE_AIO)) {
174 s->aio_ctx = laio_init();
175 if (!s->aio_ctx) {
176 goto out_free_buf;
177 }
178 s->use_aio = 1;
179 } else
180 #endif
181 {
182 s->aio_ctx = paio_init();
183 if (!s->aio_ctx) {
184 goto out_free_buf;
185 }
186 s->use_aio = 0;
187 }
188
189 return 0;
190
191 out_free_buf:
192 qemu_vfree(s->aligned_buf);
193 out_close:
194 close(fd);
195 return -errno;
196 }
197
198 static int raw_open(BlockDriverState *bs, const char *filename, int flags)
199 {
200 BDRVRawState *s = bs->opaque;
201 int open_flags = 0;
202
203 s->type = FTYPE_FILE;
204 if (flags & BDRV_O_CREAT)
205 open_flags = O_CREAT | O_TRUNC;
206
207 return raw_open_common(bs, filename, flags, open_flags);
208 }
209
210 /* XXX: use host sector size if necessary with:
211 #ifdef DIOCGSECTORSIZE
212 {
213 unsigned int sectorsize = 512;
214 if (!ioctl(fd, DIOCGSECTORSIZE, &sectorsize) &&
215 sectorsize > bufsize)
216 bufsize = sectorsize;
217 }
218 #endif
219 #ifdef CONFIG_COCOA
220 u_int32_t blockSize = 512;
221 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
222 bufsize = blockSize;
223 }
224 #endif
225 */
226
227 /*
228 * offset and count are in bytes, but must be multiples of 512 for files
229 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
230 *
231 * This function may be called without alignment if the caller ensures
232 * that O_DIRECT is not in effect.
233 */
234 static int raw_pread_aligned(BlockDriverState *bs, int64_t offset,
235 uint8_t *buf, int count)
236 {
237 BDRVRawState *s = bs->opaque;
238 int ret;
239
240 ret = fd_open(bs);
241 if (ret < 0)
242 return ret;
243
244 if (offset >= 0 && lseek(s->fd, offset, SEEK_SET) == (off_t)-1) {
245 ++(s->lseek_err_cnt);
246 if(s->lseek_err_cnt <= 10) {
247 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
248 "] lseek failed : %d = %s\n",
249 s->fd, bs->filename, offset, buf, count,
250 bs->total_sectors, errno, strerror(errno));
251 }
252 return -1;
253 }
254 s->lseek_err_cnt=0;
255
256 ret = read(s->fd, buf, count);
257 if (ret == count)
258 goto label__raw_read__success;
259
260 /* Allow reads beyond the end (needed for pwrite) */
261 if ((ret == 0) && bs->growable) {
262 int64_t size = raw_getlength(bs);
263 if (offset >= size) {
264 memset(buf, 0, count);
265 ret = count;
266 goto label__raw_read__success;
267 }
268 }
269
270 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
271 "] read failed %d : %d = %s\n",
272 s->fd, bs->filename, offset, buf, count,
273 bs->total_sectors, ret, errno, strerror(errno));
274
275 /* Try harder for CDrom. */
276 if (bs->type == BDRV_TYPE_CDROM) {
277 lseek(s->fd, offset, SEEK_SET);
278 ret = read(s->fd, buf, count);
279 if (ret == count)
280 goto label__raw_read__success;
281 lseek(s->fd, offset, SEEK_SET);
282 ret = read(s->fd, buf, count);
283 if (ret == count)
284 goto label__raw_read__success;
285
286 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
287 "] retry read failed %d : %d = %s\n",
288 s->fd, bs->filename, offset, buf, count,
289 bs->total_sectors, ret, errno, strerror(errno));
290 }
291
292 label__raw_read__success:
293
294 return (ret < 0) ? -errno : ret;
295 }
296
297 /*
298 * offset and count are in bytes, but must be multiples of 512 for files
299 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
300 *
301 * This function may be called without alignment if the caller ensures
302 * that O_DIRECT is not in effect.
303 */
304 static int raw_pwrite_aligned(BlockDriverState *bs, int64_t offset,
305 const uint8_t *buf, int count)
306 {
307 BDRVRawState *s = bs->opaque;
308 int ret;
309
310 ret = fd_open(bs);
311 if (ret < 0)
312 return -errno;
313
314 if (offset >= 0 && lseek(s->fd, offset, SEEK_SET) == (off_t)-1) {
315 ++(s->lseek_err_cnt);
316 if(s->lseek_err_cnt) {
317 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%"
318 PRId64 "] lseek failed : %d = %s\n",
319 s->fd, bs->filename, offset, buf, count,
320 bs->total_sectors, errno, strerror(errno));
321 }
322 return -EIO;
323 }
324 s->lseek_err_cnt = 0;
325
326 ret = write(s->fd, buf, count);
327 if (ret == count)
328 goto label__raw_write__success;
329
330 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
331 "] write failed %d : %d = %s\n",
332 s->fd, bs->filename, offset, buf, count,
333 bs->total_sectors, ret, errno, strerror(errno));
334
335 label__raw_write__success:
336
337 return (ret < 0) ? -errno : ret;
338 }
339
340
341 /*
342 * offset and count are in bytes and possibly not aligned. For files opened
343 * with O_DIRECT, necessary alignments are ensured before calling
344 * raw_pread_aligned to do the actual read.
345 */
346 static int raw_pread(BlockDriverState *bs, int64_t offset,
347 uint8_t *buf, int count)
348 {
349 BDRVRawState *s = bs->opaque;
350 int size, ret, shift, sum;
351
352 sum = 0;
353
354 if (s->aligned_buf != NULL) {
355
356 if (offset & 0x1ff) {
357 /* align offset on a 512 bytes boundary */
358
359 shift = offset & 0x1ff;
360 size = (shift + count + 0x1ff) & ~0x1ff;
361 if (size > ALIGNED_BUFFER_SIZE)
362 size = ALIGNED_BUFFER_SIZE;
363 ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, size);
364 if (ret < 0)
365 return ret;
366
367 size = 512 - shift;
368 if (size > count)
369 size = count;
370 memcpy(buf, s->aligned_buf + shift, size);
371
372 buf += size;
373 offset += size;
374 count -= size;
375 sum += size;
376
377 if (count == 0)
378 return sum;
379 }
380 if (count & 0x1ff || (uintptr_t) buf & 0x1ff) {
381
382 /* read on aligned buffer */
383
384 while (count) {
385
386 size = (count + 0x1ff) & ~0x1ff;
387 if (size > ALIGNED_BUFFER_SIZE)
388 size = ALIGNED_BUFFER_SIZE;
389
390 ret = raw_pread_aligned(bs, offset, s->aligned_buf, size);
391 if (ret < 0)
392 return ret;
393
394 size = ret;
395 if (size > count)
396 size = count;
397
398 memcpy(buf, s->aligned_buf, size);
399
400 buf += size;
401 offset += size;
402 count -= size;
403 sum += size;
404 }
405
406 return sum;
407 }
408 }
409
410 return raw_pread_aligned(bs, offset, buf, count) + sum;
411 }
412
413 static int raw_read(BlockDriverState *bs, int64_t sector_num,
414 uint8_t *buf, int nb_sectors)
415 {
416 int ret;
417
418 ret = raw_pread(bs, sector_num * 512, buf, nb_sectors * 512);
419 if (ret == (nb_sectors * 512))
420 ret = 0;
421 return ret;
422 }
423
424 /*
425 * offset and count are in bytes and possibly not aligned. For files opened
426 * with O_DIRECT, necessary alignments are ensured before calling
427 * raw_pwrite_aligned to do the actual write.
428 */
429 static int raw_pwrite(BlockDriverState *bs, int64_t offset,
430 const uint8_t *buf, int count)
431 {
432 BDRVRawState *s = bs->opaque;
433 int size, ret, shift, sum;
434
435 sum = 0;
436
437 if (s->aligned_buf != NULL) {
438
439 if (offset & 0x1ff) {
440 /* align offset on a 512 bytes boundary */
441 shift = offset & 0x1ff;
442 ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, 512);
443 if (ret < 0)
444 return ret;
445
446 size = 512 - shift;
447 if (size > count)
448 size = count;
449 memcpy(s->aligned_buf + shift, buf, size);
450
451 ret = raw_pwrite_aligned(bs, offset - shift, s->aligned_buf, 512);
452 if (ret < 0)
453 return ret;
454
455 buf += size;
456 offset += size;
457 count -= size;
458 sum += size;
459
460 if (count == 0)
461 return sum;
462 }
463 if (count & 0x1ff || (uintptr_t) buf & 0x1ff) {
464
465 while ((size = (count & ~0x1ff)) != 0) {
466
467 if (size > ALIGNED_BUFFER_SIZE)
468 size = ALIGNED_BUFFER_SIZE;
469
470 memcpy(s->aligned_buf, buf, size);
471
472 ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, size);
473 if (ret < 0)
474 return ret;
475
476 buf += ret;
477 offset += ret;
478 count -= ret;
479 sum += ret;
480 }
481 /* here, count < 512 because (count & ~0x1ff) == 0 */
482 if (count) {
483 ret = raw_pread_aligned(bs, offset, s->aligned_buf, 512);
484 if (ret < 0)
485 return ret;
486 memcpy(s->aligned_buf, buf, count);
487
488 ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, 512);
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
502 static 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 * 512, buf, nb_sectors * 512);
507 if (ret == (nb_sectors * 512))
508 ret = 0;
509 return ret;
510 }
511
512 /*
513 * Check if all memory in this vector is sector aligned.
514 */
515 static int qiov_is_aligned(QEMUIOVector *qiov)
516 {
517 int i;
518
519 for (i = 0; i < qiov->niov; i++) {
520 if ((uintptr_t) qiov->iov[i].iov_base % 512) {
521 return 0;
522 }
523 }
524
525 return 1;
526 }
527
528 static BlockDriverAIOCB *raw_aio_submit(BlockDriverState *bs,
529 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
530 BlockDriverCompletionFunc *cb, void *opaque, int type)
531 {
532 BDRVRawState *s = bs->opaque;
533
534 if (fd_open(bs) < 0)
535 return NULL;
536
537 /*
538 * If O_DIRECT is used the buffer needs to be aligned on a sector
539 * boundary. Check if this is the case or telll the low-level
540 * driver that it needs to copy the buffer.
541 */
542 if (s->aligned_buf) {
543 if (!qiov_is_aligned(qiov)) {
544 type |= QEMU_AIO_MISALIGNED;
545 } else if (s->use_aio) {
546 return laio_submit(bs, s->aio_ctx, s->fd, sector_num, qiov,
547 nb_sectors, cb, opaque, type);
548 }
549 }
550
551 return paio_submit(bs, s->aio_ctx, s->fd, sector_num, qiov, nb_sectors,
552 cb, opaque, type);
553 }
554
555 static BlockDriverAIOCB *raw_aio_readv(BlockDriverState *bs,
556 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
557 BlockDriverCompletionFunc *cb, void *opaque)
558 {
559 return raw_aio_submit(bs, sector_num, qiov, nb_sectors,
560 cb, opaque, QEMU_AIO_READ);
561 }
562
563 static BlockDriverAIOCB *raw_aio_writev(BlockDriverState *bs,
564 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
565 BlockDriverCompletionFunc *cb, void *opaque)
566 {
567 return raw_aio_submit(bs, sector_num, qiov, nb_sectors,
568 cb, opaque, QEMU_AIO_WRITE);
569 }
570
571 static void raw_close(BlockDriverState *bs)
572 {
573 BDRVRawState *s = bs->opaque;
574 if (s->fd >= 0) {
575 close(s->fd);
576 s->fd = -1;
577 if (s->aligned_buf != NULL)
578 qemu_free(s->aligned_buf);
579 }
580 }
581
582 static int raw_truncate(BlockDriverState *bs, int64_t offset)
583 {
584 BDRVRawState *s = bs->opaque;
585 if (s->type != FTYPE_FILE)
586 return -ENOTSUP;
587 if (ftruncate(s->fd, offset) < 0)
588 return -errno;
589 return 0;
590 }
591
592 #ifdef __OpenBSD__
593 static int64_t raw_getlength(BlockDriverState *bs)
594 {
595 BDRVRawState *s = bs->opaque;
596 int fd = s->fd;
597 struct stat st;
598
599 if (fstat(fd, &st))
600 return -1;
601 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
602 struct disklabel dl;
603
604 if (ioctl(fd, DIOCGDINFO, &dl))
605 return -1;
606 return (uint64_t)dl.d_secsize *
607 dl.d_partitions[DISKPART(st.st_rdev)].p_size;
608 } else
609 return st.st_size;
610 }
611 #else /* !__OpenBSD__ */
612 static int64_t raw_getlength(BlockDriverState *bs)
613 {
614 BDRVRawState *s = bs->opaque;
615 int fd = s->fd;
616 int64_t size;
617 #ifdef CONFIG_BSD
618 struct stat sb;
619 #ifdef __FreeBSD__
620 int reopened = 0;
621 #endif
622 #endif
623 #ifdef __sun__
624 struct dk_minfo minfo;
625 int rv;
626 #endif
627 int ret;
628
629 ret = fd_open(bs);
630 if (ret < 0)
631 return ret;
632
633 #ifdef CONFIG_BSD
634 #ifdef __FreeBSD__
635 again:
636 #endif
637 if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
638 #ifdef DIOCGMEDIASIZE
639 if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
640 #elif defined(DIOCGPART)
641 {
642 struct partinfo pi;
643 if (ioctl(fd, DIOCGPART, &pi) == 0)
644 size = pi.media_size;
645 else
646 size = 0;
647 }
648 if (size == 0)
649 #endif
650 #ifdef CONFIG_COCOA
651 size = LONG_LONG_MAX;
652 #else
653 size = lseek(fd, 0LL, SEEK_END);
654 #endif
655 #ifdef __FreeBSD__
656 switch(s->type) {
657 case FTYPE_CD:
658 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
659 if (size == 2048LL * (unsigned)-1)
660 size = 0;
661 /* XXX no disc? maybe we need to reopen... */
662 if (size <= 0 && !reopened && cdrom_reopen(bs) >= 0) {
663 reopened = 1;
664 goto again;
665 }
666 }
667 #endif
668 } else
669 #endif
670 #ifdef __sun__
671 /*
672 * use the DKIOCGMEDIAINFO ioctl to read the size.
673 */
674 rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo );
675 if ( rv != -1 ) {
676 size = minfo.dki_lbsize * minfo.dki_capacity;
677 } else /* there are reports that lseek on some devices
678 fails, but irc discussion said that contingency
679 on contingency was overkill */
680 #endif
681 {
682 size = lseek(fd, 0, SEEK_END);
683 }
684 return size;
685 }
686 #endif
687
688 static int raw_create(const char *filename, QEMUOptionParameter *options)
689 {
690 int fd;
691 int result = 0;
692 int64_t total_size = 0;
693
694 /* Read out options */
695 while (options && options->name) {
696 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
697 total_size = options->value.n / 512;
698 }
699 options++;
700 }
701
702 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
703 0644);
704 if (fd < 0) {
705 result = -errno;
706 } else {
707 if (ftruncate(fd, total_size * 512) != 0) {
708 result = -errno;
709 }
710 if (close(fd) != 0) {
711 result = -errno;
712 }
713 }
714 return result;
715 }
716
717 static void raw_flush(BlockDriverState *bs)
718 {
719 BDRVRawState *s = bs->opaque;
720 fsync(s->fd);
721 }
722
723
724 static QEMUOptionParameter raw_create_options[] = {
725 {
726 .name = BLOCK_OPT_SIZE,
727 .type = OPT_SIZE,
728 .help = "Virtual disk size"
729 },
730 { NULL }
731 };
732
733 static BlockDriver bdrv_raw = {
734 .format_name = "raw",
735 .instance_size = sizeof(BDRVRawState),
736 .bdrv_probe = NULL, /* no probe for protocols */
737 .bdrv_open = raw_open,
738 .bdrv_read = raw_read,
739 .bdrv_write = raw_write,
740 .bdrv_close = raw_close,
741 .bdrv_create = raw_create,
742 .bdrv_flush = raw_flush,
743
744 .bdrv_aio_readv = raw_aio_readv,
745 .bdrv_aio_writev = raw_aio_writev,
746
747 .bdrv_truncate = raw_truncate,
748 .bdrv_getlength = raw_getlength,
749
750 .create_options = raw_create_options,
751 };
752
753 /***********************************************/
754 /* host device */
755
756 #ifdef CONFIG_COCOA
757 static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
758 static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
759
760 kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
761 {
762 kern_return_t kernResult;
763 mach_port_t masterPort;
764 CFMutableDictionaryRef classesToMatch;
765
766 kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
767 if ( KERN_SUCCESS != kernResult ) {
768 printf( "IOMasterPort returned %d\n", kernResult );
769 }
770
771 classesToMatch = IOServiceMatching( kIOCDMediaClass );
772 if ( classesToMatch == NULL ) {
773 printf( "IOServiceMatching returned a NULL dictionary.\n" );
774 } else {
775 CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
776 }
777 kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
778 if ( KERN_SUCCESS != kernResult )
779 {
780 printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
781 }
782
783 return kernResult;
784 }
785
786 kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
787 {
788 io_object_t nextMedia;
789 kern_return_t kernResult = KERN_FAILURE;
790 *bsdPath = '\0';
791 nextMedia = IOIteratorNext( mediaIterator );
792 if ( nextMedia )
793 {
794 CFTypeRef bsdPathAsCFString;
795 bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
796 if ( bsdPathAsCFString ) {
797 size_t devPathLength;
798 strcpy( bsdPath, _PATH_DEV );
799 strcat( bsdPath, "r" );
800 devPathLength = strlen( bsdPath );
801 if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
802 kernResult = KERN_SUCCESS;
803 }
804 CFRelease( bsdPathAsCFString );
805 }
806 IOObjectRelease( nextMedia );
807 }
808
809 return kernResult;
810 }
811
812 #endif
813
814 static int hdev_probe_device(const char *filename)
815 {
816 struct stat st;
817
818 /* allow a dedicated CD-ROM driver to match with a higher priority */
819 if (strstart(filename, "/dev/cdrom", NULL))
820 return 50;
821
822 if (stat(filename, &st) >= 0 &&
823 (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
824 return 100;
825 }
826
827 return 0;
828 }
829
830 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
831 {
832 BDRVRawState *s = bs->opaque;
833
834 #ifdef CONFIG_COCOA
835 if (strstart(filename, "/dev/cdrom", NULL)) {
836 kern_return_t kernResult;
837 io_iterator_t mediaIterator;
838 char bsdPath[ MAXPATHLEN ];
839 int fd;
840
841 kernResult = FindEjectableCDMedia( &mediaIterator );
842 kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
843
844 if ( bsdPath[ 0 ] != '\0' ) {
845 strcat(bsdPath,"s0");
846 /* some CDs don't have a partition 0 */
847 fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
848 if (fd < 0) {
849 bsdPath[strlen(bsdPath)-1] = '1';
850 } else {
851 close(fd);
852 }
853 filename = bsdPath;
854 }
855
856 if ( mediaIterator )
857 IOObjectRelease( mediaIterator );
858 }
859 #endif
860
861 s->type = FTYPE_FILE;
862 #if defined(__linux__)
863 if (strstart(filename, "/dev/sg", NULL)) {
864 bs->sg = 1;
865 }
866 #endif
867
868 return raw_open_common(bs, filename, flags, 0);
869 }
870
871 #if defined(__linux__)
872 /* Note: we do not have a reliable method to detect if the floppy is
873 present. The current method is to try to open the floppy at every
874 I/O and to keep it opened during a few hundreds of ms. */
875 static int fd_open(BlockDriverState *bs)
876 {
877 BDRVRawState *s = bs->opaque;
878 int last_media_present;
879
880 if (s->type != FTYPE_FD)
881 return 0;
882 last_media_present = (s->fd >= 0);
883 if (s->fd >= 0 &&
884 (qemu_get_clock(rt_clock) - s->fd_open_time) >= FD_OPEN_TIMEOUT) {
885 close(s->fd);
886 s->fd = -1;
887 #ifdef DEBUG_FLOPPY
888 printf("Floppy closed\n");
889 #endif
890 }
891 if (s->fd < 0) {
892 if (s->fd_got_error &&
893 (qemu_get_clock(rt_clock) - s->fd_error_time) < FD_OPEN_TIMEOUT) {
894 #ifdef DEBUG_FLOPPY
895 printf("No floppy (open delayed)\n");
896 #endif
897 return -EIO;
898 }
899 s->fd = open(bs->filename, s->open_flags & ~O_NONBLOCK);
900 if (s->fd < 0) {
901 s->fd_error_time = qemu_get_clock(rt_clock);
902 s->fd_got_error = 1;
903 if (last_media_present)
904 s->fd_media_changed = 1;
905 #ifdef DEBUG_FLOPPY
906 printf("No floppy\n");
907 #endif
908 return -EIO;
909 }
910 #ifdef DEBUG_FLOPPY
911 printf("Floppy opened\n");
912 #endif
913 }
914 if (!last_media_present)
915 s->fd_media_changed = 1;
916 s->fd_open_time = qemu_get_clock(rt_clock);
917 s->fd_got_error = 0;
918 return 0;
919 }
920
921 static int hdev_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
922 {
923 BDRVRawState *s = bs->opaque;
924
925 return ioctl(s->fd, req, buf);
926 }
927
928 static BlockDriverAIOCB *hdev_aio_ioctl(BlockDriverState *bs,
929 unsigned long int req, void *buf,
930 BlockDriverCompletionFunc *cb, void *opaque)
931 {
932 BDRVRawState *s = bs->opaque;
933
934 if (fd_open(bs) < 0)
935 return NULL;
936 return paio_ioctl(bs, s->fd, req, buf, cb, opaque);
937 }
938
939 #elif defined(__FreeBSD__)
940 static int fd_open(BlockDriverState *bs)
941 {
942 BDRVRawState *s = bs->opaque;
943
944 /* this is just to ensure s->fd is sane (its called by io ops) */
945 if (s->fd >= 0)
946 return 0;
947 return -EIO;
948 }
949 #else /* !linux && !FreeBSD */
950
951 static int fd_open(BlockDriverState *bs)
952 {
953 return 0;
954 }
955
956 #endif /* !linux && !FreeBSD */
957
958 static int hdev_create(const char *filename, QEMUOptionParameter *options)
959 {
960 int fd;
961 int ret = 0;
962 struct stat stat_buf;
963 int64_t total_size = 0;
964
965 /* Read out options */
966 while (options && options->name) {
967 if (!strcmp(options->name, "size")) {
968 total_size = options->value.n / 512;
969 }
970 options++;
971 }
972
973 fd = open(filename, O_WRONLY | O_BINARY);
974 if (fd < 0)
975 return -EIO;
976
977 if (fstat(fd, &stat_buf) < 0)
978 ret = -EIO;
979 else if (!S_ISBLK(stat_buf.st_mode) && !S_ISCHR(stat_buf.st_mode))
980 ret = -EIO;
981 else if (lseek(fd, 0, SEEK_END) < total_size * 512)
982 ret = -ENOSPC;
983
984 close(fd);
985 return ret;
986 }
987
988 static BlockDriver bdrv_host_device = {
989 .format_name = "host_device",
990 .instance_size = sizeof(BDRVRawState),
991 .bdrv_probe_device = hdev_probe_device,
992 .bdrv_open = hdev_open,
993 .bdrv_close = raw_close,
994 .bdrv_create = hdev_create,
995 .bdrv_flush = raw_flush,
996
997 .bdrv_aio_readv = raw_aio_readv,
998 .bdrv_aio_writev = raw_aio_writev,
999
1000 .bdrv_read = raw_read,
1001 .bdrv_write = raw_write,
1002 .bdrv_getlength = raw_getlength,
1003
1004 /* generic scsi device */
1005 #ifdef __linux__
1006 .bdrv_ioctl = hdev_ioctl,
1007 .bdrv_aio_ioctl = hdev_aio_ioctl,
1008 #endif
1009 };
1010
1011 #ifdef __linux__
1012 static int floppy_open(BlockDriverState *bs, const char *filename, int flags)
1013 {
1014 BDRVRawState *s = bs->opaque;
1015 int ret;
1016
1017 s->type = FTYPE_FD;
1018
1019 /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
1020 ret = raw_open_common(bs, filename, flags, O_NONBLOCK);
1021 if (ret)
1022 return ret;
1023
1024 /* close fd so that we can reopen it as needed */
1025 close(s->fd);
1026 s->fd = -1;
1027 s->fd_media_changed = 1;
1028
1029 return 0;
1030 }
1031
1032 static int floppy_probe_device(const char *filename)
1033 {
1034 if (strstart(filename, "/dev/fd", NULL))
1035 return 100;
1036 return 0;
1037 }
1038
1039
1040 static int floppy_is_inserted(BlockDriverState *bs)
1041 {
1042 return fd_open(bs) >= 0;
1043 }
1044
1045 static int floppy_media_changed(BlockDriverState *bs)
1046 {
1047 BDRVRawState *s = bs->opaque;
1048 int ret;
1049
1050 /*
1051 * XXX: we do not have a true media changed indication.
1052 * It does not work if the floppy is changed without trying to read it.
1053 */
1054 fd_open(bs);
1055 ret = s->fd_media_changed;
1056 s->fd_media_changed = 0;
1057 #ifdef DEBUG_FLOPPY
1058 printf("Floppy changed=%d\n", ret);
1059 #endif
1060 return ret;
1061 }
1062
1063 static int floppy_eject(BlockDriverState *bs, int eject_flag)
1064 {
1065 BDRVRawState *s = bs->opaque;
1066 int fd;
1067
1068 if (s->fd >= 0) {
1069 close(s->fd);
1070 s->fd = -1;
1071 }
1072 fd = open(bs->filename, s->open_flags | O_NONBLOCK);
1073 if (fd >= 0) {
1074 if (ioctl(fd, FDEJECT, 0) < 0)
1075 perror("FDEJECT");
1076 close(fd);
1077 }
1078
1079 return 0;
1080 }
1081
1082 static BlockDriver bdrv_host_floppy = {
1083 .format_name = "host_floppy",
1084 .instance_size = sizeof(BDRVRawState),
1085 .bdrv_probe_device = floppy_probe_device,
1086 .bdrv_open = floppy_open,
1087 .bdrv_close = raw_close,
1088 .bdrv_create = hdev_create,
1089 .bdrv_flush = raw_flush,
1090
1091 .bdrv_aio_readv = raw_aio_readv,
1092 .bdrv_aio_writev = raw_aio_writev,
1093
1094 .bdrv_read = raw_read,
1095 .bdrv_write = raw_write,
1096 .bdrv_getlength = raw_getlength,
1097
1098 /* removable device support */
1099 .bdrv_is_inserted = floppy_is_inserted,
1100 .bdrv_media_changed = floppy_media_changed,
1101 .bdrv_eject = floppy_eject,
1102 };
1103
1104 static int cdrom_open(BlockDriverState *bs, const char *filename, int flags)
1105 {
1106 BDRVRawState *s = bs->opaque;
1107
1108 s->type = FTYPE_CD;
1109
1110 /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
1111 return raw_open_common(bs, filename, flags, O_NONBLOCK);
1112 }
1113
1114 static int cdrom_probe_device(const char *filename)
1115 {
1116 if (strstart(filename, "/dev/cd", NULL))
1117 return 100;
1118 return 0;
1119 }
1120
1121 static int cdrom_is_inserted(BlockDriverState *bs)
1122 {
1123 BDRVRawState *s = bs->opaque;
1124 int ret;
1125
1126 ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
1127 if (ret == CDS_DISC_OK)
1128 return 1;
1129 return 0;
1130 }
1131
1132 static int cdrom_eject(BlockDriverState *bs, int eject_flag)
1133 {
1134 BDRVRawState *s = bs->opaque;
1135
1136 if (eject_flag) {
1137 if (ioctl(s->fd, CDROMEJECT, NULL) < 0)
1138 perror("CDROMEJECT");
1139 } else {
1140 if (ioctl(s->fd, CDROMCLOSETRAY, NULL) < 0)
1141 perror("CDROMEJECT");
1142 }
1143
1144 return 0;
1145 }
1146
1147 static int cdrom_set_locked(BlockDriverState *bs, int locked)
1148 {
1149 BDRVRawState *s = bs->opaque;
1150
1151 if (ioctl(s->fd, CDROM_LOCKDOOR, locked) < 0) {
1152 /*
1153 * Note: an error can happen if the distribution automatically
1154 * mounts the CD-ROM
1155 */
1156 /* perror("CDROM_LOCKDOOR"); */
1157 }
1158
1159 return 0;
1160 }
1161
1162 static BlockDriver bdrv_host_cdrom = {
1163 .format_name = "host_cdrom",
1164 .instance_size = sizeof(BDRVRawState),
1165 .bdrv_probe_device = cdrom_probe_device,
1166 .bdrv_open = cdrom_open,
1167 .bdrv_close = raw_close,
1168 .bdrv_create = hdev_create,
1169 .bdrv_flush = raw_flush,
1170
1171 .bdrv_aio_readv = raw_aio_readv,
1172 .bdrv_aio_writev = raw_aio_writev,
1173
1174 .bdrv_read = raw_read,
1175 .bdrv_write = raw_write,
1176 .bdrv_getlength = raw_getlength,
1177
1178 /* removable device support */
1179 .bdrv_is_inserted = cdrom_is_inserted,
1180 .bdrv_eject = cdrom_eject,
1181 .bdrv_set_locked = cdrom_set_locked,
1182
1183 /* generic scsi device */
1184 .bdrv_ioctl = hdev_ioctl,
1185 .bdrv_aio_ioctl = hdev_aio_ioctl,
1186 };
1187 #endif /* __linux__ */
1188
1189 #ifdef __FreeBSD__
1190 static int cdrom_open(BlockDriverState *bs, const char *filename, int flags)
1191 {
1192 BDRVRawState *s = bs->opaque;
1193 int ret;
1194
1195 s->type = FTYPE_CD;
1196
1197 ret = raw_open_common(bs, filename, flags, 0);
1198 if (ret)
1199 return ret;
1200
1201 /* make sure the door isnt locked at this time */
1202 ioctl(s->fd, CDIOCALLOW);
1203 return 0;
1204 }
1205
1206 static int cdrom_probe_device(const char *filename)
1207 {
1208 if (strstart(filename, "/dev/cd", NULL) ||
1209 strstart(filename, "/dev/acd", NULL))
1210 return 100;
1211 return 0;
1212 }
1213
1214 static int cdrom_reopen(BlockDriverState *bs)
1215 {
1216 BDRVRawState *s = bs->opaque;
1217 int fd;
1218
1219 /*
1220 * Force reread of possibly changed/newly loaded disc,
1221 * FreeBSD seems to not notice sometimes...
1222 */
1223 if (s->fd >= 0)
1224 close(s->fd);
1225 fd = open(bs->filename, s->open_flags, 0644);
1226 if (fd < 0) {
1227 s->fd = -1;
1228 return -EIO;
1229 }
1230 s->fd = fd;
1231
1232 /* make sure the door isnt locked at this time */
1233 ioctl(s->fd, CDIOCALLOW);
1234 return 0;
1235 }
1236
1237 static int cdrom_is_inserted(BlockDriverState *bs)
1238 {
1239 return raw_getlength(bs) > 0;
1240 }
1241
1242 static int cdrom_eject(BlockDriverState *bs, int eject_flag)
1243 {
1244 BDRVRawState *s = bs->opaque;
1245
1246 if (s->fd < 0)
1247 return -ENOTSUP;
1248
1249 (void) ioctl(s->fd, CDIOCALLOW);
1250
1251 if (eject_flag) {
1252 if (ioctl(s->fd, CDIOCEJECT) < 0)
1253 perror("CDIOCEJECT");
1254 } else {
1255 if (ioctl(s->fd, CDIOCCLOSE) < 0)
1256 perror("CDIOCCLOSE");
1257 }
1258
1259 if (cdrom_reopen(bs) < 0)
1260 return -ENOTSUP;
1261 return 0;
1262 }
1263
1264 static int cdrom_set_locked(BlockDriverState *bs, int locked)
1265 {
1266 BDRVRawState *s = bs->opaque;
1267
1268 if (s->fd < 0)
1269 return -ENOTSUP;
1270 if (ioctl(s->fd, (locked ? CDIOCPREVENT : CDIOCALLOW)) < 0) {
1271 /*
1272 * Note: an error can happen if the distribution automatically
1273 * mounts the CD-ROM
1274 */
1275 /* perror("CDROM_LOCKDOOR"); */
1276 }
1277
1278 return 0;
1279 }
1280
1281 static BlockDriver bdrv_host_cdrom = {
1282 .format_name = "host_cdrom",
1283 .instance_size = sizeof(BDRVRawState),
1284 .bdrv_probe_device = cdrom_probe_device,
1285 .bdrv_open = cdrom_open,
1286 .bdrv_close = raw_close,
1287 .bdrv_create = hdev_create,
1288 .bdrv_flush = raw_flush,
1289
1290 .bdrv_aio_readv = raw_aio_readv,
1291 .bdrv_aio_writev = raw_aio_writev,
1292
1293 .bdrv_read = raw_read,
1294 .bdrv_write = raw_write,
1295 .bdrv_getlength = raw_getlength,
1296
1297 /* removable device support */
1298 .bdrv_is_inserted = cdrom_is_inserted,
1299 .bdrv_eject = cdrom_eject,
1300 .bdrv_set_locked = cdrom_set_locked,
1301 };
1302 #endif /* __FreeBSD__ */
1303
1304 static void bdrv_raw_init(void)
1305 {
1306 /*
1307 * Register all the drivers. Note that order is important, the driver
1308 * registered last will get probed first.
1309 */
1310 bdrv_register(&bdrv_raw);
1311 bdrv_register(&bdrv_host_device);
1312 #ifdef __linux__
1313 bdrv_register(&bdrv_host_floppy);
1314 bdrv_register(&bdrv_host_cdrom);
1315 #endif
1316 #ifdef __FreeBSD__
1317 bdrv_register(&bdrv_host_cdrom);
1318 #endif
1319 }
1320
1321 block_init(bdrv_raw_init);