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