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