xref: /openbmc/qemu/block/file-posix.c (revision 1141159c)
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 
25 #include "qemu/osdep.h"
26 #include "qapi/error.h"
27 #include "qemu/cutils.h"
28 #include "qemu/error-report.h"
29 #include "block/block-io.h"
30 #include "block/block_int.h"
31 #include "qemu/module.h"
32 #include "qemu/option.h"
33 #include "qemu/units.h"
34 #include "qemu/memalign.h"
35 #include "trace.h"
36 #include "block/thread-pool.h"
37 #include "qemu/iov.h"
38 #include "block/raw-aio.h"
39 #include "qapi/qmp/qdict.h"
40 #include "qapi/qmp/qstring.h"
41 
42 #include "scsi/pr-manager.h"
43 #include "scsi/constants.h"
44 
45 #if defined(__APPLE__) && (__MACH__)
46 #include <sys/ioctl.h>
47 #if defined(HAVE_HOST_BLOCK_DEVICE)
48 #include <paths.h>
49 #include <sys/param.h>
50 #include <sys/mount.h>
51 #include <IOKit/IOKitLib.h>
52 #include <IOKit/IOBSD.h>
53 #include <IOKit/storage/IOMediaBSDClient.h>
54 #include <IOKit/storage/IOMedia.h>
55 #include <IOKit/storage/IOCDMedia.h>
56 //#include <IOKit/storage/IOCDTypes.h>
57 #include <IOKit/storage/IODVDMedia.h>
58 #include <CoreFoundation/CoreFoundation.h>
59 #endif /* defined(HAVE_HOST_BLOCK_DEVICE) */
60 #endif
61 
62 #ifdef __sun__
63 #define _POSIX_PTHREAD_SEMANTICS 1
64 #include <sys/dkio.h>
65 #endif
66 #ifdef __linux__
67 #include <sys/ioctl.h>
68 #include <sys/param.h>
69 #include <sys/syscall.h>
70 #include <sys/vfs.h>
71 #if defined(CONFIG_BLKZONED)
72 #include <linux/blkzoned.h>
73 #endif
74 #include <linux/cdrom.h>
75 #include <linux/fd.h>
76 #include <linux/fs.h>
77 #include <linux/hdreg.h>
78 #include <linux/magic.h>
79 #include <scsi/sg.h>
80 #ifdef __s390__
81 #include <asm/dasd.h>
82 #endif
83 #ifndef FS_NOCOW_FL
84 #define FS_NOCOW_FL                     0x00800000 /* Do not cow file */
85 #endif
86 #endif
87 #if defined(CONFIG_FALLOCATE_PUNCH_HOLE) || defined(CONFIG_FALLOCATE_ZERO_RANGE)
88 #include <linux/falloc.h>
89 #endif
90 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
91 #include <sys/disk.h>
92 #include <sys/cdio.h>
93 #endif
94 
95 #ifdef __OpenBSD__
96 #include <sys/ioctl.h>
97 #include <sys/disklabel.h>
98 #include <sys/dkio.h>
99 #endif
100 
101 #ifdef __NetBSD__
102 #include <sys/ioctl.h>
103 #include <sys/disklabel.h>
104 #include <sys/dkio.h>
105 #include <sys/disk.h>
106 #endif
107 
108 #ifdef __DragonFly__
109 #include <sys/ioctl.h>
110 #include <sys/diskslice.h>
111 #endif
112 
113 /* OS X does not have O_DSYNC */
114 #ifndef O_DSYNC
115 #ifdef O_SYNC
116 #define O_DSYNC O_SYNC
117 #elif defined(O_FSYNC)
118 #define O_DSYNC O_FSYNC
119 #endif
120 #endif
121 
122 /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */
123 #ifndef O_DIRECT
124 #define O_DIRECT O_DSYNC
125 #endif
126 
127 #define FTYPE_FILE   0
128 #define FTYPE_CD     1
129 
130 #define MAX_BLOCKSIZE	4096
131 
132 /* Posix file locking bytes. Libvirt takes byte 0, we start from higher bytes,
133  * leaving a few more bytes for its future use. */
134 #define RAW_LOCK_PERM_BASE             100
135 #define RAW_LOCK_SHARED_BASE           200
136 
137 typedef struct BDRVRawState {
138     int fd;
139     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     /* The perms bits whose corresponding bytes are already locked in
149      * s->fd. */
150     uint64_t locked_perm;
151     uint64_t locked_shared_perm;
152 
153     uint64_t aio_max_batch;
154 
155     int perm_change_fd;
156     int perm_change_flags;
157     BDRVReopenState *reopen_state;
158 
159     bool has_discard:1;
160     bool has_write_zeroes:1;
161     bool use_linux_aio:1;
162     bool use_linux_io_uring:1;
163     int64_t *offset; /* offset of zone append operation */
164     int page_cache_inconsistent; /* errno from fdatasync failure */
165     bool has_fallocate;
166     bool needs_alignment;
167     bool force_alignment;
168     bool drop_cache;
169     bool check_cache_dropped;
170     struct {
171         uint64_t discard_nb_ok;
172         uint64_t discard_nb_failed;
173         uint64_t discard_bytes_ok;
174     } stats;
175 
176     PRManager *pr_mgr;
177 } BDRVRawState;
178 
179 typedef struct BDRVRawReopenState {
180     int open_flags;
181     bool drop_cache;
182     bool check_cache_dropped;
183 } BDRVRawReopenState;
184 
185 static int fd_open(BlockDriverState *bs)
186 {
187     BDRVRawState *s = bs->opaque;
188 
189     /* this is just to ensure s->fd is sane (its called by io ops) */
190     if (s->fd >= 0) {
191         return 0;
192     }
193     return -EIO;
194 }
195 
196 static int64_t coroutine_fn raw_co_getlength(BlockDriverState *bs);
197 
198 typedef struct RawPosixAIOData {
199     BlockDriverState *bs;
200     int aio_type;
201     int aio_fildes;
202 
203     off_t aio_offset;
204     uint64_t aio_nbytes;
205 
206     union {
207         struct {
208             struct iovec *iov;
209             int niov;
210         } io;
211         struct {
212             uint64_t cmd;
213             void *buf;
214         } ioctl;
215         struct {
216             int aio_fd2;
217             off_t aio_offset2;
218         } copy_range;
219         struct {
220             PreallocMode prealloc;
221             Error **errp;
222         } truncate;
223         struct {
224             unsigned int *nr_zones;
225             BlockZoneDescriptor *zones;
226         } zone_report;
227         struct {
228             unsigned long op;
229         } zone_mgmt;
230     };
231 } RawPosixAIOData;
232 
233 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
234 static int cdrom_reopen(BlockDriverState *bs);
235 #endif
236 
237 /*
238  * Elide EAGAIN and EACCES details when failing to lock, as this
239  * indicates that the specified file region is already locked by
240  * another process, which is considered a common scenario.
241  */
242 #define raw_lock_error_setg_errno(errp, err, fmt, ...)                  \
243     do {                                                                \
244         if ((err) == EAGAIN || (err) == EACCES) {                       \
245             error_setg((errp), (fmt), ## __VA_ARGS__);                  \
246         } else {                                                        \
247             error_setg_errno((errp), (err), (fmt), ## __VA_ARGS__);     \
248         }                                                               \
249     } while (0)
250 
251 #if defined(__NetBSD__)
252 static int raw_normalize_devicepath(const char **filename, Error **errp)
253 {
254     static char namebuf[PATH_MAX];
255     const char *dp, *fname;
256     struct stat sb;
257 
258     fname = *filename;
259     dp = strrchr(fname, '/');
260     if (lstat(fname, &sb) < 0) {
261         error_setg_file_open(errp, errno, fname);
262         return -errno;
263     }
264 
265     if (!S_ISBLK(sb.st_mode)) {
266         return 0;
267     }
268 
269     if (dp == NULL) {
270         snprintf(namebuf, PATH_MAX, "r%s", fname);
271     } else {
272         snprintf(namebuf, PATH_MAX, "%.*s/r%s",
273             (int)(dp - fname), fname, dp + 1);
274     }
275     *filename = namebuf;
276     warn_report("%s is a block device, using %s", fname, *filename);
277 
278     return 0;
279 }
280 #else
281 static int raw_normalize_devicepath(const char **filename, Error **errp)
282 {
283     return 0;
284 }
285 #endif
286 
287 /*
288  * Get logical block size via ioctl. On success store it in @sector_size_p.
289  */
290 static int probe_logical_blocksize(int fd, unsigned int *sector_size_p)
291 {
292     unsigned int sector_size;
293     bool success = false;
294     int i;
295 
296     errno = ENOTSUP;
297     static const unsigned long ioctl_list[] = {
298 #ifdef BLKSSZGET
299         BLKSSZGET,
300 #endif
301 #ifdef DKIOCGETBLOCKSIZE
302         DKIOCGETBLOCKSIZE,
303 #endif
304 #ifdef DIOCGSECTORSIZE
305         DIOCGSECTORSIZE,
306 #endif
307     };
308 
309     /* Try a few ioctls to get the right size */
310     for (i = 0; i < (int)ARRAY_SIZE(ioctl_list); i++) {
311         if (ioctl(fd, ioctl_list[i], &sector_size) >= 0) {
312             *sector_size_p = sector_size;
313             success = true;
314         }
315     }
316 
317     return success ? 0 : -errno;
318 }
319 
320 /**
321  * Get physical block size of @fd.
322  * On success, store it in @blk_size and return 0.
323  * On failure, return -errno.
324  */
325 static int probe_physical_blocksize(int fd, unsigned int *blk_size)
326 {
327 #ifdef BLKPBSZGET
328     if (ioctl(fd, BLKPBSZGET, blk_size) < 0) {
329         return -errno;
330     }
331     return 0;
332 #else
333     return -ENOTSUP;
334 #endif
335 }
336 
337 /*
338  * Returns true if no alignment restrictions are necessary even for files
339  * opened with O_DIRECT.
340  *
341  * raw_probe_alignment() probes the required alignment and assume that 1 means
342  * the probing failed, so it falls back to a safe default of 4k. This can be
343  * avoided if we know that byte alignment is okay for the file.
344  */
345 static bool dio_byte_aligned(int fd)
346 {
347 #ifdef __linux__
348     struct statfs buf;
349     int ret;
350 
351     ret = fstatfs(fd, &buf);
352     if (ret == 0 && buf.f_type == NFS_SUPER_MAGIC) {
353         return true;
354     }
355 #endif
356     return false;
357 }
358 
359 static bool raw_needs_alignment(BlockDriverState *bs)
360 {
361     BDRVRawState *s = bs->opaque;
362 
363     if ((bs->open_flags & BDRV_O_NOCACHE) != 0 && !dio_byte_aligned(s->fd)) {
364         return true;
365     }
366 
367     return s->force_alignment;
368 }
369 
370 /* Check if read is allowed with given memory buffer and length.
371  *
372  * This function is used to check O_DIRECT memory buffer and request alignment.
373  */
374 static bool raw_is_io_aligned(int fd, void *buf, size_t len)
375 {
376     ssize_t ret = pread(fd, buf, len, 0);
377 
378     if (ret >= 0) {
379         return true;
380     }
381 
382 #ifdef __linux__
383     /* The Linux kernel returns EINVAL for misaligned O_DIRECT reads.  Ignore
384      * other errors (e.g. real I/O error), which could happen on a failed
385      * drive, since we only care about probing alignment.
386      */
387     if (errno != EINVAL) {
388         return true;
389     }
390 #endif
391 
392     return false;
393 }
394 
395 static void raw_probe_alignment(BlockDriverState *bs, int fd, Error **errp)
396 {
397     BDRVRawState *s = bs->opaque;
398     char *buf;
399     size_t max_align = MAX(MAX_BLOCKSIZE, qemu_real_host_page_size());
400     size_t alignments[] = {1, 512, 1024, 2048, 4096};
401 
402     /* For SCSI generic devices the alignment is not really used.
403        With buffered I/O, we don't have any restrictions. */
404     if (bdrv_is_sg(bs) || !s->needs_alignment) {
405         bs->bl.request_alignment = 1;
406         s->buf_align = 1;
407         return;
408     }
409 
410     bs->bl.request_alignment = 0;
411     s->buf_align = 0;
412     /* Let's try to use the logical blocksize for the alignment. */
413     if (probe_logical_blocksize(fd, &bs->bl.request_alignment) < 0) {
414         bs->bl.request_alignment = 0;
415     }
416 
417 #ifdef __linux__
418     /*
419      * The XFS ioctl definitions are shipped in extra packages that might
420      * not always be available. Since we just need the XFS_IOC_DIOINFO ioctl
421      * here, we simply use our own definition instead:
422      */
423     struct xfs_dioattr {
424         uint32_t d_mem;
425         uint32_t d_miniosz;
426         uint32_t d_maxiosz;
427     } da;
428     if (ioctl(fd, _IOR('X', 30, struct xfs_dioattr), &da) >= 0) {
429         bs->bl.request_alignment = da.d_miniosz;
430         /* The kernel returns wrong information for d_mem */
431         /* s->buf_align = da.d_mem; */
432     }
433 #endif
434 
435     /*
436      * If we could not get the sizes so far, we can only guess them. First try
437      * to detect request alignment, since it is more likely to succeed. Then
438      * try to detect buf_align, which cannot be detected in some cases (e.g.
439      * Gluster). If buf_align cannot be detected, we fallback to the value of
440      * request_alignment.
441      */
442 
443     if (!bs->bl.request_alignment) {
444         int i;
445         size_t align;
446         buf = qemu_memalign(max_align, max_align);
447         for (i = 0; i < ARRAY_SIZE(alignments); i++) {
448             align = alignments[i];
449             if (raw_is_io_aligned(fd, buf, align)) {
450                 /* Fallback to safe value. */
451                 bs->bl.request_alignment = (align != 1) ? align : max_align;
452                 break;
453             }
454         }
455         qemu_vfree(buf);
456     }
457 
458     if (!s->buf_align) {
459         int i;
460         size_t align;
461         buf = qemu_memalign(max_align, 2 * max_align);
462         for (i = 0; i < ARRAY_SIZE(alignments); i++) {
463             align = alignments[i];
464             if (raw_is_io_aligned(fd, buf + align, max_align)) {
465                 /* Fallback to request_alignment. */
466                 s->buf_align = (align != 1) ? align : bs->bl.request_alignment;
467                 break;
468             }
469         }
470         qemu_vfree(buf);
471     }
472 
473     if (!s->buf_align || !bs->bl.request_alignment) {
474         error_setg(errp, "Could not find working O_DIRECT alignment");
475         error_append_hint(errp, "Try cache.direct=off\n");
476     }
477 }
478 
479 static int check_hdev_writable(int fd)
480 {
481 #if defined(BLKROGET)
482     /* Linux block devices can be configured "read-only" using blockdev(8).
483      * This is independent of device node permissions and therefore open(2)
484      * with O_RDWR succeeds.  Actual writes fail with EPERM.
485      *
486      * bdrv_open() is supposed to fail if the disk is read-only.  Explicitly
487      * check for read-only block devices so that Linux block devices behave
488      * properly.
489      */
490     struct stat st;
491     int readonly = 0;
492 
493     if (fstat(fd, &st)) {
494         return -errno;
495     }
496 
497     if (!S_ISBLK(st.st_mode)) {
498         return 0;
499     }
500 
501     if (ioctl(fd, BLKROGET, &readonly) < 0) {
502         return -errno;
503     }
504 
505     if (readonly) {
506         return -EACCES;
507     }
508 #endif /* defined(BLKROGET) */
509     return 0;
510 }
511 
512 static void raw_parse_flags(int bdrv_flags, int *open_flags, bool has_writers)
513 {
514     bool read_write = false;
515     assert(open_flags != NULL);
516 
517     *open_flags |= O_BINARY;
518     *open_flags &= ~O_ACCMODE;
519 
520     if (bdrv_flags & BDRV_O_AUTO_RDONLY) {
521         read_write = has_writers;
522     } else if (bdrv_flags & BDRV_O_RDWR) {
523         read_write = true;
524     }
525 
526     if (read_write) {
527         *open_flags |= O_RDWR;
528     } else {
529         *open_flags |= O_RDONLY;
530     }
531 
532     /* Use O_DSYNC for write-through caching, no flags for write-back caching,
533      * and O_DIRECT for no caching. */
534     if ((bdrv_flags & BDRV_O_NOCACHE)) {
535         *open_flags |= O_DIRECT;
536     }
537 }
538 
539 static void raw_parse_filename(const char *filename, QDict *options,
540                                Error **errp)
541 {
542     bdrv_parse_filename_strip_prefix(filename, "file:", options);
543 }
544 
545 static QemuOptsList raw_runtime_opts = {
546     .name = "raw",
547     .head = QTAILQ_HEAD_INITIALIZER(raw_runtime_opts.head),
548     .desc = {
549         {
550             .name = "filename",
551             .type = QEMU_OPT_STRING,
552             .help = "File name of the image",
553         },
554         {
555             .name = "aio",
556             .type = QEMU_OPT_STRING,
557             .help = "host AIO implementation (threads, native, io_uring)",
558         },
559         {
560             .name = "aio-max-batch",
561             .type = QEMU_OPT_NUMBER,
562             .help = "AIO max batch size (0 = auto handled by AIO backend, default: 0)",
563         },
564         {
565             .name = "locking",
566             .type = QEMU_OPT_STRING,
567             .help = "file locking mode (on/off/auto, default: auto)",
568         },
569         {
570             .name = "pr-manager",
571             .type = QEMU_OPT_STRING,
572             .help = "id of persistent reservation manager object (default: none)",
573         },
574 #if defined(__linux__)
575         {
576             .name = "drop-cache",
577             .type = QEMU_OPT_BOOL,
578             .help = "invalidate page cache during live migration (default: on)",
579         },
580 #endif
581         {
582             .name = "x-check-cache-dropped",
583             .type = QEMU_OPT_BOOL,
584             .help = "check that page cache was dropped on live migration (default: off)"
585         },
586         { /* end of list */ }
587     },
588 };
589 
590 static const char *const mutable_opts[] = { "x-check-cache-dropped", NULL };
591 
592 static int raw_open_common(BlockDriverState *bs, QDict *options,
593                            int bdrv_flags, int open_flags,
594                            bool device, Error **errp)
595 {
596     BDRVRawState *s = bs->opaque;
597     QemuOpts *opts;
598     Error *local_err = NULL;
599     const char *filename = NULL;
600     const char *str;
601     BlockdevAioOptions aio, aio_default;
602     int fd, ret;
603     struct stat st;
604     OnOffAuto locking;
605 
606     opts = qemu_opts_create(&raw_runtime_opts, NULL, 0, &error_abort);
607     if (!qemu_opts_absorb_qdict(opts, options, errp)) {
608         ret = -EINVAL;
609         goto fail;
610     }
611 
612     filename = qemu_opt_get(opts, "filename");
613 
614     ret = raw_normalize_devicepath(&filename, errp);
615     if (ret != 0) {
616         goto fail;
617     }
618 
619     if (bdrv_flags & BDRV_O_NATIVE_AIO) {
620         aio_default = BLOCKDEV_AIO_OPTIONS_NATIVE;
621 #ifdef CONFIG_LINUX_IO_URING
622     } else if (bdrv_flags & BDRV_O_IO_URING) {
623         aio_default = BLOCKDEV_AIO_OPTIONS_IO_URING;
624 #endif
625     } else {
626         aio_default = BLOCKDEV_AIO_OPTIONS_THREADS;
627     }
628 
629     aio = qapi_enum_parse(&BlockdevAioOptions_lookup,
630                           qemu_opt_get(opts, "aio"),
631                           aio_default, &local_err);
632     if (local_err) {
633         error_propagate(errp, local_err);
634         ret = -EINVAL;
635         goto fail;
636     }
637 
638     s->use_linux_aio = (aio == BLOCKDEV_AIO_OPTIONS_NATIVE);
639 #ifdef CONFIG_LINUX_IO_URING
640     s->use_linux_io_uring = (aio == BLOCKDEV_AIO_OPTIONS_IO_URING);
641 #endif
642 
643     s->aio_max_batch = qemu_opt_get_number(opts, "aio-max-batch", 0);
644 
645     locking = qapi_enum_parse(&OnOffAuto_lookup,
646                               qemu_opt_get(opts, "locking"),
647                               ON_OFF_AUTO_AUTO, &local_err);
648     if (local_err) {
649         error_propagate(errp, local_err);
650         ret = -EINVAL;
651         goto fail;
652     }
653     switch (locking) {
654     case ON_OFF_AUTO_ON:
655         s->use_lock = true;
656         if (!qemu_has_ofd_lock()) {
657             warn_report("File lock requested but OFD locking syscall is "
658                         "unavailable, falling back to POSIX file locks");
659             error_printf("Due to the implementation, locks can be lost "
660                          "unexpectedly.\n");
661         }
662         break;
663     case ON_OFF_AUTO_OFF:
664         s->use_lock = false;
665         break;
666     case ON_OFF_AUTO_AUTO:
667         s->use_lock = qemu_has_ofd_lock();
668         break;
669     default:
670         abort();
671     }
672 
673     str = qemu_opt_get(opts, "pr-manager");
674     if (str) {
675         s->pr_mgr = pr_manager_lookup(str, &local_err);
676         if (local_err) {
677             error_propagate(errp, local_err);
678             ret = -EINVAL;
679             goto fail;
680         }
681     }
682 
683     s->drop_cache = qemu_opt_get_bool(opts, "drop-cache", true);
684     s->check_cache_dropped = qemu_opt_get_bool(opts, "x-check-cache-dropped",
685                                                false);
686 
687     s->open_flags = open_flags;
688     raw_parse_flags(bdrv_flags, &s->open_flags, false);
689 
690     s->fd = -1;
691     fd = qemu_open(filename, s->open_flags, errp);
692     ret = fd < 0 ? -errno : 0;
693 
694     if (ret < 0) {
695         if (ret == -EROFS) {
696             ret = -EACCES;
697         }
698         goto fail;
699     }
700     s->fd = fd;
701 
702     /* Check s->open_flags rather than bdrv_flags due to auto-read-only */
703     if (s->open_flags & O_RDWR) {
704         ret = check_hdev_writable(s->fd);
705         if (ret < 0) {
706             error_setg_errno(errp, -ret, "The device is not writable");
707             goto fail;
708         }
709     }
710 
711     s->perm = 0;
712     s->shared_perm = BLK_PERM_ALL;
713 
714 #ifdef CONFIG_LINUX_AIO
715      /* Currently Linux does AIO only for files opened with O_DIRECT */
716     if (s->use_linux_aio) {
717         if (!(s->open_flags & O_DIRECT)) {
718             error_setg(errp, "aio=native was specified, but it requires "
719                              "cache.direct=on, which was not specified.");
720             ret = -EINVAL;
721             goto fail;
722         }
723         if (!aio_setup_linux_aio(bdrv_get_aio_context(bs), errp)) {
724             error_prepend(errp, "Unable to use native AIO: ");
725             goto fail;
726         }
727     }
728 #else
729     if (s->use_linux_aio) {
730         error_setg(errp, "aio=native was specified, but is not supported "
731                          "in this build.");
732         ret = -EINVAL;
733         goto fail;
734     }
735 #endif /* !defined(CONFIG_LINUX_AIO) */
736 
737 #ifdef CONFIG_LINUX_IO_URING
738     if (s->use_linux_io_uring) {
739         if (!aio_setup_linux_io_uring(bdrv_get_aio_context(bs), errp)) {
740             error_prepend(errp, "Unable to use io_uring: ");
741             goto fail;
742         }
743     }
744 #else
745     if (s->use_linux_io_uring) {
746         error_setg(errp, "aio=io_uring was specified, but is not supported "
747                          "in this build.");
748         ret = -EINVAL;
749         goto fail;
750     }
751 #endif /* !defined(CONFIG_LINUX_IO_URING) */
752 
753     s->has_discard = true;
754     s->has_write_zeroes = true;
755 
756     if (fstat(s->fd, &st) < 0) {
757         ret = -errno;
758         error_setg_errno(errp, errno, "Could not stat file");
759         goto fail;
760     }
761 
762     if (!device) {
763         if (!S_ISREG(st.st_mode)) {
764             error_setg(errp, "'%s' driver requires '%s' to be a regular file",
765                        bs->drv->format_name, bs->filename);
766             ret = -EINVAL;
767             goto fail;
768         } else {
769             s->has_fallocate = true;
770         }
771     } else {
772         if (!(S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
773             error_setg(errp, "'%s' driver requires '%s' to be either "
774                        "a character or block device",
775                        bs->drv->format_name, bs->filename);
776             ret = -EINVAL;
777             goto fail;
778         }
779     }
780 #ifdef CONFIG_BLKZONED
781     /*
782      * The kernel page cache does not reliably work for writes to SWR zones
783      * of zoned block device because it can not guarantee the order of writes.
784      */
785     if ((bs->bl.zoned != BLK_Z_NONE) &&
786         (!(s->open_flags & O_DIRECT))) {
787         error_setg(errp, "The driver supports zoned devices, and it requires "
788                          "cache.direct=on, which was not specified.");
789         return -EINVAL; /* No host kernel page cache */
790     }
791 #endif
792 
793     if (S_ISBLK(st.st_mode)) {
794 #ifdef __linux__
795         /* On Linux 3.10, BLKDISCARD leaves stale data in the page cache.  Do
796          * not rely on the contents of discarded blocks unless using O_DIRECT.
797          * Same for BLKZEROOUT.
798          */
799         if (!(bs->open_flags & BDRV_O_NOCACHE)) {
800             s->has_write_zeroes = false;
801         }
802 #endif
803     }
804 #ifdef __FreeBSD__
805     if (S_ISCHR(st.st_mode)) {
806         /*
807          * The file is a char device (disk), which on FreeBSD isn't behind
808          * a pager, so force all requests to be aligned. This is needed
809          * so QEMU makes sure all IO operations on the device are aligned
810          * to sector size, or else FreeBSD will reject them with EINVAL.
811          */
812         s->force_alignment = true;
813     }
814 #endif
815     s->needs_alignment = raw_needs_alignment(bs);
816 
817     bs->supported_zero_flags = BDRV_REQ_MAY_UNMAP | BDRV_REQ_NO_FALLBACK;
818     if (S_ISREG(st.st_mode)) {
819         /* When extending regular files, we get zeros from the OS */
820         bs->supported_truncate_flags = BDRV_REQ_ZERO_WRITE;
821     }
822     ret = 0;
823 fail:
824     if (ret < 0 && s->fd != -1) {
825         qemu_close(s->fd);
826     }
827     if (filename && (bdrv_flags & BDRV_O_TEMPORARY)) {
828         unlink(filename);
829     }
830     qemu_opts_del(opts);
831     return ret;
832 }
833 
834 static int raw_open(BlockDriverState *bs, QDict *options, int flags,
835                     Error **errp)
836 {
837     BDRVRawState *s = bs->opaque;
838 
839     s->type = FTYPE_FILE;
840     return raw_open_common(bs, options, flags, 0, false, errp);
841 }
842 
843 typedef enum {
844     RAW_PL_PREPARE,
845     RAW_PL_COMMIT,
846     RAW_PL_ABORT,
847 } RawPermLockOp;
848 
849 #define PERM_FOREACH(i) \
850     for ((i) = 0; (1ULL << (i)) <= BLK_PERM_ALL; i++)
851 
852 /* Lock bytes indicated by @perm_lock_bits and @shared_perm_lock_bits in the
853  * file; if @unlock == true, also unlock the unneeded bytes.
854  * @shared_perm_lock_bits is the mask of all permissions that are NOT shared.
855  */
856 static int raw_apply_lock_bytes(BDRVRawState *s, int fd,
857                                 uint64_t perm_lock_bits,
858                                 uint64_t shared_perm_lock_bits,
859                                 bool unlock, Error **errp)
860 {
861     int ret;
862     int i;
863     uint64_t locked_perm, locked_shared_perm;
864 
865     if (s) {
866         locked_perm = s->locked_perm;
867         locked_shared_perm = s->locked_shared_perm;
868     } else {
869         /*
870          * We don't have the previous bits, just lock/unlock for each of the
871          * requested bits.
872          */
873         if (unlock) {
874             locked_perm = BLK_PERM_ALL;
875             locked_shared_perm = BLK_PERM_ALL;
876         } else {
877             locked_perm = 0;
878             locked_shared_perm = 0;
879         }
880     }
881 
882     PERM_FOREACH(i) {
883         int off = RAW_LOCK_PERM_BASE + i;
884         uint64_t bit = (1ULL << i);
885         if ((perm_lock_bits & bit) && !(locked_perm & bit)) {
886             ret = qemu_lock_fd(fd, off, 1, false);
887             if (ret) {
888                 raw_lock_error_setg_errno(errp, -ret, "Failed to lock byte %d",
889                                           off);
890                 return ret;
891             } else if (s) {
892                 s->locked_perm |= bit;
893             }
894         } else if (unlock && (locked_perm & bit) && !(perm_lock_bits & bit)) {
895             ret = qemu_unlock_fd(fd, off, 1);
896             if (ret) {
897                 error_setg_errno(errp, -ret, "Failed to unlock byte %d", off);
898                 return ret;
899             } else if (s) {
900                 s->locked_perm &= ~bit;
901             }
902         }
903     }
904     PERM_FOREACH(i) {
905         int off = RAW_LOCK_SHARED_BASE + i;
906         uint64_t bit = (1ULL << i);
907         if ((shared_perm_lock_bits & bit) && !(locked_shared_perm & bit)) {
908             ret = qemu_lock_fd(fd, off, 1, false);
909             if (ret) {
910                 raw_lock_error_setg_errno(errp, -ret, "Failed to lock byte %d",
911                                           off);
912                 return ret;
913             } else if (s) {
914                 s->locked_shared_perm |= bit;
915             }
916         } else if (unlock && (locked_shared_perm & bit) &&
917                    !(shared_perm_lock_bits & bit)) {
918             ret = qemu_unlock_fd(fd, off, 1);
919             if (ret) {
920                 error_setg_errno(errp, -ret, "Failed to unlock byte %d", off);
921                 return ret;
922             } else if (s) {
923                 s->locked_shared_perm &= ~bit;
924             }
925         }
926     }
927     return 0;
928 }
929 
930 /* Check "unshared" bytes implied by @perm and ~@shared_perm in the file. */
931 static int raw_check_lock_bytes(int fd, uint64_t perm, uint64_t shared_perm,
932                                 Error **errp)
933 {
934     int ret;
935     int i;
936 
937     PERM_FOREACH(i) {
938         int off = RAW_LOCK_SHARED_BASE + i;
939         uint64_t p = 1ULL << i;
940         if (perm & p) {
941             ret = qemu_lock_fd_test(fd, off, 1, true);
942             if (ret) {
943                 char *perm_name = bdrv_perm_names(p);
944 
945                 raw_lock_error_setg_errno(errp, -ret,
946                                           "Failed to get \"%s\" lock",
947                                           perm_name);
948                 g_free(perm_name);
949                 return ret;
950             }
951         }
952     }
953     PERM_FOREACH(i) {
954         int off = RAW_LOCK_PERM_BASE + i;
955         uint64_t p = 1ULL << i;
956         if (!(shared_perm & p)) {
957             ret = qemu_lock_fd_test(fd, off, 1, true);
958             if (ret) {
959                 char *perm_name = bdrv_perm_names(p);
960 
961                 raw_lock_error_setg_errno(errp, -ret,
962                                           "Failed to get shared \"%s\" lock",
963                                           perm_name);
964                 g_free(perm_name);
965                 return ret;
966             }
967         }
968     }
969     return 0;
970 }
971 
972 static int raw_handle_perm_lock(BlockDriverState *bs,
973                                 RawPermLockOp op,
974                                 uint64_t new_perm, uint64_t new_shared,
975                                 Error **errp)
976 {
977     BDRVRawState *s = bs->opaque;
978     int ret = 0;
979     Error *local_err = NULL;
980 
981     if (!s->use_lock) {
982         return 0;
983     }
984 
985     if (bdrv_get_flags(bs) & BDRV_O_INACTIVE) {
986         return 0;
987     }
988 
989     switch (op) {
990     case RAW_PL_PREPARE:
991         if ((s->perm | new_perm) == s->perm &&
992             (s->shared_perm & new_shared) == s->shared_perm)
993         {
994             /*
995              * We are going to unlock bytes, it should not fail. If it fail due
996              * to some fs-dependent permission-unrelated reasons (which occurs
997              * sometimes on NFS and leads to abort in bdrv_replace_child) we
998              * can't prevent such errors by any check here. And we ignore them
999              * anyway in ABORT and COMMIT.
1000              */
1001             return 0;
1002         }
1003         ret = raw_apply_lock_bytes(s, s->fd, s->perm | new_perm,
1004                                    ~s->shared_perm | ~new_shared,
1005                                    false, errp);
1006         if (!ret) {
1007             ret = raw_check_lock_bytes(s->fd, new_perm, new_shared, errp);
1008             if (!ret) {
1009                 return 0;
1010             }
1011             error_append_hint(errp,
1012                               "Is another process using the image [%s]?\n",
1013                               bs->filename);
1014         }
1015         /* fall through to unlock bytes. */
1016     case RAW_PL_ABORT:
1017         raw_apply_lock_bytes(s, s->fd, s->perm, ~s->shared_perm,
1018                              true, &local_err);
1019         if (local_err) {
1020             /* Theoretically the above call only unlocks bytes and it cannot
1021              * fail. Something weird happened, report it.
1022              */
1023             warn_report_err(local_err);
1024         }
1025         break;
1026     case RAW_PL_COMMIT:
1027         raw_apply_lock_bytes(s, s->fd, new_perm, ~new_shared,
1028                              true, &local_err);
1029         if (local_err) {
1030             /* Theoretically the above call only unlocks bytes and it cannot
1031              * fail. Something weird happened, report it.
1032              */
1033             warn_report_err(local_err);
1034         }
1035         break;
1036     }
1037     return ret;
1038 }
1039 
1040 /* Sets a specific flag */
1041 static int fcntl_setfl(int fd, int flag)
1042 {
1043     int flags;
1044 
1045     flags = fcntl(fd, F_GETFL);
1046     if (flags == -1) {
1047         return -errno;
1048     }
1049     if (fcntl(fd, F_SETFL, flags | flag) == -1) {
1050         return -errno;
1051     }
1052     return 0;
1053 }
1054 
1055 static int raw_reconfigure_getfd(BlockDriverState *bs, int flags,
1056                                  int *open_flags, uint64_t perm, bool force_dup,
1057                                  Error **errp)
1058 {
1059     BDRVRawState *s = bs->opaque;
1060     int fd = -1;
1061     int ret;
1062     bool has_writers = perm &
1063         (BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED | BLK_PERM_RESIZE);
1064     int fcntl_flags = O_APPEND | O_NONBLOCK;
1065 #ifdef O_NOATIME
1066     fcntl_flags |= O_NOATIME;
1067 #endif
1068 
1069     *open_flags = 0;
1070     if (s->type == FTYPE_CD) {
1071         *open_flags |= O_NONBLOCK;
1072     }
1073 
1074     raw_parse_flags(flags, open_flags, has_writers);
1075 
1076 #ifdef O_ASYNC
1077     /* Not all operating systems have O_ASYNC, and those that don't
1078      * will not let us track the state into rs->open_flags (typically
1079      * you achieve the same effect with an ioctl, for example I_SETSIG
1080      * on Solaris). But we do not use O_ASYNC, so that's fine.
1081      */
1082     assert((s->open_flags & O_ASYNC) == 0);
1083 #endif
1084 
1085     if (!force_dup && *open_flags == s->open_flags) {
1086         /* We're lucky, the existing fd is fine */
1087         return s->fd;
1088     }
1089 
1090     if ((*open_flags & ~fcntl_flags) == (s->open_flags & ~fcntl_flags)) {
1091         /* dup the original fd */
1092         fd = qemu_dup(s->fd);
1093         if (fd >= 0) {
1094             ret = fcntl_setfl(fd, *open_flags);
1095             if (ret) {
1096                 qemu_close(fd);
1097                 fd = -1;
1098             }
1099         }
1100     }
1101 
1102     /* If we cannot use fcntl, or fcntl failed, fall back to qemu_open() */
1103     if (fd == -1) {
1104         const char *normalized_filename = bs->filename;
1105         ret = raw_normalize_devicepath(&normalized_filename, errp);
1106         if (ret >= 0) {
1107             fd = qemu_open(normalized_filename, *open_flags, errp);
1108             if (fd == -1) {
1109                 return -1;
1110             }
1111         }
1112     }
1113 
1114     if (fd != -1 && (*open_flags & O_RDWR)) {
1115         ret = check_hdev_writable(fd);
1116         if (ret < 0) {
1117             qemu_close(fd);
1118             error_setg_errno(errp, -ret, "The device is not writable");
1119             return -1;
1120         }
1121     }
1122 
1123     return fd;
1124 }
1125 
1126 static int raw_reopen_prepare(BDRVReopenState *state,
1127                               BlockReopenQueue *queue, Error **errp)
1128 {
1129     BDRVRawState *s;
1130     BDRVRawReopenState *rs;
1131     QemuOpts *opts;
1132     int ret;
1133 
1134     assert(state != NULL);
1135     assert(state->bs != NULL);
1136 
1137     s = state->bs->opaque;
1138 
1139     state->opaque = g_new0(BDRVRawReopenState, 1);
1140     rs = state->opaque;
1141 
1142     /* Handle options changes */
1143     opts = qemu_opts_create(&raw_runtime_opts, NULL, 0, &error_abort);
1144     if (!qemu_opts_absorb_qdict(opts, state->options, errp)) {
1145         ret = -EINVAL;
1146         goto out;
1147     }
1148 
1149     rs->drop_cache = qemu_opt_get_bool_del(opts, "drop-cache", true);
1150     rs->check_cache_dropped =
1151         qemu_opt_get_bool_del(opts, "x-check-cache-dropped", false);
1152 
1153     /* This driver's reopen function doesn't currently allow changing
1154      * other options, so let's put them back in the original QDict and
1155      * bdrv_reopen_prepare() will detect changes and complain. */
1156     qemu_opts_to_qdict(opts, state->options);
1157 
1158     /*
1159      * As part of reopen prepare we also want to create new fd by
1160      * raw_reconfigure_getfd(). But it wants updated "perm", when in
1161      * bdrv_reopen_multiple() .bdrv_reopen_prepare() callback called prior to
1162      * permission update. Happily, permission update is always a part (a seprate
1163      * stage) of bdrv_reopen_multiple() so we can rely on this fact and
1164      * reconfigure fd in raw_check_perm().
1165      */
1166 
1167     s->reopen_state = state;
1168     ret = 0;
1169 
1170 out:
1171     qemu_opts_del(opts);
1172     return ret;
1173 }
1174 
1175 static void raw_reopen_commit(BDRVReopenState *state)
1176 {
1177     BDRVRawReopenState *rs = state->opaque;
1178     BDRVRawState *s = state->bs->opaque;
1179 
1180     s->drop_cache = rs->drop_cache;
1181     s->check_cache_dropped = rs->check_cache_dropped;
1182     s->open_flags = rs->open_flags;
1183     g_free(state->opaque);
1184     state->opaque = NULL;
1185 
1186     assert(s->reopen_state == state);
1187     s->reopen_state = NULL;
1188 }
1189 
1190 
1191 static void raw_reopen_abort(BDRVReopenState *state)
1192 {
1193     BDRVRawReopenState *rs = state->opaque;
1194     BDRVRawState *s = state->bs->opaque;
1195 
1196      /* nothing to do if NULL, we didn't get far enough */
1197     if (rs == NULL) {
1198         return;
1199     }
1200 
1201     g_free(state->opaque);
1202     state->opaque = NULL;
1203 
1204     assert(s->reopen_state == state);
1205     s->reopen_state = NULL;
1206 }
1207 
1208 static int hdev_get_max_hw_transfer(int fd, struct stat *st)
1209 {
1210 #ifdef BLKSECTGET
1211     if (S_ISBLK(st->st_mode)) {
1212         unsigned short max_sectors = 0;
1213         if (ioctl(fd, BLKSECTGET, &max_sectors) == 0) {
1214             return max_sectors * 512;
1215         }
1216     } else {
1217         int max_bytes = 0;
1218         if (ioctl(fd, BLKSECTGET, &max_bytes) == 0) {
1219             return max_bytes;
1220         }
1221     }
1222     return -errno;
1223 #else
1224     return -ENOSYS;
1225 #endif
1226 }
1227 
1228 /*
1229  * Get a sysfs attribute value as character string.
1230  */
1231 #ifdef CONFIG_LINUX
1232 static int get_sysfs_str_val(struct stat *st, const char *attribute,
1233                              char **val) {
1234     g_autofree char *sysfspath = NULL;
1235     int ret;
1236     size_t len;
1237 
1238     if (!S_ISBLK(st->st_mode)) {
1239         return -ENOTSUP;
1240     }
1241 
1242     sysfspath = g_strdup_printf("/sys/dev/block/%u:%u/queue/%s",
1243                                 major(st->st_rdev), minor(st->st_rdev),
1244                                 attribute);
1245     ret = g_file_get_contents(sysfspath, val, &len, NULL);
1246     if (ret == -1) {
1247         return -ENOENT;
1248     }
1249 
1250     /* The file is ended with '\n' */
1251     char *p;
1252     p = *val;
1253     if (*(p + len - 1) == '\n') {
1254         *(p + len - 1) = '\0';
1255     }
1256     return ret;
1257 }
1258 #endif
1259 
1260 #if defined(CONFIG_BLKZONED)
1261 static int get_sysfs_zoned_model(struct stat *st, BlockZoneModel *zoned)
1262 {
1263     g_autofree char *val = NULL;
1264     int ret;
1265 
1266     ret = get_sysfs_str_val(st, "zoned", &val);
1267     if (ret < 0) {
1268         return ret;
1269     }
1270 
1271     if (strcmp(val, "host-managed") == 0) {
1272         *zoned = BLK_Z_HM;
1273     } else if (strcmp(val, "host-aware") == 0) {
1274         *zoned = BLK_Z_HA;
1275     } else if (strcmp(val, "none") == 0) {
1276         *zoned = BLK_Z_NONE;
1277     } else {
1278         return -ENOTSUP;
1279     }
1280     return 0;
1281 }
1282 #endif /* defined(CONFIG_BLKZONED) */
1283 
1284 /*
1285  * Get a sysfs attribute value as a long integer.
1286  */
1287 #ifdef CONFIG_LINUX
1288 static long get_sysfs_long_val(struct stat *st, const char *attribute)
1289 {
1290     g_autofree char *str = NULL;
1291     const char *end;
1292     long val;
1293     int ret;
1294 
1295     ret = get_sysfs_str_val(st, attribute, &str);
1296     if (ret < 0) {
1297         return ret;
1298     }
1299 
1300     /* The file is ended with '\n', pass 'end' to accept that. */
1301     ret = qemu_strtol(str, &end, 10, &val);
1302     if (ret == 0 && end && *end == '\0') {
1303         ret = val;
1304     }
1305     return ret;
1306 }
1307 #endif
1308 
1309 static int hdev_get_max_segments(int fd, struct stat *st)
1310 {
1311 #ifdef CONFIG_LINUX
1312     int ret;
1313 
1314     if (S_ISCHR(st->st_mode)) {
1315         if (ioctl(fd, SG_GET_SG_TABLESIZE, &ret) == 0) {
1316             return ret;
1317         }
1318         return -ENOTSUP;
1319     }
1320     return get_sysfs_long_val(st, "max_segments");
1321 #else
1322     return -ENOTSUP;
1323 #endif
1324 }
1325 
1326 #if defined(CONFIG_BLKZONED)
1327 /*
1328  * If the reset_all flag is true, then the wps of zone whose state is
1329  * not readonly or offline should be all reset to the start sector.
1330  * Else, take the real wp of the device.
1331  */
1332 static int get_zones_wp(BlockDriverState *bs, int fd, int64_t offset,
1333                         unsigned int nrz, bool reset_all)
1334 {
1335     struct blk_zone *blkz;
1336     size_t rep_size;
1337     uint64_t sector = offset >> BDRV_SECTOR_BITS;
1338     BlockZoneWps *wps = bs->wps;
1339     unsigned int j = offset / bs->bl.zone_size;
1340     unsigned int n = 0, i = 0;
1341     int ret;
1342     rep_size = sizeof(struct blk_zone_report) + nrz * sizeof(struct blk_zone);
1343     g_autofree struct blk_zone_report *rep = NULL;
1344 
1345     rep = g_malloc(rep_size);
1346     blkz = (struct blk_zone *)(rep + 1);
1347     while (n < nrz) {
1348         memset(rep, 0, rep_size);
1349         rep->sector = sector;
1350         rep->nr_zones = nrz - n;
1351 
1352         do {
1353             ret = ioctl(fd, BLKREPORTZONE, rep);
1354         } while (ret != 0 && errno == EINTR);
1355         if (ret != 0) {
1356             error_report("%d: ioctl BLKREPORTZONE at %" PRId64 " failed %d",
1357                     fd, offset, errno);
1358             return -errno;
1359         }
1360 
1361         if (!rep->nr_zones) {
1362             break;
1363         }
1364 
1365         for (i = 0; i < rep->nr_zones; ++i, ++n, ++j) {
1366             /*
1367              * The wp tracking cares only about sequential writes required and
1368              * sequential write preferred zones so that the wp can advance to
1369              * the right location.
1370              * Use the most significant bit of the wp location to indicate the
1371              * zone type: 0 for SWR/SWP zones and 1 for conventional zones.
1372              */
1373             if (blkz[i].type == BLK_ZONE_TYPE_CONVENTIONAL) {
1374                 wps->wp[j] |= 1ULL << 63;
1375             } else {
1376                 switch(blkz[i].cond) {
1377                 case BLK_ZONE_COND_FULL:
1378                 case BLK_ZONE_COND_READONLY:
1379                     /* Zone not writable */
1380                     wps->wp[j] = (blkz[i].start + blkz[i].len) << BDRV_SECTOR_BITS;
1381                     break;
1382                 case BLK_ZONE_COND_OFFLINE:
1383                     /* Zone not writable nor readable */
1384                     wps->wp[j] = (blkz[i].start) << BDRV_SECTOR_BITS;
1385                     break;
1386                 default:
1387                     if (reset_all) {
1388                         wps->wp[j] = blkz[i].start << BDRV_SECTOR_BITS;
1389                     } else {
1390                         wps->wp[j] = blkz[i].wp << BDRV_SECTOR_BITS;
1391                     }
1392                     break;
1393                 }
1394             }
1395         }
1396         sector = blkz[i - 1].start + blkz[i - 1].len;
1397     }
1398 
1399     return 0;
1400 }
1401 
1402 static void update_zones_wp(BlockDriverState *bs, int fd, int64_t offset,
1403                             unsigned int nrz)
1404 {
1405     if (get_zones_wp(bs, fd, offset, nrz, 0) < 0) {
1406         error_report("update zone wp failed");
1407     }
1408 }
1409 
1410 static void raw_refresh_zoned_limits(BlockDriverState *bs, struct stat *st,
1411                                      Error **errp)
1412 {
1413     BDRVRawState *s = bs->opaque;
1414     BlockZoneModel zoned;
1415     int ret;
1416 
1417     bs->bl.zoned = BLK_Z_NONE;
1418 
1419     ret = get_sysfs_zoned_model(st, &zoned);
1420     if (ret < 0 || zoned == BLK_Z_NONE) {
1421         return;
1422     }
1423     bs->bl.zoned = zoned;
1424 
1425     ret = get_sysfs_long_val(st, "max_open_zones");
1426     if (ret >= 0) {
1427         bs->bl.max_open_zones = ret;
1428     }
1429 
1430     ret = get_sysfs_long_val(st, "max_active_zones");
1431     if (ret >= 0) {
1432         bs->bl.max_active_zones = ret;
1433     }
1434 
1435     /*
1436      * The zoned device must at least have zone size and nr_zones fields.
1437      */
1438     ret = get_sysfs_long_val(st, "chunk_sectors");
1439     if (ret < 0) {
1440         error_setg_errno(errp, -ret, "Unable to read chunk_sectors "
1441                                      "sysfs attribute");
1442         return;
1443     } else if (!ret) {
1444         error_setg(errp, "Read 0 from chunk_sectors sysfs attribute");
1445         return;
1446     }
1447     bs->bl.zone_size = ret << BDRV_SECTOR_BITS;
1448 
1449     ret = get_sysfs_long_val(st, "nr_zones");
1450     if (ret < 0) {
1451         error_setg_errno(errp, -ret, "Unable to read nr_zones "
1452                                      "sysfs attribute");
1453         return;
1454     } else if (!ret) {
1455         error_setg(errp, "Read 0 from nr_zones sysfs attribute");
1456         return;
1457     }
1458     bs->bl.nr_zones = ret;
1459 
1460     ret = get_sysfs_long_val(st, "zone_append_max_bytes");
1461     if (ret > 0) {
1462         bs->bl.max_append_sectors = ret >> BDRV_SECTOR_BITS;
1463     }
1464 
1465     ret = get_sysfs_long_val(st, "physical_block_size");
1466     if (ret >= 0) {
1467         bs->bl.write_granularity = ret;
1468     }
1469 
1470     /* The refresh_limits() function can be called multiple times. */
1471     g_free(bs->wps);
1472     bs->wps = g_malloc(sizeof(BlockZoneWps) +
1473             sizeof(int64_t) * bs->bl.nr_zones);
1474     ret = get_zones_wp(bs, s->fd, 0, bs->bl.nr_zones, 0);
1475     if (ret < 0) {
1476         error_setg_errno(errp, -ret, "report wps failed");
1477         bs->wps = NULL;
1478         return;
1479     }
1480     qemu_co_mutex_init(&bs->wps->colock);
1481 }
1482 #else /* !defined(CONFIG_BLKZONED) */
1483 static void raw_refresh_zoned_limits(BlockDriverState *bs, struct stat *st,
1484                                      Error **errp)
1485 {
1486     bs->bl.zoned = BLK_Z_NONE;
1487 }
1488 #endif /* !defined(CONFIG_BLKZONED) */
1489 
1490 static void raw_refresh_limits(BlockDriverState *bs, Error **errp)
1491 {
1492     BDRVRawState *s = bs->opaque;
1493     struct stat st;
1494 
1495     s->needs_alignment = raw_needs_alignment(bs);
1496     raw_probe_alignment(bs, s->fd, errp);
1497 
1498     bs->bl.min_mem_alignment = s->buf_align;
1499     bs->bl.opt_mem_alignment = MAX(s->buf_align, qemu_real_host_page_size());
1500 
1501     /*
1502      * Maximum transfers are best effort, so it is okay to ignore any
1503      * errors.  That said, based on the man page errors in fstat would be
1504      * very much unexpected; the only possible case seems to be ENOMEM.
1505      */
1506     if (fstat(s->fd, &st)) {
1507         return;
1508     }
1509 
1510 #if defined(__APPLE__) && (__MACH__)
1511     struct statfs buf;
1512 
1513     if (!fstatfs(s->fd, &buf)) {
1514         bs->bl.opt_transfer = buf.f_iosize;
1515         bs->bl.pdiscard_alignment = buf.f_bsize;
1516     }
1517 #endif
1518 
1519     if (bdrv_is_sg(bs) || S_ISBLK(st.st_mode)) {
1520         int ret = hdev_get_max_hw_transfer(s->fd, &st);
1521 
1522         if (ret > 0 && ret <= BDRV_REQUEST_MAX_BYTES) {
1523             bs->bl.max_hw_transfer = ret;
1524         }
1525 
1526         ret = hdev_get_max_segments(s->fd, &st);
1527         if (ret > 0) {
1528             bs->bl.max_hw_iov = ret;
1529         }
1530     }
1531 
1532     raw_refresh_zoned_limits(bs, &st, errp);
1533 }
1534 
1535 static int check_for_dasd(int fd)
1536 {
1537 #ifdef BIODASDINFO2
1538     struct dasd_information2_t info = {0};
1539 
1540     return ioctl(fd, BIODASDINFO2, &info);
1541 #else
1542     return -1;
1543 #endif
1544 }
1545 
1546 /**
1547  * Try to get @bs's logical and physical block size.
1548  * On success, store them in @bsz and return zero.
1549  * On failure, return negative errno.
1550  */
1551 static int hdev_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz)
1552 {
1553     BDRVRawState *s = bs->opaque;
1554     int ret;
1555 
1556     /* If DASD or zoned devices, get blocksizes */
1557     if (check_for_dasd(s->fd) < 0) {
1558         /* zoned devices are not DASD */
1559         if (bs->bl.zoned == BLK_Z_NONE) {
1560             return -ENOTSUP;
1561         }
1562     }
1563     ret = probe_logical_blocksize(s->fd, &bsz->log);
1564     if (ret < 0) {
1565         return ret;
1566     }
1567     return probe_physical_blocksize(s->fd, &bsz->phys);
1568 }
1569 
1570 /**
1571  * Try to get @bs's geometry: cyls, heads, sectors.
1572  * On success, store them in @geo and return 0.
1573  * On failure return -errno.
1574  * (Allows block driver to assign default geometry values that guest sees)
1575  */
1576 #ifdef __linux__
1577 static int hdev_probe_geometry(BlockDriverState *bs, HDGeometry *geo)
1578 {
1579     BDRVRawState *s = bs->opaque;
1580     struct hd_geometry ioctl_geo = {0};
1581 
1582     /* If DASD, get its geometry */
1583     if (check_for_dasd(s->fd) < 0) {
1584         return -ENOTSUP;
1585     }
1586     if (ioctl(s->fd, HDIO_GETGEO, &ioctl_geo) < 0) {
1587         return -errno;
1588     }
1589     /* HDIO_GETGEO may return success even though geo contains zeros
1590        (e.g. certain multipath setups) */
1591     if (!ioctl_geo.heads || !ioctl_geo.sectors || !ioctl_geo.cylinders) {
1592         return -ENOTSUP;
1593     }
1594     /* Do not return a geometry for partition */
1595     if (ioctl_geo.start != 0) {
1596         return -ENOTSUP;
1597     }
1598     geo->heads = ioctl_geo.heads;
1599     geo->sectors = ioctl_geo.sectors;
1600     geo->cylinders = ioctl_geo.cylinders;
1601 
1602     return 0;
1603 }
1604 #else /* __linux__ */
1605 static int hdev_probe_geometry(BlockDriverState *bs, HDGeometry *geo)
1606 {
1607     return -ENOTSUP;
1608 }
1609 #endif
1610 
1611 #if defined(__linux__)
1612 static int handle_aiocb_ioctl(void *opaque)
1613 {
1614     RawPosixAIOData *aiocb = opaque;
1615     int ret;
1616 
1617     ret = RETRY_ON_EINTR(
1618         ioctl(aiocb->aio_fildes, aiocb->ioctl.cmd, aiocb->ioctl.buf)
1619     );
1620     if (ret == -1) {
1621         return -errno;
1622     }
1623 
1624     return 0;
1625 }
1626 #endif /* linux */
1627 
1628 static int handle_aiocb_flush(void *opaque)
1629 {
1630     RawPosixAIOData *aiocb = opaque;
1631     BDRVRawState *s = aiocb->bs->opaque;
1632     int ret;
1633 
1634     if (s->page_cache_inconsistent) {
1635         return -s->page_cache_inconsistent;
1636     }
1637 
1638     ret = qemu_fdatasync(aiocb->aio_fildes);
1639     if (ret == -1) {
1640         trace_file_flush_fdatasync_failed(errno);
1641 
1642         /* There is no clear definition of the semantics of a failing fsync(),
1643          * so we may have to assume the worst. The sad truth is that this
1644          * assumption is correct for Linux. Some pages are now probably marked
1645          * clean in the page cache even though they are inconsistent with the
1646          * on-disk contents. The next fdatasync() call would succeed, but no
1647          * further writeback attempt will be made. We can't get back to a state
1648          * in which we know what is on disk (we would have to rewrite
1649          * everything that was touched since the last fdatasync() at least), so
1650          * make bdrv_flush() fail permanently. Given that the behaviour isn't
1651          * really defined, I have little hope that other OSes are doing better.
1652          *
1653          * Obviously, this doesn't affect O_DIRECT, which bypasses the page
1654          * cache. */
1655         if ((s->open_flags & O_DIRECT) == 0) {
1656             s->page_cache_inconsistent = errno;
1657         }
1658         return -errno;
1659     }
1660     return 0;
1661 }
1662 
1663 #ifdef CONFIG_PREADV
1664 
1665 static bool preadv_present = true;
1666 
1667 static ssize_t
1668 qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset)
1669 {
1670     return preadv(fd, iov, nr_iov, offset);
1671 }
1672 
1673 static ssize_t
1674 qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset)
1675 {
1676     return pwritev(fd, iov, nr_iov, offset);
1677 }
1678 
1679 #else
1680 
1681 static bool preadv_present = false;
1682 
1683 static ssize_t
1684 qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset)
1685 {
1686     return -ENOSYS;
1687 }
1688 
1689 static ssize_t
1690 qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset)
1691 {
1692     return -ENOSYS;
1693 }
1694 
1695 #endif
1696 
1697 static ssize_t handle_aiocb_rw_vector(RawPosixAIOData *aiocb)
1698 {
1699     ssize_t len;
1700 
1701     len = RETRY_ON_EINTR(
1702         (aiocb->aio_type & (QEMU_AIO_WRITE | QEMU_AIO_ZONE_APPEND)) ?
1703             qemu_pwritev(aiocb->aio_fildes,
1704                            aiocb->io.iov,
1705                            aiocb->io.niov,
1706                            aiocb->aio_offset) :
1707             qemu_preadv(aiocb->aio_fildes,
1708                           aiocb->io.iov,
1709                           aiocb->io.niov,
1710                           aiocb->aio_offset)
1711     );
1712 
1713     if (len == -1) {
1714         return -errno;
1715     }
1716     return len;
1717 }
1718 
1719 /*
1720  * Read/writes the data to/from a given linear buffer.
1721  *
1722  * Returns the number of bytes handles or -errno in case of an error. Short
1723  * reads are only returned if the end of the file is reached.
1724  */
1725 static ssize_t handle_aiocb_rw_linear(RawPosixAIOData *aiocb, char *buf)
1726 {
1727     ssize_t offset = 0;
1728     ssize_t len;
1729 
1730     while (offset < aiocb->aio_nbytes) {
1731         if (aiocb->aio_type & (QEMU_AIO_WRITE | QEMU_AIO_ZONE_APPEND)) {
1732             len = pwrite(aiocb->aio_fildes,
1733                          (const char *)buf + offset,
1734                          aiocb->aio_nbytes - offset,
1735                          aiocb->aio_offset + offset);
1736         } else {
1737             len = pread(aiocb->aio_fildes,
1738                         buf + offset,
1739                         aiocb->aio_nbytes - offset,
1740                         aiocb->aio_offset + offset);
1741         }
1742         if (len == -1 && errno == EINTR) {
1743             continue;
1744         } else if (len == -1 && errno == EINVAL &&
1745                    (aiocb->bs->open_flags & BDRV_O_NOCACHE) &&
1746                    !(aiocb->aio_type & QEMU_AIO_WRITE) &&
1747                    offset > 0) {
1748             /* O_DIRECT pread() may fail with EINVAL when offset is unaligned
1749              * after a short read.  Assume that O_DIRECT short reads only occur
1750              * at EOF.  Therefore this is a short read, not an I/O error.
1751              */
1752             break;
1753         } else if (len == -1) {
1754             offset = -errno;
1755             break;
1756         } else if (len == 0) {
1757             break;
1758         }
1759         offset += len;
1760     }
1761 
1762     return offset;
1763 }
1764 
1765 static int handle_aiocb_rw(void *opaque)
1766 {
1767     RawPosixAIOData *aiocb = opaque;
1768     ssize_t nbytes;
1769     char *buf;
1770 
1771     if (!(aiocb->aio_type & QEMU_AIO_MISALIGNED)) {
1772         /*
1773          * If there is just a single buffer, and it is properly aligned
1774          * we can just use plain pread/pwrite without any problems.
1775          */
1776         if (aiocb->io.niov == 1) {
1777             nbytes = handle_aiocb_rw_linear(aiocb, aiocb->io.iov->iov_base);
1778             goto out;
1779         }
1780         /*
1781          * We have more than one iovec, and all are properly aligned.
1782          *
1783          * Try preadv/pwritev first and fall back to linearizing the
1784          * buffer if it's not supported.
1785          */
1786         if (preadv_present) {
1787             nbytes = handle_aiocb_rw_vector(aiocb);
1788             if (nbytes == aiocb->aio_nbytes ||
1789                 (nbytes < 0 && nbytes != -ENOSYS)) {
1790                 goto out;
1791             }
1792             preadv_present = false;
1793         }
1794 
1795         /*
1796          * XXX(hch): short read/write.  no easy way to handle the reminder
1797          * using these interfaces.  For now retry using plain
1798          * pread/pwrite?
1799          */
1800     }
1801 
1802     /*
1803      * Ok, we have to do it the hard way, copy all segments into
1804      * a single aligned buffer.
1805      */
1806     buf = qemu_try_blockalign(aiocb->bs, aiocb->aio_nbytes);
1807     if (buf == NULL) {
1808         nbytes = -ENOMEM;
1809         goto out;
1810     }
1811 
1812     if (aiocb->aio_type & QEMU_AIO_WRITE) {
1813         char *p = buf;
1814         int i;
1815 
1816         for (i = 0; i < aiocb->io.niov; ++i) {
1817             memcpy(p, aiocb->io.iov[i].iov_base, aiocb->io.iov[i].iov_len);
1818             p += aiocb->io.iov[i].iov_len;
1819         }
1820         assert(p - buf == aiocb->aio_nbytes);
1821     }
1822 
1823     nbytes = handle_aiocb_rw_linear(aiocb, buf);
1824     if (!(aiocb->aio_type & (QEMU_AIO_WRITE | QEMU_AIO_ZONE_APPEND))) {
1825         char *p = buf;
1826         size_t count = aiocb->aio_nbytes, copy;
1827         int i;
1828 
1829         for (i = 0; i < aiocb->io.niov && count; ++i) {
1830             copy = count;
1831             if (copy > aiocb->io.iov[i].iov_len) {
1832                 copy = aiocb->io.iov[i].iov_len;
1833             }
1834             memcpy(aiocb->io.iov[i].iov_base, p, copy);
1835             assert(count >= copy);
1836             p     += copy;
1837             count -= copy;
1838         }
1839         assert(count == 0);
1840     }
1841     qemu_vfree(buf);
1842 
1843 out:
1844     if (nbytes == aiocb->aio_nbytes) {
1845         return 0;
1846     } else if (nbytes >= 0 && nbytes < aiocb->aio_nbytes) {
1847         if (aiocb->aio_type & QEMU_AIO_WRITE) {
1848             return -EINVAL;
1849         } else {
1850             iov_memset(aiocb->io.iov, aiocb->io.niov, nbytes,
1851                       0, aiocb->aio_nbytes - nbytes);
1852             return 0;
1853         }
1854     } else {
1855         assert(nbytes < 0);
1856         return nbytes;
1857     }
1858 }
1859 
1860 #if defined(CONFIG_FALLOCATE) || defined(BLKZEROOUT) || defined(BLKDISCARD)
1861 static int translate_err(int err)
1862 {
1863     if (err == -ENODEV || err == -ENOSYS || err == -EOPNOTSUPP ||
1864         err == -ENOTTY) {
1865         err = -ENOTSUP;
1866     }
1867     return err;
1868 }
1869 #endif
1870 
1871 #ifdef CONFIG_FALLOCATE
1872 static int do_fallocate(int fd, int mode, off_t offset, off_t len)
1873 {
1874     do {
1875         if (fallocate(fd, mode, offset, len) == 0) {
1876             return 0;
1877         }
1878     } while (errno == EINTR);
1879     return translate_err(-errno);
1880 }
1881 #endif
1882 
1883 static ssize_t handle_aiocb_write_zeroes_block(RawPosixAIOData *aiocb)
1884 {
1885     int ret = -ENOTSUP;
1886     BDRVRawState *s = aiocb->bs->opaque;
1887 
1888     if (!s->has_write_zeroes) {
1889         return -ENOTSUP;
1890     }
1891 
1892 #ifdef BLKZEROOUT
1893     /* The BLKZEROOUT implementation in the kernel doesn't set
1894      * BLKDEV_ZERO_NOFALLBACK, so we can't call this if we have to avoid slow
1895      * fallbacks. */
1896     if (!(aiocb->aio_type & QEMU_AIO_NO_FALLBACK)) {
1897         do {
1898             uint64_t range[2] = { aiocb->aio_offset, aiocb->aio_nbytes };
1899             if (ioctl(aiocb->aio_fildes, BLKZEROOUT, range) == 0) {
1900                 return 0;
1901             }
1902         } while (errno == EINTR);
1903 
1904         ret = translate_err(-errno);
1905         if (ret == -ENOTSUP) {
1906             s->has_write_zeroes = false;
1907         }
1908     }
1909 #endif
1910 
1911     return ret;
1912 }
1913 
1914 static int handle_aiocb_write_zeroes(void *opaque)
1915 {
1916     RawPosixAIOData *aiocb = opaque;
1917 #ifdef CONFIG_FALLOCATE
1918     BDRVRawState *s = aiocb->bs->opaque;
1919     int64_t len;
1920 #endif
1921 
1922     if (aiocb->aio_type & QEMU_AIO_BLKDEV) {
1923         return handle_aiocb_write_zeroes_block(aiocb);
1924     }
1925 
1926 #ifdef CONFIG_FALLOCATE_ZERO_RANGE
1927     if (s->has_write_zeroes) {
1928         int ret = do_fallocate(s->fd, FALLOC_FL_ZERO_RANGE,
1929                                aiocb->aio_offset, aiocb->aio_nbytes);
1930         if (ret == -ENOTSUP) {
1931             s->has_write_zeroes = false;
1932         } else if (ret == 0 || ret != -EINVAL) {
1933             return ret;
1934         }
1935         /*
1936          * Note: Some file systems do not like unaligned byte ranges, and
1937          * return EINVAL in such a case, though they should not do it according
1938          * to the man-page of fallocate(). Thus we simply ignore this return
1939          * value and try the other fallbacks instead.
1940          */
1941     }
1942 #endif
1943 
1944 #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
1945     if (s->has_discard && s->has_fallocate) {
1946         int ret = do_fallocate(s->fd,
1947                                FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
1948                                aiocb->aio_offset, aiocb->aio_nbytes);
1949         if (ret == 0) {
1950             ret = do_fallocate(s->fd, 0, aiocb->aio_offset, aiocb->aio_nbytes);
1951             if (ret == 0 || ret != -ENOTSUP) {
1952                 return ret;
1953             }
1954             s->has_fallocate = false;
1955         } else if (ret == -EINVAL) {
1956             /*
1957              * Some file systems like older versions of GPFS do not like un-
1958              * aligned byte ranges, and return EINVAL in such a case, though
1959              * they should not do it according to the man-page of fallocate().
1960              * Warn about the bad filesystem and try the final fallback instead.
1961              */
1962             warn_report_once("Your file system is misbehaving: "
1963                              "fallocate(FALLOC_FL_PUNCH_HOLE) returned EINVAL. "
1964                              "Please report this bug to your file system "
1965                              "vendor.");
1966         } else if (ret != -ENOTSUP) {
1967             return ret;
1968         } else {
1969             s->has_discard = false;
1970         }
1971     }
1972 #endif
1973 
1974 #ifdef CONFIG_FALLOCATE
1975     /* Last resort: we are trying to extend the file with zeroed data. This
1976      * can be done via fallocate(fd, 0) */
1977     len = raw_co_getlength(aiocb->bs);
1978     if (s->has_fallocate && len >= 0 && aiocb->aio_offset >= len) {
1979         int ret = do_fallocate(s->fd, 0, aiocb->aio_offset, aiocb->aio_nbytes);
1980         if (ret == 0 || ret != -ENOTSUP) {
1981             return ret;
1982         }
1983         s->has_fallocate = false;
1984     }
1985 #endif
1986 
1987     return -ENOTSUP;
1988 }
1989 
1990 static int handle_aiocb_write_zeroes_unmap(void *opaque)
1991 {
1992     RawPosixAIOData *aiocb = opaque;
1993     BDRVRawState *s G_GNUC_UNUSED = aiocb->bs->opaque;
1994 
1995     /* First try to write zeros and unmap at the same time */
1996 
1997 #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
1998     int ret = do_fallocate(s->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
1999                            aiocb->aio_offset, aiocb->aio_nbytes);
2000     switch (ret) {
2001     case -ENOTSUP:
2002     case -EINVAL:
2003     case -EBUSY:
2004         break;
2005     default:
2006         return ret;
2007     }
2008 #endif
2009 
2010     /* If we couldn't manage to unmap while guaranteed that the area reads as
2011      * all-zero afterwards, just write zeroes without unmapping */
2012     return handle_aiocb_write_zeroes(aiocb);
2013 }
2014 
2015 #ifndef HAVE_COPY_FILE_RANGE
2016 static off_t copy_file_range(int in_fd, off_t *in_off, int out_fd,
2017                              off_t *out_off, size_t len, unsigned int flags)
2018 {
2019 #ifdef __NR_copy_file_range
2020     return syscall(__NR_copy_file_range, in_fd, in_off, out_fd,
2021                    out_off, len, flags);
2022 #else
2023     errno = ENOSYS;
2024     return -1;
2025 #endif
2026 }
2027 #endif
2028 
2029 /*
2030  * parse_zone - Fill a zone descriptor
2031  */
2032 #if defined(CONFIG_BLKZONED)
2033 static inline int parse_zone(struct BlockZoneDescriptor *zone,
2034                               const struct blk_zone *blkz) {
2035     zone->start = blkz->start << BDRV_SECTOR_BITS;
2036     zone->length = blkz->len << BDRV_SECTOR_BITS;
2037     zone->wp = blkz->wp << BDRV_SECTOR_BITS;
2038 
2039 #ifdef HAVE_BLK_ZONE_REP_CAPACITY
2040     zone->cap = blkz->capacity << BDRV_SECTOR_BITS;
2041 #else
2042     zone->cap = blkz->len << BDRV_SECTOR_BITS;
2043 #endif
2044 
2045     switch (blkz->type) {
2046     case BLK_ZONE_TYPE_SEQWRITE_REQ:
2047         zone->type = BLK_ZT_SWR;
2048         break;
2049     case BLK_ZONE_TYPE_SEQWRITE_PREF:
2050         zone->type = BLK_ZT_SWP;
2051         break;
2052     case BLK_ZONE_TYPE_CONVENTIONAL:
2053         zone->type = BLK_ZT_CONV;
2054         break;
2055     default:
2056         error_report("Unsupported zone type: 0x%x", blkz->type);
2057         return -ENOTSUP;
2058     }
2059 
2060     switch (blkz->cond) {
2061     case BLK_ZONE_COND_NOT_WP:
2062         zone->state = BLK_ZS_NOT_WP;
2063         break;
2064     case BLK_ZONE_COND_EMPTY:
2065         zone->state = BLK_ZS_EMPTY;
2066         break;
2067     case BLK_ZONE_COND_IMP_OPEN:
2068         zone->state = BLK_ZS_IOPEN;
2069         break;
2070     case BLK_ZONE_COND_EXP_OPEN:
2071         zone->state = BLK_ZS_EOPEN;
2072         break;
2073     case BLK_ZONE_COND_CLOSED:
2074         zone->state = BLK_ZS_CLOSED;
2075         break;
2076     case BLK_ZONE_COND_READONLY:
2077         zone->state = BLK_ZS_RDONLY;
2078         break;
2079     case BLK_ZONE_COND_FULL:
2080         zone->state = BLK_ZS_FULL;
2081         break;
2082     case BLK_ZONE_COND_OFFLINE:
2083         zone->state = BLK_ZS_OFFLINE;
2084         break;
2085     default:
2086         error_report("Unsupported zone state: 0x%x", blkz->cond);
2087         return -ENOTSUP;
2088     }
2089     return 0;
2090 }
2091 #endif
2092 
2093 #if defined(CONFIG_BLKZONED)
2094 static int handle_aiocb_zone_report(void *opaque)
2095 {
2096     RawPosixAIOData *aiocb = opaque;
2097     int fd = aiocb->aio_fildes;
2098     unsigned int *nr_zones = aiocb->zone_report.nr_zones;
2099     BlockZoneDescriptor *zones = aiocb->zone_report.zones;
2100     /* zoned block devices use 512-byte sectors */
2101     uint64_t sector = aiocb->aio_offset / 512;
2102 
2103     struct blk_zone *blkz;
2104     size_t rep_size;
2105     unsigned int nrz;
2106     int ret;
2107     unsigned int n = 0, i = 0;
2108 
2109     nrz = *nr_zones;
2110     rep_size = sizeof(struct blk_zone_report) + nrz * sizeof(struct blk_zone);
2111     g_autofree struct blk_zone_report *rep = NULL;
2112     rep = g_malloc(rep_size);
2113 
2114     blkz = (struct blk_zone *)(rep + 1);
2115     while (n < nrz) {
2116         memset(rep, 0, rep_size);
2117         rep->sector = sector;
2118         rep->nr_zones = nrz - n;
2119 
2120         do {
2121             ret = ioctl(fd, BLKREPORTZONE, rep);
2122         } while (ret != 0 && errno == EINTR);
2123         if (ret != 0) {
2124             error_report("%d: ioctl BLKREPORTZONE at %" PRId64 " failed %d",
2125                          fd, sector, errno);
2126             return -errno;
2127         }
2128 
2129         if (!rep->nr_zones) {
2130             break;
2131         }
2132 
2133         for (i = 0; i < rep->nr_zones; i++, n++) {
2134             ret = parse_zone(&zones[n], &blkz[i]);
2135             if (ret != 0) {
2136                 return ret;
2137             }
2138 
2139             /* The next report should start after the last zone reported */
2140             sector = blkz[i].start + blkz[i].len;
2141         }
2142     }
2143 
2144     *nr_zones = n;
2145     return 0;
2146 }
2147 #endif
2148 
2149 #if defined(CONFIG_BLKZONED)
2150 static int handle_aiocb_zone_mgmt(void *opaque)
2151 {
2152     RawPosixAIOData *aiocb = opaque;
2153     int fd = aiocb->aio_fildes;
2154     uint64_t sector = aiocb->aio_offset / 512;
2155     int64_t nr_sectors = aiocb->aio_nbytes / 512;
2156     struct blk_zone_range range;
2157     int ret;
2158 
2159     /* Execute the operation */
2160     range.sector = sector;
2161     range.nr_sectors = nr_sectors;
2162     do {
2163         ret = ioctl(fd, aiocb->zone_mgmt.op, &range);
2164     } while (ret != 0 && errno == EINTR);
2165 
2166     return ret < 0 ? -errno : ret;
2167 }
2168 #endif
2169 
2170 static int handle_aiocb_copy_range(void *opaque)
2171 {
2172     RawPosixAIOData *aiocb = opaque;
2173     uint64_t bytes = aiocb->aio_nbytes;
2174     off_t in_off = aiocb->aio_offset;
2175     off_t out_off = aiocb->copy_range.aio_offset2;
2176 
2177     while (bytes) {
2178         ssize_t ret = copy_file_range(aiocb->aio_fildes, &in_off,
2179                                       aiocb->copy_range.aio_fd2, &out_off,
2180                                       bytes, 0);
2181         trace_file_copy_file_range(aiocb->bs, aiocb->aio_fildes, in_off,
2182                                    aiocb->copy_range.aio_fd2, out_off, bytes,
2183                                    0, ret);
2184         if (ret == 0) {
2185             /* No progress (e.g. when beyond EOF), let the caller fall back to
2186              * buffer I/O. */
2187             return -ENOSPC;
2188         }
2189         if (ret < 0) {
2190             switch (errno) {
2191             case ENOSYS:
2192                 return -ENOTSUP;
2193             case EINTR:
2194                 continue;
2195             default:
2196                 return -errno;
2197             }
2198         }
2199         bytes -= ret;
2200     }
2201     return 0;
2202 }
2203 
2204 static int handle_aiocb_discard(void *opaque)
2205 {
2206     RawPosixAIOData *aiocb = opaque;
2207     int ret = -ENOTSUP;
2208     BDRVRawState *s = aiocb->bs->opaque;
2209 
2210     if (!s->has_discard) {
2211         return -ENOTSUP;
2212     }
2213 
2214     if (aiocb->aio_type & QEMU_AIO_BLKDEV) {
2215 #ifdef BLKDISCARD
2216         do {
2217             uint64_t range[2] = { aiocb->aio_offset, aiocb->aio_nbytes };
2218             if (ioctl(aiocb->aio_fildes, BLKDISCARD, range) == 0) {
2219                 return 0;
2220             }
2221         } while (errno == EINTR);
2222 
2223         ret = translate_err(-errno);
2224 #endif
2225     } else {
2226 #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
2227         ret = do_fallocate(s->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
2228                            aiocb->aio_offset, aiocb->aio_nbytes);
2229         ret = translate_err(ret);
2230 #elif defined(__APPLE__) && (__MACH__)
2231         fpunchhole_t fpunchhole;
2232         fpunchhole.fp_flags = 0;
2233         fpunchhole.reserved = 0;
2234         fpunchhole.fp_offset = aiocb->aio_offset;
2235         fpunchhole.fp_length = aiocb->aio_nbytes;
2236         if (fcntl(s->fd, F_PUNCHHOLE, &fpunchhole) == -1) {
2237             ret = errno == ENODEV ? -ENOTSUP : -errno;
2238         } else {
2239             ret = 0;
2240         }
2241 #endif
2242     }
2243 
2244     if (ret == -ENOTSUP) {
2245         s->has_discard = false;
2246     }
2247     return ret;
2248 }
2249 
2250 /*
2251  * Help alignment probing by allocating the first block.
2252  *
2253  * When reading with direct I/O from unallocated area on Gluster backed by XFS,
2254  * reading succeeds regardless of request length. In this case we fallback to
2255  * safe alignment which is not optimal. Allocating the first block avoids this
2256  * fallback.
2257  *
2258  * fd may be opened with O_DIRECT, but we don't know the buffer alignment or
2259  * request alignment, so we use safe values.
2260  *
2261  * Returns: 0 on success, -errno on failure. Since this is an optimization,
2262  * caller may ignore failures.
2263  */
2264 static int allocate_first_block(int fd, size_t max_size)
2265 {
2266     size_t write_size = (max_size < MAX_BLOCKSIZE)
2267         ? BDRV_SECTOR_SIZE
2268         : MAX_BLOCKSIZE;
2269     size_t max_align = MAX(MAX_BLOCKSIZE, qemu_real_host_page_size());
2270     void *buf;
2271     ssize_t n;
2272     int ret;
2273 
2274     buf = qemu_memalign(max_align, write_size);
2275     memset(buf, 0, write_size);
2276 
2277     n = RETRY_ON_EINTR(pwrite(fd, buf, write_size, 0));
2278 
2279     ret = (n == -1) ? -errno : 0;
2280 
2281     qemu_vfree(buf);
2282     return ret;
2283 }
2284 
2285 static int handle_aiocb_truncate(void *opaque)
2286 {
2287     RawPosixAIOData *aiocb = opaque;
2288     int result = 0;
2289     int64_t current_length = 0;
2290     char *buf = NULL;
2291     struct stat st;
2292     int fd = aiocb->aio_fildes;
2293     int64_t offset = aiocb->aio_offset;
2294     PreallocMode prealloc = aiocb->truncate.prealloc;
2295     Error **errp = aiocb->truncate.errp;
2296 
2297     if (fstat(fd, &st) < 0) {
2298         result = -errno;
2299         error_setg_errno(errp, -result, "Could not stat file");
2300         return result;
2301     }
2302 
2303     current_length = st.st_size;
2304     if (current_length > offset && prealloc != PREALLOC_MODE_OFF) {
2305         error_setg(errp, "Cannot use preallocation for shrinking files");
2306         return -ENOTSUP;
2307     }
2308 
2309     switch (prealloc) {
2310 #ifdef CONFIG_POSIX_FALLOCATE
2311     case PREALLOC_MODE_FALLOC:
2312         /*
2313          * Truncating before posix_fallocate() makes it about twice slower on
2314          * file systems that do not support fallocate(), trying to check if a
2315          * block is allocated before allocating it, so don't do that here.
2316          */
2317         if (offset != current_length) {
2318             result = -posix_fallocate(fd, current_length,
2319                                       offset - current_length);
2320             if (result != 0) {
2321                 /* posix_fallocate() doesn't set errno. */
2322                 error_setg_errno(errp, -result,
2323                                  "Could not preallocate new data");
2324             } else if (current_length == 0) {
2325                 /*
2326                  * posix_fallocate() uses fallocate() if the filesystem
2327                  * supports it, or fallback to manually writing zeroes. If
2328                  * fallocate() was used, unaligned reads from the fallocated
2329                  * area in raw_probe_alignment() will succeed, hence we need to
2330                  * allocate the first block.
2331                  *
2332                  * Optimize future alignment probing; ignore failures.
2333                  */
2334                 allocate_first_block(fd, offset);
2335             }
2336         } else {
2337             result = 0;
2338         }
2339         goto out;
2340 #endif
2341     case PREALLOC_MODE_FULL:
2342     {
2343         int64_t num = 0, left = offset - current_length;
2344         off_t seek_result;
2345 
2346         /*
2347          * Knowing the final size from the beginning could allow the file
2348          * system driver to do less allocations and possibly avoid
2349          * fragmentation of the file.
2350          */
2351         if (ftruncate(fd, offset) != 0) {
2352             result = -errno;
2353             error_setg_errno(errp, -result, "Could not resize file");
2354             goto out;
2355         }
2356 
2357         buf = g_malloc0(65536);
2358 
2359         seek_result = lseek(fd, current_length, SEEK_SET);
2360         if (seek_result < 0) {
2361             result = -errno;
2362             error_setg_errno(errp, -result,
2363                              "Failed to seek to the old end of file");
2364             goto out;
2365         }
2366 
2367         while (left > 0) {
2368             num = MIN(left, 65536);
2369             result = write(fd, buf, num);
2370             if (result < 0) {
2371                 if (errno == EINTR) {
2372                     continue;
2373                 }
2374                 result = -errno;
2375                 error_setg_errno(errp, -result,
2376                                  "Could not write zeros for preallocation");
2377                 goto out;
2378             }
2379             left -= result;
2380         }
2381         if (result >= 0) {
2382             result = fsync(fd);
2383             if (result < 0) {
2384                 result = -errno;
2385                 error_setg_errno(errp, -result,
2386                                  "Could not flush file to disk");
2387                 goto out;
2388             }
2389         }
2390         goto out;
2391     }
2392     case PREALLOC_MODE_OFF:
2393         if (ftruncate(fd, offset) != 0) {
2394             result = -errno;
2395             error_setg_errno(errp, -result, "Could not resize file");
2396         } else if (current_length == 0 && offset > current_length) {
2397             /* Optimize future alignment probing; ignore failures. */
2398             allocate_first_block(fd, offset);
2399         }
2400         return result;
2401     default:
2402         result = -ENOTSUP;
2403         error_setg(errp, "Unsupported preallocation mode: %s",
2404                    PreallocMode_str(prealloc));
2405         return result;
2406     }
2407 
2408 out:
2409     if (result < 0) {
2410         if (ftruncate(fd, current_length) < 0) {
2411             error_report("Failed to restore old file length: %s",
2412                          strerror(errno));
2413         }
2414     }
2415 
2416     g_free(buf);
2417     return result;
2418 }
2419 
2420 static int coroutine_fn raw_thread_pool_submit(ThreadPoolFunc func, void *arg)
2421 {
2422     return thread_pool_submit_co(func, arg);
2423 }
2424 
2425 /*
2426  * Check if all memory in this vector is sector aligned.
2427  */
2428 static bool bdrv_qiov_is_aligned(BlockDriverState *bs, QEMUIOVector *qiov)
2429 {
2430     int i;
2431     size_t alignment = bdrv_min_mem_align(bs);
2432     size_t len = bs->bl.request_alignment;
2433     IO_CODE();
2434 
2435     for (i = 0; i < qiov->niov; i++) {
2436         if ((uintptr_t) qiov->iov[i].iov_base % alignment) {
2437             return false;
2438         }
2439         if (qiov->iov[i].iov_len % len) {
2440             return false;
2441         }
2442     }
2443 
2444     return true;
2445 }
2446 
2447 static int coroutine_fn raw_co_prw(BlockDriverState *bs, uint64_t offset,
2448                                    uint64_t bytes, QEMUIOVector *qiov, int type)
2449 {
2450     BDRVRawState *s = bs->opaque;
2451     RawPosixAIOData acb;
2452     int ret;
2453 
2454     if (fd_open(bs) < 0)
2455         return -EIO;
2456 #if defined(CONFIG_BLKZONED)
2457     if ((type & (QEMU_AIO_WRITE | QEMU_AIO_ZONE_APPEND)) && bs->wps) {
2458         qemu_co_mutex_lock(&bs->wps->colock);
2459         if (type & QEMU_AIO_ZONE_APPEND && bs->bl.zone_size) {
2460             int index = offset / bs->bl.zone_size;
2461             offset = bs->wps->wp[index];
2462         }
2463     }
2464 #endif
2465 
2466     /*
2467      * When using O_DIRECT, the request must be aligned to be able to use
2468      * either libaio or io_uring interface. If not fail back to regular thread
2469      * pool read/write code which emulates this for us if we
2470      * set QEMU_AIO_MISALIGNED.
2471      */
2472     if (s->needs_alignment && !bdrv_qiov_is_aligned(bs, qiov)) {
2473         type |= QEMU_AIO_MISALIGNED;
2474 #ifdef CONFIG_LINUX_IO_URING
2475     } else if (s->use_linux_io_uring) {
2476         assert(qiov->size == bytes);
2477         ret = luring_co_submit(bs, s->fd, offset, qiov, type);
2478         goto out;
2479 #endif
2480 #ifdef CONFIG_LINUX_AIO
2481     } else if (s->use_linux_aio) {
2482         assert(qiov->size == bytes);
2483         ret = laio_co_submit(s->fd, offset, qiov, type,
2484                               s->aio_max_batch);
2485         goto out;
2486 #endif
2487     }
2488 
2489     acb = (RawPosixAIOData) {
2490         .bs             = bs,
2491         .aio_fildes     = s->fd,
2492         .aio_type       = type,
2493         .aio_offset     = offset,
2494         .aio_nbytes     = bytes,
2495         .io             = {
2496             .iov            = qiov->iov,
2497             .niov           = qiov->niov,
2498         },
2499     };
2500 
2501     assert(qiov->size == bytes);
2502     ret = raw_thread_pool_submit(handle_aiocb_rw, &acb);
2503     goto out; /* Avoid the compiler err of unused label */
2504 
2505 out:
2506 #if defined(CONFIG_BLKZONED)
2507 {
2508     BlockZoneWps *wps = bs->wps;
2509     if (ret == 0) {
2510         if ((type & (QEMU_AIO_WRITE | QEMU_AIO_ZONE_APPEND))
2511             && wps && bs->bl.zone_size) {
2512             uint64_t *wp = &wps->wp[offset / bs->bl.zone_size];
2513             if (!BDRV_ZT_IS_CONV(*wp)) {
2514                 if (type & QEMU_AIO_ZONE_APPEND) {
2515                     *s->offset = *wp;
2516                     trace_zbd_zone_append_complete(bs, *s->offset
2517                         >> BDRV_SECTOR_BITS);
2518                 }
2519                 /* Advance the wp if needed */
2520                 if (offset + bytes > *wp) {
2521                     *wp = offset + bytes;
2522                 }
2523             }
2524         }
2525     } else {
2526         if (type & (QEMU_AIO_WRITE | QEMU_AIO_ZONE_APPEND)) {
2527             update_zones_wp(bs, s->fd, 0, 1);
2528         }
2529     }
2530 
2531     if ((type & (QEMU_AIO_WRITE | QEMU_AIO_ZONE_APPEND)) && wps) {
2532         qemu_co_mutex_unlock(&wps->colock);
2533     }
2534 }
2535 #endif
2536     return ret;
2537 }
2538 
2539 static int coroutine_fn raw_co_preadv(BlockDriverState *bs, int64_t offset,
2540                                       int64_t bytes, QEMUIOVector *qiov,
2541                                       BdrvRequestFlags flags)
2542 {
2543     return raw_co_prw(bs, offset, bytes, qiov, QEMU_AIO_READ);
2544 }
2545 
2546 static int coroutine_fn raw_co_pwritev(BlockDriverState *bs, int64_t offset,
2547                                        int64_t bytes, QEMUIOVector *qiov,
2548                                        BdrvRequestFlags flags)
2549 {
2550     return raw_co_prw(bs, offset, bytes, qiov, QEMU_AIO_WRITE);
2551 }
2552 
2553 static void coroutine_fn raw_co_io_plug(BlockDriverState *bs)
2554 {
2555     BDRVRawState __attribute__((unused)) *s = bs->opaque;
2556 #ifdef CONFIG_LINUX_AIO
2557     if (s->use_linux_aio) {
2558         laio_io_plug();
2559     }
2560 #endif
2561 #ifdef CONFIG_LINUX_IO_URING
2562     if (s->use_linux_io_uring) {
2563         luring_io_plug();
2564     }
2565 #endif
2566 }
2567 
2568 static void coroutine_fn raw_co_io_unplug(BlockDriverState *bs)
2569 {
2570     BDRVRawState __attribute__((unused)) *s = bs->opaque;
2571 #ifdef CONFIG_LINUX_AIO
2572     if (s->use_linux_aio) {
2573         laio_io_unplug(s->aio_max_batch);
2574     }
2575 #endif
2576 #ifdef CONFIG_LINUX_IO_URING
2577     if (s->use_linux_io_uring) {
2578         luring_io_unplug();
2579     }
2580 #endif
2581 }
2582 
2583 static int coroutine_fn raw_co_flush_to_disk(BlockDriverState *bs)
2584 {
2585     BDRVRawState *s = bs->opaque;
2586     RawPosixAIOData acb;
2587     int ret;
2588 
2589     ret = fd_open(bs);
2590     if (ret < 0) {
2591         return ret;
2592     }
2593 
2594     acb = (RawPosixAIOData) {
2595         .bs             = bs,
2596         .aio_fildes     = s->fd,
2597         .aio_type       = QEMU_AIO_FLUSH,
2598     };
2599 
2600 #ifdef CONFIG_LINUX_IO_URING
2601     if (s->use_linux_io_uring) {
2602         return luring_co_submit(bs, s->fd, 0, NULL, QEMU_AIO_FLUSH);
2603     }
2604 #endif
2605     return raw_thread_pool_submit(handle_aiocb_flush, &acb);
2606 }
2607 
2608 static void raw_aio_attach_aio_context(BlockDriverState *bs,
2609                                        AioContext *new_context)
2610 {
2611     BDRVRawState __attribute__((unused)) *s = bs->opaque;
2612 #ifdef CONFIG_LINUX_AIO
2613     if (s->use_linux_aio) {
2614         Error *local_err = NULL;
2615         if (!aio_setup_linux_aio(new_context, &local_err)) {
2616             error_reportf_err(local_err, "Unable to use native AIO, "
2617                                          "falling back to thread pool: ");
2618             s->use_linux_aio = false;
2619         }
2620     }
2621 #endif
2622 #ifdef CONFIG_LINUX_IO_URING
2623     if (s->use_linux_io_uring) {
2624         Error *local_err = NULL;
2625         if (!aio_setup_linux_io_uring(new_context, &local_err)) {
2626             error_reportf_err(local_err, "Unable to use linux io_uring, "
2627                                          "falling back to thread pool: ");
2628             s->use_linux_io_uring = false;
2629         }
2630     }
2631 #endif
2632 }
2633 
2634 static void raw_close(BlockDriverState *bs)
2635 {
2636     BDRVRawState *s = bs->opaque;
2637 
2638     if (s->fd >= 0) {
2639 #if defined(CONFIG_BLKZONED)
2640         g_free(bs->wps);
2641 #endif
2642         qemu_close(s->fd);
2643         s->fd = -1;
2644     }
2645 }
2646 
2647 /**
2648  * Truncates the given regular file @fd to @offset and, when growing, fills the
2649  * new space according to @prealloc.
2650  *
2651  * Returns: 0 on success, -errno on failure.
2652  */
2653 static int coroutine_fn
2654 raw_regular_truncate(BlockDriverState *bs, int fd, int64_t offset,
2655                      PreallocMode prealloc, Error **errp)
2656 {
2657     RawPosixAIOData acb;
2658 
2659     acb = (RawPosixAIOData) {
2660         .bs             = bs,
2661         .aio_fildes     = fd,
2662         .aio_type       = QEMU_AIO_TRUNCATE,
2663         .aio_offset     = offset,
2664         .truncate       = {
2665             .prealloc       = prealloc,
2666             .errp           = errp,
2667         },
2668     };
2669 
2670     return raw_thread_pool_submit(handle_aiocb_truncate, &acb);
2671 }
2672 
2673 static int coroutine_fn raw_co_truncate(BlockDriverState *bs, int64_t offset,
2674                                         bool exact, PreallocMode prealloc,
2675                                         BdrvRequestFlags flags, Error **errp)
2676 {
2677     BDRVRawState *s = bs->opaque;
2678     struct stat st;
2679     int ret;
2680 
2681     if (fstat(s->fd, &st)) {
2682         ret = -errno;
2683         error_setg_errno(errp, -ret, "Failed to fstat() the file");
2684         return ret;
2685     }
2686 
2687     if (S_ISREG(st.st_mode)) {
2688         /* Always resizes to the exact @offset */
2689         return raw_regular_truncate(bs, s->fd, offset, prealloc, errp);
2690     }
2691 
2692     if (prealloc != PREALLOC_MODE_OFF) {
2693         error_setg(errp, "Preallocation mode '%s' unsupported for this "
2694                    "non-regular file", PreallocMode_str(prealloc));
2695         return -ENOTSUP;
2696     }
2697 
2698     if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
2699         int64_t cur_length = raw_co_getlength(bs);
2700 
2701         if (offset != cur_length && exact) {
2702             error_setg(errp, "Cannot resize device files");
2703             return -ENOTSUP;
2704         } else if (offset > cur_length) {
2705             error_setg(errp, "Cannot grow device files");
2706             return -EINVAL;
2707         }
2708     } else {
2709         error_setg(errp, "Resizing this file is not supported");
2710         return -ENOTSUP;
2711     }
2712 
2713     return 0;
2714 }
2715 
2716 #ifdef __OpenBSD__
2717 static int64_t coroutine_fn raw_co_getlength(BlockDriverState *bs)
2718 {
2719     BDRVRawState *s = bs->opaque;
2720     int fd = s->fd;
2721     struct stat st;
2722 
2723     if (fstat(fd, &st))
2724         return -errno;
2725     if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
2726         struct disklabel dl;
2727 
2728         if (ioctl(fd, DIOCGDINFO, &dl))
2729             return -errno;
2730         return (uint64_t)dl.d_secsize *
2731             dl.d_partitions[DISKPART(st.st_rdev)].p_size;
2732     } else
2733         return st.st_size;
2734 }
2735 #elif defined(__NetBSD__)
2736 static int64_t coroutine_fn raw_co_getlength(BlockDriverState *bs)
2737 {
2738     BDRVRawState *s = bs->opaque;
2739     int fd = s->fd;
2740     struct stat st;
2741 
2742     if (fstat(fd, &st))
2743         return -errno;
2744     if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
2745         struct dkwedge_info dkw;
2746 
2747         if (ioctl(fd, DIOCGWEDGEINFO, &dkw) != -1) {
2748             return dkw.dkw_size * 512;
2749         } else {
2750             struct disklabel dl;
2751 
2752             if (ioctl(fd, DIOCGDINFO, &dl))
2753                 return -errno;
2754             return (uint64_t)dl.d_secsize *
2755                 dl.d_partitions[DISKPART(st.st_rdev)].p_size;
2756         }
2757     } else
2758         return st.st_size;
2759 }
2760 #elif defined(__sun__)
2761 static int64_t coroutine_fn raw_co_getlength(BlockDriverState *bs)
2762 {
2763     BDRVRawState *s = bs->opaque;
2764     struct dk_minfo minfo;
2765     int ret;
2766     int64_t size;
2767 
2768     ret = fd_open(bs);
2769     if (ret < 0) {
2770         return ret;
2771     }
2772 
2773     /*
2774      * Use the DKIOCGMEDIAINFO ioctl to read the size.
2775      */
2776     ret = ioctl(s->fd, DKIOCGMEDIAINFO, &minfo);
2777     if (ret != -1) {
2778         return minfo.dki_lbsize * minfo.dki_capacity;
2779     }
2780 
2781     /*
2782      * There are reports that lseek on some devices fails, but
2783      * irc discussion said that contingency on contingency was overkill.
2784      */
2785     size = lseek(s->fd, 0, SEEK_END);
2786     if (size < 0) {
2787         return -errno;
2788     }
2789     return size;
2790 }
2791 #elif defined(CONFIG_BSD)
2792 static int64_t coroutine_fn raw_co_getlength(BlockDriverState *bs)
2793 {
2794     BDRVRawState *s = bs->opaque;
2795     int fd = s->fd;
2796     int64_t size;
2797     struct stat sb;
2798 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
2799     int reopened = 0;
2800 #endif
2801     int ret;
2802 
2803     ret = fd_open(bs);
2804     if (ret < 0)
2805         return ret;
2806 
2807 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
2808 again:
2809 #endif
2810     if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
2811         size = 0;
2812 #ifdef DIOCGMEDIASIZE
2813         if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size)) {
2814             size = 0;
2815         }
2816 #endif
2817 #ifdef DIOCGPART
2818         if (size == 0) {
2819             struct partinfo pi;
2820             if (ioctl(fd, DIOCGPART, &pi) == 0) {
2821                 size = pi.media_size;
2822             }
2823         }
2824 #endif
2825 #if defined(DKIOCGETBLOCKCOUNT) && defined(DKIOCGETBLOCKSIZE)
2826         if (size == 0) {
2827             uint64_t sectors = 0;
2828             uint32_t sector_size = 0;
2829 
2830             if (ioctl(fd, DKIOCGETBLOCKCOUNT, &sectors) == 0
2831                && ioctl(fd, DKIOCGETBLOCKSIZE, &sector_size) == 0) {
2832                 size = sectors * sector_size;
2833             }
2834         }
2835 #endif
2836         if (size == 0) {
2837             size = lseek(fd, 0LL, SEEK_END);
2838         }
2839         if (size < 0) {
2840             return -errno;
2841         }
2842 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
2843         switch(s->type) {
2844         case FTYPE_CD:
2845             /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
2846             if (size == 2048LL * (unsigned)-1)
2847                 size = 0;
2848             /* XXX no disc?  maybe we need to reopen... */
2849             if (size <= 0 && !reopened && cdrom_reopen(bs) >= 0) {
2850                 reopened = 1;
2851                 goto again;
2852             }
2853         }
2854 #endif
2855     } else {
2856         size = lseek(fd, 0, SEEK_END);
2857         if (size < 0) {
2858             return -errno;
2859         }
2860     }
2861     return size;
2862 }
2863 #else
2864 static int64_t coroutine_fn raw_co_getlength(BlockDriverState *bs)
2865 {
2866     BDRVRawState *s = bs->opaque;
2867     int ret;
2868     int64_t size;
2869 
2870     ret = fd_open(bs);
2871     if (ret < 0) {
2872         return ret;
2873     }
2874 
2875     size = lseek(s->fd, 0, SEEK_END);
2876     if (size < 0) {
2877         return -errno;
2878     }
2879     return size;
2880 }
2881 #endif
2882 
2883 static int64_t coroutine_fn raw_co_get_allocated_file_size(BlockDriverState *bs)
2884 {
2885     struct stat st;
2886     BDRVRawState *s = bs->opaque;
2887 
2888     if (fstat(s->fd, &st) < 0) {
2889         return -errno;
2890     }
2891     return (int64_t)st.st_blocks * 512;
2892 }
2893 
2894 static int coroutine_fn
2895 raw_co_create(BlockdevCreateOptions *options, Error **errp)
2896 {
2897     BlockdevCreateOptionsFile *file_opts;
2898     Error *local_err = NULL;
2899     int fd;
2900     uint64_t perm, shared;
2901     int result = 0;
2902 
2903     /* Validate options and set default values */
2904     assert(options->driver == BLOCKDEV_DRIVER_FILE);
2905     file_opts = &options->u.file;
2906 
2907     if (!file_opts->has_nocow) {
2908         file_opts->nocow = false;
2909     }
2910     if (!file_opts->has_preallocation) {
2911         file_opts->preallocation = PREALLOC_MODE_OFF;
2912     }
2913     if (!file_opts->has_extent_size_hint) {
2914         file_opts->extent_size_hint = 1 * MiB;
2915     }
2916     if (file_opts->extent_size_hint > UINT32_MAX) {
2917         result = -EINVAL;
2918         error_setg(errp, "Extent size hint is too large");
2919         goto out;
2920     }
2921 
2922     /* Create file */
2923     fd = qemu_create(file_opts->filename, O_RDWR | O_BINARY, 0644, errp);
2924     if (fd < 0) {
2925         result = -errno;
2926         goto out;
2927     }
2928 
2929     /* Take permissions: We want to discard everything, so we need
2930      * BLK_PERM_WRITE; and truncation to the desired size requires
2931      * BLK_PERM_RESIZE.
2932      * On the other hand, we cannot share the RESIZE permission
2933      * because we promise that after this function, the file has the
2934      * size given in the options.  If someone else were to resize it
2935      * concurrently, we could not guarantee that.
2936      * Note that after this function, we can no longer guarantee that
2937      * the file is not touched by a third party, so it may be resized
2938      * then. */
2939     perm = BLK_PERM_WRITE | BLK_PERM_RESIZE;
2940     shared = BLK_PERM_ALL & ~BLK_PERM_RESIZE;
2941 
2942     /* Step one: Take locks */
2943     result = raw_apply_lock_bytes(NULL, fd, perm, ~shared, false, errp);
2944     if (result < 0) {
2945         goto out_close;
2946     }
2947 
2948     /* Step two: Check that nobody else has taken conflicting locks */
2949     result = raw_check_lock_bytes(fd, perm, shared, errp);
2950     if (result < 0) {
2951         error_append_hint(errp,
2952                           "Is another process using the image [%s]?\n",
2953                           file_opts->filename);
2954         goto out_unlock;
2955     }
2956 
2957     /* Clear the file by truncating it to 0 */
2958     result = raw_regular_truncate(NULL, fd, 0, PREALLOC_MODE_OFF, errp);
2959     if (result < 0) {
2960         goto out_unlock;
2961     }
2962 
2963     if (file_opts->nocow) {
2964 #ifdef __linux__
2965         /* Set NOCOW flag to solve performance issue on fs like btrfs.
2966          * This is an optimisation. The FS_IOC_SETFLAGS ioctl return value
2967          * will be ignored since any failure of this operation should not
2968          * block the left work.
2969          */
2970         int attr;
2971         if (ioctl(fd, FS_IOC_GETFLAGS, &attr) == 0) {
2972             attr |= FS_NOCOW_FL;
2973             ioctl(fd, FS_IOC_SETFLAGS, &attr);
2974         }
2975 #endif
2976     }
2977 #ifdef FS_IOC_FSSETXATTR
2978     /*
2979      * Try to set the extent size hint. Failure is not fatal, and a warning is
2980      * only printed if the option was explicitly specified.
2981      */
2982     {
2983         struct fsxattr attr;
2984         result = ioctl(fd, FS_IOC_FSGETXATTR, &attr);
2985         if (result == 0) {
2986             attr.fsx_xflags |= FS_XFLAG_EXTSIZE;
2987             attr.fsx_extsize = file_opts->extent_size_hint;
2988             result = ioctl(fd, FS_IOC_FSSETXATTR, &attr);
2989         }
2990         if (result < 0 && file_opts->has_extent_size_hint &&
2991             file_opts->extent_size_hint)
2992         {
2993             warn_report("Failed to set extent size hint: %s",
2994                         strerror(errno));
2995         }
2996     }
2997 #endif
2998 
2999     /* Resize and potentially preallocate the file to the desired
3000      * final size */
3001     result = raw_regular_truncate(NULL, fd, file_opts->size,
3002                                   file_opts->preallocation, errp);
3003     if (result < 0) {
3004         goto out_unlock;
3005     }
3006 
3007 out_unlock:
3008     raw_apply_lock_bytes(NULL, fd, 0, 0, true, &local_err);
3009     if (local_err) {
3010         /* The above call should not fail, and if it does, that does
3011          * not mean the whole creation operation has failed.  So
3012          * report it the user for their convenience, but do not report
3013          * it to the caller. */
3014         warn_report_err(local_err);
3015     }
3016 
3017 out_close:
3018     if (qemu_close(fd) != 0 && result == 0) {
3019         result = -errno;
3020         error_setg_errno(errp, -result, "Could not close the new file");
3021     }
3022 out:
3023     return result;
3024 }
3025 
3026 static int coroutine_fn GRAPH_RDLOCK
3027 raw_co_create_opts(BlockDriver *drv, const char *filename,
3028                    QemuOpts *opts, Error **errp)
3029 {
3030     BlockdevCreateOptions options;
3031     int64_t total_size = 0;
3032     int64_t extent_size_hint = 0;
3033     bool has_extent_size_hint = false;
3034     bool nocow = false;
3035     PreallocMode prealloc;
3036     char *buf = NULL;
3037     Error *local_err = NULL;
3038 
3039     /* Skip file: protocol prefix */
3040     strstart(filename, "file:", &filename);
3041 
3042     /* Read out options */
3043     total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
3044                           BDRV_SECTOR_SIZE);
3045     if (qemu_opt_get(opts, BLOCK_OPT_EXTENT_SIZE_HINT)) {
3046         has_extent_size_hint = true;
3047         extent_size_hint =
3048             qemu_opt_get_size_del(opts, BLOCK_OPT_EXTENT_SIZE_HINT, -1);
3049     }
3050     nocow = qemu_opt_get_bool(opts, BLOCK_OPT_NOCOW, false);
3051     buf = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC);
3052     prealloc = qapi_enum_parse(&PreallocMode_lookup, buf,
3053                                PREALLOC_MODE_OFF, &local_err);
3054     g_free(buf);
3055     if (local_err) {
3056         error_propagate(errp, local_err);
3057         return -EINVAL;
3058     }
3059 
3060     options = (BlockdevCreateOptions) {
3061         .driver     = BLOCKDEV_DRIVER_FILE,
3062         .u.file     = {
3063             .filename           = (char *) filename,
3064             .size               = total_size,
3065             .has_preallocation  = true,
3066             .preallocation      = prealloc,
3067             .has_nocow          = true,
3068             .nocow              = nocow,
3069             .has_extent_size_hint = has_extent_size_hint,
3070             .extent_size_hint   = extent_size_hint,
3071         },
3072     };
3073     return raw_co_create(&options, errp);
3074 }
3075 
3076 static int coroutine_fn raw_co_delete_file(BlockDriverState *bs,
3077                                            Error **errp)
3078 {
3079     struct stat st;
3080     int ret;
3081 
3082     if (!(stat(bs->filename, &st) == 0) || !S_ISREG(st.st_mode)) {
3083         error_setg_errno(errp, ENOENT, "%s is not a regular file",
3084                          bs->filename);
3085         return -ENOENT;
3086     }
3087 
3088     ret = unlink(bs->filename);
3089     if (ret < 0) {
3090         ret = -errno;
3091         error_setg_errno(errp, -ret, "Error when deleting file %s",
3092                          bs->filename);
3093     }
3094 
3095     return ret;
3096 }
3097 
3098 /*
3099  * Find allocation range in @bs around offset @start.
3100  * May change underlying file descriptor's file offset.
3101  * If @start is not in a hole, store @start in @data, and the
3102  * beginning of the next hole in @hole, and return 0.
3103  * If @start is in a non-trailing hole, store @start in @hole and the
3104  * beginning of the next non-hole in @data, and return 0.
3105  * If @start is in a trailing hole or beyond EOF, return -ENXIO.
3106  * If we can't find out, return a negative errno other than -ENXIO.
3107  */
3108 static int find_allocation(BlockDriverState *bs, off_t start,
3109                            off_t *data, off_t *hole)
3110 {
3111 #if defined SEEK_HOLE && defined SEEK_DATA
3112     BDRVRawState *s = bs->opaque;
3113     off_t offs;
3114 
3115     /*
3116      * SEEK_DATA cases:
3117      * D1. offs == start: start is in data
3118      * D2. offs > start: start is in a hole, next data at offs
3119      * D3. offs < 0, errno = ENXIO: either start is in a trailing hole
3120      *                              or start is beyond EOF
3121      *     If the latter happens, the file has been truncated behind
3122      *     our back since we opened it.  All bets are off then.
3123      *     Treating like a trailing hole is simplest.
3124      * D4. offs < 0, errno != ENXIO: we learned nothing
3125      */
3126     offs = lseek(s->fd, start, SEEK_DATA);
3127     if (offs < 0) {
3128         return -errno;          /* D3 or D4 */
3129     }
3130 
3131     if (offs < start) {
3132         /* This is not a valid return by lseek().  We are safe to just return
3133          * -EIO in this case, and we'll treat it like D4. */
3134         return -EIO;
3135     }
3136 
3137     if (offs > start) {
3138         /* D2: in hole, next data at offs */
3139         *hole = start;
3140         *data = offs;
3141         return 0;
3142     }
3143 
3144     /* D1: in data, end not yet known */
3145 
3146     /*
3147      * SEEK_HOLE cases:
3148      * H1. offs == start: start is in a hole
3149      *     If this happens here, a hole has been dug behind our back
3150      *     since the previous lseek().
3151      * H2. offs > start: either start is in data, next hole at offs,
3152      *                   or start is in trailing hole, EOF at offs
3153      *     Linux treats trailing holes like any other hole: offs ==
3154      *     start.  Solaris seeks to EOF instead: offs > start (blech).
3155      *     If that happens here, a hole has been dug behind our back
3156      *     since the previous lseek().
3157      * H3. offs < 0, errno = ENXIO: start is beyond EOF
3158      *     If this happens, the file has been truncated behind our
3159      *     back since we opened it.  Treat it like a trailing hole.
3160      * H4. offs < 0, errno != ENXIO: we learned nothing
3161      *     Pretend we know nothing at all, i.e. "forget" about D1.
3162      */
3163     offs = lseek(s->fd, start, SEEK_HOLE);
3164     if (offs < 0) {
3165         return -errno;          /* D1 and (H3 or H4) */
3166     }
3167 
3168     if (offs < start) {
3169         /* This is not a valid return by lseek().  We are safe to just return
3170          * -EIO in this case, and we'll treat it like H4. */
3171         return -EIO;
3172     }
3173 
3174     if (offs > start) {
3175         /*
3176          * D1 and H2: either in data, next hole at offs, or it was in
3177          * data but is now in a trailing hole.  In the latter case,
3178          * all bets are off.  Treating it as if it there was data all
3179          * the way to EOF is safe, so simply do that.
3180          */
3181         *data = start;
3182         *hole = offs;
3183         return 0;
3184     }
3185 
3186     /* D1 and H1 */
3187     return -EBUSY;
3188 #else
3189     return -ENOTSUP;
3190 #endif
3191 }
3192 
3193 /*
3194  * Returns the allocation status of the specified offset.
3195  *
3196  * The block layer guarantees 'offset' and 'bytes' are within bounds.
3197  *
3198  * 'pnum' is set to the number of bytes (including and immediately following
3199  * the specified offset) that are known to be in the same
3200  * allocated/unallocated state.
3201  *
3202  * 'bytes' is a soft cap for 'pnum'.  If the information is free, 'pnum' may
3203  * well exceed it.
3204  */
3205 static int coroutine_fn raw_co_block_status(BlockDriverState *bs,
3206                                             bool want_zero,
3207                                             int64_t offset,
3208                                             int64_t bytes, int64_t *pnum,
3209                                             int64_t *map,
3210                                             BlockDriverState **file)
3211 {
3212     off_t data = 0, hole = 0;
3213     int ret;
3214 
3215     assert(QEMU_IS_ALIGNED(offset | bytes, bs->bl.request_alignment));
3216 
3217     ret = fd_open(bs);
3218     if (ret < 0) {
3219         return ret;
3220     }
3221 
3222     if (!want_zero) {
3223         *pnum = bytes;
3224         *map = offset;
3225         *file = bs;
3226         return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID;
3227     }
3228 
3229     ret = find_allocation(bs, offset, &data, &hole);
3230     if (ret == -ENXIO) {
3231         /* Trailing hole */
3232         *pnum = bytes;
3233         ret = BDRV_BLOCK_ZERO;
3234     } else if (ret < 0) {
3235         /* No info available, so pretend there are no holes */
3236         *pnum = bytes;
3237         ret = BDRV_BLOCK_DATA;
3238     } else if (data == offset) {
3239         /* On a data extent, compute bytes to the end of the extent,
3240          * possibly including a partial sector at EOF. */
3241         *pnum = hole - offset;
3242 
3243         /*
3244          * We are not allowed to return partial sectors, though, so
3245          * round up if necessary.
3246          */
3247         if (!QEMU_IS_ALIGNED(*pnum, bs->bl.request_alignment)) {
3248             int64_t file_length = raw_co_getlength(bs);
3249             if (file_length > 0) {
3250                 /* Ignore errors, this is just a safeguard */
3251                 assert(hole == file_length);
3252             }
3253             *pnum = ROUND_UP(*pnum, bs->bl.request_alignment);
3254         }
3255 
3256         ret = BDRV_BLOCK_DATA;
3257     } else {
3258         /* On a hole, compute bytes to the beginning of the next extent.  */
3259         assert(hole == offset);
3260         *pnum = data - offset;
3261         ret = BDRV_BLOCK_ZERO;
3262     }
3263     *map = offset;
3264     *file = bs;
3265     return ret | BDRV_BLOCK_OFFSET_VALID;
3266 }
3267 
3268 #if defined(__linux__)
3269 /* Verify that the file is not in the page cache */
3270 static void coroutine_fn check_cache_dropped(BlockDriverState *bs, Error **errp)
3271 {
3272     const size_t window_size = 128 * 1024 * 1024;
3273     BDRVRawState *s = bs->opaque;
3274     void *window = NULL;
3275     size_t length = 0;
3276     unsigned char *vec;
3277     size_t page_size;
3278     off_t offset;
3279     off_t end;
3280 
3281     /* mincore(2) page status information requires 1 byte per page */
3282     page_size = sysconf(_SC_PAGESIZE);
3283     vec = g_malloc(DIV_ROUND_UP(window_size, page_size));
3284 
3285     end = raw_co_getlength(bs);
3286 
3287     for (offset = 0; offset < end; offset += window_size) {
3288         void *new_window;
3289         size_t new_length;
3290         size_t vec_end;
3291         size_t i;
3292         int ret;
3293 
3294         /* Unmap previous window if size has changed */
3295         new_length = MIN(end - offset, window_size);
3296         if (new_length != length) {
3297             munmap(window, length);
3298             window = NULL;
3299             length = 0;
3300         }
3301 
3302         new_window = mmap(window, new_length, PROT_NONE, MAP_PRIVATE,
3303                           s->fd, offset);
3304         if (new_window == MAP_FAILED) {
3305             error_setg_errno(errp, errno, "mmap failed");
3306             break;
3307         }
3308 
3309         window = new_window;
3310         length = new_length;
3311 
3312         ret = mincore(window, length, vec);
3313         if (ret < 0) {
3314             error_setg_errno(errp, errno, "mincore failed");
3315             break;
3316         }
3317 
3318         vec_end = DIV_ROUND_UP(length, page_size);
3319         for (i = 0; i < vec_end; i++) {
3320             if (vec[i] & 0x1) {
3321                 break;
3322             }
3323         }
3324         if (i < vec_end) {
3325             error_setg(errp, "page cache still in use!");
3326             break;
3327         }
3328     }
3329 
3330     if (window) {
3331         munmap(window, length);
3332     }
3333 
3334     g_free(vec);
3335 }
3336 #endif /* __linux__ */
3337 
3338 static void coroutine_fn GRAPH_RDLOCK
3339 raw_co_invalidate_cache(BlockDriverState *bs, Error **errp)
3340 {
3341     BDRVRawState *s = bs->opaque;
3342     int ret;
3343 
3344     ret = fd_open(bs);
3345     if (ret < 0) {
3346         error_setg_errno(errp, -ret, "The file descriptor is not open");
3347         return;
3348     }
3349 
3350     if (!s->drop_cache) {
3351         return;
3352     }
3353 
3354     if (s->open_flags & O_DIRECT) {
3355         return; /* No host kernel page cache */
3356     }
3357 
3358 #if defined(__linux__)
3359     /* This sets the scene for the next syscall... */
3360     ret = bdrv_co_flush(bs);
3361     if (ret < 0) {
3362         error_setg_errno(errp, -ret, "flush failed");
3363         return;
3364     }
3365 
3366     /* Linux does not invalidate pages that are dirty, locked, or mmapped by a
3367      * process.  These limitations are okay because we just fsynced the file,
3368      * we don't use mmap, and the file should not be in use by other processes.
3369      */
3370     ret = posix_fadvise(s->fd, 0, 0, POSIX_FADV_DONTNEED);
3371     if (ret != 0) { /* the return value is a positive errno */
3372         error_setg_errno(errp, ret, "fadvise failed");
3373         return;
3374     }
3375 
3376     if (s->check_cache_dropped) {
3377         check_cache_dropped(bs, errp);
3378     }
3379 #else /* __linux__ */
3380     /* Do nothing.  Live migration to a remote host with cache.direct=off is
3381      * unsupported on other host operating systems.  Cache consistency issues
3382      * may occur but no error is reported here, partly because that's the
3383      * historical behavior and partly because it's hard to differentiate valid
3384      * configurations that should not cause errors.
3385      */
3386 #endif /* !__linux__ */
3387 }
3388 
3389 static void raw_account_discard(BDRVRawState *s, uint64_t nbytes, int ret)
3390 {
3391     if (ret) {
3392         s->stats.discard_nb_failed++;
3393     } else {
3394         s->stats.discard_nb_ok++;
3395         s->stats.discard_bytes_ok += nbytes;
3396     }
3397 }
3398 
3399 /*
3400  * zone report - Get a zone block device's information in the form
3401  * of an array of zone descriptors.
3402  * zones is an array of zone descriptors to hold zone information on reply;
3403  * offset can be any byte within the entire size of the device;
3404  * nr_zones is the maxium number of sectors the command should operate on.
3405  */
3406 #if defined(CONFIG_BLKZONED)
3407 static int coroutine_fn raw_co_zone_report(BlockDriverState *bs, int64_t offset,
3408                                            unsigned int *nr_zones,
3409                                            BlockZoneDescriptor *zones) {
3410     BDRVRawState *s = bs->opaque;
3411     RawPosixAIOData acb = (RawPosixAIOData) {
3412         .bs         = bs,
3413         .aio_fildes = s->fd,
3414         .aio_type   = QEMU_AIO_ZONE_REPORT,
3415         .aio_offset = offset,
3416         .zone_report    = {
3417             .nr_zones       = nr_zones,
3418             .zones          = zones,
3419         },
3420     };
3421 
3422     trace_zbd_zone_report(bs, *nr_zones, offset >> BDRV_SECTOR_BITS);
3423     return raw_thread_pool_submit(handle_aiocb_zone_report, &acb);
3424 }
3425 #endif
3426 
3427 /*
3428  * zone management operations - Execute an operation on a zone
3429  */
3430 #if defined(CONFIG_BLKZONED)
3431 static int coroutine_fn raw_co_zone_mgmt(BlockDriverState *bs, BlockZoneOp op,
3432         int64_t offset, int64_t len) {
3433     BDRVRawState *s = bs->opaque;
3434     RawPosixAIOData acb;
3435     int64_t zone_size, zone_size_mask;
3436     const char *op_name;
3437     unsigned long zo;
3438     int ret;
3439     BlockZoneWps *wps = bs->wps;
3440     int64_t capacity = bs->total_sectors << BDRV_SECTOR_BITS;
3441 
3442     zone_size = bs->bl.zone_size;
3443     zone_size_mask = zone_size - 1;
3444     if (offset & zone_size_mask) {
3445         error_report("sector offset %" PRId64 " is not aligned to zone size "
3446                      "%" PRId64 "", offset / 512, zone_size / 512);
3447         return -EINVAL;
3448     }
3449 
3450     if (((offset + len) < capacity && len & zone_size_mask) ||
3451         offset + len > capacity) {
3452         error_report("number of sectors %" PRId64 " is not aligned to zone size"
3453                       " %" PRId64 "", len / 512, zone_size / 512);
3454         return -EINVAL;
3455     }
3456 
3457     uint32_t i = offset / bs->bl.zone_size;
3458     uint32_t nrz = len / bs->bl.zone_size;
3459     uint64_t *wp = &wps->wp[i];
3460     if (BDRV_ZT_IS_CONV(*wp) && len != capacity) {
3461         error_report("zone mgmt operations are not allowed for conventional zones");
3462         return -EIO;
3463     }
3464 
3465     switch (op) {
3466     case BLK_ZO_OPEN:
3467         op_name = "BLKOPENZONE";
3468         zo = BLKOPENZONE;
3469         break;
3470     case BLK_ZO_CLOSE:
3471         op_name = "BLKCLOSEZONE";
3472         zo = BLKCLOSEZONE;
3473         break;
3474     case BLK_ZO_FINISH:
3475         op_name = "BLKFINISHZONE";
3476         zo = BLKFINISHZONE;
3477         break;
3478     case BLK_ZO_RESET:
3479         op_name = "BLKRESETZONE";
3480         zo = BLKRESETZONE;
3481         break;
3482     default:
3483         error_report("Unsupported zone op: 0x%x", op);
3484         return -ENOTSUP;
3485     }
3486 
3487     acb = (RawPosixAIOData) {
3488         .bs             = bs,
3489         .aio_fildes     = s->fd,
3490         .aio_type       = QEMU_AIO_ZONE_MGMT,
3491         .aio_offset     = offset,
3492         .aio_nbytes     = len,
3493         .zone_mgmt  = {
3494             .op = zo,
3495         },
3496     };
3497 
3498     trace_zbd_zone_mgmt(bs, op_name, offset >> BDRV_SECTOR_BITS,
3499                         len >> BDRV_SECTOR_BITS);
3500     ret = raw_thread_pool_submit(handle_aiocb_zone_mgmt, &acb);
3501     if (ret != 0) {
3502         update_zones_wp(bs, s->fd, offset, i);
3503         error_report("ioctl %s failed %d", op_name, ret);
3504         return ret;
3505     }
3506 
3507     if (zo == BLKRESETZONE && len == capacity) {
3508         ret = get_zones_wp(bs, s->fd, 0, bs->bl.nr_zones, 1);
3509         if (ret < 0) {
3510             error_report("reporting single wp failed");
3511             return ret;
3512         }
3513     } else if (zo == BLKRESETZONE) {
3514         for (unsigned int j = 0; j < nrz; ++j) {
3515             wp[j] = offset + j * zone_size;
3516         }
3517     } else if (zo == BLKFINISHZONE) {
3518         for (unsigned int j = 0; j < nrz; ++j) {
3519             /* The zoned device allows the last zone smaller that the
3520              * zone size. */
3521             wp[j] = MIN(offset + (j + 1) * zone_size, offset + len);
3522         }
3523     }
3524 
3525     return ret;
3526 }
3527 #endif
3528 
3529 #if defined(CONFIG_BLKZONED)
3530 static int coroutine_fn raw_co_zone_append(BlockDriverState *bs,
3531                                            int64_t *offset,
3532                                            QEMUIOVector *qiov,
3533                                            BdrvRequestFlags flags) {
3534     assert(flags == 0);
3535     int64_t zone_size_mask = bs->bl.zone_size - 1;
3536     int64_t iov_len = 0;
3537     int64_t len = 0;
3538     BDRVRawState *s = bs->opaque;
3539     s->offset = offset;
3540 
3541     if (*offset & zone_size_mask) {
3542         error_report("sector offset %" PRId64 " is not aligned to zone size "
3543                      "%" PRId32 "", *offset / 512, bs->bl.zone_size / 512);
3544         return -EINVAL;
3545     }
3546 
3547     int64_t wg = bs->bl.write_granularity;
3548     int64_t wg_mask = wg - 1;
3549     for (int i = 0; i < qiov->niov; i++) {
3550         iov_len = qiov->iov[i].iov_len;
3551         if (iov_len & wg_mask) {
3552             error_report("len of IOVector[%d] %" PRId64 " is not aligned to "
3553                          "block size %" PRId64 "", i, iov_len, wg);
3554             return -EINVAL;
3555         }
3556         len += iov_len;
3557     }
3558 
3559     trace_zbd_zone_append(bs, *offset >> BDRV_SECTOR_BITS);
3560     return raw_co_prw(bs, *offset, len, qiov, QEMU_AIO_ZONE_APPEND);
3561 }
3562 #endif
3563 
3564 static coroutine_fn int
3565 raw_do_pdiscard(BlockDriverState *bs, int64_t offset, int64_t bytes,
3566                 bool blkdev)
3567 {
3568     BDRVRawState *s = bs->opaque;
3569     RawPosixAIOData acb;
3570     int ret;
3571 
3572     acb = (RawPosixAIOData) {
3573         .bs             = bs,
3574         .aio_fildes     = s->fd,
3575         .aio_type       = QEMU_AIO_DISCARD,
3576         .aio_offset     = offset,
3577         .aio_nbytes     = bytes,
3578     };
3579 
3580     if (blkdev) {
3581         acb.aio_type |= QEMU_AIO_BLKDEV;
3582     }
3583 
3584     ret = raw_thread_pool_submit(handle_aiocb_discard, &acb);
3585     raw_account_discard(s, bytes, ret);
3586     return ret;
3587 }
3588 
3589 static coroutine_fn int
3590 raw_co_pdiscard(BlockDriverState *bs, int64_t offset, int64_t bytes)
3591 {
3592     return raw_do_pdiscard(bs, offset, bytes, false);
3593 }
3594 
3595 static int coroutine_fn
3596 raw_do_pwrite_zeroes(BlockDriverState *bs, int64_t offset, int64_t bytes,
3597                      BdrvRequestFlags flags, bool blkdev)
3598 {
3599     BDRVRawState *s = bs->opaque;
3600     RawPosixAIOData acb;
3601     ThreadPoolFunc *handler;
3602 
3603 #ifdef CONFIG_FALLOCATE
3604     if (offset + bytes > bs->total_sectors * BDRV_SECTOR_SIZE) {
3605         BdrvTrackedRequest *req;
3606 
3607         /*
3608          * This is a workaround for a bug in the Linux XFS driver,
3609          * where writes submitted through the AIO interface will be
3610          * discarded if they happen beyond a concurrently running
3611          * fallocate() that increases the file length (i.e., both the
3612          * write and the fallocate() happen beyond the EOF).
3613          *
3614          * To work around it, we extend the tracked request for this
3615          * zero write until INT64_MAX (effectively infinity), and mark
3616          * it as serializing.
3617          *
3618          * We have to enable this workaround for all filesystems and
3619          * AIO modes (not just XFS with aio=native), because for
3620          * remote filesystems we do not know the host configuration.
3621          */
3622 
3623         req = bdrv_co_get_self_request(bs);
3624         assert(req);
3625         assert(req->type == BDRV_TRACKED_WRITE);
3626         assert(req->offset <= offset);
3627         assert(req->offset + req->bytes >= offset + bytes);
3628 
3629         req->bytes = BDRV_MAX_LENGTH - req->offset;
3630 
3631         bdrv_check_request(req->offset, req->bytes, &error_abort);
3632 
3633         bdrv_make_request_serialising(req, bs->bl.request_alignment);
3634     }
3635 #endif
3636 
3637     acb = (RawPosixAIOData) {
3638         .bs             = bs,
3639         .aio_fildes     = s->fd,
3640         .aio_type       = QEMU_AIO_WRITE_ZEROES,
3641         .aio_offset     = offset,
3642         .aio_nbytes     = bytes,
3643     };
3644 
3645     if (blkdev) {
3646         acb.aio_type |= QEMU_AIO_BLKDEV;
3647     }
3648     if (flags & BDRV_REQ_NO_FALLBACK) {
3649         acb.aio_type |= QEMU_AIO_NO_FALLBACK;
3650     }
3651 
3652     if (flags & BDRV_REQ_MAY_UNMAP) {
3653         acb.aio_type |= QEMU_AIO_DISCARD;
3654         handler = handle_aiocb_write_zeroes_unmap;
3655     } else {
3656         handler = handle_aiocb_write_zeroes;
3657     }
3658 
3659     return raw_thread_pool_submit(handler, &acb);
3660 }
3661 
3662 static int coroutine_fn raw_co_pwrite_zeroes(
3663     BlockDriverState *bs, int64_t offset,
3664     int64_t bytes, BdrvRequestFlags flags)
3665 {
3666     return raw_do_pwrite_zeroes(bs, offset, bytes, flags, false);
3667 }
3668 
3669 static int coroutine_fn
3670 raw_co_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
3671 {
3672     return 0;
3673 }
3674 
3675 static ImageInfoSpecific *raw_get_specific_info(BlockDriverState *bs,
3676                                                 Error **errp)
3677 {
3678     ImageInfoSpecificFile *file_info = g_new0(ImageInfoSpecificFile, 1);
3679     ImageInfoSpecific *spec_info = g_new(ImageInfoSpecific, 1);
3680 
3681     *spec_info = (ImageInfoSpecific){
3682         .type = IMAGE_INFO_SPECIFIC_KIND_FILE,
3683         .u.file.data = file_info,
3684     };
3685 
3686 #ifdef FS_IOC_FSGETXATTR
3687     {
3688         BDRVRawState *s = bs->opaque;
3689         struct fsxattr attr;
3690         int ret;
3691 
3692         ret = ioctl(s->fd, FS_IOC_FSGETXATTR, &attr);
3693         if (!ret && attr.fsx_extsize != 0) {
3694             file_info->has_extent_size_hint = true;
3695             file_info->extent_size_hint = attr.fsx_extsize;
3696         }
3697     }
3698 #endif
3699 
3700     return spec_info;
3701 }
3702 
3703 static BlockStatsSpecificFile get_blockstats_specific_file(BlockDriverState *bs)
3704 {
3705     BDRVRawState *s = bs->opaque;
3706     return (BlockStatsSpecificFile) {
3707         .discard_nb_ok = s->stats.discard_nb_ok,
3708         .discard_nb_failed = s->stats.discard_nb_failed,
3709         .discard_bytes_ok = s->stats.discard_bytes_ok,
3710     };
3711 }
3712 
3713 static BlockStatsSpecific *raw_get_specific_stats(BlockDriverState *bs)
3714 {
3715     BlockStatsSpecific *stats = g_new(BlockStatsSpecific, 1);
3716 
3717     stats->driver = BLOCKDEV_DRIVER_FILE;
3718     stats->u.file = get_blockstats_specific_file(bs);
3719 
3720     return stats;
3721 }
3722 
3723 #if defined(HAVE_HOST_BLOCK_DEVICE)
3724 static BlockStatsSpecific *hdev_get_specific_stats(BlockDriverState *bs)
3725 {
3726     BlockStatsSpecific *stats = g_new(BlockStatsSpecific, 1);
3727 
3728     stats->driver = BLOCKDEV_DRIVER_HOST_DEVICE;
3729     stats->u.host_device = get_blockstats_specific_file(bs);
3730 
3731     return stats;
3732 }
3733 #endif /* HAVE_HOST_BLOCK_DEVICE */
3734 
3735 static QemuOptsList raw_create_opts = {
3736     .name = "raw-create-opts",
3737     .head = QTAILQ_HEAD_INITIALIZER(raw_create_opts.head),
3738     .desc = {
3739         {
3740             .name = BLOCK_OPT_SIZE,
3741             .type = QEMU_OPT_SIZE,
3742             .help = "Virtual disk size"
3743         },
3744         {
3745             .name = BLOCK_OPT_NOCOW,
3746             .type = QEMU_OPT_BOOL,
3747             .help = "Turn off copy-on-write (valid only on btrfs)"
3748         },
3749         {
3750             .name = BLOCK_OPT_PREALLOC,
3751             .type = QEMU_OPT_STRING,
3752             .help = "Preallocation mode (allowed values: off"
3753 #ifdef CONFIG_POSIX_FALLOCATE
3754                     ", falloc"
3755 #endif
3756                     ", full)"
3757         },
3758         {
3759             .name = BLOCK_OPT_EXTENT_SIZE_HINT,
3760             .type = QEMU_OPT_SIZE,
3761             .help = "Extent size hint for the image file, 0 to disable"
3762         },
3763         { /* end of list */ }
3764     }
3765 };
3766 
3767 static int raw_check_perm(BlockDriverState *bs, uint64_t perm, uint64_t shared,
3768                           Error **errp)
3769 {
3770     BDRVRawState *s = bs->opaque;
3771     int input_flags = s->reopen_state ? s->reopen_state->flags : bs->open_flags;
3772     int open_flags;
3773     int ret;
3774 
3775     /* We may need a new fd if auto-read-only switches the mode */
3776     ret = raw_reconfigure_getfd(bs, input_flags, &open_flags, perm,
3777                                 false, errp);
3778     if (ret < 0) {
3779         return ret;
3780     } else if (ret != s->fd) {
3781         Error *local_err = NULL;
3782 
3783         /*
3784          * Fail already check_perm() if we can't get a working O_DIRECT
3785          * alignment with the new fd.
3786          */
3787         raw_probe_alignment(bs, ret, &local_err);
3788         if (local_err) {
3789             error_propagate(errp, local_err);
3790             return -EINVAL;
3791         }
3792 
3793         s->perm_change_fd = ret;
3794         s->perm_change_flags = open_flags;
3795     }
3796 
3797     /* Prepare permissions on old fd to avoid conflicts between old and new,
3798      * but keep everything locked that new will need. */
3799     ret = raw_handle_perm_lock(bs, RAW_PL_PREPARE, perm, shared, errp);
3800     if (ret < 0) {
3801         goto fail;
3802     }
3803 
3804     /* Copy locks to the new fd */
3805     if (s->perm_change_fd && s->use_lock) {
3806         ret = raw_apply_lock_bytes(NULL, s->perm_change_fd, perm, ~shared,
3807                                    false, errp);
3808         if (ret < 0) {
3809             raw_handle_perm_lock(bs, RAW_PL_ABORT, 0, 0, NULL);
3810             goto fail;
3811         }
3812     }
3813     return 0;
3814 
3815 fail:
3816     if (s->perm_change_fd) {
3817         qemu_close(s->perm_change_fd);
3818     }
3819     s->perm_change_fd = 0;
3820     return ret;
3821 }
3822 
3823 static void raw_set_perm(BlockDriverState *bs, uint64_t perm, uint64_t shared)
3824 {
3825     BDRVRawState *s = bs->opaque;
3826 
3827     /* For reopen, we have already switched to the new fd (.bdrv_set_perm is
3828      * called after .bdrv_reopen_commit) */
3829     if (s->perm_change_fd && s->fd != s->perm_change_fd) {
3830         qemu_close(s->fd);
3831         s->fd = s->perm_change_fd;
3832         s->open_flags = s->perm_change_flags;
3833     }
3834     s->perm_change_fd = 0;
3835 
3836     raw_handle_perm_lock(bs, RAW_PL_COMMIT, perm, shared, NULL);
3837     s->perm = perm;
3838     s->shared_perm = shared;
3839 }
3840 
3841 static void raw_abort_perm_update(BlockDriverState *bs)
3842 {
3843     BDRVRawState *s = bs->opaque;
3844 
3845     /* For reopen, .bdrv_reopen_abort is called afterwards and will close
3846      * the file descriptor. */
3847     if (s->perm_change_fd) {
3848         qemu_close(s->perm_change_fd);
3849     }
3850     s->perm_change_fd = 0;
3851 
3852     raw_handle_perm_lock(bs, RAW_PL_ABORT, 0, 0, NULL);
3853 }
3854 
3855 static int coroutine_fn GRAPH_RDLOCK raw_co_copy_range_from(
3856         BlockDriverState *bs, BdrvChild *src, int64_t src_offset,
3857         BdrvChild *dst, int64_t dst_offset, int64_t bytes,
3858         BdrvRequestFlags read_flags, BdrvRequestFlags write_flags)
3859 {
3860     return bdrv_co_copy_range_to(src, src_offset, dst, dst_offset, bytes,
3861                                  read_flags, write_flags);
3862 }
3863 
3864 static int coroutine_fn GRAPH_RDLOCK
3865 raw_co_copy_range_to(BlockDriverState *bs,
3866                      BdrvChild *src, int64_t src_offset,
3867                      BdrvChild *dst, int64_t dst_offset,
3868                      int64_t bytes, BdrvRequestFlags read_flags,
3869                      BdrvRequestFlags write_flags)
3870 {
3871     RawPosixAIOData acb;
3872     BDRVRawState *s = bs->opaque;
3873     BDRVRawState *src_s;
3874 
3875     assert(dst->bs == bs);
3876     if (src->bs->drv->bdrv_co_copy_range_to != raw_co_copy_range_to) {
3877         return -ENOTSUP;
3878     }
3879 
3880     src_s = src->bs->opaque;
3881     if (fd_open(src->bs) < 0 || fd_open(dst->bs) < 0) {
3882         return -EIO;
3883     }
3884 
3885     acb = (RawPosixAIOData) {
3886         .bs             = bs,
3887         .aio_type       = QEMU_AIO_COPY_RANGE,
3888         .aio_fildes     = src_s->fd,
3889         .aio_offset     = src_offset,
3890         .aio_nbytes     = bytes,
3891         .copy_range     = {
3892             .aio_fd2        = s->fd,
3893             .aio_offset2    = dst_offset,
3894         },
3895     };
3896 
3897     return raw_thread_pool_submit(handle_aiocb_copy_range, &acb);
3898 }
3899 
3900 BlockDriver bdrv_file = {
3901     .format_name = "file",
3902     .protocol_name = "file",
3903     .instance_size = sizeof(BDRVRawState),
3904     .bdrv_needs_filename = true,
3905     .bdrv_probe = NULL, /* no probe for protocols */
3906     .bdrv_parse_filename = raw_parse_filename,
3907     .bdrv_file_open = raw_open,
3908     .bdrv_reopen_prepare = raw_reopen_prepare,
3909     .bdrv_reopen_commit = raw_reopen_commit,
3910     .bdrv_reopen_abort = raw_reopen_abort,
3911     .bdrv_close = raw_close,
3912     .bdrv_co_create = raw_co_create,
3913     .bdrv_co_create_opts = raw_co_create_opts,
3914     .bdrv_has_zero_init = bdrv_has_zero_init_1,
3915     .bdrv_co_block_status = raw_co_block_status,
3916     .bdrv_co_invalidate_cache = raw_co_invalidate_cache,
3917     .bdrv_co_pwrite_zeroes = raw_co_pwrite_zeroes,
3918     .bdrv_co_delete_file = raw_co_delete_file,
3919 
3920     .bdrv_co_preadv         = raw_co_preadv,
3921     .bdrv_co_pwritev        = raw_co_pwritev,
3922     .bdrv_co_flush_to_disk  = raw_co_flush_to_disk,
3923     .bdrv_co_pdiscard       = raw_co_pdiscard,
3924     .bdrv_co_copy_range_from = raw_co_copy_range_from,
3925     .bdrv_co_copy_range_to  = raw_co_copy_range_to,
3926     .bdrv_refresh_limits = raw_refresh_limits,
3927     .bdrv_co_io_plug        = raw_co_io_plug,
3928     .bdrv_co_io_unplug      = raw_co_io_unplug,
3929     .bdrv_attach_aio_context = raw_aio_attach_aio_context,
3930 
3931     .bdrv_co_truncate                   = raw_co_truncate,
3932     .bdrv_co_getlength                  = raw_co_getlength,
3933     .bdrv_co_get_info                   = raw_co_get_info,
3934     .bdrv_get_specific_info             = raw_get_specific_info,
3935     .bdrv_co_get_allocated_file_size    = raw_co_get_allocated_file_size,
3936     .bdrv_get_specific_stats = raw_get_specific_stats,
3937     .bdrv_check_perm = raw_check_perm,
3938     .bdrv_set_perm   = raw_set_perm,
3939     .bdrv_abort_perm_update = raw_abort_perm_update,
3940     .create_opts = &raw_create_opts,
3941     .mutable_opts = mutable_opts,
3942 };
3943 
3944 /***********************************************/
3945 /* host device */
3946 
3947 #if defined(HAVE_HOST_BLOCK_DEVICE)
3948 
3949 #if defined(__APPLE__) && defined(__MACH__)
3950 static kern_return_t GetBSDPath(io_iterator_t mediaIterator, char *bsdPath,
3951                                 CFIndex maxPathSize, int flags);
3952 
3953 #if !defined(MAC_OS_VERSION_12_0) \
3954     || (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_VERSION_12_0)
3955 #define IOMainPort IOMasterPort
3956 #endif
3957 
3958 static char *FindEjectableOpticalMedia(io_iterator_t *mediaIterator)
3959 {
3960     kern_return_t kernResult = KERN_FAILURE;
3961     mach_port_t mainPort;
3962     CFMutableDictionaryRef  classesToMatch;
3963     const char *matching_array[] = {kIODVDMediaClass, kIOCDMediaClass};
3964     char *mediaType = NULL;
3965 
3966     kernResult = IOMainPort(MACH_PORT_NULL, &mainPort);
3967     if ( KERN_SUCCESS != kernResult ) {
3968         printf("IOMainPort returned %d\n", kernResult);
3969     }
3970 
3971     int index;
3972     for (index = 0; index < ARRAY_SIZE(matching_array); index++) {
3973         classesToMatch = IOServiceMatching(matching_array[index]);
3974         if (classesToMatch == NULL) {
3975             error_report("IOServiceMatching returned NULL for %s",
3976                          matching_array[index]);
3977             continue;
3978         }
3979         CFDictionarySetValue(classesToMatch, CFSTR(kIOMediaEjectableKey),
3980                              kCFBooleanTrue);
3981         kernResult = IOServiceGetMatchingServices(mainPort, classesToMatch,
3982                                                   mediaIterator);
3983         if (kernResult != KERN_SUCCESS) {
3984             error_report("Note: IOServiceGetMatchingServices returned %d",
3985                          kernResult);
3986             continue;
3987         }
3988 
3989         /* If a match was found, leave the loop */
3990         if (*mediaIterator != 0) {
3991             trace_file_FindEjectableOpticalMedia(matching_array[index]);
3992             mediaType = g_strdup(matching_array[index]);
3993             break;
3994         }
3995     }
3996     return mediaType;
3997 }
3998 
3999 kern_return_t GetBSDPath(io_iterator_t mediaIterator, char *bsdPath,
4000                          CFIndex maxPathSize, int flags)
4001 {
4002     io_object_t     nextMedia;
4003     kern_return_t   kernResult = KERN_FAILURE;
4004     *bsdPath = '\0';
4005     nextMedia = IOIteratorNext( mediaIterator );
4006     if ( nextMedia )
4007     {
4008         CFTypeRef   bsdPathAsCFString;
4009     bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
4010         if ( bsdPathAsCFString ) {
4011             size_t devPathLength;
4012             strcpy( bsdPath, _PATH_DEV );
4013             if (flags & BDRV_O_NOCACHE) {
4014                 strcat(bsdPath, "r");
4015             }
4016             devPathLength = strlen( bsdPath );
4017             if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
4018                 kernResult = KERN_SUCCESS;
4019             }
4020             CFRelease( bsdPathAsCFString );
4021         }
4022         IOObjectRelease( nextMedia );
4023     }
4024 
4025     return kernResult;
4026 }
4027 
4028 /* Sets up a real cdrom for use in QEMU */
4029 static bool setup_cdrom(char *bsd_path, Error **errp)
4030 {
4031     int index, num_of_test_partitions = 2, fd;
4032     char test_partition[MAXPATHLEN];
4033     bool partition_found = false;
4034 
4035     /* look for a working partition */
4036     for (index = 0; index < num_of_test_partitions; index++) {
4037         snprintf(test_partition, sizeof(test_partition), "%ss%d", bsd_path,
4038                  index);
4039         fd = qemu_open(test_partition, O_RDONLY | O_BINARY | O_LARGEFILE, NULL);
4040         if (fd >= 0) {
4041             partition_found = true;
4042             qemu_close(fd);
4043             break;
4044         }
4045     }
4046 
4047     /* if a working partition on the device was not found */
4048     if (partition_found == false) {
4049         error_setg(errp, "Failed to find a working partition on disc");
4050     } else {
4051         trace_file_setup_cdrom(test_partition);
4052         pstrcpy(bsd_path, MAXPATHLEN, test_partition);
4053     }
4054     return partition_found;
4055 }
4056 
4057 /* Prints directions on mounting and unmounting a device */
4058 static void print_unmounting_directions(const char *file_name)
4059 {
4060     error_report("If device %s is mounted on the desktop, unmount"
4061                  " it first before using it in QEMU", file_name);
4062     error_report("Command to unmount device: diskutil unmountDisk %s",
4063                  file_name);
4064     error_report("Command to mount device: diskutil mountDisk %s", file_name);
4065 }
4066 
4067 #endif /* defined(__APPLE__) && defined(__MACH__) */
4068 
4069 static int hdev_probe_device(const char *filename)
4070 {
4071     struct stat st;
4072 
4073     /* allow a dedicated CD-ROM driver to match with a higher priority */
4074     if (strstart(filename, "/dev/cdrom", NULL))
4075         return 50;
4076 
4077     if (stat(filename, &st) >= 0 &&
4078             (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
4079         return 100;
4080     }
4081 
4082     return 0;
4083 }
4084 
4085 static void hdev_parse_filename(const char *filename, QDict *options,
4086                                 Error **errp)
4087 {
4088     bdrv_parse_filename_strip_prefix(filename, "host_device:", options);
4089 }
4090 
4091 static bool hdev_is_sg(BlockDriverState *bs)
4092 {
4093 
4094 #if defined(__linux__)
4095 
4096     BDRVRawState *s = bs->opaque;
4097     struct stat st;
4098     struct sg_scsi_id scsiid;
4099     int sg_version;
4100     int ret;
4101 
4102     if (stat(bs->filename, &st) < 0 || !S_ISCHR(st.st_mode)) {
4103         return false;
4104     }
4105 
4106     ret = ioctl(s->fd, SG_GET_VERSION_NUM, &sg_version);
4107     if (ret < 0) {
4108         return false;
4109     }
4110 
4111     ret = ioctl(s->fd, SG_GET_SCSI_ID, &scsiid);
4112     if (ret >= 0) {
4113         trace_file_hdev_is_sg(scsiid.scsi_type, sg_version);
4114         return true;
4115     }
4116 
4117 #endif
4118 
4119     return false;
4120 }
4121 
4122 static int hdev_open(BlockDriverState *bs, QDict *options, int flags,
4123                      Error **errp)
4124 {
4125     BDRVRawState *s = bs->opaque;
4126     int ret;
4127 
4128 #if defined(__APPLE__) && defined(__MACH__)
4129     /*
4130      * Caution: while qdict_get_str() is fine, getting non-string types
4131      * would require more care.  When @options come from -blockdev or
4132      * blockdev_add, its members are typed according to the QAPI
4133      * schema, but when they come from -drive, they're all QString.
4134      */
4135     const char *filename = qdict_get_str(options, "filename");
4136     char bsd_path[MAXPATHLEN] = "";
4137     bool error_occurred = false;
4138 
4139     /* If using a real cdrom */
4140     if (strcmp(filename, "/dev/cdrom") == 0) {
4141         char *mediaType = NULL;
4142         kern_return_t ret_val;
4143         io_iterator_t mediaIterator = 0;
4144 
4145         mediaType = FindEjectableOpticalMedia(&mediaIterator);
4146         if (mediaType == NULL) {
4147             error_setg(errp, "Please make sure your CD/DVD is in the optical"
4148                        " drive");
4149             error_occurred = true;
4150             goto hdev_open_Mac_error;
4151         }
4152 
4153         ret_val = GetBSDPath(mediaIterator, bsd_path, sizeof(bsd_path), flags);
4154         if (ret_val != KERN_SUCCESS) {
4155             error_setg(errp, "Could not get BSD path for optical drive");
4156             error_occurred = true;
4157             goto hdev_open_Mac_error;
4158         }
4159 
4160         /* If a real optical drive was not found */
4161         if (bsd_path[0] == '\0') {
4162             error_setg(errp, "Failed to obtain bsd path for optical drive");
4163             error_occurred = true;
4164             goto hdev_open_Mac_error;
4165         }
4166 
4167         /* If using a cdrom disc and finding a partition on the disc failed */
4168         if (strncmp(mediaType, kIOCDMediaClass, 9) == 0 &&
4169             setup_cdrom(bsd_path, errp) == false) {
4170             print_unmounting_directions(bsd_path);
4171             error_occurred = true;
4172             goto hdev_open_Mac_error;
4173         }
4174 
4175         qdict_put_str(options, "filename", bsd_path);
4176 
4177 hdev_open_Mac_error:
4178         g_free(mediaType);
4179         if (mediaIterator) {
4180             IOObjectRelease(mediaIterator);
4181         }
4182         if (error_occurred) {
4183             return -ENOENT;
4184         }
4185     }
4186 #endif /* defined(__APPLE__) && defined(__MACH__) */
4187 
4188     s->type = FTYPE_FILE;
4189 
4190     ret = raw_open_common(bs, options, flags, 0, true, errp);
4191     if (ret < 0) {
4192 #if defined(__APPLE__) && defined(__MACH__)
4193         if (*bsd_path) {
4194             filename = bsd_path;
4195         }
4196         /* if a physical device experienced an error while being opened */
4197         if (strncmp(filename, "/dev/", 5) == 0) {
4198             print_unmounting_directions(filename);
4199         }
4200 #endif /* defined(__APPLE__) && defined(__MACH__) */
4201         return ret;
4202     }
4203 
4204     /* Since this does ioctl the device must be already opened */
4205     bs->sg = hdev_is_sg(bs);
4206 
4207     return ret;
4208 }
4209 
4210 #if defined(__linux__)
4211 static int coroutine_fn
4212 hdev_co_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
4213 {
4214     BDRVRawState *s = bs->opaque;
4215     RawPosixAIOData acb;
4216     int ret;
4217 
4218     ret = fd_open(bs);
4219     if (ret < 0) {
4220         return ret;
4221     }
4222 
4223     if (req == SG_IO && s->pr_mgr) {
4224         struct sg_io_hdr *io_hdr = buf;
4225         if (io_hdr->cmdp[0] == PERSISTENT_RESERVE_OUT ||
4226             io_hdr->cmdp[0] == PERSISTENT_RESERVE_IN) {
4227             return pr_manager_execute(s->pr_mgr, qemu_get_current_aio_context(),
4228                                       s->fd, io_hdr);
4229         }
4230     }
4231 
4232     acb = (RawPosixAIOData) {
4233         .bs         = bs,
4234         .aio_type   = QEMU_AIO_IOCTL,
4235         .aio_fildes = s->fd,
4236         .aio_offset = 0,
4237         .ioctl      = {
4238             .buf        = buf,
4239             .cmd        = req,
4240         },
4241     };
4242 
4243     return raw_thread_pool_submit(handle_aiocb_ioctl, &acb);
4244 }
4245 #endif /* linux */
4246 
4247 static coroutine_fn int
4248 hdev_co_pdiscard(BlockDriverState *bs, int64_t offset, int64_t bytes)
4249 {
4250     BDRVRawState *s = bs->opaque;
4251     int ret;
4252 
4253     ret = fd_open(bs);
4254     if (ret < 0) {
4255         raw_account_discard(s, bytes, ret);
4256         return ret;
4257     }
4258     return raw_do_pdiscard(bs, offset, bytes, true);
4259 }
4260 
4261 static coroutine_fn int hdev_co_pwrite_zeroes(BlockDriverState *bs,
4262     int64_t offset, int64_t bytes, BdrvRequestFlags flags)
4263 {
4264     int rc;
4265 
4266     rc = fd_open(bs);
4267     if (rc < 0) {
4268         return rc;
4269     }
4270 
4271     return raw_do_pwrite_zeroes(bs, offset, bytes, flags, true);
4272 }
4273 
4274 static BlockDriver bdrv_host_device = {
4275     .format_name        = "host_device",
4276     .protocol_name        = "host_device",
4277     .instance_size      = sizeof(BDRVRawState),
4278     .bdrv_needs_filename = true,
4279     .bdrv_probe_device  = hdev_probe_device,
4280     .bdrv_parse_filename = hdev_parse_filename,
4281     .bdrv_file_open     = hdev_open,
4282     .bdrv_close         = raw_close,
4283     .bdrv_reopen_prepare = raw_reopen_prepare,
4284     .bdrv_reopen_commit  = raw_reopen_commit,
4285     .bdrv_reopen_abort   = raw_reopen_abort,
4286     .bdrv_co_create_opts = bdrv_co_create_opts_simple,
4287     .create_opts         = &bdrv_create_opts_simple,
4288     .mutable_opts        = mutable_opts,
4289     .bdrv_co_invalidate_cache = raw_co_invalidate_cache,
4290     .bdrv_co_pwrite_zeroes = hdev_co_pwrite_zeroes,
4291 
4292     .bdrv_co_preadv         = raw_co_preadv,
4293     .bdrv_co_pwritev        = raw_co_pwritev,
4294     .bdrv_co_flush_to_disk  = raw_co_flush_to_disk,
4295     .bdrv_co_pdiscard       = hdev_co_pdiscard,
4296     .bdrv_co_copy_range_from = raw_co_copy_range_from,
4297     .bdrv_co_copy_range_to  = raw_co_copy_range_to,
4298     .bdrv_refresh_limits = raw_refresh_limits,
4299     .bdrv_co_io_plug        = raw_co_io_plug,
4300     .bdrv_co_io_unplug      = raw_co_io_unplug,
4301     .bdrv_attach_aio_context = raw_aio_attach_aio_context,
4302 
4303     .bdrv_co_truncate                   = raw_co_truncate,
4304     .bdrv_co_getlength                  = raw_co_getlength,
4305     .bdrv_co_get_info                   = raw_co_get_info,
4306     .bdrv_get_specific_info             = raw_get_specific_info,
4307     .bdrv_co_get_allocated_file_size    = raw_co_get_allocated_file_size,
4308     .bdrv_get_specific_stats = hdev_get_specific_stats,
4309     .bdrv_check_perm = raw_check_perm,
4310     .bdrv_set_perm   = raw_set_perm,
4311     .bdrv_abort_perm_update = raw_abort_perm_update,
4312     .bdrv_probe_blocksizes = hdev_probe_blocksizes,
4313     .bdrv_probe_geometry = hdev_probe_geometry,
4314 
4315     /* generic scsi device */
4316 #ifdef __linux__
4317     .bdrv_co_ioctl          = hdev_co_ioctl,
4318 #endif
4319 
4320     /* zoned device */
4321 #if defined(CONFIG_BLKZONED)
4322     /* zone management operations */
4323     .bdrv_co_zone_report = raw_co_zone_report,
4324     .bdrv_co_zone_mgmt = raw_co_zone_mgmt,
4325     .bdrv_co_zone_append = raw_co_zone_append,
4326 #endif
4327 };
4328 
4329 #if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
4330 static void cdrom_parse_filename(const char *filename, QDict *options,
4331                                  Error **errp)
4332 {
4333     bdrv_parse_filename_strip_prefix(filename, "host_cdrom:", options);
4334 }
4335 
4336 static void cdrom_refresh_limits(BlockDriverState *bs, Error **errp)
4337 {
4338     bs->bl.has_variable_length = true;
4339     raw_refresh_limits(bs, errp);
4340 }
4341 #endif
4342 
4343 #ifdef __linux__
4344 static int cdrom_open(BlockDriverState *bs, QDict *options, int flags,
4345                       Error **errp)
4346 {
4347     BDRVRawState *s = bs->opaque;
4348 
4349     s->type = FTYPE_CD;
4350 
4351     /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
4352     return raw_open_common(bs, options, flags, O_NONBLOCK, true, errp);
4353 }
4354 
4355 static int cdrom_probe_device(const char *filename)
4356 {
4357     int fd, ret;
4358     int prio = 0;
4359     struct stat st;
4360 
4361     fd = qemu_open(filename, O_RDONLY | O_NONBLOCK, NULL);
4362     if (fd < 0) {
4363         goto out;
4364     }
4365     ret = fstat(fd, &st);
4366     if (ret == -1 || !S_ISBLK(st.st_mode)) {
4367         goto outc;
4368     }
4369 
4370     /* Attempt to detect via a CDROM specific ioctl */
4371     ret = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
4372     if (ret >= 0)
4373         prio = 100;
4374 
4375 outc:
4376     qemu_close(fd);
4377 out:
4378     return prio;
4379 }
4380 
4381 static bool coroutine_fn cdrom_co_is_inserted(BlockDriverState *bs)
4382 {
4383     BDRVRawState *s = bs->opaque;
4384     int ret;
4385 
4386     ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
4387     return ret == CDS_DISC_OK;
4388 }
4389 
4390 static void coroutine_fn cdrom_co_eject(BlockDriverState *bs, bool eject_flag)
4391 {
4392     BDRVRawState *s = bs->opaque;
4393 
4394     if (eject_flag) {
4395         if (ioctl(s->fd, CDROMEJECT, NULL) < 0)
4396             perror("CDROMEJECT");
4397     } else {
4398         if (ioctl(s->fd, CDROMCLOSETRAY, NULL) < 0)
4399             perror("CDROMEJECT");
4400     }
4401 }
4402 
4403 static void coroutine_fn cdrom_co_lock_medium(BlockDriverState *bs, bool locked)
4404 {
4405     BDRVRawState *s = bs->opaque;
4406 
4407     if (ioctl(s->fd, CDROM_LOCKDOOR, locked) < 0) {
4408         /*
4409          * Note: an error can happen if the distribution automatically
4410          * mounts the CD-ROM
4411          */
4412         /* perror("CDROM_LOCKDOOR"); */
4413     }
4414 }
4415 
4416 static BlockDriver bdrv_host_cdrom = {
4417     .format_name        = "host_cdrom",
4418     .protocol_name      = "host_cdrom",
4419     .instance_size      = sizeof(BDRVRawState),
4420     .bdrv_needs_filename = true,
4421     .bdrv_probe_device	= cdrom_probe_device,
4422     .bdrv_parse_filename = cdrom_parse_filename,
4423     .bdrv_file_open     = cdrom_open,
4424     .bdrv_close         = raw_close,
4425     .bdrv_reopen_prepare = raw_reopen_prepare,
4426     .bdrv_reopen_commit  = raw_reopen_commit,
4427     .bdrv_reopen_abort   = raw_reopen_abort,
4428     .bdrv_co_create_opts = bdrv_co_create_opts_simple,
4429     .create_opts         = &bdrv_create_opts_simple,
4430     .mutable_opts        = mutable_opts,
4431     .bdrv_co_invalidate_cache = raw_co_invalidate_cache,
4432 
4433     .bdrv_co_preadv         = raw_co_preadv,
4434     .bdrv_co_pwritev        = raw_co_pwritev,
4435     .bdrv_co_flush_to_disk  = raw_co_flush_to_disk,
4436     .bdrv_refresh_limits    = cdrom_refresh_limits,
4437     .bdrv_co_io_plug        = raw_co_io_plug,
4438     .bdrv_co_io_unplug      = raw_co_io_unplug,
4439     .bdrv_attach_aio_context = raw_aio_attach_aio_context,
4440 
4441     .bdrv_co_truncate                   = raw_co_truncate,
4442     .bdrv_co_getlength                  = raw_co_getlength,
4443     .bdrv_co_get_allocated_file_size    = raw_co_get_allocated_file_size,
4444 
4445     /* removable device support */
4446     .bdrv_co_is_inserted    = cdrom_co_is_inserted,
4447     .bdrv_co_eject          = cdrom_co_eject,
4448     .bdrv_co_lock_medium    = cdrom_co_lock_medium,
4449 
4450     /* generic scsi device */
4451     .bdrv_co_ioctl      = hdev_co_ioctl,
4452 };
4453 #endif /* __linux__ */
4454 
4455 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
4456 static int cdrom_open(BlockDriverState *bs, QDict *options, int flags,
4457                       Error **errp)
4458 {
4459     BDRVRawState *s = bs->opaque;
4460     int ret;
4461 
4462     s->type = FTYPE_CD;
4463 
4464     ret = raw_open_common(bs, options, flags, 0, true, errp);
4465     if (ret) {
4466         return ret;
4467     }
4468 
4469     /* make sure the door isn't locked at this time */
4470     ioctl(s->fd, CDIOCALLOW);
4471     return 0;
4472 }
4473 
4474 static int cdrom_probe_device(const char *filename)
4475 {
4476     if (strstart(filename, "/dev/cd", NULL) ||
4477             strstart(filename, "/dev/acd", NULL))
4478         return 100;
4479     return 0;
4480 }
4481 
4482 static int cdrom_reopen(BlockDriverState *bs)
4483 {
4484     BDRVRawState *s = bs->opaque;
4485     int fd;
4486 
4487     /*
4488      * Force reread of possibly changed/newly loaded disc,
4489      * FreeBSD seems to not notice sometimes...
4490      */
4491     if (s->fd >= 0)
4492         qemu_close(s->fd);
4493     fd = qemu_open(bs->filename, s->open_flags, NULL);
4494     if (fd < 0) {
4495         s->fd = -1;
4496         return -EIO;
4497     }
4498     s->fd = fd;
4499 
4500     /* make sure the door isn't locked at this time */
4501     ioctl(s->fd, CDIOCALLOW);
4502     return 0;
4503 }
4504 
4505 static bool coroutine_fn cdrom_co_is_inserted(BlockDriverState *bs)
4506 {
4507     return raw_co_getlength(bs) > 0;
4508 }
4509 
4510 static void coroutine_fn cdrom_co_eject(BlockDriverState *bs, bool eject_flag)
4511 {
4512     BDRVRawState *s = bs->opaque;
4513 
4514     if (s->fd < 0)
4515         return;
4516 
4517     (void) ioctl(s->fd, CDIOCALLOW);
4518 
4519     if (eject_flag) {
4520         if (ioctl(s->fd, CDIOCEJECT) < 0)
4521             perror("CDIOCEJECT");
4522     } else {
4523         if (ioctl(s->fd, CDIOCCLOSE) < 0)
4524             perror("CDIOCCLOSE");
4525     }
4526 
4527     cdrom_reopen(bs);
4528 }
4529 
4530 static void coroutine_fn cdrom_co_lock_medium(BlockDriverState *bs, bool locked)
4531 {
4532     BDRVRawState *s = bs->opaque;
4533 
4534     if (s->fd < 0)
4535         return;
4536     if (ioctl(s->fd, (locked ? CDIOCPREVENT : CDIOCALLOW)) < 0) {
4537         /*
4538          * Note: an error can happen if the distribution automatically
4539          * mounts the CD-ROM
4540          */
4541         /* perror("CDROM_LOCKDOOR"); */
4542     }
4543 }
4544 
4545 static BlockDriver bdrv_host_cdrom = {
4546     .format_name        = "host_cdrom",
4547     .protocol_name      = "host_cdrom",
4548     .instance_size      = sizeof(BDRVRawState),
4549     .bdrv_needs_filename = true,
4550     .bdrv_probe_device	= cdrom_probe_device,
4551     .bdrv_parse_filename = cdrom_parse_filename,
4552     .bdrv_file_open     = cdrom_open,
4553     .bdrv_close         = raw_close,
4554     .bdrv_reopen_prepare = raw_reopen_prepare,
4555     .bdrv_reopen_commit  = raw_reopen_commit,
4556     .bdrv_reopen_abort   = raw_reopen_abort,
4557     .bdrv_co_create_opts = bdrv_co_create_opts_simple,
4558     .create_opts         = &bdrv_create_opts_simple,
4559     .mutable_opts       = mutable_opts,
4560 
4561     .bdrv_co_preadv         = raw_co_preadv,
4562     .bdrv_co_pwritev        = raw_co_pwritev,
4563     .bdrv_co_flush_to_disk  = raw_co_flush_to_disk,
4564     .bdrv_refresh_limits    = cdrom_refresh_limits,
4565     .bdrv_co_io_plug        = raw_co_io_plug,
4566     .bdrv_co_io_unplug      = raw_co_io_unplug,
4567     .bdrv_attach_aio_context = raw_aio_attach_aio_context,
4568 
4569     .bdrv_co_truncate                   = raw_co_truncate,
4570     .bdrv_co_getlength                  = raw_co_getlength,
4571     .bdrv_co_get_allocated_file_size    = raw_co_get_allocated_file_size,
4572 
4573     /* removable device support */
4574     .bdrv_co_is_inserted     = cdrom_co_is_inserted,
4575     .bdrv_co_eject           = cdrom_co_eject,
4576     .bdrv_co_lock_medium     = cdrom_co_lock_medium,
4577 };
4578 #endif /* __FreeBSD__ */
4579 
4580 #endif /* HAVE_HOST_BLOCK_DEVICE */
4581 
4582 static void bdrv_file_init(void)
4583 {
4584     /*
4585      * Register all the drivers.  Note that order is important, the driver
4586      * registered last will get probed first.
4587      */
4588     bdrv_register(&bdrv_file);
4589 #if defined(HAVE_HOST_BLOCK_DEVICE)
4590     bdrv_register(&bdrv_host_device);
4591 #ifdef __linux__
4592     bdrv_register(&bdrv_host_cdrom);
4593 #endif
4594 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
4595     bdrv_register(&bdrv_host_cdrom);
4596 #endif
4597 #endif /* HAVE_HOST_BLOCK_DEVICE */
4598 }
4599 
4600 block_init(bdrv_file_init);
4601