]> git.proxmox.com Git - qemu.git/blob - block/raw-posix.c
gluster: Use bdrv_open options instead of filename
[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/log.h"
27 #include "block/block_int.h"
28 #include "qemu/module.h"
29 #include "trace.h"
30 #include "block/thread-pool.h"
31 #include "qemu/iov.h"
32 #include "raw-aio.h"
33
34 #if defined(__APPLE__) && (__MACH__)
35 #include <paths.h>
36 #include <sys/param.h>
37 #include <IOKit/IOKitLib.h>
38 #include <IOKit/IOBSD.h>
39 #include <IOKit/storage/IOMediaBSDClient.h>
40 #include <IOKit/storage/IOMedia.h>
41 #include <IOKit/storage/IOCDMedia.h>
42 //#include <IOKit/storage/IOCDTypes.h>
43 #include <CoreFoundation/CoreFoundation.h>
44 #endif
45
46 #ifdef __sun__
47 #define _POSIX_PTHREAD_SEMANTICS 1
48 #include <sys/dkio.h>
49 #endif
50 #ifdef __linux__
51 #include <sys/types.h>
52 #include <sys/stat.h>
53 #include <sys/ioctl.h>
54 #include <sys/param.h>
55 #include <linux/cdrom.h>
56 #include <linux/fd.h>
57 #include <linux/fs.h>
58 #endif
59 #ifdef CONFIG_FIEMAP
60 #include <linux/fiemap.h>
61 #endif
62 #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
63 #include <linux/falloc.h>
64 #endif
65 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
66 #include <sys/disk.h>
67 #include <sys/cdio.h>
68 #endif
69
70 #ifdef __OpenBSD__
71 #include <sys/ioctl.h>
72 #include <sys/disklabel.h>
73 #include <sys/dkio.h>
74 #endif
75
76 #ifdef __NetBSD__
77 #include <sys/ioctl.h>
78 #include <sys/disklabel.h>
79 #include <sys/dkio.h>
80 #include <sys/disk.h>
81 #endif
82
83 #ifdef __DragonFly__
84 #include <sys/ioctl.h>
85 #include <sys/diskslice.h>
86 #endif
87
88 #ifdef CONFIG_XFS
89 #include <xfs/xfs.h>
90 #endif
91
92 //#define DEBUG_FLOPPY
93
94 //#define DEBUG_BLOCK
95 #if defined(DEBUG_BLOCK)
96 #define DEBUG_BLOCK_PRINT(formatCstr, ...) do { if (qemu_log_enabled()) \
97 { qemu_log(formatCstr, ## __VA_ARGS__); qemu_log_flush(); } } while (0)
98 #else
99 #define DEBUG_BLOCK_PRINT(formatCstr, ...)
100 #endif
101
102 /* OS X does not have O_DSYNC */
103 #ifndef O_DSYNC
104 #ifdef O_SYNC
105 #define O_DSYNC O_SYNC
106 #elif defined(O_FSYNC)
107 #define O_DSYNC O_FSYNC
108 #endif
109 #endif
110
111 /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */
112 #ifndef O_DIRECT
113 #define O_DIRECT O_DSYNC
114 #endif
115
116 #define FTYPE_FILE 0
117 #define FTYPE_CD 1
118 #define FTYPE_FD 2
119
120 /* if the FD is not accessed during that time (in ns), we try to
121 reopen it to see if the disk has been changed */
122 #define FD_OPEN_TIMEOUT (1000000000)
123
124 #define MAX_BLOCKSIZE 4096
125
126 typedef struct BDRVRawState {
127 int fd;
128 int type;
129 int open_flags;
130 #if defined(__linux__)
131 /* linux floppy specific */
132 int64_t fd_open_time;
133 int64_t fd_error_time;
134 int fd_got_error;
135 int fd_media_changed;
136 #endif
137 #ifdef CONFIG_LINUX_AIO
138 int use_aio;
139 void *aio_ctx;
140 #endif
141 #ifdef CONFIG_XFS
142 bool is_xfs : 1;
143 #endif
144 bool has_discard : 1;
145 } BDRVRawState;
146
147 typedef struct BDRVRawReopenState {
148 int fd;
149 int open_flags;
150 #ifdef CONFIG_LINUX_AIO
151 int use_aio;
152 #endif
153 } BDRVRawReopenState;
154
155 static int fd_open(BlockDriverState *bs);
156 static int64_t raw_getlength(BlockDriverState *bs);
157
158 typedef struct RawPosixAIOData {
159 BlockDriverState *bs;
160 int aio_fildes;
161 union {
162 struct iovec *aio_iov;
163 void *aio_ioctl_buf;
164 };
165 int aio_niov;
166 uint64_t aio_nbytes;
167 #define aio_ioctl_cmd aio_nbytes /* for QEMU_AIO_IOCTL */
168 off_t aio_offset;
169 int aio_type;
170 } RawPosixAIOData;
171
172 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
173 static int cdrom_reopen(BlockDriverState *bs);
174 #endif
175
176 #if defined(__NetBSD__)
177 static int raw_normalize_devicepath(const char **filename)
178 {
179 static char namebuf[PATH_MAX];
180 const char *dp, *fname;
181 struct stat sb;
182
183 fname = *filename;
184 dp = strrchr(fname, '/');
185 if (lstat(fname, &sb) < 0) {
186 fprintf(stderr, "%s: stat failed: %s\n",
187 fname, strerror(errno));
188 return -errno;
189 }
190
191 if (!S_ISBLK(sb.st_mode)) {
192 return 0;
193 }
194
195 if (dp == NULL) {
196 snprintf(namebuf, PATH_MAX, "r%s", fname);
197 } else {
198 snprintf(namebuf, PATH_MAX, "%.*s/r%s",
199 (int)(dp - fname), fname, dp + 1);
200 }
201 fprintf(stderr, "%s is a block device", fname);
202 *filename = namebuf;
203 fprintf(stderr, ", using %s\n", *filename);
204
205 return 0;
206 }
207 #else
208 static int raw_normalize_devicepath(const char **filename)
209 {
210 return 0;
211 }
212 #endif
213
214 static void raw_parse_flags(int bdrv_flags, int *open_flags)
215 {
216 assert(open_flags != NULL);
217
218 *open_flags |= O_BINARY;
219 *open_flags &= ~O_ACCMODE;
220 if (bdrv_flags & BDRV_O_RDWR) {
221 *open_flags |= O_RDWR;
222 } else {
223 *open_flags |= O_RDONLY;
224 }
225
226 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
227 * and O_DIRECT for no caching. */
228 if ((bdrv_flags & BDRV_O_NOCACHE)) {
229 *open_flags |= O_DIRECT;
230 }
231 }
232
233 #ifdef CONFIG_LINUX_AIO
234 static int raw_set_aio(void **aio_ctx, int *use_aio, int bdrv_flags)
235 {
236 int ret = -1;
237 assert(aio_ctx != NULL);
238 assert(use_aio != NULL);
239 /*
240 * Currently Linux do AIO only for files opened with O_DIRECT
241 * specified so check NOCACHE flag too
242 */
243 if ((bdrv_flags & (BDRV_O_NOCACHE|BDRV_O_NATIVE_AIO)) ==
244 (BDRV_O_NOCACHE|BDRV_O_NATIVE_AIO)) {
245
246 /* if non-NULL, laio_init() has already been run */
247 if (*aio_ctx == NULL) {
248 *aio_ctx = laio_init();
249 if (!*aio_ctx) {
250 goto error;
251 }
252 }
253 *use_aio = 1;
254 } else {
255 *use_aio = 0;
256 }
257
258 ret = 0;
259
260 error:
261 return ret;
262 }
263 #endif
264
265 static QemuOptsList raw_runtime_opts = {
266 .name = "raw",
267 .head = QTAILQ_HEAD_INITIALIZER(raw_runtime_opts.head),
268 .desc = {
269 {
270 .name = "filename",
271 .type = QEMU_OPT_STRING,
272 .help = "File name of the image",
273 },
274 { /* end of list */ }
275 },
276 };
277
278 static int raw_open_common(BlockDriverState *bs, QDict *options,
279 int bdrv_flags, int open_flags)
280 {
281 BDRVRawState *s = bs->opaque;
282 QemuOpts *opts;
283 Error *local_err = NULL;
284 const char *filename;
285 int fd, ret;
286
287 opts = qemu_opts_create_nofail(&raw_runtime_opts);
288 qemu_opts_absorb_qdict(opts, options, &local_err);
289 if (error_is_set(&local_err)) {
290 qerror_report_err(local_err);
291 error_free(local_err);
292 ret = -EINVAL;
293 goto fail;
294 }
295
296 filename = qemu_opt_get(opts, "filename");
297
298 ret = raw_normalize_devicepath(&filename);
299 if (ret != 0) {
300 goto fail;
301 }
302
303 s->open_flags = open_flags;
304 raw_parse_flags(bdrv_flags, &s->open_flags);
305
306 s->fd = -1;
307 fd = qemu_open(filename, s->open_flags, 0644);
308 if (fd < 0) {
309 ret = -errno;
310 if (ret == -EROFS) {
311 ret = -EACCES;
312 }
313 goto fail;
314 }
315 s->fd = fd;
316
317 #ifdef CONFIG_LINUX_AIO
318 if (raw_set_aio(&s->aio_ctx, &s->use_aio, bdrv_flags)) {
319 qemu_close(fd);
320 ret = -errno;
321 goto fail;
322 }
323 #endif
324
325 s->has_discard = 1;
326 #ifdef CONFIG_XFS
327 if (platform_test_xfs_fd(s->fd)) {
328 s->is_xfs = 1;
329 }
330 #endif
331
332 ret = 0;
333 fail:
334 qemu_opts_del(opts);
335 return ret;
336 }
337
338 static int raw_open(BlockDriverState *bs, const char *filename,
339 QDict *options, int flags)
340 {
341 BDRVRawState *s = bs->opaque;
342
343 s->type = FTYPE_FILE;
344 return raw_open_common(bs, options, flags, 0);
345 }
346
347 static int raw_reopen_prepare(BDRVReopenState *state,
348 BlockReopenQueue *queue, Error **errp)
349 {
350 BDRVRawState *s;
351 BDRVRawReopenState *raw_s;
352 int ret = 0;
353
354 assert(state != NULL);
355 assert(state->bs != NULL);
356
357 s = state->bs->opaque;
358
359 state->opaque = g_malloc0(sizeof(BDRVRawReopenState));
360 raw_s = state->opaque;
361
362 #ifdef CONFIG_LINUX_AIO
363 raw_s->use_aio = s->use_aio;
364
365 /* we can use s->aio_ctx instead of a copy, because the use_aio flag is
366 * valid in the 'false' condition even if aio_ctx is set, and raw_set_aio()
367 * won't override aio_ctx if aio_ctx is non-NULL */
368 if (raw_set_aio(&s->aio_ctx, &raw_s->use_aio, state->flags)) {
369 return -1;
370 }
371 #endif
372
373 if (s->type == FTYPE_FD || s->type == FTYPE_CD) {
374 raw_s->open_flags |= O_NONBLOCK;
375 }
376
377 raw_parse_flags(state->flags, &raw_s->open_flags);
378
379 raw_s->fd = -1;
380
381 int fcntl_flags = O_APPEND | O_NONBLOCK;
382 #ifdef O_NOATIME
383 fcntl_flags |= O_NOATIME;
384 #endif
385
386 #ifdef O_ASYNC
387 /* Not all operating systems have O_ASYNC, and those that don't
388 * will not let us track the state into raw_s->open_flags (typically
389 * you achieve the same effect with an ioctl, for example I_SETSIG
390 * on Solaris). But we do not use O_ASYNC, so that's fine.
391 */
392 assert((s->open_flags & O_ASYNC) == 0);
393 #endif
394
395 if ((raw_s->open_flags & ~fcntl_flags) == (s->open_flags & ~fcntl_flags)) {
396 /* dup the original fd */
397 /* TODO: use qemu fcntl wrapper */
398 #ifdef F_DUPFD_CLOEXEC
399 raw_s->fd = fcntl(s->fd, F_DUPFD_CLOEXEC, 0);
400 #else
401 raw_s->fd = dup(s->fd);
402 if (raw_s->fd != -1) {
403 qemu_set_cloexec(raw_s->fd);
404 }
405 #endif
406 if (raw_s->fd >= 0) {
407 ret = fcntl_setfl(raw_s->fd, raw_s->open_flags);
408 if (ret) {
409 qemu_close(raw_s->fd);
410 raw_s->fd = -1;
411 }
412 }
413 }
414
415 /* If we cannot use fcntl, or fcntl failed, fall back to qemu_open() */
416 if (raw_s->fd == -1) {
417 assert(!(raw_s->open_flags & O_CREAT));
418 raw_s->fd = qemu_open(state->bs->filename, raw_s->open_flags);
419 if (raw_s->fd == -1) {
420 ret = -1;
421 }
422 }
423 return ret;
424 }
425
426
427 static void raw_reopen_commit(BDRVReopenState *state)
428 {
429 BDRVRawReopenState *raw_s = state->opaque;
430 BDRVRawState *s = state->bs->opaque;
431
432 s->open_flags = raw_s->open_flags;
433
434 qemu_close(s->fd);
435 s->fd = raw_s->fd;
436 #ifdef CONFIG_LINUX_AIO
437 s->use_aio = raw_s->use_aio;
438 #endif
439
440 g_free(state->opaque);
441 state->opaque = NULL;
442 }
443
444
445 static void raw_reopen_abort(BDRVReopenState *state)
446 {
447 BDRVRawReopenState *raw_s = state->opaque;
448
449 /* nothing to do if NULL, we didn't get far enough */
450 if (raw_s == NULL) {
451 return;
452 }
453
454 if (raw_s->fd >= 0) {
455 qemu_close(raw_s->fd);
456 raw_s->fd = -1;
457 }
458 g_free(state->opaque);
459 state->opaque = NULL;
460 }
461
462
463 /* XXX: use host sector size if necessary with:
464 #ifdef DIOCGSECTORSIZE
465 {
466 unsigned int sectorsize = 512;
467 if (!ioctl(fd, DIOCGSECTORSIZE, &sectorsize) &&
468 sectorsize > bufsize)
469 bufsize = sectorsize;
470 }
471 #endif
472 #ifdef CONFIG_COCOA
473 uint32_t blockSize = 512;
474 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
475 bufsize = blockSize;
476 }
477 #endif
478 */
479
480 static ssize_t handle_aiocb_ioctl(RawPosixAIOData *aiocb)
481 {
482 int ret;
483
484 ret = ioctl(aiocb->aio_fildes, aiocb->aio_ioctl_cmd, aiocb->aio_ioctl_buf);
485 if (ret == -1) {
486 return -errno;
487 }
488
489 return 0;
490 }
491
492 static ssize_t handle_aiocb_flush(RawPosixAIOData *aiocb)
493 {
494 int ret;
495
496 ret = qemu_fdatasync(aiocb->aio_fildes);
497 if (ret == -1) {
498 return -errno;
499 }
500 return 0;
501 }
502
503 #ifdef CONFIG_PREADV
504
505 static bool preadv_present = true;
506
507 static ssize_t
508 qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset)
509 {
510 return preadv(fd, iov, nr_iov, offset);
511 }
512
513 static ssize_t
514 qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset)
515 {
516 return pwritev(fd, iov, nr_iov, offset);
517 }
518
519 #else
520
521 static bool preadv_present = false;
522
523 static ssize_t
524 qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset)
525 {
526 return -ENOSYS;
527 }
528
529 static ssize_t
530 qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset)
531 {
532 return -ENOSYS;
533 }
534
535 #endif
536
537 static ssize_t handle_aiocb_rw_vector(RawPosixAIOData *aiocb)
538 {
539 ssize_t len;
540
541 do {
542 if (aiocb->aio_type & QEMU_AIO_WRITE)
543 len = qemu_pwritev(aiocb->aio_fildes,
544 aiocb->aio_iov,
545 aiocb->aio_niov,
546 aiocb->aio_offset);
547 else
548 len = qemu_preadv(aiocb->aio_fildes,
549 aiocb->aio_iov,
550 aiocb->aio_niov,
551 aiocb->aio_offset);
552 } while (len == -1 && errno == EINTR);
553
554 if (len == -1) {
555 return -errno;
556 }
557 return len;
558 }
559
560 /*
561 * Read/writes the data to/from a given linear buffer.
562 *
563 * Returns the number of bytes handles or -errno in case of an error. Short
564 * reads are only returned if the end of the file is reached.
565 */
566 static ssize_t handle_aiocb_rw_linear(RawPosixAIOData *aiocb, char *buf)
567 {
568 ssize_t offset = 0;
569 ssize_t len;
570
571 while (offset < aiocb->aio_nbytes) {
572 if (aiocb->aio_type & QEMU_AIO_WRITE) {
573 len = pwrite(aiocb->aio_fildes,
574 (const char *)buf + offset,
575 aiocb->aio_nbytes - offset,
576 aiocb->aio_offset + offset);
577 } else {
578 len = pread(aiocb->aio_fildes,
579 buf + offset,
580 aiocb->aio_nbytes - offset,
581 aiocb->aio_offset + offset);
582 }
583 if (len == -1 && errno == EINTR) {
584 continue;
585 } else if (len == -1) {
586 offset = -errno;
587 break;
588 } else if (len == 0) {
589 break;
590 }
591 offset += len;
592 }
593
594 return offset;
595 }
596
597 static ssize_t handle_aiocb_rw(RawPosixAIOData *aiocb)
598 {
599 ssize_t nbytes;
600 char *buf;
601
602 if (!(aiocb->aio_type & QEMU_AIO_MISALIGNED)) {
603 /*
604 * If there is just a single buffer, and it is properly aligned
605 * we can just use plain pread/pwrite without any problems.
606 */
607 if (aiocb->aio_niov == 1) {
608 return handle_aiocb_rw_linear(aiocb, aiocb->aio_iov->iov_base);
609 }
610 /*
611 * We have more than one iovec, and all are properly aligned.
612 *
613 * Try preadv/pwritev first and fall back to linearizing the
614 * buffer if it's not supported.
615 */
616 if (preadv_present) {
617 nbytes = handle_aiocb_rw_vector(aiocb);
618 if (nbytes == aiocb->aio_nbytes ||
619 (nbytes < 0 && nbytes != -ENOSYS)) {
620 return nbytes;
621 }
622 preadv_present = false;
623 }
624
625 /*
626 * XXX(hch): short read/write. no easy way to handle the reminder
627 * using these interfaces. For now retry using plain
628 * pread/pwrite?
629 */
630 }
631
632 /*
633 * Ok, we have to do it the hard way, copy all segments into
634 * a single aligned buffer.
635 */
636 buf = qemu_blockalign(aiocb->bs, aiocb->aio_nbytes);
637 if (aiocb->aio_type & QEMU_AIO_WRITE) {
638 char *p = buf;
639 int i;
640
641 for (i = 0; i < aiocb->aio_niov; ++i) {
642 memcpy(p, aiocb->aio_iov[i].iov_base, aiocb->aio_iov[i].iov_len);
643 p += aiocb->aio_iov[i].iov_len;
644 }
645 }
646
647 nbytes = handle_aiocb_rw_linear(aiocb, buf);
648 if (!(aiocb->aio_type & QEMU_AIO_WRITE)) {
649 char *p = buf;
650 size_t count = aiocb->aio_nbytes, copy;
651 int i;
652
653 for (i = 0; i < aiocb->aio_niov && count; ++i) {
654 copy = count;
655 if (copy > aiocb->aio_iov[i].iov_len) {
656 copy = aiocb->aio_iov[i].iov_len;
657 }
658 memcpy(aiocb->aio_iov[i].iov_base, p, copy);
659 p += copy;
660 count -= copy;
661 }
662 }
663 qemu_vfree(buf);
664
665 return nbytes;
666 }
667
668 #ifdef CONFIG_XFS
669 static int xfs_discard(BDRVRawState *s, int64_t offset, uint64_t bytes)
670 {
671 struct xfs_flock64 fl;
672
673 memset(&fl, 0, sizeof(fl));
674 fl.l_whence = SEEK_SET;
675 fl.l_start = offset;
676 fl.l_len = bytes;
677
678 if (xfsctl(NULL, s->fd, XFS_IOC_UNRESVSP64, &fl) < 0) {
679 DEBUG_BLOCK_PRINT("cannot punch hole (%s)\n", strerror(errno));
680 return -errno;
681 }
682
683 return 0;
684 }
685 #endif
686
687 static ssize_t handle_aiocb_discard(RawPosixAIOData *aiocb)
688 {
689 int ret = -EOPNOTSUPP;
690 BDRVRawState *s = aiocb->bs->opaque;
691
692 if (s->has_discard == 0) {
693 return 0;
694 }
695
696 if (aiocb->aio_type & QEMU_AIO_BLKDEV) {
697 #ifdef BLKDISCARD
698 do {
699 uint64_t range[2] = { aiocb->aio_offset, aiocb->aio_nbytes };
700 if (ioctl(aiocb->aio_fildes, BLKDISCARD, range) == 0) {
701 return 0;
702 }
703 } while (errno == EINTR);
704
705 ret = -errno;
706 #endif
707 } else {
708 #ifdef CONFIG_XFS
709 if (s->is_xfs) {
710 return xfs_discard(s, aiocb->aio_offset, aiocb->aio_nbytes);
711 }
712 #endif
713
714 #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
715 do {
716 if (fallocate(s->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
717 aiocb->aio_offset, aiocb->aio_nbytes) == 0) {
718 return 0;
719 }
720 } while (errno == EINTR);
721
722 ret = -errno;
723 #endif
724 }
725
726 if (ret == -ENODEV || ret == -ENOSYS || ret == -EOPNOTSUPP ||
727 ret == -ENOTTY) {
728 s->has_discard = 0;
729 ret = 0;
730 }
731 return ret;
732 }
733
734 static int aio_worker(void *arg)
735 {
736 RawPosixAIOData *aiocb = arg;
737 ssize_t ret = 0;
738
739 switch (aiocb->aio_type & QEMU_AIO_TYPE_MASK) {
740 case QEMU_AIO_READ:
741 ret = handle_aiocb_rw(aiocb);
742 if (ret >= 0 && ret < aiocb->aio_nbytes && aiocb->bs->growable) {
743 iov_memset(aiocb->aio_iov, aiocb->aio_niov, ret,
744 0, aiocb->aio_nbytes - ret);
745
746 ret = aiocb->aio_nbytes;
747 }
748 if (ret == aiocb->aio_nbytes) {
749 ret = 0;
750 } else if (ret >= 0 && ret < aiocb->aio_nbytes) {
751 ret = -EINVAL;
752 }
753 break;
754 case QEMU_AIO_WRITE:
755 ret = handle_aiocb_rw(aiocb);
756 if (ret == aiocb->aio_nbytes) {
757 ret = 0;
758 } else if (ret >= 0 && ret < aiocb->aio_nbytes) {
759 ret = -EINVAL;
760 }
761 break;
762 case QEMU_AIO_FLUSH:
763 ret = handle_aiocb_flush(aiocb);
764 break;
765 case QEMU_AIO_IOCTL:
766 ret = handle_aiocb_ioctl(aiocb);
767 break;
768 case QEMU_AIO_DISCARD:
769 ret = handle_aiocb_discard(aiocb);
770 break;
771 default:
772 fprintf(stderr, "invalid aio request (0x%x)\n", aiocb->aio_type);
773 ret = -EINVAL;
774 break;
775 }
776
777 g_slice_free(RawPosixAIOData, aiocb);
778 return ret;
779 }
780
781 static BlockDriverAIOCB *paio_submit(BlockDriverState *bs, int fd,
782 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
783 BlockDriverCompletionFunc *cb, void *opaque, int type)
784 {
785 RawPosixAIOData *acb = g_slice_new(RawPosixAIOData);
786 ThreadPool *pool;
787
788 acb->bs = bs;
789 acb->aio_type = type;
790 acb->aio_fildes = fd;
791
792 if (qiov) {
793 acb->aio_iov = qiov->iov;
794 acb->aio_niov = qiov->niov;
795 }
796 acb->aio_nbytes = nb_sectors * 512;
797 acb->aio_offset = sector_num * 512;
798
799 trace_paio_submit(acb, opaque, sector_num, nb_sectors, type);
800 pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
801 return thread_pool_submit_aio(pool, aio_worker, acb, cb, opaque);
802 }
803
804 static BlockDriverAIOCB *raw_aio_submit(BlockDriverState *bs,
805 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
806 BlockDriverCompletionFunc *cb, void *opaque, int type)
807 {
808 BDRVRawState *s = bs->opaque;
809
810 if (fd_open(bs) < 0)
811 return NULL;
812
813 /*
814 * If O_DIRECT is used the buffer needs to be aligned on a sector
815 * boundary. Check if this is the case or tell the low-level
816 * driver that it needs to copy the buffer.
817 */
818 if ((bs->open_flags & BDRV_O_NOCACHE)) {
819 if (!bdrv_qiov_is_aligned(bs, qiov)) {
820 type |= QEMU_AIO_MISALIGNED;
821 #ifdef CONFIG_LINUX_AIO
822 } else if (s->use_aio) {
823 return laio_submit(bs, s->aio_ctx, s->fd, sector_num, qiov,
824 nb_sectors, cb, opaque, type);
825 #endif
826 }
827 }
828
829 return paio_submit(bs, s->fd, sector_num, qiov, nb_sectors,
830 cb, opaque, type);
831 }
832
833 static BlockDriverAIOCB *raw_aio_readv(BlockDriverState *bs,
834 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
835 BlockDriverCompletionFunc *cb, void *opaque)
836 {
837 return raw_aio_submit(bs, sector_num, qiov, nb_sectors,
838 cb, opaque, QEMU_AIO_READ);
839 }
840
841 static BlockDriverAIOCB *raw_aio_writev(BlockDriverState *bs,
842 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
843 BlockDriverCompletionFunc *cb, void *opaque)
844 {
845 return raw_aio_submit(bs, sector_num, qiov, nb_sectors,
846 cb, opaque, QEMU_AIO_WRITE);
847 }
848
849 static BlockDriverAIOCB *raw_aio_flush(BlockDriverState *bs,
850 BlockDriverCompletionFunc *cb, void *opaque)
851 {
852 BDRVRawState *s = bs->opaque;
853
854 if (fd_open(bs) < 0)
855 return NULL;
856
857 return paio_submit(bs, s->fd, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH);
858 }
859
860 static void raw_close(BlockDriverState *bs)
861 {
862 BDRVRawState *s = bs->opaque;
863 if (s->fd >= 0) {
864 qemu_close(s->fd);
865 s->fd = -1;
866 }
867 }
868
869 static int raw_truncate(BlockDriverState *bs, int64_t offset)
870 {
871 BDRVRawState *s = bs->opaque;
872 struct stat st;
873
874 if (fstat(s->fd, &st)) {
875 return -errno;
876 }
877
878 if (S_ISREG(st.st_mode)) {
879 if (ftruncate(s->fd, offset) < 0) {
880 return -errno;
881 }
882 } else if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
883 if (offset > raw_getlength(bs)) {
884 return -EINVAL;
885 }
886 } else {
887 return -ENOTSUP;
888 }
889
890 return 0;
891 }
892
893 #ifdef __OpenBSD__
894 static int64_t raw_getlength(BlockDriverState *bs)
895 {
896 BDRVRawState *s = bs->opaque;
897 int fd = s->fd;
898 struct stat st;
899
900 if (fstat(fd, &st))
901 return -1;
902 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
903 struct disklabel dl;
904
905 if (ioctl(fd, DIOCGDINFO, &dl))
906 return -1;
907 return (uint64_t)dl.d_secsize *
908 dl.d_partitions[DISKPART(st.st_rdev)].p_size;
909 } else
910 return st.st_size;
911 }
912 #elif defined(__NetBSD__)
913 static int64_t raw_getlength(BlockDriverState *bs)
914 {
915 BDRVRawState *s = bs->opaque;
916 int fd = s->fd;
917 struct stat st;
918
919 if (fstat(fd, &st))
920 return -1;
921 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
922 struct dkwedge_info dkw;
923
924 if (ioctl(fd, DIOCGWEDGEINFO, &dkw) != -1) {
925 return dkw.dkw_size * 512;
926 } else {
927 struct disklabel dl;
928
929 if (ioctl(fd, DIOCGDINFO, &dl))
930 return -1;
931 return (uint64_t)dl.d_secsize *
932 dl.d_partitions[DISKPART(st.st_rdev)].p_size;
933 }
934 } else
935 return st.st_size;
936 }
937 #elif defined(__sun__)
938 static int64_t raw_getlength(BlockDriverState *bs)
939 {
940 BDRVRawState *s = bs->opaque;
941 struct dk_minfo minfo;
942 int ret;
943
944 ret = fd_open(bs);
945 if (ret < 0) {
946 return ret;
947 }
948
949 /*
950 * Use the DKIOCGMEDIAINFO ioctl to read the size.
951 */
952 ret = ioctl(s->fd, DKIOCGMEDIAINFO, &minfo);
953 if (ret != -1) {
954 return minfo.dki_lbsize * minfo.dki_capacity;
955 }
956
957 /*
958 * There are reports that lseek on some devices fails, but
959 * irc discussion said that contingency on contingency was overkill.
960 */
961 return lseek(s->fd, 0, SEEK_END);
962 }
963 #elif defined(CONFIG_BSD)
964 static int64_t raw_getlength(BlockDriverState *bs)
965 {
966 BDRVRawState *s = bs->opaque;
967 int fd = s->fd;
968 int64_t size;
969 struct stat sb;
970 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
971 int reopened = 0;
972 #endif
973 int ret;
974
975 ret = fd_open(bs);
976 if (ret < 0)
977 return ret;
978
979 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
980 again:
981 #endif
982 if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
983 #ifdef DIOCGMEDIASIZE
984 if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
985 #elif defined(DIOCGPART)
986 {
987 struct partinfo pi;
988 if (ioctl(fd, DIOCGPART, &pi) == 0)
989 size = pi.media_size;
990 else
991 size = 0;
992 }
993 if (size == 0)
994 #endif
995 #if defined(__APPLE__) && defined(__MACH__)
996 size = LONG_LONG_MAX;
997 #else
998 size = lseek(fd, 0LL, SEEK_END);
999 #endif
1000 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1001 switch(s->type) {
1002 case FTYPE_CD:
1003 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
1004 if (size == 2048LL * (unsigned)-1)
1005 size = 0;
1006 /* XXX no disc? maybe we need to reopen... */
1007 if (size <= 0 && !reopened && cdrom_reopen(bs) >= 0) {
1008 reopened = 1;
1009 goto again;
1010 }
1011 }
1012 #endif
1013 } else {
1014 size = lseek(fd, 0, SEEK_END);
1015 }
1016 return size;
1017 }
1018 #else
1019 static int64_t raw_getlength(BlockDriverState *bs)
1020 {
1021 BDRVRawState *s = bs->opaque;
1022 int ret;
1023
1024 ret = fd_open(bs);
1025 if (ret < 0) {
1026 return ret;
1027 }
1028
1029 return lseek(s->fd, 0, SEEK_END);
1030 }
1031 #endif
1032
1033 static int64_t raw_get_allocated_file_size(BlockDriverState *bs)
1034 {
1035 struct stat st;
1036 BDRVRawState *s = bs->opaque;
1037
1038 if (fstat(s->fd, &st) < 0) {
1039 return -errno;
1040 }
1041 return (int64_t)st.st_blocks * 512;
1042 }
1043
1044 static int raw_create(const char *filename, QEMUOptionParameter *options)
1045 {
1046 int fd;
1047 int result = 0;
1048 int64_t total_size = 0;
1049
1050 /* Read out options */
1051 while (options && options->name) {
1052 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
1053 total_size = options->value.n / BDRV_SECTOR_SIZE;
1054 }
1055 options++;
1056 }
1057
1058 fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
1059 0644);
1060 if (fd < 0) {
1061 result = -errno;
1062 } else {
1063 if (ftruncate(fd, total_size * BDRV_SECTOR_SIZE) != 0) {
1064 result = -errno;
1065 }
1066 if (qemu_close(fd) != 0) {
1067 result = -errno;
1068 }
1069 }
1070 return result;
1071 }
1072
1073 /*
1074 * Returns true iff the specified sector is present in the disk image. Drivers
1075 * not implementing the functionality are assumed to not support backing files,
1076 * hence all their sectors are reported as allocated.
1077 *
1078 * If 'sector_num' is beyond the end of the disk image the return value is 0
1079 * and 'pnum' is set to 0.
1080 *
1081 * 'pnum' is set to the number of sectors (including and immediately following
1082 * the specified sector) that are known to be in the same
1083 * allocated/unallocated state.
1084 *
1085 * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes
1086 * beyond the end of the disk image it will be clamped.
1087 */
1088 static int coroutine_fn raw_co_is_allocated(BlockDriverState *bs,
1089 int64_t sector_num,
1090 int nb_sectors, int *pnum)
1091 {
1092 off_t start, data, hole;
1093 int ret;
1094
1095 ret = fd_open(bs);
1096 if (ret < 0) {
1097 return ret;
1098 }
1099
1100 start = sector_num * BDRV_SECTOR_SIZE;
1101
1102 #ifdef CONFIG_FIEMAP
1103
1104 BDRVRawState *s = bs->opaque;
1105 struct {
1106 struct fiemap fm;
1107 struct fiemap_extent fe;
1108 } f;
1109
1110 f.fm.fm_start = start;
1111 f.fm.fm_length = (int64_t)nb_sectors * BDRV_SECTOR_SIZE;
1112 f.fm.fm_flags = 0;
1113 f.fm.fm_extent_count = 1;
1114 f.fm.fm_reserved = 0;
1115 if (ioctl(s->fd, FS_IOC_FIEMAP, &f) == -1) {
1116 /* Assume everything is allocated. */
1117 *pnum = nb_sectors;
1118 return 1;
1119 }
1120
1121 if (f.fm.fm_mapped_extents == 0) {
1122 /* No extents found, data is beyond f.fm.fm_start + f.fm.fm_length.
1123 * f.fm.fm_start + f.fm.fm_length must be clamped to the file size!
1124 */
1125 off_t length = lseek(s->fd, 0, SEEK_END);
1126 hole = f.fm.fm_start;
1127 data = MIN(f.fm.fm_start + f.fm.fm_length, length);
1128 } else {
1129 data = f.fe.fe_logical;
1130 hole = f.fe.fe_logical + f.fe.fe_length;
1131 }
1132
1133 #elif defined SEEK_HOLE && defined SEEK_DATA
1134
1135 BDRVRawState *s = bs->opaque;
1136
1137 hole = lseek(s->fd, start, SEEK_HOLE);
1138 if (hole == -1) {
1139 /* -ENXIO indicates that sector_num was past the end of the file.
1140 * There is a virtual hole there. */
1141 assert(errno != -ENXIO);
1142
1143 /* Most likely EINVAL. Assume everything is allocated. */
1144 *pnum = nb_sectors;
1145 return 1;
1146 }
1147
1148 if (hole > start) {
1149 data = start;
1150 } else {
1151 /* On a hole. We need another syscall to find its end. */
1152 data = lseek(s->fd, start, SEEK_DATA);
1153 if (data == -1) {
1154 data = lseek(s->fd, 0, SEEK_END);
1155 }
1156 }
1157 #else
1158 *pnum = nb_sectors;
1159 return 1;
1160 #endif
1161
1162 if (data <= start) {
1163 /* On a data extent, compute sectors to the end of the extent. */
1164 *pnum = MIN(nb_sectors, (hole - start) / BDRV_SECTOR_SIZE);
1165 return 1;
1166 } else {
1167 /* On a hole, compute sectors to the beginning of the next extent. */
1168 *pnum = MIN(nb_sectors, (data - start) / BDRV_SECTOR_SIZE);
1169 return 0;
1170 }
1171 }
1172
1173 static coroutine_fn BlockDriverAIOCB *raw_aio_discard(BlockDriverState *bs,
1174 int64_t sector_num, int nb_sectors,
1175 BlockDriverCompletionFunc *cb, void *opaque)
1176 {
1177 BDRVRawState *s = bs->opaque;
1178
1179 return paio_submit(bs, s->fd, sector_num, NULL, nb_sectors,
1180 cb, opaque, QEMU_AIO_DISCARD);
1181 }
1182
1183 static QEMUOptionParameter raw_create_options[] = {
1184 {
1185 .name = BLOCK_OPT_SIZE,
1186 .type = OPT_SIZE,
1187 .help = "Virtual disk size"
1188 },
1189 { NULL }
1190 };
1191
1192 static BlockDriver bdrv_file = {
1193 .format_name = "file",
1194 .protocol_name = "file",
1195 .instance_size = sizeof(BDRVRawState),
1196 .bdrv_probe = NULL, /* no probe for protocols */
1197 .bdrv_file_open = raw_open,
1198 .bdrv_reopen_prepare = raw_reopen_prepare,
1199 .bdrv_reopen_commit = raw_reopen_commit,
1200 .bdrv_reopen_abort = raw_reopen_abort,
1201 .bdrv_close = raw_close,
1202 .bdrv_create = raw_create,
1203 .bdrv_co_is_allocated = raw_co_is_allocated,
1204
1205 .bdrv_aio_readv = raw_aio_readv,
1206 .bdrv_aio_writev = raw_aio_writev,
1207 .bdrv_aio_flush = raw_aio_flush,
1208 .bdrv_aio_discard = raw_aio_discard,
1209
1210 .bdrv_truncate = raw_truncate,
1211 .bdrv_getlength = raw_getlength,
1212 .bdrv_get_allocated_file_size
1213 = raw_get_allocated_file_size,
1214
1215 .create_options = raw_create_options,
1216 };
1217
1218 /***********************************************/
1219 /* host device */
1220
1221 #if defined(__APPLE__) && defined(__MACH__)
1222 static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
1223 static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
1224
1225 kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
1226 {
1227 kern_return_t kernResult;
1228 mach_port_t masterPort;
1229 CFMutableDictionaryRef classesToMatch;
1230
1231 kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
1232 if ( KERN_SUCCESS != kernResult ) {
1233 printf( "IOMasterPort returned %d\n", kernResult );
1234 }
1235
1236 classesToMatch = IOServiceMatching( kIOCDMediaClass );
1237 if ( classesToMatch == NULL ) {
1238 printf( "IOServiceMatching returned a NULL dictionary.\n" );
1239 } else {
1240 CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
1241 }
1242 kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
1243 if ( KERN_SUCCESS != kernResult )
1244 {
1245 printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
1246 }
1247
1248 return kernResult;
1249 }
1250
1251 kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
1252 {
1253 io_object_t nextMedia;
1254 kern_return_t kernResult = KERN_FAILURE;
1255 *bsdPath = '\0';
1256 nextMedia = IOIteratorNext( mediaIterator );
1257 if ( nextMedia )
1258 {
1259 CFTypeRef bsdPathAsCFString;
1260 bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
1261 if ( bsdPathAsCFString ) {
1262 size_t devPathLength;
1263 strcpy( bsdPath, _PATH_DEV );
1264 strcat( bsdPath, "r" );
1265 devPathLength = strlen( bsdPath );
1266 if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
1267 kernResult = KERN_SUCCESS;
1268 }
1269 CFRelease( bsdPathAsCFString );
1270 }
1271 IOObjectRelease( nextMedia );
1272 }
1273
1274 return kernResult;
1275 }
1276
1277 #endif
1278
1279 static int hdev_probe_device(const char *filename)
1280 {
1281 struct stat st;
1282
1283 /* allow a dedicated CD-ROM driver to match with a higher priority */
1284 if (strstart(filename, "/dev/cdrom", NULL))
1285 return 50;
1286
1287 if (stat(filename, &st) >= 0 &&
1288 (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
1289 return 100;
1290 }
1291
1292 return 0;
1293 }
1294
1295 static int check_hdev_writable(BDRVRawState *s)
1296 {
1297 #if defined(BLKROGET)
1298 /* Linux block devices can be configured "read-only" using blockdev(8).
1299 * This is independent of device node permissions and therefore open(2)
1300 * with O_RDWR succeeds. Actual writes fail with EPERM.
1301 *
1302 * bdrv_open() is supposed to fail if the disk is read-only. Explicitly
1303 * check for read-only block devices so that Linux block devices behave
1304 * properly.
1305 */
1306 struct stat st;
1307 int readonly = 0;
1308
1309 if (fstat(s->fd, &st)) {
1310 return -errno;
1311 }
1312
1313 if (!S_ISBLK(st.st_mode)) {
1314 return 0;
1315 }
1316
1317 if (ioctl(s->fd, BLKROGET, &readonly) < 0) {
1318 return -errno;
1319 }
1320
1321 if (readonly) {
1322 return -EACCES;
1323 }
1324 #endif /* defined(BLKROGET) */
1325 return 0;
1326 }
1327
1328 static int hdev_open(BlockDriverState *bs, const char *dummy,
1329 QDict *options, int flags)
1330 {
1331 BDRVRawState *s = bs->opaque;
1332 int ret;
1333 const char *filename = qdict_get_str(options, "filename");
1334
1335 #if defined(__APPLE__) && defined(__MACH__)
1336 if (strstart(filename, "/dev/cdrom", NULL)) {
1337 kern_return_t kernResult;
1338 io_iterator_t mediaIterator;
1339 char bsdPath[ MAXPATHLEN ];
1340 int fd;
1341
1342 kernResult = FindEjectableCDMedia( &mediaIterator );
1343 kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
1344
1345 if ( bsdPath[ 0 ] != '\0' ) {
1346 strcat(bsdPath,"s0");
1347 /* some CDs don't have a partition 0 */
1348 fd = qemu_open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
1349 if (fd < 0) {
1350 bsdPath[strlen(bsdPath)-1] = '1';
1351 } else {
1352 qemu_close(fd);
1353 }
1354 filename = bsdPath;
1355 }
1356
1357 if ( mediaIterator )
1358 IOObjectRelease( mediaIterator );
1359 }
1360 #endif
1361
1362 s->type = FTYPE_FILE;
1363 #if defined(__linux__)
1364 {
1365 char resolved_path[ MAXPATHLEN ], *temp;
1366
1367 temp = realpath(filename, resolved_path);
1368 if (temp && strstart(temp, "/dev/sg", NULL)) {
1369 bs->sg = 1;
1370 }
1371 }
1372 #endif
1373
1374 ret = raw_open_common(bs, options, flags, 0);
1375 if (ret < 0) {
1376 return ret;
1377 }
1378
1379 if (flags & BDRV_O_RDWR) {
1380 ret = check_hdev_writable(s);
1381 if (ret < 0) {
1382 raw_close(bs);
1383 return ret;
1384 }
1385 }
1386
1387 return ret;
1388 }
1389
1390 #if defined(__linux__)
1391 /* Note: we do not have a reliable method to detect if the floppy is
1392 present. The current method is to try to open the floppy at every
1393 I/O and to keep it opened during a few hundreds of ms. */
1394 static int fd_open(BlockDriverState *bs)
1395 {
1396 BDRVRawState *s = bs->opaque;
1397 int last_media_present;
1398
1399 if (s->type != FTYPE_FD)
1400 return 0;
1401 last_media_present = (s->fd >= 0);
1402 if (s->fd >= 0 &&
1403 (get_clock() - s->fd_open_time) >= FD_OPEN_TIMEOUT) {
1404 qemu_close(s->fd);
1405 s->fd = -1;
1406 #ifdef DEBUG_FLOPPY
1407 printf("Floppy closed\n");
1408 #endif
1409 }
1410 if (s->fd < 0) {
1411 if (s->fd_got_error &&
1412 (get_clock() - s->fd_error_time) < FD_OPEN_TIMEOUT) {
1413 #ifdef DEBUG_FLOPPY
1414 printf("No floppy (open delayed)\n");
1415 #endif
1416 return -EIO;
1417 }
1418 s->fd = qemu_open(bs->filename, s->open_flags & ~O_NONBLOCK);
1419 if (s->fd < 0) {
1420 s->fd_error_time = get_clock();
1421 s->fd_got_error = 1;
1422 if (last_media_present)
1423 s->fd_media_changed = 1;
1424 #ifdef DEBUG_FLOPPY
1425 printf("No floppy\n");
1426 #endif
1427 return -EIO;
1428 }
1429 #ifdef DEBUG_FLOPPY
1430 printf("Floppy opened\n");
1431 #endif
1432 }
1433 if (!last_media_present)
1434 s->fd_media_changed = 1;
1435 s->fd_open_time = get_clock();
1436 s->fd_got_error = 0;
1437 return 0;
1438 }
1439
1440 static int hdev_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1441 {
1442 BDRVRawState *s = bs->opaque;
1443
1444 return ioctl(s->fd, req, buf);
1445 }
1446
1447 static BlockDriverAIOCB *hdev_aio_ioctl(BlockDriverState *bs,
1448 unsigned long int req, void *buf,
1449 BlockDriverCompletionFunc *cb, void *opaque)
1450 {
1451 BDRVRawState *s = bs->opaque;
1452 RawPosixAIOData *acb;
1453 ThreadPool *pool;
1454
1455 if (fd_open(bs) < 0)
1456 return NULL;
1457
1458 acb = g_slice_new(RawPosixAIOData);
1459 acb->bs = bs;
1460 acb->aio_type = QEMU_AIO_IOCTL;
1461 acb->aio_fildes = s->fd;
1462 acb->aio_offset = 0;
1463 acb->aio_ioctl_buf = buf;
1464 acb->aio_ioctl_cmd = req;
1465 pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
1466 return thread_pool_submit_aio(pool, aio_worker, acb, cb, opaque);
1467 }
1468
1469 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1470 static int fd_open(BlockDriverState *bs)
1471 {
1472 BDRVRawState *s = bs->opaque;
1473
1474 /* this is just to ensure s->fd is sane (its called by io ops) */
1475 if (s->fd >= 0)
1476 return 0;
1477 return -EIO;
1478 }
1479 #else /* !linux && !FreeBSD */
1480
1481 static int fd_open(BlockDriverState *bs)
1482 {
1483 return 0;
1484 }
1485
1486 #endif /* !linux && !FreeBSD */
1487
1488 static coroutine_fn BlockDriverAIOCB *hdev_aio_discard(BlockDriverState *bs,
1489 int64_t sector_num, int nb_sectors,
1490 BlockDriverCompletionFunc *cb, void *opaque)
1491 {
1492 BDRVRawState *s = bs->opaque;
1493
1494 if (fd_open(bs) < 0) {
1495 return NULL;
1496 }
1497 return paio_submit(bs, s->fd, sector_num, NULL, nb_sectors,
1498 cb, opaque, QEMU_AIO_DISCARD|QEMU_AIO_BLKDEV);
1499 }
1500
1501 static int hdev_create(const char *filename, QEMUOptionParameter *options)
1502 {
1503 int fd;
1504 int ret = 0;
1505 struct stat stat_buf;
1506 int64_t total_size = 0;
1507
1508 /* Read out options */
1509 while (options && options->name) {
1510 if (!strcmp(options->name, "size")) {
1511 total_size = options->value.n / BDRV_SECTOR_SIZE;
1512 }
1513 options++;
1514 }
1515
1516 fd = qemu_open(filename, O_WRONLY | O_BINARY);
1517 if (fd < 0)
1518 return -errno;
1519
1520 if (fstat(fd, &stat_buf) < 0)
1521 ret = -errno;
1522 else if (!S_ISBLK(stat_buf.st_mode) && !S_ISCHR(stat_buf.st_mode))
1523 ret = -ENODEV;
1524 else if (lseek(fd, 0, SEEK_END) < total_size * BDRV_SECTOR_SIZE)
1525 ret = -ENOSPC;
1526
1527 qemu_close(fd);
1528 return ret;
1529 }
1530
1531 static int hdev_has_zero_init(BlockDriverState *bs)
1532 {
1533 return 0;
1534 }
1535
1536 static BlockDriver bdrv_host_device = {
1537 .format_name = "host_device",
1538 .protocol_name = "host_device",
1539 .instance_size = sizeof(BDRVRawState),
1540 .bdrv_probe_device = hdev_probe_device,
1541 .bdrv_file_open = hdev_open,
1542 .bdrv_close = raw_close,
1543 .bdrv_reopen_prepare = raw_reopen_prepare,
1544 .bdrv_reopen_commit = raw_reopen_commit,
1545 .bdrv_reopen_abort = raw_reopen_abort,
1546 .bdrv_create = hdev_create,
1547 .create_options = raw_create_options,
1548 .bdrv_has_zero_init = hdev_has_zero_init,
1549
1550 .bdrv_aio_readv = raw_aio_readv,
1551 .bdrv_aio_writev = raw_aio_writev,
1552 .bdrv_aio_flush = raw_aio_flush,
1553 .bdrv_aio_discard = hdev_aio_discard,
1554
1555 .bdrv_truncate = raw_truncate,
1556 .bdrv_getlength = raw_getlength,
1557 .bdrv_get_allocated_file_size
1558 = raw_get_allocated_file_size,
1559
1560 /* generic scsi device */
1561 #ifdef __linux__
1562 .bdrv_ioctl = hdev_ioctl,
1563 .bdrv_aio_ioctl = hdev_aio_ioctl,
1564 #endif
1565 };
1566
1567 #ifdef __linux__
1568 static int floppy_open(BlockDriverState *bs, const char *filename,
1569 QDict *options, int flags)
1570 {
1571 BDRVRawState *s = bs->opaque;
1572 int ret;
1573
1574 s->type = FTYPE_FD;
1575
1576 /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
1577 ret = raw_open_common(bs, options, flags, O_NONBLOCK);
1578 if (ret)
1579 return ret;
1580
1581 /* close fd so that we can reopen it as needed */
1582 qemu_close(s->fd);
1583 s->fd = -1;
1584 s->fd_media_changed = 1;
1585
1586 return 0;
1587 }
1588
1589 static int floppy_probe_device(const char *filename)
1590 {
1591 int fd, ret;
1592 int prio = 0;
1593 struct floppy_struct fdparam;
1594 struct stat st;
1595
1596 if (strstart(filename, "/dev/fd", NULL) &&
1597 !strstart(filename, "/dev/fdset/", NULL)) {
1598 prio = 50;
1599 }
1600
1601 fd = qemu_open(filename, O_RDONLY | O_NONBLOCK);
1602 if (fd < 0) {
1603 goto out;
1604 }
1605 ret = fstat(fd, &st);
1606 if (ret == -1 || !S_ISBLK(st.st_mode)) {
1607 goto outc;
1608 }
1609
1610 /* Attempt to detect via a floppy specific ioctl */
1611 ret = ioctl(fd, FDGETPRM, &fdparam);
1612 if (ret >= 0)
1613 prio = 100;
1614
1615 outc:
1616 qemu_close(fd);
1617 out:
1618 return prio;
1619 }
1620
1621
1622 static int floppy_is_inserted(BlockDriverState *bs)
1623 {
1624 return fd_open(bs) >= 0;
1625 }
1626
1627 static int floppy_media_changed(BlockDriverState *bs)
1628 {
1629 BDRVRawState *s = bs->opaque;
1630 int ret;
1631
1632 /*
1633 * XXX: we do not have a true media changed indication.
1634 * It does not work if the floppy is changed without trying to read it.
1635 */
1636 fd_open(bs);
1637 ret = s->fd_media_changed;
1638 s->fd_media_changed = 0;
1639 #ifdef DEBUG_FLOPPY
1640 printf("Floppy changed=%d\n", ret);
1641 #endif
1642 return ret;
1643 }
1644
1645 static void floppy_eject(BlockDriverState *bs, bool eject_flag)
1646 {
1647 BDRVRawState *s = bs->opaque;
1648 int fd;
1649
1650 if (s->fd >= 0) {
1651 qemu_close(s->fd);
1652 s->fd = -1;
1653 }
1654 fd = qemu_open(bs->filename, s->open_flags | O_NONBLOCK);
1655 if (fd >= 0) {
1656 if (ioctl(fd, FDEJECT, 0) < 0)
1657 perror("FDEJECT");
1658 qemu_close(fd);
1659 }
1660 }
1661
1662 static BlockDriver bdrv_host_floppy = {
1663 .format_name = "host_floppy",
1664 .protocol_name = "host_floppy",
1665 .instance_size = sizeof(BDRVRawState),
1666 .bdrv_probe_device = floppy_probe_device,
1667 .bdrv_file_open = floppy_open,
1668 .bdrv_close = raw_close,
1669 .bdrv_reopen_prepare = raw_reopen_prepare,
1670 .bdrv_reopen_commit = raw_reopen_commit,
1671 .bdrv_reopen_abort = raw_reopen_abort,
1672 .bdrv_create = hdev_create,
1673 .create_options = raw_create_options,
1674 .bdrv_has_zero_init = hdev_has_zero_init,
1675
1676 .bdrv_aio_readv = raw_aio_readv,
1677 .bdrv_aio_writev = raw_aio_writev,
1678 .bdrv_aio_flush = raw_aio_flush,
1679
1680 .bdrv_truncate = raw_truncate,
1681 .bdrv_getlength = raw_getlength,
1682 .bdrv_get_allocated_file_size
1683 = raw_get_allocated_file_size,
1684
1685 /* removable device support */
1686 .bdrv_is_inserted = floppy_is_inserted,
1687 .bdrv_media_changed = floppy_media_changed,
1688 .bdrv_eject = floppy_eject,
1689 };
1690
1691 static int cdrom_open(BlockDriverState *bs, const char *filename,
1692 QDict *options, int flags)
1693 {
1694 BDRVRawState *s = bs->opaque;
1695
1696 s->type = FTYPE_CD;
1697
1698 /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
1699 return raw_open_common(bs, options, flags, O_NONBLOCK);
1700 }
1701
1702 static int cdrom_probe_device(const char *filename)
1703 {
1704 int fd, ret;
1705 int prio = 0;
1706 struct stat st;
1707
1708 fd = qemu_open(filename, O_RDONLY | O_NONBLOCK);
1709 if (fd < 0) {
1710 goto out;
1711 }
1712 ret = fstat(fd, &st);
1713 if (ret == -1 || !S_ISBLK(st.st_mode)) {
1714 goto outc;
1715 }
1716
1717 /* Attempt to detect via a CDROM specific ioctl */
1718 ret = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
1719 if (ret >= 0)
1720 prio = 100;
1721
1722 outc:
1723 qemu_close(fd);
1724 out:
1725 return prio;
1726 }
1727
1728 static int cdrom_is_inserted(BlockDriverState *bs)
1729 {
1730 BDRVRawState *s = bs->opaque;
1731 int ret;
1732
1733 ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
1734 if (ret == CDS_DISC_OK)
1735 return 1;
1736 return 0;
1737 }
1738
1739 static void cdrom_eject(BlockDriverState *bs, bool eject_flag)
1740 {
1741 BDRVRawState *s = bs->opaque;
1742
1743 if (eject_flag) {
1744 if (ioctl(s->fd, CDROMEJECT, NULL) < 0)
1745 perror("CDROMEJECT");
1746 } else {
1747 if (ioctl(s->fd, CDROMCLOSETRAY, NULL) < 0)
1748 perror("CDROMEJECT");
1749 }
1750 }
1751
1752 static void cdrom_lock_medium(BlockDriverState *bs, bool locked)
1753 {
1754 BDRVRawState *s = bs->opaque;
1755
1756 if (ioctl(s->fd, CDROM_LOCKDOOR, locked) < 0) {
1757 /*
1758 * Note: an error can happen if the distribution automatically
1759 * mounts the CD-ROM
1760 */
1761 /* perror("CDROM_LOCKDOOR"); */
1762 }
1763 }
1764
1765 static BlockDriver bdrv_host_cdrom = {
1766 .format_name = "host_cdrom",
1767 .protocol_name = "host_cdrom",
1768 .instance_size = sizeof(BDRVRawState),
1769 .bdrv_probe_device = cdrom_probe_device,
1770 .bdrv_file_open = cdrom_open,
1771 .bdrv_close = raw_close,
1772 .bdrv_reopen_prepare = raw_reopen_prepare,
1773 .bdrv_reopen_commit = raw_reopen_commit,
1774 .bdrv_reopen_abort = raw_reopen_abort,
1775 .bdrv_create = hdev_create,
1776 .create_options = raw_create_options,
1777 .bdrv_has_zero_init = hdev_has_zero_init,
1778
1779 .bdrv_aio_readv = raw_aio_readv,
1780 .bdrv_aio_writev = raw_aio_writev,
1781 .bdrv_aio_flush = raw_aio_flush,
1782
1783 .bdrv_truncate = raw_truncate,
1784 .bdrv_getlength = raw_getlength,
1785 .bdrv_get_allocated_file_size
1786 = raw_get_allocated_file_size,
1787
1788 /* removable device support */
1789 .bdrv_is_inserted = cdrom_is_inserted,
1790 .bdrv_eject = cdrom_eject,
1791 .bdrv_lock_medium = cdrom_lock_medium,
1792
1793 /* generic scsi device */
1794 .bdrv_ioctl = hdev_ioctl,
1795 .bdrv_aio_ioctl = hdev_aio_ioctl,
1796 };
1797 #endif /* __linux__ */
1798
1799 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
1800 static int cdrom_open(BlockDriverState *bs, const char *filename,
1801 QDict *options, int flags)
1802 {
1803 BDRVRawState *s = bs->opaque;
1804 int ret;
1805
1806 s->type = FTYPE_CD;
1807
1808 ret = raw_open_common(bs, options, flags, 0);
1809 if (ret)
1810 return ret;
1811
1812 /* make sure the door isn't locked at this time */
1813 ioctl(s->fd, CDIOCALLOW);
1814 return 0;
1815 }
1816
1817 static int cdrom_probe_device(const char *filename)
1818 {
1819 if (strstart(filename, "/dev/cd", NULL) ||
1820 strstart(filename, "/dev/acd", NULL))
1821 return 100;
1822 return 0;
1823 }
1824
1825 static int cdrom_reopen(BlockDriverState *bs)
1826 {
1827 BDRVRawState *s = bs->opaque;
1828 int fd;
1829
1830 /*
1831 * Force reread of possibly changed/newly loaded disc,
1832 * FreeBSD seems to not notice sometimes...
1833 */
1834 if (s->fd >= 0)
1835 qemu_close(s->fd);
1836 fd = qemu_open(bs->filename, s->open_flags, 0644);
1837 if (fd < 0) {
1838 s->fd = -1;
1839 return -EIO;
1840 }
1841 s->fd = fd;
1842
1843 /* make sure the door isn't locked at this time */
1844 ioctl(s->fd, CDIOCALLOW);
1845 return 0;
1846 }
1847
1848 static int cdrom_is_inserted(BlockDriverState *bs)
1849 {
1850 return raw_getlength(bs) > 0;
1851 }
1852
1853 static void cdrom_eject(BlockDriverState *bs, bool eject_flag)
1854 {
1855 BDRVRawState *s = bs->opaque;
1856
1857 if (s->fd < 0)
1858 return;
1859
1860 (void) ioctl(s->fd, CDIOCALLOW);
1861
1862 if (eject_flag) {
1863 if (ioctl(s->fd, CDIOCEJECT) < 0)
1864 perror("CDIOCEJECT");
1865 } else {
1866 if (ioctl(s->fd, CDIOCCLOSE) < 0)
1867 perror("CDIOCCLOSE");
1868 }
1869
1870 cdrom_reopen(bs);
1871 }
1872
1873 static void cdrom_lock_medium(BlockDriverState *bs, bool locked)
1874 {
1875 BDRVRawState *s = bs->opaque;
1876
1877 if (s->fd < 0)
1878 return;
1879 if (ioctl(s->fd, (locked ? CDIOCPREVENT : CDIOCALLOW)) < 0) {
1880 /*
1881 * Note: an error can happen if the distribution automatically
1882 * mounts the CD-ROM
1883 */
1884 /* perror("CDROM_LOCKDOOR"); */
1885 }
1886 }
1887
1888 static BlockDriver bdrv_host_cdrom = {
1889 .format_name = "host_cdrom",
1890 .protocol_name = "host_cdrom",
1891 .instance_size = sizeof(BDRVRawState),
1892 .bdrv_probe_device = cdrom_probe_device,
1893 .bdrv_file_open = cdrom_open,
1894 .bdrv_close = raw_close,
1895 .bdrv_reopen_prepare = raw_reopen_prepare,
1896 .bdrv_reopen_commit = raw_reopen_commit,
1897 .bdrv_reopen_abort = raw_reopen_abort,
1898 .bdrv_create = hdev_create,
1899 .create_options = raw_create_options,
1900 .bdrv_has_zero_init = hdev_has_zero_init,
1901
1902 .bdrv_aio_readv = raw_aio_readv,
1903 .bdrv_aio_writev = raw_aio_writev,
1904 .bdrv_aio_flush = raw_aio_flush,
1905
1906 .bdrv_truncate = raw_truncate,
1907 .bdrv_getlength = raw_getlength,
1908 .bdrv_get_allocated_file_size
1909 = raw_get_allocated_file_size,
1910
1911 /* removable device support */
1912 .bdrv_is_inserted = cdrom_is_inserted,
1913 .bdrv_eject = cdrom_eject,
1914 .bdrv_lock_medium = cdrom_lock_medium,
1915 };
1916 #endif /* __FreeBSD__ */
1917
1918 #ifdef CONFIG_LINUX_AIO
1919 /**
1920 * Return the file descriptor for Linux AIO
1921 *
1922 * This function is a layering violation and should be removed when it becomes
1923 * possible to call the block layer outside the global mutex. It allows the
1924 * caller to hijack the file descriptor so I/O can be performed outside the
1925 * block layer.
1926 */
1927 int raw_get_aio_fd(BlockDriverState *bs)
1928 {
1929 BDRVRawState *s;
1930
1931 if (!bs->drv) {
1932 return -ENOMEDIUM;
1933 }
1934
1935 if (bs->drv == bdrv_find_format("raw")) {
1936 bs = bs->file;
1937 }
1938
1939 /* raw-posix has several protocols so just check for raw_aio_readv */
1940 if (bs->drv->bdrv_aio_readv != raw_aio_readv) {
1941 return -ENOTSUP;
1942 }
1943
1944 s = bs->opaque;
1945 if (!s->use_aio) {
1946 return -ENOTSUP;
1947 }
1948 return s->fd;
1949 }
1950 #endif /* CONFIG_LINUX_AIO */
1951
1952 static void bdrv_file_init(void)
1953 {
1954 /*
1955 * Register all the drivers. Note that order is important, the driver
1956 * registered last will get probed first.
1957 */
1958 bdrv_register(&bdrv_file);
1959 bdrv_register(&bdrv_host_device);
1960 #ifdef __linux__
1961 bdrv_register(&bdrv_host_floppy);
1962 bdrv_register(&bdrv_host_cdrom);
1963 #endif
1964 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1965 bdrv_register(&bdrv_host_cdrom);
1966 #endif
1967 }
1968
1969 block_init(bdrv_file_init);