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