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