xref: /qemu/block/file-posix.c (revision 99dbfd1d)
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/osdep.h"
25 #include "qapi/error.h"
26 #include "qemu/cutils.h"
27 #include "qemu/error-report.h"
28 #include "qemu/timer.h"
29 #include "qemu/log.h"
30 #include "block/block_int.h"
31 #include "qemu/module.h"
32 #include "trace.h"
33 #include "block/thread-pool.h"
34 #include "qemu/iov.h"
35 #include "block/raw-aio.h"
36 #include "qapi/util.h"
37 #include "qapi/qmp/qstring.h"
38 
39 #if defined(__APPLE__) && (__MACH__)
40 #include <paths.h>
41 #include <sys/param.h>
42 #include <IOKit/IOKitLib.h>
43 #include <IOKit/IOBSD.h>
44 #include <IOKit/storage/IOMediaBSDClient.h>
45 #include <IOKit/storage/IOMedia.h>
46 #include <IOKit/storage/IOCDMedia.h>
47 //#include <IOKit/storage/IOCDTypes.h>
48 #include <IOKit/storage/IODVDMedia.h>
49 #include <CoreFoundation/CoreFoundation.h>
50 #endif
51 
52 #ifdef __sun__
53 #define _POSIX_PTHREAD_SEMANTICS 1
54 #include <sys/dkio.h>
55 #endif
56 #ifdef __linux__
57 #include <sys/ioctl.h>
58 #include <sys/param.h>
59 #include <linux/cdrom.h>
60 #include <linux/fd.h>
61 #include <linux/fs.h>
62 #include <linux/hdreg.h>
63 #include <scsi/sg.h>
64 #ifdef __s390__
65 #include <asm/dasd.h>
66 #endif
67 #ifndef FS_NOCOW_FL
68 #define FS_NOCOW_FL                     0x00800000 /* Do not cow file */
69 #endif
70 #endif
71 #if defined(CONFIG_FALLOCATE_PUNCH_HOLE) || defined(CONFIG_FALLOCATE_ZERO_RANGE)
72 #include <linux/falloc.h>
73 #endif
74 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
75 #include <sys/disk.h>
76 #include <sys/cdio.h>
77 #endif
78 
79 #ifdef __OpenBSD__
80 #include <sys/ioctl.h>
81 #include <sys/disklabel.h>
82 #include <sys/dkio.h>
83 #endif
84 
85 #ifdef __NetBSD__
86 #include <sys/ioctl.h>
87 #include <sys/disklabel.h>
88 #include <sys/dkio.h>
89 #include <sys/disk.h>
90 #endif
91 
92 #ifdef __DragonFly__
93 #include <sys/ioctl.h>
94 #include <sys/diskslice.h>
95 #endif
96 
97 #ifdef CONFIG_XFS
98 #include <xfs/xfs.h>
99 #endif
100 
101 //#define DEBUG_BLOCK
102 
103 #ifdef DEBUG_BLOCK
104 # define DEBUG_BLOCK_PRINT 1
105 #else
106 # define DEBUG_BLOCK_PRINT 0
107 #endif
108 #define DPRINTF(fmt, ...) \
109 do { \
110     if (DEBUG_BLOCK_PRINT) { \
111         printf(fmt, ## __VA_ARGS__); \
112     } \
113 } while (0)
114 
115 /* OS X does not have O_DSYNC */
116 #ifndef O_DSYNC
117 #ifdef O_SYNC
118 #define O_DSYNC O_SYNC
119 #elif defined(O_FSYNC)
120 #define O_DSYNC O_FSYNC
121 #endif
122 #endif
123 
124 /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */
125 #ifndef O_DIRECT
126 #define O_DIRECT O_DSYNC
127 #endif
128 
129 #define FTYPE_FILE   0
130 #define FTYPE_CD     1
131 
132 #define MAX_BLOCKSIZE	4096
133 
134 typedef struct BDRVRawState {
135     int fd;
136     int type;
137     int open_flags;
138     size_t buf_align;
139 
140 #ifdef CONFIG_XFS
141     bool is_xfs:1;
142 #endif
143     bool has_discard:1;
144     bool has_write_zeroes:1;
145     bool discard_zeroes:1;
146     bool use_linux_aio:1;
147     bool has_fallocate;
148     bool needs_alignment;
149 } BDRVRawState;
150 
151 typedef struct BDRVRawReopenState {
152     int fd;
153     int open_flags;
154 } BDRVRawReopenState;
155 
156 static int fd_open(BlockDriverState *bs);
157 static int64_t raw_getlength(BlockDriverState *bs);
158 
159 typedef struct RawPosixAIOData {
160     BlockDriverState *bs;
161     int aio_fildes;
162     union {
163         struct iovec *aio_iov;
164         void *aio_ioctl_buf;
165     };
166     int aio_niov;
167     uint64_t aio_nbytes;
168 #define aio_ioctl_cmd   aio_nbytes /* for QEMU_AIO_IOCTL */
169     off_t aio_offset;
170     int aio_type;
171 } RawPosixAIOData;
172 
173 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
174 static int cdrom_reopen(BlockDriverState *bs);
175 #endif
176 
177 #if defined(__NetBSD__)
178 static int raw_normalize_devicepath(const char **filename)
179 {
180     static char namebuf[PATH_MAX];
181     const char *dp, *fname;
182     struct stat sb;
183 
184     fname = *filename;
185     dp = strrchr(fname, '/');
186     if (lstat(fname, &sb) < 0) {
187         fprintf(stderr, "%s: stat failed: %s\n",
188             fname, strerror(errno));
189         return -errno;
190     }
191 
192     if (!S_ISBLK(sb.st_mode)) {
193         return 0;
194     }
195 
196     if (dp == NULL) {
197         snprintf(namebuf, PATH_MAX, "r%s", fname);
198     } else {
199         snprintf(namebuf, PATH_MAX, "%.*s/r%s",
200             (int)(dp - fname), fname, dp + 1);
201     }
202     fprintf(stderr, "%s is a block device", fname);
203     *filename = namebuf;
204     fprintf(stderr, ", using %s\n", *filename);
205 
206     return 0;
207 }
208 #else
209 static int raw_normalize_devicepath(const char **filename)
210 {
211     return 0;
212 }
213 #endif
214 
215 /*
216  * Get logical block size via ioctl. On success store it in @sector_size_p.
217  */
218 static int probe_logical_blocksize(int fd, unsigned int *sector_size_p)
219 {
220     unsigned int sector_size;
221     bool success = false;
222 
223     errno = ENOTSUP;
224 
225     /* Try a few ioctls to get the right size */
226 #ifdef BLKSSZGET
227     if (ioctl(fd, BLKSSZGET, &sector_size) >= 0) {
228         *sector_size_p = sector_size;
229         success = true;
230     }
231 #endif
232 #ifdef DKIOCGETBLOCKSIZE
233     if (ioctl(fd, DKIOCGETBLOCKSIZE, &sector_size) >= 0) {
234         *sector_size_p = sector_size;
235         success = true;
236     }
237 #endif
238 #ifdef DIOCGSECTORSIZE
239     if (ioctl(fd, DIOCGSECTORSIZE, &sector_size) >= 0) {
240         *sector_size_p = sector_size;
241         success = true;
242     }
243 #endif
244 
245     return success ? 0 : -errno;
246 }
247 
248 /**
249  * Get physical block size of @fd.
250  * On success, store it in @blk_size and return 0.
251  * On failure, return -errno.
252  */
253 static int probe_physical_blocksize(int fd, unsigned int *blk_size)
254 {
255 #ifdef BLKPBSZGET
256     if (ioctl(fd, BLKPBSZGET, blk_size) < 0) {
257         return -errno;
258     }
259     return 0;
260 #else
261     return -ENOTSUP;
262 #endif
263 }
264 
265 /* Check if read is allowed with given memory buffer and length.
266  *
267  * This function is used to check O_DIRECT memory buffer and request alignment.
268  */
269 static bool raw_is_io_aligned(int fd, void *buf, size_t len)
270 {
271     ssize_t ret = pread(fd, buf, len, 0);
272 
273     if (ret >= 0) {
274         return true;
275     }
276 
277 #ifdef __linux__
278     /* The Linux kernel returns EINVAL for misaligned O_DIRECT reads.  Ignore
279      * other errors (e.g. real I/O error), which could happen on a failed
280      * drive, since we only care about probing alignment.
281      */
282     if (errno != EINVAL) {
283         return true;
284     }
285 #endif
286 
287     return false;
288 }
289 
290 static void raw_probe_alignment(BlockDriverState *bs, int fd, Error **errp)
291 {
292     BDRVRawState *s = bs->opaque;
293     char *buf;
294     size_t max_align = MAX(MAX_BLOCKSIZE, getpagesize());
295 
296     /* For SCSI generic devices the alignment is not really used.
297        With buffered I/O, we don't have any restrictions. */
298     if (bdrv_is_sg(bs) || !s->needs_alignment) {
299         bs->bl.request_alignment = 1;
300         s->buf_align = 1;
301         return;
302     }
303 
304     bs->bl.request_alignment = 0;
305     s->buf_align = 0;
306     /* Let's try to use the logical blocksize for the alignment. */
307     if (probe_logical_blocksize(fd, &bs->bl.request_alignment) < 0) {
308         bs->bl.request_alignment = 0;
309     }
310 #ifdef CONFIG_XFS
311     if (s->is_xfs) {
312         struct dioattr da;
313         if (xfsctl(NULL, fd, XFS_IOC_DIOINFO, &da) >= 0) {
314             bs->bl.request_alignment = da.d_miniosz;
315             /* The kernel returns wrong information for d_mem */
316             /* s->buf_align = da.d_mem; */
317         }
318     }
319 #endif
320 
321     /* If we could not get the sizes so far, we can only guess them */
322     if (!s->buf_align) {
323         size_t align;
324         buf = qemu_memalign(max_align, 2 * max_align);
325         for (align = 512; align <= max_align; align <<= 1) {
326             if (raw_is_io_aligned(fd, buf + align, max_align)) {
327                 s->buf_align = align;
328                 break;
329             }
330         }
331         qemu_vfree(buf);
332     }
333 
334     if (!bs->bl.request_alignment) {
335         size_t align;
336         buf = qemu_memalign(s->buf_align, max_align);
337         for (align = 512; align <= max_align; align <<= 1) {
338             if (raw_is_io_aligned(fd, buf, align)) {
339                 bs->bl.request_alignment = align;
340                 break;
341             }
342         }
343         qemu_vfree(buf);
344     }
345 
346     if (!s->buf_align || !bs->bl.request_alignment) {
347         error_setg(errp, "Could not find working O_DIRECT alignment");
348         error_append_hint(errp, "Try cache.direct=off\n");
349     }
350 }
351 
352 static void raw_parse_flags(int bdrv_flags, int *open_flags)
353 {
354     assert(open_flags != NULL);
355 
356     *open_flags |= O_BINARY;
357     *open_flags &= ~O_ACCMODE;
358     if (bdrv_flags & BDRV_O_RDWR) {
359         *open_flags |= O_RDWR;
360     } else {
361         *open_flags |= O_RDONLY;
362     }
363 
364     /* Use O_DSYNC for write-through caching, no flags for write-back caching,
365      * and O_DIRECT for no caching. */
366     if ((bdrv_flags & BDRV_O_NOCACHE)) {
367         *open_flags |= O_DIRECT;
368     }
369 }
370 
371 static void raw_parse_filename(const char *filename, QDict *options,
372                                Error **errp)
373 {
374     /* The filename does not have to be prefixed by the protocol name, since
375      * "file" is the default protocol; therefore, the return value of this
376      * function call can be ignored. */
377     strstart(filename, "file:", &filename);
378 
379     qdict_put_obj(options, "filename", QOBJECT(qstring_from_str(filename)));
380 }
381 
382 static QemuOptsList raw_runtime_opts = {
383     .name = "raw",
384     .head = QTAILQ_HEAD_INITIALIZER(raw_runtime_opts.head),
385     .desc = {
386         {
387             .name = "filename",
388             .type = QEMU_OPT_STRING,
389             .help = "File name of the image",
390         },
391         {
392             .name = "aio",
393             .type = QEMU_OPT_STRING,
394             .help = "host AIO implementation (threads, native)",
395         },
396         { /* end of list */ }
397     },
398 };
399 
400 static int raw_open_common(BlockDriverState *bs, QDict *options,
401                            int bdrv_flags, int open_flags, Error **errp)
402 {
403     BDRVRawState *s = bs->opaque;
404     QemuOpts *opts;
405     Error *local_err = NULL;
406     const char *filename = NULL;
407     BlockdevAioOptions aio, aio_default;
408     int fd, ret;
409     struct stat st;
410 
411     opts = qemu_opts_create(&raw_runtime_opts, NULL, 0, &error_abort);
412     qemu_opts_absorb_qdict(opts, options, &local_err);
413     if (local_err) {
414         error_propagate(errp, local_err);
415         ret = -EINVAL;
416         goto fail;
417     }
418 
419     filename = qemu_opt_get(opts, "filename");
420 
421     ret = raw_normalize_devicepath(&filename);
422     if (ret != 0) {
423         error_setg_errno(errp, -ret, "Could not normalize device path");
424         goto fail;
425     }
426 
427     aio_default = (bdrv_flags & BDRV_O_NATIVE_AIO)
428                   ? BLOCKDEV_AIO_OPTIONS_NATIVE
429                   : BLOCKDEV_AIO_OPTIONS_THREADS;
430     aio = qapi_enum_parse(BlockdevAioOptions_lookup, qemu_opt_get(opts, "aio"),
431                           BLOCKDEV_AIO_OPTIONS__MAX, aio_default, &local_err);
432     if (local_err) {
433         error_propagate(errp, local_err);
434         ret = -EINVAL;
435         goto fail;
436     }
437     s->use_linux_aio = (aio == BLOCKDEV_AIO_OPTIONS_NATIVE);
438 
439     s->open_flags = open_flags;
440     raw_parse_flags(bdrv_flags, &s->open_flags);
441 
442     s->fd = -1;
443     fd = qemu_open(filename, s->open_flags, 0644);
444     if (fd < 0) {
445         ret = -errno;
446         error_setg_errno(errp, errno, "Could not open '%s'", filename);
447         if (ret == -EROFS) {
448             ret = -EACCES;
449         }
450         goto fail;
451     }
452     s->fd = fd;
453 
454 #ifdef CONFIG_LINUX_AIO
455      /* Currently Linux does AIO only for files opened with O_DIRECT */
456     if (s->use_linux_aio && !(s->open_flags & O_DIRECT)) {
457         error_setg(errp, "aio=native was specified, but it requires "
458                          "cache.direct=on, which was not specified.");
459         ret = -EINVAL;
460         goto fail;
461     }
462 #else
463     if (s->use_linux_aio) {
464         error_setg(errp, "aio=native was specified, but is not supported "
465                          "in this build.");
466         ret = -EINVAL;
467         goto fail;
468     }
469 #endif /* !defined(CONFIG_LINUX_AIO) */
470 
471     s->has_discard = true;
472     s->has_write_zeroes = true;
473     bs->supported_zero_flags = BDRV_REQ_MAY_UNMAP;
474     if ((bs->open_flags & BDRV_O_NOCACHE) != 0) {
475         s->needs_alignment = true;
476     }
477 
478     if (fstat(s->fd, &st) < 0) {
479         ret = -errno;
480         error_setg_errno(errp, errno, "Could not stat file");
481         goto fail;
482     }
483     if (S_ISREG(st.st_mode)) {
484         s->discard_zeroes = true;
485         s->has_fallocate = true;
486     }
487     if (S_ISBLK(st.st_mode)) {
488 #ifdef BLKDISCARDZEROES
489         unsigned int arg;
490         if (ioctl(s->fd, BLKDISCARDZEROES, &arg) == 0 && arg) {
491             s->discard_zeroes = true;
492         }
493 #endif
494 #ifdef __linux__
495         /* On Linux 3.10, BLKDISCARD leaves stale data in the page cache.  Do
496          * not rely on the contents of discarded blocks unless using O_DIRECT.
497          * Same for BLKZEROOUT.
498          */
499         if (!(bs->open_flags & BDRV_O_NOCACHE)) {
500             s->discard_zeroes = false;
501             s->has_write_zeroes = false;
502         }
503 #endif
504     }
505 #ifdef __FreeBSD__
506     if (S_ISCHR(st.st_mode)) {
507         /*
508          * The file is a char device (disk), which on FreeBSD isn't behind
509          * a pager, so force all requests to be aligned. This is needed
510          * so QEMU makes sure all IO operations on the device are aligned
511          * to sector size, or else FreeBSD will reject them with EINVAL.
512          */
513         s->needs_alignment = true;
514     }
515 #endif
516 
517 #ifdef CONFIG_XFS
518     if (platform_test_xfs_fd(s->fd)) {
519         s->is_xfs = true;
520     }
521 #endif
522 
523     ret = 0;
524 fail:
525     if (filename && (bdrv_flags & BDRV_O_TEMPORARY)) {
526         unlink(filename);
527     }
528     qemu_opts_del(opts);
529     return ret;
530 }
531 
532 static int raw_open(BlockDriverState *bs, QDict *options, int flags,
533                     Error **errp)
534 {
535     BDRVRawState *s = bs->opaque;
536 
537     s->type = FTYPE_FILE;
538     return raw_open_common(bs, options, flags, 0, errp);
539 }
540 
541 static int raw_reopen_prepare(BDRVReopenState *state,
542                               BlockReopenQueue *queue, Error **errp)
543 {
544     BDRVRawState *s;
545     BDRVRawReopenState *rs;
546     int ret = 0;
547     Error *local_err = NULL;
548 
549     assert(state != NULL);
550     assert(state->bs != NULL);
551 
552     s = state->bs->opaque;
553 
554     state->opaque = g_new0(BDRVRawReopenState, 1);
555     rs = state->opaque;
556 
557     if (s->type == FTYPE_CD) {
558         rs->open_flags |= O_NONBLOCK;
559     }
560 
561     raw_parse_flags(state->flags, &rs->open_flags);
562 
563     rs->fd = -1;
564 
565     int fcntl_flags = O_APPEND | O_NONBLOCK;
566 #ifdef O_NOATIME
567     fcntl_flags |= O_NOATIME;
568 #endif
569 
570 #ifdef O_ASYNC
571     /* Not all operating systems have O_ASYNC, and those that don't
572      * will not let us track the state into rs->open_flags (typically
573      * you achieve the same effect with an ioctl, for example I_SETSIG
574      * on Solaris). But we do not use O_ASYNC, so that's fine.
575      */
576     assert((s->open_flags & O_ASYNC) == 0);
577 #endif
578 
579     if ((rs->open_flags & ~fcntl_flags) == (s->open_flags & ~fcntl_flags)) {
580         /* dup the original fd */
581         rs->fd = qemu_dup(s->fd);
582         if (rs->fd >= 0) {
583             ret = fcntl_setfl(rs->fd, rs->open_flags);
584             if (ret) {
585                 qemu_close(rs->fd);
586                 rs->fd = -1;
587             }
588         }
589     }
590 
591     /* If we cannot use fcntl, or fcntl failed, fall back to qemu_open() */
592     if (rs->fd == -1) {
593         const char *normalized_filename = state->bs->filename;
594         ret = raw_normalize_devicepath(&normalized_filename);
595         if (ret < 0) {
596             error_setg_errno(errp, -ret, "Could not normalize device path");
597         } else {
598             assert(!(rs->open_flags & O_CREAT));
599             rs->fd = qemu_open(normalized_filename, rs->open_flags);
600             if (rs->fd == -1) {
601                 error_setg_errno(errp, errno, "Could not reopen file");
602                 ret = -1;
603             }
604         }
605     }
606 
607     /* Fail already reopen_prepare() if we can't get a working O_DIRECT
608      * alignment with the new fd. */
609     if (rs->fd != -1) {
610         raw_probe_alignment(state->bs, rs->fd, &local_err);
611         if (local_err) {
612             qemu_close(rs->fd);
613             rs->fd = -1;
614             error_propagate(errp, local_err);
615             ret = -EINVAL;
616         }
617     }
618 
619     return ret;
620 }
621 
622 static void raw_reopen_commit(BDRVReopenState *state)
623 {
624     BDRVRawReopenState *rs = state->opaque;
625     BDRVRawState *s = state->bs->opaque;
626 
627     s->open_flags = rs->open_flags;
628 
629     qemu_close(s->fd);
630     s->fd = rs->fd;
631 
632     g_free(state->opaque);
633     state->opaque = NULL;
634 }
635 
636 
637 static void raw_reopen_abort(BDRVReopenState *state)
638 {
639     BDRVRawReopenState *rs = state->opaque;
640 
641      /* nothing to do if NULL, we didn't get far enough */
642     if (rs == NULL) {
643         return;
644     }
645 
646     if (rs->fd >= 0) {
647         qemu_close(rs->fd);
648         rs->fd = -1;
649     }
650     g_free(state->opaque);
651     state->opaque = NULL;
652 }
653 
654 static int hdev_get_max_transfer_length(BlockDriverState *bs, int fd)
655 {
656 #ifdef BLKSECTGET
657     int max_bytes = 0;
658     short max_sectors = 0;
659     if (bs->sg && ioctl(fd, BLKSECTGET, &max_bytes) == 0) {
660         return max_bytes;
661     } else if (!bs->sg && ioctl(fd, BLKSECTGET, &max_sectors) == 0) {
662         return max_sectors << BDRV_SECTOR_BITS;
663     } else {
664         return -errno;
665     }
666 #else
667     return -ENOSYS;
668 #endif
669 }
670 
671 static void raw_refresh_limits(BlockDriverState *bs, Error **errp)
672 {
673     BDRVRawState *s = bs->opaque;
674     struct stat st;
675 
676     if (!fstat(s->fd, &st)) {
677         if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode)) {
678             int ret = hdev_get_max_transfer_length(bs, s->fd);
679             if (ret > 0 && ret <= BDRV_REQUEST_MAX_BYTES) {
680                 bs->bl.max_transfer = pow2floor(ret);
681             }
682         }
683     }
684 
685     raw_probe_alignment(bs, s->fd, errp);
686     bs->bl.min_mem_alignment = s->buf_align;
687     bs->bl.opt_mem_alignment = MAX(s->buf_align, getpagesize());
688 }
689 
690 static int check_for_dasd(int fd)
691 {
692 #ifdef BIODASDINFO2
693     struct dasd_information2_t info = {0};
694 
695     return ioctl(fd, BIODASDINFO2, &info);
696 #else
697     return -1;
698 #endif
699 }
700 
701 /**
702  * Try to get @bs's logical and physical block size.
703  * On success, store them in @bsz and return zero.
704  * On failure, return negative errno.
705  */
706 static int hdev_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz)
707 {
708     BDRVRawState *s = bs->opaque;
709     int ret;
710 
711     /* If DASD, get blocksizes */
712     if (check_for_dasd(s->fd) < 0) {
713         return -ENOTSUP;
714     }
715     ret = probe_logical_blocksize(s->fd, &bsz->log);
716     if (ret < 0) {
717         return ret;
718     }
719     return probe_physical_blocksize(s->fd, &bsz->phys);
720 }
721 
722 /**
723  * Try to get @bs's geometry: cyls, heads, sectors.
724  * On success, store them in @geo and return 0.
725  * On failure return -errno.
726  * (Allows block driver to assign default geometry values that guest sees)
727  */
728 #ifdef __linux__
729 static int hdev_probe_geometry(BlockDriverState *bs, HDGeometry *geo)
730 {
731     BDRVRawState *s = bs->opaque;
732     struct hd_geometry ioctl_geo = {0};
733 
734     /* If DASD, get its geometry */
735     if (check_for_dasd(s->fd) < 0) {
736         return -ENOTSUP;
737     }
738     if (ioctl(s->fd, HDIO_GETGEO, &ioctl_geo) < 0) {
739         return -errno;
740     }
741     /* HDIO_GETGEO may return success even though geo contains zeros
742        (e.g. certain multipath setups) */
743     if (!ioctl_geo.heads || !ioctl_geo.sectors || !ioctl_geo.cylinders) {
744         return -ENOTSUP;
745     }
746     /* Do not return a geometry for partition */
747     if (ioctl_geo.start != 0) {
748         return -ENOTSUP;
749     }
750     geo->heads = ioctl_geo.heads;
751     geo->sectors = ioctl_geo.sectors;
752     geo->cylinders = ioctl_geo.cylinders;
753 
754     return 0;
755 }
756 #else /* __linux__ */
757 static int hdev_probe_geometry(BlockDriverState *bs, HDGeometry *geo)
758 {
759     return -ENOTSUP;
760 }
761 #endif
762 
763 static ssize_t handle_aiocb_ioctl(RawPosixAIOData *aiocb)
764 {
765     int ret;
766 
767     ret = ioctl(aiocb->aio_fildes, aiocb->aio_ioctl_cmd, aiocb->aio_ioctl_buf);
768     if (ret == -1) {
769         return -errno;
770     }
771 
772     return 0;
773 }
774 
775 static ssize_t handle_aiocb_flush(RawPosixAIOData *aiocb)
776 {
777     int ret;
778 
779     ret = qemu_fdatasync(aiocb->aio_fildes);
780     if (ret == -1) {
781         return -errno;
782     }
783     return 0;
784 }
785 
786 #ifdef CONFIG_PREADV
787 
788 static bool preadv_present = true;
789 
790 static ssize_t
791 qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset)
792 {
793     return preadv(fd, iov, nr_iov, offset);
794 }
795 
796 static ssize_t
797 qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset)
798 {
799     return pwritev(fd, iov, nr_iov, offset);
800 }
801 
802 #else
803 
804 static bool preadv_present = false;
805 
806 static ssize_t
807 qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset)
808 {
809     return -ENOSYS;
810 }
811 
812 static ssize_t
813 qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset)
814 {
815     return -ENOSYS;
816 }
817 
818 #endif
819 
820 static ssize_t handle_aiocb_rw_vector(RawPosixAIOData *aiocb)
821 {
822     ssize_t len;
823 
824     do {
825         if (aiocb->aio_type & QEMU_AIO_WRITE)
826             len = qemu_pwritev(aiocb->aio_fildes,
827                                aiocb->aio_iov,
828                                aiocb->aio_niov,
829                                aiocb->aio_offset);
830          else
831             len = qemu_preadv(aiocb->aio_fildes,
832                               aiocb->aio_iov,
833                               aiocb->aio_niov,
834                               aiocb->aio_offset);
835     } while (len == -1 && errno == EINTR);
836 
837     if (len == -1) {
838         return -errno;
839     }
840     return len;
841 }
842 
843 /*
844  * Read/writes the data to/from a given linear buffer.
845  *
846  * Returns the number of bytes handles or -errno in case of an error. Short
847  * reads are only returned if the end of the file is reached.
848  */
849 static ssize_t handle_aiocb_rw_linear(RawPosixAIOData *aiocb, char *buf)
850 {
851     ssize_t offset = 0;
852     ssize_t len;
853 
854     while (offset < aiocb->aio_nbytes) {
855         if (aiocb->aio_type & QEMU_AIO_WRITE) {
856             len = pwrite(aiocb->aio_fildes,
857                          (const char *)buf + offset,
858                          aiocb->aio_nbytes - offset,
859                          aiocb->aio_offset + offset);
860         } else {
861             len = pread(aiocb->aio_fildes,
862                         buf + offset,
863                         aiocb->aio_nbytes - offset,
864                         aiocb->aio_offset + offset);
865         }
866         if (len == -1 && errno == EINTR) {
867             continue;
868         } else if (len == -1 && errno == EINVAL &&
869                    (aiocb->bs->open_flags & BDRV_O_NOCACHE) &&
870                    !(aiocb->aio_type & QEMU_AIO_WRITE) &&
871                    offset > 0) {
872             /* O_DIRECT pread() may fail with EINVAL when offset is unaligned
873              * after a short read.  Assume that O_DIRECT short reads only occur
874              * at EOF.  Therefore this is a short read, not an I/O error.
875              */
876             break;
877         } else if (len == -1) {
878             offset = -errno;
879             break;
880         } else if (len == 0) {
881             break;
882         }
883         offset += len;
884     }
885 
886     return offset;
887 }
888 
889 static ssize_t handle_aiocb_rw(RawPosixAIOData *aiocb)
890 {
891     ssize_t nbytes;
892     char *buf;
893 
894     if (!(aiocb->aio_type & QEMU_AIO_MISALIGNED)) {
895         /*
896          * If there is just a single buffer, and it is properly aligned
897          * we can just use plain pread/pwrite without any problems.
898          */
899         if (aiocb->aio_niov == 1) {
900              return handle_aiocb_rw_linear(aiocb, aiocb->aio_iov->iov_base);
901         }
902         /*
903          * We have more than one iovec, and all are properly aligned.
904          *
905          * Try preadv/pwritev first and fall back to linearizing the
906          * buffer if it's not supported.
907          */
908         if (preadv_present) {
909             nbytes = handle_aiocb_rw_vector(aiocb);
910             if (nbytes == aiocb->aio_nbytes ||
911                 (nbytes < 0 && nbytes != -ENOSYS)) {
912                 return nbytes;
913             }
914             preadv_present = false;
915         }
916 
917         /*
918          * XXX(hch): short read/write.  no easy way to handle the reminder
919          * using these interfaces.  For now retry using plain
920          * pread/pwrite?
921          */
922     }
923 
924     /*
925      * Ok, we have to do it the hard way, copy all segments into
926      * a single aligned buffer.
927      */
928     buf = qemu_try_blockalign(aiocb->bs, aiocb->aio_nbytes);
929     if (buf == NULL) {
930         return -ENOMEM;
931     }
932 
933     if (aiocb->aio_type & QEMU_AIO_WRITE) {
934         char *p = buf;
935         int i;
936 
937         for (i = 0; i < aiocb->aio_niov; ++i) {
938             memcpy(p, aiocb->aio_iov[i].iov_base, aiocb->aio_iov[i].iov_len);
939             p += aiocb->aio_iov[i].iov_len;
940         }
941         assert(p - buf == aiocb->aio_nbytes);
942     }
943 
944     nbytes = handle_aiocb_rw_linear(aiocb, buf);
945     if (!(aiocb->aio_type & QEMU_AIO_WRITE)) {
946         char *p = buf;
947         size_t count = aiocb->aio_nbytes, copy;
948         int i;
949 
950         for (i = 0; i < aiocb->aio_niov && count; ++i) {
951             copy = count;
952             if (copy > aiocb->aio_iov[i].iov_len) {
953                 copy = aiocb->aio_iov[i].iov_len;
954             }
955             memcpy(aiocb->aio_iov[i].iov_base, p, copy);
956             assert(count >= copy);
957             p     += copy;
958             count -= copy;
959         }
960         assert(count == 0);
961     }
962     qemu_vfree(buf);
963 
964     return nbytes;
965 }
966 
967 #ifdef CONFIG_XFS
968 static int xfs_write_zeroes(BDRVRawState *s, int64_t offset, uint64_t bytes)
969 {
970     struct xfs_flock64 fl;
971     int err;
972 
973     memset(&fl, 0, sizeof(fl));
974     fl.l_whence = SEEK_SET;
975     fl.l_start = offset;
976     fl.l_len = bytes;
977 
978     if (xfsctl(NULL, s->fd, XFS_IOC_ZERO_RANGE, &fl) < 0) {
979         err = errno;
980         DPRINTF("cannot write zero range (%s)\n", strerror(errno));
981         return -err;
982     }
983 
984     return 0;
985 }
986 
987 static int xfs_discard(BDRVRawState *s, int64_t offset, uint64_t bytes)
988 {
989     struct xfs_flock64 fl;
990     int err;
991 
992     memset(&fl, 0, sizeof(fl));
993     fl.l_whence = SEEK_SET;
994     fl.l_start = offset;
995     fl.l_len = bytes;
996 
997     if (xfsctl(NULL, s->fd, XFS_IOC_UNRESVSP64, &fl) < 0) {
998         err = errno;
999         DPRINTF("cannot punch hole (%s)\n", strerror(errno));
1000         return -err;
1001     }
1002 
1003     return 0;
1004 }
1005 #endif
1006 
1007 static int translate_err(int err)
1008 {
1009     if (err == -ENODEV || err == -ENOSYS || err == -EOPNOTSUPP ||
1010         err == -ENOTTY) {
1011         err = -ENOTSUP;
1012     }
1013     return err;
1014 }
1015 
1016 #ifdef CONFIG_FALLOCATE
1017 static int do_fallocate(int fd, int mode, off_t offset, off_t len)
1018 {
1019     do {
1020         if (fallocate(fd, mode, offset, len) == 0) {
1021             return 0;
1022         }
1023     } while (errno == EINTR);
1024     return translate_err(-errno);
1025 }
1026 #endif
1027 
1028 static ssize_t handle_aiocb_write_zeroes_block(RawPosixAIOData *aiocb)
1029 {
1030     int ret = -ENOTSUP;
1031     BDRVRawState *s = aiocb->bs->opaque;
1032 
1033     if (!s->has_write_zeroes) {
1034         return -ENOTSUP;
1035     }
1036 
1037 #ifdef BLKZEROOUT
1038     do {
1039         uint64_t range[2] = { aiocb->aio_offset, aiocb->aio_nbytes };
1040         if (ioctl(aiocb->aio_fildes, BLKZEROOUT, range) == 0) {
1041             return 0;
1042         }
1043     } while (errno == EINTR);
1044 
1045     ret = translate_err(-errno);
1046 #endif
1047 
1048     if (ret == -ENOTSUP) {
1049         s->has_write_zeroes = false;
1050     }
1051     return ret;
1052 }
1053 
1054 static ssize_t handle_aiocb_write_zeroes(RawPosixAIOData *aiocb)
1055 {
1056 #if defined(CONFIG_FALLOCATE) || defined(CONFIG_XFS)
1057     BDRVRawState *s = aiocb->bs->opaque;
1058 #endif
1059 
1060     if (aiocb->aio_type & QEMU_AIO_BLKDEV) {
1061         return handle_aiocb_write_zeroes_block(aiocb);
1062     }
1063 
1064 #ifdef CONFIG_XFS
1065     if (s->is_xfs) {
1066         return xfs_write_zeroes(s, aiocb->aio_offset, aiocb->aio_nbytes);
1067     }
1068 #endif
1069 
1070 #ifdef CONFIG_FALLOCATE_ZERO_RANGE
1071     if (s->has_write_zeroes) {
1072         int ret = do_fallocate(s->fd, FALLOC_FL_ZERO_RANGE,
1073                                aiocb->aio_offset, aiocb->aio_nbytes);
1074         if (ret == 0 || ret != -ENOTSUP) {
1075             return ret;
1076         }
1077         s->has_write_zeroes = false;
1078     }
1079 #endif
1080 
1081 #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
1082     if (s->has_discard && s->has_fallocate) {
1083         int ret = do_fallocate(s->fd,
1084                                FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
1085                                aiocb->aio_offset, aiocb->aio_nbytes);
1086         if (ret == 0) {
1087             ret = do_fallocate(s->fd, 0, aiocb->aio_offset, aiocb->aio_nbytes);
1088             if (ret == 0 || ret != -ENOTSUP) {
1089                 return ret;
1090             }
1091             s->has_fallocate = false;
1092         } else if (ret != -ENOTSUP) {
1093             return ret;
1094         } else {
1095             s->has_discard = false;
1096         }
1097     }
1098 #endif
1099 
1100 #ifdef CONFIG_FALLOCATE
1101     if (s->has_fallocate && aiocb->aio_offset >= bdrv_getlength(aiocb->bs)) {
1102         int ret = do_fallocate(s->fd, 0, aiocb->aio_offset, aiocb->aio_nbytes);
1103         if (ret == 0 || ret != -ENOTSUP) {
1104             return ret;
1105         }
1106         s->has_fallocate = false;
1107     }
1108 #endif
1109 
1110     return -ENOTSUP;
1111 }
1112 
1113 static ssize_t handle_aiocb_discard(RawPosixAIOData *aiocb)
1114 {
1115     int ret = -EOPNOTSUPP;
1116     BDRVRawState *s = aiocb->bs->opaque;
1117 
1118     if (!s->has_discard) {
1119         return -ENOTSUP;
1120     }
1121 
1122     if (aiocb->aio_type & QEMU_AIO_BLKDEV) {
1123 #ifdef BLKDISCARD
1124         do {
1125             uint64_t range[2] = { aiocb->aio_offset, aiocb->aio_nbytes };
1126             if (ioctl(aiocb->aio_fildes, BLKDISCARD, range) == 0) {
1127                 return 0;
1128             }
1129         } while (errno == EINTR);
1130 
1131         ret = -errno;
1132 #endif
1133     } else {
1134 #ifdef CONFIG_XFS
1135         if (s->is_xfs) {
1136             return xfs_discard(s, aiocb->aio_offset, aiocb->aio_nbytes);
1137         }
1138 #endif
1139 
1140 #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
1141         ret = do_fallocate(s->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
1142                            aiocb->aio_offset, aiocb->aio_nbytes);
1143 #endif
1144     }
1145 
1146     ret = translate_err(ret);
1147     if (ret == -ENOTSUP) {
1148         s->has_discard = false;
1149     }
1150     return ret;
1151 }
1152 
1153 static int aio_worker(void *arg)
1154 {
1155     RawPosixAIOData *aiocb = arg;
1156     ssize_t ret = 0;
1157 
1158     switch (aiocb->aio_type & QEMU_AIO_TYPE_MASK) {
1159     case QEMU_AIO_READ:
1160         ret = handle_aiocb_rw(aiocb);
1161         if (ret >= 0 && ret < aiocb->aio_nbytes) {
1162             iov_memset(aiocb->aio_iov, aiocb->aio_niov, ret,
1163                       0, aiocb->aio_nbytes - ret);
1164 
1165             ret = aiocb->aio_nbytes;
1166         }
1167         if (ret == aiocb->aio_nbytes) {
1168             ret = 0;
1169         } else if (ret >= 0 && ret < aiocb->aio_nbytes) {
1170             ret = -EINVAL;
1171         }
1172         break;
1173     case QEMU_AIO_WRITE:
1174         ret = handle_aiocb_rw(aiocb);
1175         if (ret == aiocb->aio_nbytes) {
1176             ret = 0;
1177         } else if (ret >= 0 && ret < aiocb->aio_nbytes) {
1178             ret = -EINVAL;
1179         }
1180         break;
1181     case QEMU_AIO_FLUSH:
1182         ret = handle_aiocb_flush(aiocb);
1183         break;
1184     case QEMU_AIO_IOCTL:
1185         ret = handle_aiocb_ioctl(aiocb);
1186         break;
1187     case QEMU_AIO_DISCARD:
1188         ret = handle_aiocb_discard(aiocb);
1189         break;
1190     case QEMU_AIO_WRITE_ZEROES:
1191         ret = handle_aiocb_write_zeroes(aiocb);
1192         break;
1193     default:
1194         fprintf(stderr, "invalid aio request (0x%x)\n", aiocb->aio_type);
1195         ret = -EINVAL;
1196         break;
1197     }
1198 
1199     g_free(aiocb);
1200     return ret;
1201 }
1202 
1203 static int paio_submit_co(BlockDriverState *bs, int fd,
1204                           int64_t offset, QEMUIOVector *qiov,
1205                           int count, int type)
1206 {
1207     RawPosixAIOData *acb = g_new(RawPosixAIOData, 1);
1208     ThreadPool *pool;
1209 
1210     acb->bs = bs;
1211     acb->aio_type = type;
1212     acb->aio_fildes = fd;
1213 
1214     acb->aio_nbytes = count;
1215     acb->aio_offset = offset;
1216 
1217     if (qiov) {
1218         acb->aio_iov = qiov->iov;
1219         acb->aio_niov = qiov->niov;
1220         assert(qiov->size == count);
1221     }
1222 
1223     trace_paio_submit_co(offset, count, type);
1224     pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
1225     return thread_pool_submit_co(pool, aio_worker, acb);
1226 }
1227 
1228 static BlockAIOCB *paio_submit(BlockDriverState *bs, int fd,
1229         int64_t offset, QEMUIOVector *qiov, int count,
1230         BlockCompletionFunc *cb, void *opaque, int type)
1231 {
1232     RawPosixAIOData *acb = g_new(RawPosixAIOData, 1);
1233     ThreadPool *pool;
1234 
1235     acb->bs = bs;
1236     acb->aio_type = type;
1237     acb->aio_fildes = fd;
1238 
1239     acb->aio_nbytes = count;
1240     acb->aio_offset = offset;
1241 
1242     if (qiov) {
1243         acb->aio_iov = qiov->iov;
1244         acb->aio_niov = qiov->niov;
1245         assert(qiov->size == acb->aio_nbytes);
1246     }
1247 
1248     trace_paio_submit(acb, opaque, offset, count, type);
1249     pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
1250     return thread_pool_submit_aio(pool, aio_worker, acb, cb, opaque);
1251 }
1252 
1253 static int coroutine_fn raw_co_prw(BlockDriverState *bs, uint64_t offset,
1254                                    uint64_t bytes, QEMUIOVector *qiov, int type)
1255 {
1256     BDRVRawState *s = bs->opaque;
1257 
1258     if (fd_open(bs) < 0)
1259         return -EIO;
1260 
1261     /*
1262      * Check if the underlying device requires requests to be aligned,
1263      * and if the request we are trying to submit is aligned or not.
1264      * If this is the case tell the low-level driver that it needs
1265      * to copy the buffer.
1266      */
1267     if (s->needs_alignment) {
1268         if (!bdrv_qiov_is_aligned(bs, qiov)) {
1269             type |= QEMU_AIO_MISALIGNED;
1270 #ifdef CONFIG_LINUX_AIO
1271         } else if (s->use_linux_aio) {
1272             LinuxAioState *aio = aio_get_linux_aio(bdrv_get_aio_context(bs));
1273             assert(qiov->size == bytes);
1274             return laio_co_submit(bs, aio, s->fd, offset, qiov, type);
1275 #endif
1276         }
1277     }
1278 
1279     return paio_submit_co(bs, s->fd, offset, qiov, bytes, type);
1280 }
1281 
1282 static int coroutine_fn raw_co_preadv(BlockDriverState *bs, uint64_t offset,
1283                                       uint64_t bytes, QEMUIOVector *qiov,
1284                                       int flags)
1285 {
1286     return raw_co_prw(bs, offset, bytes, qiov, QEMU_AIO_READ);
1287 }
1288 
1289 static int coroutine_fn raw_co_pwritev(BlockDriverState *bs, uint64_t offset,
1290                                        uint64_t bytes, QEMUIOVector *qiov,
1291                                        int flags)
1292 {
1293     assert(flags == 0);
1294     return raw_co_prw(bs, offset, bytes, qiov, QEMU_AIO_WRITE);
1295 }
1296 
1297 static void raw_aio_plug(BlockDriverState *bs)
1298 {
1299 #ifdef CONFIG_LINUX_AIO
1300     BDRVRawState *s = bs->opaque;
1301     if (s->use_linux_aio) {
1302         LinuxAioState *aio = aio_get_linux_aio(bdrv_get_aio_context(bs));
1303         laio_io_plug(bs, aio);
1304     }
1305 #endif
1306 }
1307 
1308 static void raw_aio_unplug(BlockDriverState *bs)
1309 {
1310 #ifdef CONFIG_LINUX_AIO
1311     BDRVRawState *s = bs->opaque;
1312     if (s->use_linux_aio) {
1313         LinuxAioState *aio = aio_get_linux_aio(bdrv_get_aio_context(bs));
1314         laio_io_unplug(bs, aio);
1315     }
1316 #endif
1317 }
1318 
1319 static BlockAIOCB *raw_aio_flush(BlockDriverState *bs,
1320         BlockCompletionFunc *cb, void *opaque)
1321 {
1322     BDRVRawState *s = bs->opaque;
1323 
1324     if (fd_open(bs) < 0)
1325         return NULL;
1326 
1327     return paio_submit(bs, s->fd, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH);
1328 }
1329 
1330 static void raw_close(BlockDriverState *bs)
1331 {
1332     BDRVRawState *s = bs->opaque;
1333 
1334     if (s->fd >= 0) {
1335         qemu_close(s->fd);
1336         s->fd = -1;
1337     }
1338 }
1339 
1340 static int raw_truncate(BlockDriverState *bs, int64_t offset)
1341 {
1342     BDRVRawState *s = bs->opaque;
1343     struct stat st;
1344 
1345     if (fstat(s->fd, &st)) {
1346         return -errno;
1347     }
1348 
1349     if (S_ISREG(st.st_mode)) {
1350         if (ftruncate(s->fd, offset) < 0) {
1351             return -errno;
1352         }
1353     } else if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
1354        if (offset > raw_getlength(bs)) {
1355            return -EINVAL;
1356        }
1357     } else {
1358         return -ENOTSUP;
1359     }
1360 
1361     return 0;
1362 }
1363 
1364 #ifdef __OpenBSD__
1365 static int64_t raw_getlength(BlockDriverState *bs)
1366 {
1367     BDRVRawState *s = bs->opaque;
1368     int fd = s->fd;
1369     struct stat st;
1370 
1371     if (fstat(fd, &st))
1372         return -errno;
1373     if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
1374         struct disklabel dl;
1375 
1376         if (ioctl(fd, DIOCGDINFO, &dl))
1377             return -errno;
1378         return (uint64_t)dl.d_secsize *
1379             dl.d_partitions[DISKPART(st.st_rdev)].p_size;
1380     } else
1381         return st.st_size;
1382 }
1383 #elif defined(__NetBSD__)
1384 static int64_t raw_getlength(BlockDriverState *bs)
1385 {
1386     BDRVRawState *s = bs->opaque;
1387     int fd = s->fd;
1388     struct stat st;
1389 
1390     if (fstat(fd, &st))
1391         return -errno;
1392     if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
1393         struct dkwedge_info dkw;
1394 
1395         if (ioctl(fd, DIOCGWEDGEINFO, &dkw) != -1) {
1396             return dkw.dkw_size * 512;
1397         } else {
1398             struct disklabel dl;
1399 
1400             if (ioctl(fd, DIOCGDINFO, &dl))
1401                 return -errno;
1402             return (uint64_t)dl.d_secsize *
1403                 dl.d_partitions[DISKPART(st.st_rdev)].p_size;
1404         }
1405     } else
1406         return st.st_size;
1407 }
1408 #elif defined(__sun__)
1409 static int64_t raw_getlength(BlockDriverState *bs)
1410 {
1411     BDRVRawState *s = bs->opaque;
1412     struct dk_minfo minfo;
1413     int ret;
1414     int64_t size;
1415 
1416     ret = fd_open(bs);
1417     if (ret < 0) {
1418         return ret;
1419     }
1420 
1421     /*
1422      * Use the DKIOCGMEDIAINFO ioctl to read the size.
1423      */
1424     ret = ioctl(s->fd, DKIOCGMEDIAINFO, &minfo);
1425     if (ret != -1) {
1426         return minfo.dki_lbsize * minfo.dki_capacity;
1427     }
1428 
1429     /*
1430      * There are reports that lseek on some devices fails, but
1431      * irc discussion said that contingency on contingency was overkill.
1432      */
1433     size = lseek(s->fd, 0, SEEK_END);
1434     if (size < 0) {
1435         return -errno;
1436     }
1437     return size;
1438 }
1439 #elif defined(CONFIG_BSD)
1440 static int64_t raw_getlength(BlockDriverState *bs)
1441 {
1442     BDRVRawState *s = bs->opaque;
1443     int fd = s->fd;
1444     int64_t size;
1445     struct stat sb;
1446 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
1447     int reopened = 0;
1448 #endif
1449     int ret;
1450 
1451     ret = fd_open(bs);
1452     if (ret < 0)
1453         return ret;
1454 
1455 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
1456 again:
1457 #endif
1458     if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
1459 #ifdef DIOCGMEDIASIZE
1460 	if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
1461 #elif defined(DIOCGPART)
1462         {
1463                 struct partinfo pi;
1464                 if (ioctl(fd, DIOCGPART, &pi) == 0)
1465                         size = pi.media_size;
1466                 else
1467                         size = 0;
1468         }
1469         if (size == 0)
1470 #endif
1471 #if defined(__APPLE__) && defined(__MACH__)
1472         {
1473             uint64_t sectors = 0;
1474             uint32_t sector_size = 0;
1475 
1476             if (ioctl(fd, DKIOCGETBLOCKCOUNT, &sectors) == 0
1477                && ioctl(fd, DKIOCGETBLOCKSIZE, &sector_size) == 0) {
1478                 size = sectors * sector_size;
1479             } else {
1480                 size = lseek(fd, 0LL, SEEK_END);
1481                 if (size < 0) {
1482                     return -errno;
1483                 }
1484             }
1485         }
1486 #else
1487         size = lseek(fd, 0LL, SEEK_END);
1488         if (size < 0) {
1489             return -errno;
1490         }
1491 #endif
1492 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1493         switch(s->type) {
1494         case FTYPE_CD:
1495             /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
1496             if (size == 2048LL * (unsigned)-1)
1497                 size = 0;
1498             /* XXX no disc?  maybe we need to reopen... */
1499             if (size <= 0 && !reopened && cdrom_reopen(bs) >= 0) {
1500                 reopened = 1;
1501                 goto again;
1502             }
1503         }
1504 #endif
1505     } else {
1506         size = lseek(fd, 0, SEEK_END);
1507         if (size < 0) {
1508             return -errno;
1509         }
1510     }
1511     return size;
1512 }
1513 #else
1514 static int64_t raw_getlength(BlockDriverState *bs)
1515 {
1516     BDRVRawState *s = bs->opaque;
1517     int ret;
1518     int64_t size;
1519 
1520     ret = fd_open(bs);
1521     if (ret < 0) {
1522         return ret;
1523     }
1524 
1525     size = lseek(s->fd, 0, SEEK_END);
1526     if (size < 0) {
1527         return -errno;
1528     }
1529     return size;
1530 }
1531 #endif
1532 
1533 static int64_t raw_get_allocated_file_size(BlockDriverState *bs)
1534 {
1535     struct stat st;
1536     BDRVRawState *s = bs->opaque;
1537 
1538     if (fstat(s->fd, &st) < 0) {
1539         return -errno;
1540     }
1541     return (int64_t)st.st_blocks * 512;
1542 }
1543 
1544 static int raw_create(const char *filename, QemuOpts *opts, Error **errp)
1545 {
1546     int fd;
1547     int result = 0;
1548     int64_t total_size = 0;
1549     bool nocow = false;
1550     PreallocMode prealloc;
1551     char *buf = NULL;
1552     Error *local_err = NULL;
1553 
1554     strstart(filename, "file:", &filename);
1555 
1556     /* Read out options */
1557     total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
1558                           BDRV_SECTOR_SIZE);
1559     nocow = qemu_opt_get_bool(opts, BLOCK_OPT_NOCOW, false);
1560     buf = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC);
1561     prealloc = qapi_enum_parse(PreallocMode_lookup, buf,
1562                                PREALLOC_MODE__MAX, PREALLOC_MODE_OFF,
1563                                &local_err);
1564     g_free(buf);
1565     if (local_err) {
1566         error_propagate(errp, local_err);
1567         result = -EINVAL;
1568         goto out;
1569     }
1570 
1571     fd = qemu_open(filename, O_RDWR | O_CREAT | O_TRUNC | O_BINARY,
1572                    0644);
1573     if (fd < 0) {
1574         result = -errno;
1575         error_setg_errno(errp, -result, "Could not create file");
1576         goto out;
1577     }
1578 
1579     if (nocow) {
1580 #ifdef __linux__
1581         /* Set NOCOW flag to solve performance issue on fs like btrfs.
1582          * This is an optimisation. The FS_IOC_SETFLAGS ioctl return value
1583          * will be ignored since any failure of this operation should not
1584          * block the left work.
1585          */
1586         int attr;
1587         if (ioctl(fd, FS_IOC_GETFLAGS, &attr) == 0) {
1588             attr |= FS_NOCOW_FL;
1589             ioctl(fd, FS_IOC_SETFLAGS, &attr);
1590         }
1591 #endif
1592     }
1593 
1594     switch (prealloc) {
1595 #ifdef CONFIG_POSIX_FALLOCATE
1596     case PREALLOC_MODE_FALLOC:
1597         /*
1598          * Truncating before posix_fallocate() makes it about twice slower on
1599          * file systems that do not support fallocate(), trying to check if a
1600          * block is allocated before allocating it, so don't do that here.
1601          */
1602         result = -posix_fallocate(fd, 0, total_size);
1603         if (result != 0) {
1604             /* posix_fallocate() doesn't set errno. */
1605             error_setg_errno(errp, -result,
1606                              "Could not preallocate data for the new file");
1607         }
1608         break;
1609 #endif
1610     case PREALLOC_MODE_FULL:
1611     {
1612         /*
1613          * Knowing the final size from the beginning could allow the file
1614          * system driver to do less allocations and possibly avoid
1615          * fragmentation of the file.
1616          */
1617         if (ftruncate(fd, total_size) != 0) {
1618             result = -errno;
1619             error_setg_errno(errp, -result, "Could not resize file");
1620             goto out_close;
1621         }
1622 
1623         int64_t num = 0, left = total_size;
1624         buf = g_malloc0(65536);
1625 
1626         while (left > 0) {
1627             num = MIN(left, 65536);
1628             result = write(fd, buf, num);
1629             if (result < 0) {
1630                 result = -errno;
1631                 error_setg_errno(errp, -result,
1632                                  "Could not write to the new file");
1633                 break;
1634             }
1635             left -= result;
1636         }
1637         if (result >= 0) {
1638             result = fsync(fd);
1639             if (result < 0) {
1640                 result = -errno;
1641                 error_setg_errno(errp, -result,
1642                                  "Could not flush new file to disk");
1643             }
1644         }
1645         g_free(buf);
1646         break;
1647     }
1648     case PREALLOC_MODE_OFF:
1649         if (ftruncate(fd, total_size) != 0) {
1650             result = -errno;
1651             error_setg_errno(errp, -result, "Could not resize file");
1652         }
1653         break;
1654     default:
1655         result = -EINVAL;
1656         error_setg(errp, "Unsupported preallocation mode: %s",
1657                    PreallocMode_lookup[prealloc]);
1658         break;
1659     }
1660 
1661 out_close:
1662     if (qemu_close(fd) != 0 && result == 0) {
1663         result = -errno;
1664         error_setg_errno(errp, -result, "Could not close the new file");
1665     }
1666 out:
1667     return result;
1668 }
1669 
1670 /*
1671  * Find allocation range in @bs around offset @start.
1672  * May change underlying file descriptor's file offset.
1673  * If @start is not in a hole, store @start in @data, and the
1674  * beginning of the next hole in @hole, and return 0.
1675  * If @start is in a non-trailing hole, store @start in @hole and the
1676  * beginning of the next non-hole in @data, and return 0.
1677  * If @start is in a trailing hole or beyond EOF, return -ENXIO.
1678  * If we can't find out, return a negative errno other than -ENXIO.
1679  */
1680 static int find_allocation(BlockDriverState *bs, off_t start,
1681                            off_t *data, off_t *hole)
1682 {
1683 #if defined SEEK_HOLE && defined SEEK_DATA
1684     BDRVRawState *s = bs->opaque;
1685     off_t offs;
1686 
1687     /*
1688      * SEEK_DATA cases:
1689      * D1. offs == start: start is in data
1690      * D2. offs > start: start is in a hole, next data at offs
1691      * D3. offs < 0, errno = ENXIO: either start is in a trailing hole
1692      *                              or start is beyond EOF
1693      *     If the latter happens, the file has been truncated behind
1694      *     our back since we opened it.  All bets are off then.
1695      *     Treating like a trailing hole is simplest.
1696      * D4. offs < 0, errno != ENXIO: we learned nothing
1697      */
1698     offs = lseek(s->fd, start, SEEK_DATA);
1699     if (offs < 0) {
1700         return -errno;          /* D3 or D4 */
1701     }
1702     assert(offs >= start);
1703 
1704     if (offs > start) {
1705         /* D2: in hole, next data at offs */
1706         *hole = start;
1707         *data = offs;
1708         return 0;
1709     }
1710 
1711     /* D1: in data, end not yet known */
1712 
1713     /*
1714      * SEEK_HOLE cases:
1715      * H1. offs == start: start is in a hole
1716      *     If this happens here, a hole has been dug behind our back
1717      *     since the previous lseek().
1718      * H2. offs > start: either start is in data, next hole at offs,
1719      *                   or start is in trailing hole, EOF at offs
1720      *     Linux treats trailing holes like any other hole: offs ==
1721      *     start.  Solaris seeks to EOF instead: offs > start (blech).
1722      *     If that happens here, a hole has been dug behind our back
1723      *     since the previous lseek().
1724      * H3. offs < 0, errno = ENXIO: start is beyond EOF
1725      *     If this happens, the file has been truncated behind our
1726      *     back since we opened it.  Treat it like a trailing hole.
1727      * H4. offs < 0, errno != ENXIO: we learned nothing
1728      *     Pretend we know nothing at all, i.e. "forget" about D1.
1729      */
1730     offs = lseek(s->fd, start, SEEK_HOLE);
1731     if (offs < 0) {
1732         return -errno;          /* D1 and (H3 or H4) */
1733     }
1734     assert(offs >= start);
1735 
1736     if (offs > start) {
1737         /*
1738          * D1 and H2: either in data, next hole at offs, or it was in
1739          * data but is now in a trailing hole.  In the latter case,
1740          * all bets are off.  Treating it as if it there was data all
1741          * the way to EOF is safe, so simply do that.
1742          */
1743         *data = start;
1744         *hole = offs;
1745         return 0;
1746     }
1747 
1748     /* D1 and H1 */
1749     return -EBUSY;
1750 #else
1751     return -ENOTSUP;
1752 #endif
1753 }
1754 
1755 /*
1756  * Returns the allocation status of the specified sectors.
1757  *
1758  * If 'sector_num' is beyond the end of the disk image the return value is 0
1759  * and 'pnum' is set to 0.
1760  *
1761  * 'pnum' is set to the number of sectors (including and immediately following
1762  * the specified sector) that are known to be in the same
1763  * allocated/unallocated state.
1764  *
1765  * 'nb_sectors' is the max value 'pnum' should be set to.  If nb_sectors goes
1766  * beyond the end of the disk image it will be clamped.
1767  */
1768 static int64_t coroutine_fn raw_co_get_block_status(BlockDriverState *bs,
1769                                                     int64_t sector_num,
1770                                                     int nb_sectors, int *pnum,
1771                                                     BlockDriverState **file)
1772 {
1773     off_t start, data = 0, hole = 0;
1774     int64_t total_size;
1775     int ret;
1776 
1777     ret = fd_open(bs);
1778     if (ret < 0) {
1779         return ret;
1780     }
1781 
1782     start = sector_num * BDRV_SECTOR_SIZE;
1783     total_size = bdrv_getlength(bs);
1784     if (total_size < 0) {
1785         return total_size;
1786     } else if (start >= total_size) {
1787         *pnum = 0;
1788         return 0;
1789     } else if (start + nb_sectors * BDRV_SECTOR_SIZE > total_size) {
1790         nb_sectors = DIV_ROUND_UP(total_size - start, BDRV_SECTOR_SIZE);
1791     }
1792 
1793     ret = find_allocation(bs, start, &data, &hole);
1794     if (ret == -ENXIO) {
1795         /* Trailing hole */
1796         *pnum = nb_sectors;
1797         ret = BDRV_BLOCK_ZERO;
1798     } else if (ret < 0) {
1799         /* No info available, so pretend there are no holes */
1800         *pnum = nb_sectors;
1801         ret = BDRV_BLOCK_DATA;
1802     } else if (data == start) {
1803         /* On a data extent, compute sectors to the end of the extent,
1804          * possibly including a partial sector at EOF. */
1805         *pnum = MIN(nb_sectors, DIV_ROUND_UP(hole - start, BDRV_SECTOR_SIZE));
1806         ret = BDRV_BLOCK_DATA;
1807     } else {
1808         /* On a hole, compute sectors to the beginning of the next extent.  */
1809         assert(hole == start);
1810         *pnum = MIN(nb_sectors, (data - start) / BDRV_SECTOR_SIZE);
1811         ret = BDRV_BLOCK_ZERO;
1812     }
1813     *file = bs;
1814     return ret | BDRV_BLOCK_OFFSET_VALID | start;
1815 }
1816 
1817 static coroutine_fn BlockAIOCB *raw_aio_pdiscard(BlockDriverState *bs,
1818     int64_t offset, int count,
1819     BlockCompletionFunc *cb, void *opaque)
1820 {
1821     BDRVRawState *s = bs->opaque;
1822 
1823     return paio_submit(bs, s->fd, offset, NULL, count,
1824                        cb, opaque, QEMU_AIO_DISCARD);
1825 }
1826 
1827 static int coroutine_fn raw_co_pwrite_zeroes(
1828     BlockDriverState *bs, int64_t offset,
1829     int count, BdrvRequestFlags flags)
1830 {
1831     BDRVRawState *s = bs->opaque;
1832 
1833     if (!(flags & BDRV_REQ_MAY_UNMAP)) {
1834         return paio_submit_co(bs, s->fd, offset, NULL, count,
1835                               QEMU_AIO_WRITE_ZEROES);
1836     } else if (s->discard_zeroes) {
1837         return paio_submit_co(bs, s->fd, offset, NULL, count,
1838                               QEMU_AIO_DISCARD);
1839     }
1840     return -ENOTSUP;
1841 }
1842 
1843 static int raw_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1844 {
1845     BDRVRawState *s = bs->opaque;
1846 
1847     bdi->unallocated_blocks_are_zero = s->discard_zeroes;
1848     bdi->can_write_zeroes_with_unmap = s->discard_zeroes;
1849     return 0;
1850 }
1851 
1852 static QemuOptsList raw_create_opts = {
1853     .name = "raw-create-opts",
1854     .head = QTAILQ_HEAD_INITIALIZER(raw_create_opts.head),
1855     .desc = {
1856         {
1857             .name = BLOCK_OPT_SIZE,
1858             .type = QEMU_OPT_SIZE,
1859             .help = "Virtual disk size"
1860         },
1861         {
1862             .name = BLOCK_OPT_NOCOW,
1863             .type = QEMU_OPT_BOOL,
1864             .help = "Turn off copy-on-write (valid only on btrfs)"
1865         },
1866         {
1867             .name = BLOCK_OPT_PREALLOC,
1868             .type = QEMU_OPT_STRING,
1869             .help = "Preallocation mode (allowed values: off, falloc, full)"
1870         },
1871         { /* end of list */ }
1872     }
1873 };
1874 
1875 BlockDriver bdrv_file = {
1876     .format_name = "file",
1877     .protocol_name = "file",
1878     .instance_size = sizeof(BDRVRawState),
1879     .bdrv_needs_filename = true,
1880     .bdrv_probe = NULL, /* no probe for protocols */
1881     .bdrv_parse_filename = raw_parse_filename,
1882     .bdrv_file_open = raw_open,
1883     .bdrv_reopen_prepare = raw_reopen_prepare,
1884     .bdrv_reopen_commit = raw_reopen_commit,
1885     .bdrv_reopen_abort = raw_reopen_abort,
1886     .bdrv_close = raw_close,
1887     .bdrv_create = raw_create,
1888     .bdrv_has_zero_init = bdrv_has_zero_init_1,
1889     .bdrv_co_get_block_status = raw_co_get_block_status,
1890     .bdrv_co_pwrite_zeroes = raw_co_pwrite_zeroes,
1891 
1892     .bdrv_co_preadv         = raw_co_preadv,
1893     .bdrv_co_pwritev        = raw_co_pwritev,
1894     .bdrv_aio_flush = raw_aio_flush,
1895     .bdrv_aio_pdiscard = raw_aio_pdiscard,
1896     .bdrv_refresh_limits = raw_refresh_limits,
1897     .bdrv_io_plug = raw_aio_plug,
1898     .bdrv_io_unplug = raw_aio_unplug,
1899 
1900     .bdrv_truncate = raw_truncate,
1901     .bdrv_getlength = raw_getlength,
1902     .bdrv_get_info = raw_get_info,
1903     .bdrv_get_allocated_file_size
1904                         = raw_get_allocated_file_size,
1905 
1906     .create_opts = &raw_create_opts,
1907 };
1908 
1909 /***********************************************/
1910 /* host device */
1911 
1912 #if defined(__APPLE__) && defined(__MACH__)
1913 static kern_return_t GetBSDPath(io_iterator_t mediaIterator, char *bsdPath,
1914                                 CFIndex maxPathSize, int flags);
1915 static char *FindEjectableOpticalMedia(io_iterator_t *mediaIterator)
1916 {
1917     kern_return_t kernResult = KERN_FAILURE;
1918     mach_port_t     masterPort;
1919     CFMutableDictionaryRef  classesToMatch;
1920     const char *matching_array[] = {kIODVDMediaClass, kIOCDMediaClass};
1921     char *mediaType = NULL;
1922 
1923     kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
1924     if ( KERN_SUCCESS != kernResult ) {
1925         printf( "IOMasterPort returned %d\n", kernResult );
1926     }
1927 
1928     int index;
1929     for (index = 0; index < ARRAY_SIZE(matching_array); index++) {
1930         classesToMatch = IOServiceMatching(matching_array[index]);
1931         if (classesToMatch == NULL) {
1932             error_report("IOServiceMatching returned NULL for %s",
1933                          matching_array[index]);
1934             continue;
1935         }
1936         CFDictionarySetValue(classesToMatch, CFSTR(kIOMediaEjectableKey),
1937                              kCFBooleanTrue);
1938         kernResult = IOServiceGetMatchingServices(masterPort, classesToMatch,
1939                                                   mediaIterator);
1940         if (kernResult != KERN_SUCCESS) {
1941             error_report("Note: IOServiceGetMatchingServices returned %d",
1942                          kernResult);
1943             continue;
1944         }
1945 
1946         /* If a match was found, leave the loop */
1947         if (*mediaIterator != 0) {
1948             DPRINTF("Matching using %s\n", matching_array[index]);
1949             mediaType = g_strdup(matching_array[index]);
1950             break;
1951         }
1952     }
1953     return mediaType;
1954 }
1955 
1956 kern_return_t GetBSDPath(io_iterator_t mediaIterator, char *bsdPath,
1957                          CFIndex maxPathSize, int flags)
1958 {
1959     io_object_t     nextMedia;
1960     kern_return_t   kernResult = KERN_FAILURE;
1961     *bsdPath = '\0';
1962     nextMedia = IOIteratorNext( mediaIterator );
1963     if ( nextMedia )
1964     {
1965         CFTypeRef   bsdPathAsCFString;
1966     bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
1967         if ( bsdPathAsCFString ) {
1968             size_t devPathLength;
1969             strcpy( bsdPath, _PATH_DEV );
1970             if (flags & BDRV_O_NOCACHE) {
1971                 strcat(bsdPath, "r");
1972             }
1973             devPathLength = strlen( bsdPath );
1974             if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
1975                 kernResult = KERN_SUCCESS;
1976             }
1977             CFRelease( bsdPathAsCFString );
1978         }
1979         IOObjectRelease( nextMedia );
1980     }
1981 
1982     return kernResult;
1983 }
1984 
1985 /* Sets up a real cdrom for use in QEMU */
1986 static bool setup_cdrom(char *bsd_path, Error **errp)
1987 {
1988     int index, num_of_test_partitions = 2, fd;
1989     char test_partition[MAXPATHLEN];
1990     bool partition_found = false;
1991 
1992     /* look for a working partition */
1993     for (index = 0; index < num_of_test_partitions; index++) {
1994         snprintf(test_partition, sizeof(test_partition), "%ss%d", bsd_path,
1995                  index);
1996         fd = qemu_open(test_partition, O_RDONLY | O_BINARY | O_LARGEFILE);
1997         if (fd >= 0) {
1998             partition_found = true;
1999             qemu_close(fd);
2000             break;
2001         }
2002     }
2003 
2004     /* if a working partition on the device was not found */
2005     if (partition_found == false) {
2006         error_setg(errp, "Failed to find a working partition on disc");
2007     } else {
2008         DPRINTF("Using %s as optical disc\n", test_partition);
2009         pstrcpy(bsd_path, MAXPATHLEN, test_partition);
2010     }
2011     return partition_found;
2012 }
2013 
2014 /* Prints directions on mounting and unmounting a device */
2015 static void print_unmounting_directions(const char *file_name)
2016 {
2017     error_report("If device %s is mounted on the desktop, unmount"
2018                  " it first before using it in QEMU", file_name);
2019     error_report("Command to unmount device: diskutil unmountDisk %s",
2020                  file_name);
2021     error_report("Command to mount device: diskutil mountDisk %s", file_name);
2022 }
2023 
2024 #endif /* defined(__APPLE__) && defined(__MACH__) */
2025 
2026 static int hdev_probe_device(const char *filename)
2027 {
2028     struct stat st;
2029 
2030     /* allow a dedicated CD-ROM driver to match with a higher priority */
2031     if (strstart(filename, "/dev/cdrom", NULL))
2032         return 50;
2033 
2034     if (stat(filename, &st) >= 0 &&
2035             (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
2036         return 100;
2037     }
2038 
2039     return 0;
2040 }
2041 
2042 static int check_hdev_writable(BDRVRawState *s)
2043 {
2044 #if defined(BLKROGET)
2045     /* Linux block devices can be configured "read-only" using blockdev(8).
2046      * This is independent of device node permissions and therefore open(2)
2047      * with O_RDWR succeeds.  Actual writes fail with EPERM.
2048      *
2049      * bdrv_open() is supposed to fail if the disk is read-only.  Explicitly
2050      * check for read-only block devices so that Linux block devices behave
2051      * properly.
2052      */
2053     struct stat st;
2054     int readonly = 0;
2055 
2056     if (fstat(s->fd, &st)) {
2057         return -errno;
2058     }
2059 
2060     if (!S_ISBLK(st.st_mode)) {
2061         return 0;
2062     }
2063 
2064     if (ioctl(s->fd, BLKROGET, &readonly) < 0) {
2065         return -errno;
2066     }
2067 
2068     if (readonly) {
2069         return -EACCES;
2070     }
2071 #endif /* defined(BLKROGET) */
2072     return 0;
2073 }
2074 
2075 static void hdev_parse_filename(const char *filename, QDict *options,
2076                                 Error **errp)
2077 {
2078     /* The prefix is optional, just as for "file". */
2079     strstart(filename, "host_device:", &filename);
2080 
2081     qdict_put_obj(options, "filename", QOBJECT(qstring_from_str(filename)));
2082 }
2083 
2084 static bool hdev_is_sg(BlockDriverState *bs)
2085 {
2086 
2087 #if defined(__linux__)
2088 
2089     BDRVRawState *s = bs->opaque;
2090     struct stat st;
2091     struct sg_scsi_id scsiid;
2092     int sg_version;
2093     int ret;
2094 
2095     if (stat(bs->filename, &st) < 0 || !S_ISCHR(st.st_mode)) {
2096         return false;
2097     }
2098 
2099     ret = ioctl(s->fd, SG_GET_VERSION_NUM, &sg_version);
2100     if (ret < 0) {
2101         return false;
2102     }
2103 
2104     ret = ioctl(s->fd, SG_GET_SCSI_ID, &scsiid);
2105     if (ret >= 0) {
2106         DPRINTF("SG device found: type=%d, version=%d\n",
2107             scsiid.scsi_type, sg_version);
2108         return true;
2109     }
2110 
2111 #endif
2112 
2113     return false;
2114 }
2115 
2116 static int hdev_open(BlockDriverState *bs, QDict *options, int flags,
2117                      Error **errp)
2118 {
2119     BDRVRawState *s = bs->opaque;
2120     Error *local_err = NULL;
2121     int ret;
2122 
2123 #if defined(__APPLE__) && defined(__MACH__)
2124     const char *filename = qdict_get_str(options, "filename");
2125     char bsd_path[MAXPATHLEN] = "";
2126     bool error_occurred = false;
2127 
2128     /* If using a real cdrom */
2129     if (strcmp(filename, "/dev/cdrom") == 0) {
2130         char *mediaType = NULL;
2131         kern_return_t ret_val;
2132         io_iterator_t mediaIterator = 0;
2133 
2134         mediaType = FindEjectableOpticalMedia(&mediaIterator);
2135         if (mediaType == NULL) {
2136             error_setg(errp, "Please make sure your CD/DVD is in the optical"
2137                        " drive");
2138             error_occurred = true;
2139             goto hdev_open_Mac_error;
2140         }
2141 
2142         ret_val = GetBSDPath(mediaIterator, bsd_path, sizeof(bsd_path), flags);
2143         if (ret_val != KERN_SUCCESS) {
2144             error_setg(errp, "Could not get BSD path for optical drive");
2145             error_occurred = true;
2146             goto hdev_open_Mac_error;
2147         }
2148 
2149         /* If a real optical drive was not found */
2150         if (bsd_path[0] == '\0') {
2151             error_setg(errp, "Failed to obtain bsd path for optical drive");
2152             error_occurred = true;
2153             goto hdev_open_Mac_error;
2154         }
2155 
2156         /* If using a cdrom disc and finding a partition on the disc failed */
2157         if (strncmp(mediaType, kIOCDMediaClass, 9) == 0 &&
2158             setup_cdrom(bsd_path, errp) == false) {
2159             print_unmounting_directions(bsd_path);
2160             error_occurred = true;
2161             goto hdev_open_Mac_error;
2162         }
2163 
2164         qdict_put(options, "filename", qstring_from_str(bsd_path));
2165 
2166 hdev_open_Mac_error:
2167         g_free(mediaType);
2168         if (mediaIterator) {
2169             IOObjectRelease(mediaIterator);
2170         }
2171         if (error_occurred) {
2172             return -ENOENT;
2173         }
2174     }
2175 #endif /* defined(__APPLE__) && defined(__MACH__) */
2176 
2177     s->type = FTYPE_FILE;
2178 
2179     ret = raw_open_common(bs, options, flags, 0, &local_err);
2180     if (ret < 0) {
2181         error_propagate(errp, local_err);
2182 #if defined(__APPLE__) && defined(__MACH__)
2183         if (*bsd_path) {
2184             filename = bsd_path;
2185         }
2186         /* if a physical device experienced an error while being opened */
2187         if (strncmp(filename, "/dev/", 5) == 0) {
2188             print_unmounting_directions(filename);
2189         }
2190 #endif /* defined(__APPLE__) && defined(__MACH__) */
2191         return ret;
2192     }
2193 
2194     /* Since this does ioctl the device must be already opened */
2195     bs->sg = hdev_is_sg(bs);
2196 
2197     if (flags & BDRV_O_RDWR) {
2198         ret = check_hdev_writable(s);
2199         if (ret < 0) {
2200             raw_close(bs);
2201             error_setg_errno(errp, -ret, "The device is not writable");
2202             return ret;
2203         }
2204     }
2205 
2206     return ret;
2207 }
2208 
2209 #if defined(__linux__)
2210 
2211 static BlockAIOCB *hdev_aio_ioctl(BlockDriverState *bs,
2212         unsigned long int req, void *buf,
2213         BlockCompletionFunc *cb, void *opaque)
2214 {
2215     BDRVRawState *s = bs->opaque;
2216     RawPosixAIOData *acb;
2217     ThreadPool *pool;
2218 
2219     if (fd_open(bs) < 0)
2220         return NULL;
2221 
2222     acb = g_new(RawPosixAIOData, 1);
2223     acb->bs = bs;
2224     acb->aio_type = QEMU_AIO_IOCTL;
2225     acb->aio_fildes = s->fd;
2226     acb->aio_offset = 0;
2227     acb->aio_ioctl_buf = buf;
2228     acb->aio_ioctl_cmd = req;
2229     pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
2230     return thread_pool_submit_aio(pool, aio_worker, acb, cb, opaque);
2231 }
2232 #endif /* linux */
2233 
2234 static int fd_open(BlockDriverState *bs)
2235 {
2236     BDRVRawState *s = bs->opaque;
2237 
2238     /* this is just to ensure s->fd is sane (its called by io ops) */
2239     if (s->fd >= 0)
2240         return 0;
2241     return -EIO;
2242 }
2243 
2244 static coroutine_fn BlockAIOCB *hdev_aio_pdiscard(BlockDriverState *bs,
2245     int64_t offset, int count,
2246     BlockCompletionFunc *cb, void *opaque)
2247 {
2248     BDRVRawState *s = bs->opaque;
2249 
2250     if (fd_open(bs) < 0) {
2251         return NULL;
2252     }
2253     return paio_submit(bs, s->fd, offset, NULL, count,
2254                        cb, opaque, QEMU_AIO_DISCARD|QEMU_AIO_BLKDEV);
2255 }
2256 
2257 static coroutine_fn int hdev_co_pwrite_zeroes(BlockDriverState *bs,
2258     int64_t offset, int count, BdrvRequestFlags flags)
2259 {
2260     BDRVRawState *s = bs->opaque;
2261     int rc;
2262 
2263     rc = fd_open(bs);
2264     if (rc < 0) {
2265         return rc;
2266     }
2267     if (!(flags & BDRV_REQ_MAY_UNMAP)) {
2268         return paio_submit_co(bs, s->fd, offset, NULL, count,
2269                               QEMU_AIO_WRITE_ZEROES|QEMU_AIO_BLKDEV);
2270     } else if (s->discard_zeroes) {
2271         return paio_submit_co(bs, s->fd, offset, NULL, count,
2272                               QEMU_AIO_DISCARD|QEMU_AIO_BLKDEV);
2273     }
2274     return -ENOTSUP;
2275 }
2276 
2277 static int hdev_create(const char *filename, QemuOpts *opts,
2278                        Error **errp)
2279 {
2280     int fd;
2281     int ret = 0;
2282     struct stat stat_buf;
2283     int64_t total_size = 0;
2284     bool has_prefix;
2285 
2286     /* This function is used by both protocol block drivers and therefore either
2287      * of these prefixes may be given.
2288      * The return value has to be stored somewhere, otherwise this is an error
2289      * due to -Werror=unused-value. */
2290     has_prefix =
2291         strstart(filename, "host_device:", &filename) ||
2292         strstart(filename, "host_cdrom:" , &filename);
2293 
2294     (void)has_prefix;
2295 
2296     ret = raw_normalize_devicepath(&filename);
2297     if (ret < 0) {
2298         error_setg_errno(errp, -ret, "Could not normalize device path");
2299         return ret;
2300     }
2301 
2302     /* Read out options */
2303     total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
2304                           BDRV_SECTOR_SIZE);
2305 
2306     fd = qemu_open(filename, O_WRONLY | O_BINARY);
2307     if (fd < 0) {
2308         ret = -errno;
2309         error_setg_errno(errp, -ret, "Could not open device");
2310         return ret;
2311     }
2312 
2313     if (fstat(fd, &stat_buf) < 0) {
2314         ret = -errno;
2315         error_setg_errno(errp, -ret, "Could not stat device");
2316     } else if (!S_ISBLK(stat_buf.st_mode) && !S_ISCHR(stat_buf.st_mode)) {
2317         error_setg(errp,
2318                    "The given file is neither a block nor a character device");
2319         ret = -ENODEV;
2320     } else if (lseek(fd, 0, SEEK_END) < total_size) {
2321         error_setg(errp, "Device is too small");
2322         ret = -ENOSPC;
2323     }
2324 
2325     qemu_close(fd);
2326     return ret;
2327 }
2328 
2329 static BlockDriver bdrv_host_device = {
2330     .format_name        = "host_device",
2331     .protocol_name        = "host_device",
2332     .instance_size      = sizeof(BDRVRawState),
2333     .bdrv_needs_filename = true,
2334     .bdrv_probe_device  = hdev_probe_device,
2335     .bdrv_parse_filename = hdev_parse_filename,
2336     .bdrv_file_open     = hdev_open,
2337     .bdrv_close         = raw_close,
2338     .bdrv_reopen_prepare = raw_reopen_prepare,
2339     .bdrv_reopen_commit  = raw_reopen_commit,
2340     .bdrv_reopen_abort   = raw_reopen_abort,
2341     .bdrv_create         = hdev_create,
2342     .create_opts         = &raw_create_opts,
2343     .bdrv_co_pwrite_zeroes = hdev_co_pwrite_zeroes,
2344 
2345     .bdrv_co_preadv         = raw_co_preadv,
2346     .bdrv_co_pwritev        = raw_co_pwritev,
2347     .bdrv_aio_flush	= raw_aio_flush,
2348     .bdrv_aio_pdiscard   = hdev_aio_pdiscard,
2349     .bdrv_refresh_limits = raw_refresh_limits,
2350     .bdrv_io_plug = raw_aio_plug,
2351     .bdrv_io_unplug = raw_aio_unplug,
2352 
2353     .bdrv_truncate      = raw_truncate,
2354     .bdrv_getlength	= raw_getlength,
2355     .bdrv_get_info = raw_get_info,
2356     .bdrv_get_allocated_file_size
2357                         = raw_get_allocated_file_size,
2358     .bdrv_probe_blocksizes = hdev_probe_blocksizes,
2359     .bdrv_probe_geometry = hdev_probe_geometry,
2360 
2361     /* generic scsi device */
2362 #ifdef __linux__
2363     .bdrv_aio_ioctl     = hdev_aio_ioctl,
2364 #endif
2365 };
2366 
2367 #if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
2368 static void cdrom_parse_filename(const char *filename, QDict *options,
2369                                  Error **errp)
2370 {
2371     /* The prefix is optional, just as for "file". */
2372     strstart(filename, "host_cdrom:", &filename);
2373 
2374     qdict_put_obj(options, "filename", QOBJECT(qstring_from_str(filename)));
2375 }
2376 #endif
2377 
2378 #ifdef __linux__
2379 static int cdrom_open(BlockDriverState *bs, QDict *options, int flags,
2380                       Error **errp)
2381 {
2382     BDRVRawState *s = bs->opaque;
2383 
2384     s->type = FTYPE_CD;
2385 
2386     /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
2387     return raw_open_common(bs, options, flags, O_NONBLOCK, errp);
2388 }
2389 
2390 static int cdrom_probe_device(const char *filename)
2391 {
2392     int fd, ret;
2393     int prio = 0;
2394     struct stat st;
2395 
2396     fd = qemu_open(filename, O_RDONLY | O_NONBLOCK);
2397     if (fd < 0) {
2398         goto out;
2399     }
2400     ret = fstat(fd, &st);
2401     if (ret == -1 || !S_ISBLK(st.st_mode)) {
2402         goto outc;
2403     }
2404 
2405     /* Attempt to detect via a CDROM specific ioctl */
2406     ret = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
2407     if (ret >= 0)
2408         prio = 100;
2409 
2410 outc:
2411     qemu_close(fd);
2412 out:
2413     return prio;
2414 }
2415 
2416 static bool cdrom_is_inserted(BlockDriverState *bs)
2417 {
2418     BDRVRawState *s = bs->opaque;
2419     int ret;
2420 
2421     ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
2422     return ret == CDS_DISC_OK;
2423 }
2424 
2425 static void cdrom_eject(BlockDriverState *bs, bool eject_flag)
2426 {
2427     BDRVRawState *s = bs->opaque;
2428 
2429     if (eject_flag) {
2430         if (ioctl(s->fd, CDROMEJECT, NULL) < 0)
2431             perror("CDROMEJECT");
2432     } else {
2433         if (ioctl(s->fd, CDROMCLOSETRAY, NULL) < 0)
2434             perror("CDROMEJECT");
2435     }
2436 }
2437 
2438 static void cdrom_lock_medium(BlockDriverState *bs, bool locked)
2439 {
2440     BDRVRawState *s = bs->opaque;
2441 
2442     if (ioctl(s->fd, CDROM_LOCKDOOR, locked) < 0) {
2443         /*
2444          * Note: an error can happen if the distribution automatically
2445          * mounts the CD-ROM
2446          */
2447         /* perror("CDROM_LOCKDOOR"); */
2448     }
2449 }
2450 
2451 static BlockDriver bdrv_host_cdrom = {
2452     .format_name        = "host_cdrom",
2453     .protocol_name      = "host_cdrom",
2454     .instance_size      = sizeof(BDRVRawState),
2455     .bdrv_needs_filename = true,
2456     .bdrv_probe_device	= cdrom_probe_device,
2457     .bdrv_parse_filename = cdrom_parse_filename,
2458     .bdrv_file_open     = cdrom_open,
2459     .bdrv_close         = raw_close,
2460     .bdrv_reopen_prepare = raw_reopen_prepare,
2461     .bdrv_reopen_commit  = raw_reopen_commit,
2462     .bdrv_reopen_abort   = raw_reopen_abort,
2463     .bdrv_create         = hdev_create,
2464     .create_opts         = &raw_create_opts,
2465 
2466 
2467     .bdrv_co_preadv         = raw_co_preadv,
2468     .bdrv_co_pwritev        = raw_co_pwritev,
2469     .bdrv_aio_flush	= raw_aio_flush,
2470     .bdrv_refresh_limits = raw_refresh_limits,
2471     .bdrv_io_plug = raw_aio_plug,
2472     .bdrv_io_unplug = raw_aio_unplug,
2473 
2474     .bdrv_truncate      = raw_truncate,
2475     .bdrv_getlength      = raw_getlength,
2476     .has_variable_length = true,
2477     .bdrv_get_allocated_file_size
2478                         = raw_get_allocated_file_size,
2479 
2480     /* removable device support */
2481     .bdrv_is_inserted   = cdrom_is_inserted,
2482     .bdrv_eject         = cdrom_eject,
2483     .bdrv_lock_medium   = cdrom_lock_medium,
2484 
2485     /* generic scsi device */
2486     .bdrv_aio_ioctl     = hdev_aio_ioctl,
2487 };
2488 #endif /* __linux__ */
2489 
2490 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
2491 static int cdrom_open(BlockDriverState *bs, QDict *options, int flags,
2492                       Error **errp)
2493 {
2494     BDRVRawState *s = bs->opaque;
2495     Error *local_err = NULL;
2496     int ret;
2497 
2498     s->type = FTYPE_CD;
2499 
2500     ret = raw_open_common(bs, options, flags, 0, &local_err);
2501     if (ret) {
2502         error_propagate(errp, local_err);
2503         return ret;
2504     }
2505 
2506     /* make sure the door isn't locked at this time */
2507     ioctl(s->fd, CDIOCALLOW);
2508     return 0;
2509 }
2510 
2511 static int cdrom_probe_device(const char *filename)
2512 {
2513     if (strstart(filename, "/dev/cd", NULL) ||
2514             strstart(filename, "/dev/acd", NULL))
2515         return 100;
2516     return 0;
2517 }
2518 
2519 static int cdrom_reopen(BlockDriverState *bs)
2520 {
2521     BDRVRawState *s = bs->opaque;
2522     int fd;
2523 
2524     /*
2525      * Force reread of possibly changed/newly loaded disc,
2526      * FreeBSD seems to not notice sometimes...
2527      */
2528     if (s->fd >= 0)
2529         qemu_close(s->fd);
2530     fd = qemu_open(bs->filename, s->open_flags, 0644);
2531     if (fd < 0) {
2532         s->fd = -1;
2533         return -EIO;
2534     }
2535     s->fd = fd;
2536 
2537     /* make sure the door isn't locked at this time */
2538     ioctl(s->fd, CDIOCALLOW);
2539     return 0;
2540 }
2541 
2542 static bool cdrom_is_inserted(BlockDriverState *bs)
2543 {
2544     return raw_getlength(bs) > 0;
2545 }
2546 
2547 static void cdrom_eject(BlockDriverState *bs, bool eject_flag)
2548 {
2549     BDRVRawState *s = bs->opaque;
2550 
2551     if (s->fd < 0)
2552         return;
2553 
2554     (void) ioctl(s->fd, CDIOCALLOW);
2555 
2556     if (eject_flag) {
2557         if (ioctl(s->fd, CDIOCEJECT) < 0)
2558             perror("CDIOCEJECT");
2559     } else {
2560         if (ioctl(s->fd, CDIOCCLOSE) < 0)
2561             perror("CDIOCCLOSE");
2562     }
2563 
2564     cdrom_reopen(bs);
2565 }
2566 
2567 static void cdrom_lock_medium(BlockDriverState *bs, bool locked)
2568 {
2569     BDRVRawState *s = bs->opaque;
2570 
2571     if (s->fd < 0)
2572         return;
2573     if (ioctl(s->fd, (locked ? CDIOCPREVENT : CDIOCALLOW)) < 0) {
2574         /*
2575          * Note: an error can happen if the distribution automatically
2576          * mounts the CD-ROM
2577          */
2578         /* perror("CDROM_LOCKDOOR"); */
2579     }
2580 }
2581 
2582 static BlockDriver bdrv_host_cdrom = {
2583     .format_name        = "host_cdrom",
2584     .protocol_name      = "host_cdrom",
2585     .instance_size      = sizeof(BDRVRawState),
2586     .bdrv_needs_filename = true,
2587     .bdrv_probe_device	= cdrom_probe_device,
2588     .bdrv_parse_filename = cdrom_parse_filename,
2589     .bdrv_file_open     = cdrom_open,
2590     .bdrv_close         = raw_close,
2591     .bdrv_reopen_prepare = raw_reopen_prepare,
2592     .bdrv_reopen_commit  = raw_reopen_commit,
2593     .bdrv_reopen_abort   = raw_reopen_abort,
2594     .bdrv_create        = hdev_create,
2595     .create_opts        = &raw_create_opts,
2596 
2597     .bdrv_co_preadv         = raw_co_preadv,
2598     .bdrv_co_pwritev        = raw_co_pwritev,
2599     .bdrv_aio_flush	= raw_aio_flush,
2600     .bdrv_refresh_limits = raw_refresh_limits,
2601     .bdrv_io_plug = raw_aio_plug,
2602     .bdrv_io_unplug = raw_aio_unplug,
2603 
2604     .bdrv_truncate      = raw_truncate,
2605     .bdrv_getlength      = raw_getlength,
2606     .has_variable_length = true,
2607     .bdrv_get_allocated_file_size
2608                         = raw_get_allocated_file_size,
2609 
2610     /* removable device support */
2611     .bdrv_is_inserted   = cdrom_is_inserted,
2612     .bdrv_eject         = cdrom_eject,
2613     .bdrv_lock_medium   = cdrom_lock_medium,
2614 };
2615 #endif /* __FreeBSD__ */
2616 
2617 static void bdrv_file_init(void)
2618 {
2619     /*
2620      * Register all the drivers.  Note that order is important, the driver
2621      * registered last will get probed first.
2622      */
2623     bdrv_register(&bdrv_file);
2624     bdrv_register(&bdrv_host_device);
2625 #ifdef __linux__
2626     bdrv_register(&bdrv_host_cdrom);
2627 #endif
2628 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
2629     bdrv_register(&bdrv_host_cdrom);
2630 #endif
2631 }
2632 
2633 block_init(bdrv_file_init);
2634