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