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