xref: /qemu/block/file-posix.c (revision 668df86ee8076152320345d8e36be7c95ec0a09a)
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 
1607     raw_refresh_zoned_limits(bs, &st, errp);
1608 }
1609 
check_for_dasd(int fd)1610 static int check_for_dasd(int fd)
1611 {
1612 #ifdef BIODASDINFO2
1613     struct dasd_information2_t info = {0};
1614 
1615     return ioctl(fd, BIODASDINFO2, &info);
1616 #else
1617     return -1;
1618 #endif
1619 }
1620 
1621 /**
1622  * Try to get @bs's logical and physical block size.
1623  * On success, store them in @bsz and return zero.
1624  * On failure, return negative errno.
1625  */
hdev_probe_blocksizes(BlockDriverState * bs,BlockSizes * bsz)1626 static int hdev_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz)
1627 {
1628     BDRVRawState *s = bs->opaque;
1629     int ret;
1630 
1631     /* If DASD or zoned devices, get blocksizes */
1632     if (check_for_dasd(s->fd) < 0) {
1633         /* zoned devices are not DASD */
1634         if (bs->bl.zoned == BLK_Z_NONE) {
1635             return -ENOTSUP;
1636         }
1637     }
1638     ret = probe_logical_blocksize(s->fd, &bsz->log);
1639     if (ret < 0) {
1640         return ret;
1641     }
1642     return probe_physical_blocksize(s->fd, &bsz->phys);
1643 }
1644 
1645 /**
1646  * Try to get @bs's geometry: cyls, heads, sectors.
1647  * On success, store them in @geo and return 0.
1648  * On failure return -errno.
1649  * (Allows block driver to assign default geometry values that guest sees)
1650  */
1651 #ifdef __linux__
hdev_probe_geometry(BlockDriverState * bs,HDGeometry * geo)1652 static int hdev_probe_geometry(BlockDriverState *bs, HDGeometry *geo)
1653 {
1654     BDRVRawState *s = bs->opaque;
1655     struct hd_geometry ioctl_geo = {0};
1656 
1657     /* If DASD, get its geometry */
1658     if (check_for_dasd(s->fd) < 0) {
1659         return -ENOTSUP;
1660     }
1661     if (ioctl(s->fd, HDIO_GETGEO, &ioctl_geo) < 0) {
1662         return -errno;
1663     }
1664     /* HDIO_GETGEO may return success even though geo contains zeros
1665        (e.g. certain multipath setups) */
1666     if (!ioctl_geo.heads || !ioctl_geo.sectors || !ioctl_geo.cylinders) {
1667         return -ENOTSUP;
1668     }
1669     /* Do not return a geometry for partition */
1670     if (ioctl_geo.start != 0) {
1671         return -ENOTSUP;
1672     }
1673     geo->heads = ioctl_geo.heads;
1674     geo->sectors = ioctl_geo.sectors;
1675     geo->cylinders = ioctl_geo.cylinders;
1676 
1677     return 0;
1678 }
1679 #else /* __linux__ */
hdev_probe_geometry(BlockDriverState * bs,HDGeometry * geo)1680 static int hdev_probe_geometry(BlockDriverState *bs, HDGeometry *geo)
1681 {
1682     return -ENOTSUP;
1683 }
1684 #endif
1685 
1686 #if defined(__linux__)
handle_aiocb_ioctl(void * opaque)1687 static int handle_aiocb_ioctl(void *opaque)
1688 {
1689     RawPosixAIOData *aiocb = opaque;
1690     int ret;
1691 
1692     ret = RETRY_ON_EINTR(
1693         ioctl(aiocb->aio_fildes, aiocb->ioctl.cmd, aiocb->ioctl.buf)
1694     );
1695     if (ret == -1) {
1696         return -errno;
1697     }
1698 
1699     return 0;
1700 }
1701 #endif /* linux */
1702 
handle_aiocb_flush(void * opaque)1703 static int handle_aiocb_flush(void *opaque)
1704 {
1705     RawPosixAIOData *aiocb = opaque;
1706     BDRVRawState *s = aiocb->bs->opaque;
1707     int ret;
1708 
1709     if (s->page_cache_inconsistent) {
1710         return -s->page_cache_inconsistent;
1711     }
1712 
1713     ret = qemu_fdatasync(aiocb->aio_fildes);
1714     if (ret == -1) {
1715         trace_file_flush_fdatasync_failed(errno);
1716 
1717         /* There is no clear definition of the semantics of a failing fsync(),
1718          * so we may have to assume the worst. The sad truth is that this
1719          * assumption is correct for Linux. Some pages are now probably marked
1720          * clean in the page cache even though they are inconsistent with the
1721          * on-disk contents. The next fdatasync() call would succeed, but no
1722          * further writeback attempt will be made. We can't get back to a state
1723          * in which we know what is on disk (we would have to rewrite
1724          * everything that was touched since the last fdatasync() at least), so
1725          * make bdrv_flush() fail permanently. Given that the behaviour isn't
1726          * really defined, I have little hope that other OSes are doing better.
1727          *
1728          * Obviously, this doesn't affect O_DIRECT, which bypasses the page
1729          * cache. */
1730         if ((s->open_flags & O_DIRECT) == 0) {
1731             s->page_cache_inconsistent = errno;
1732         }
1733         return -errno;
1734     }
1735     return 0;
1736 }
1737 
1738 #ifdef CONFIG_PREADV
1739 
1740 static bool preadv_present = true;
1741 
1742 static ssize_t
qemu_preadv(int fd,const struct iovec * iov,int nr_iov,off_t offset)1743 qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset)
1744 {
1745     return preadv(fd, iov, nr_iov, offset);
1746 }
1747 
1748 static ssize_t
qemu_pwritev(int fd,const struct iovec * iov,int nr_iov,off_t offset)1749 qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset)
1750 {
1751     return pwritev(fd, iov, nr_iov, offset);
1752 }
1753 
1754 #else
1755 
1756 static bool preadv_present = false;
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 -ENOSYS;
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 -ENOSYS;
1768 }
1769 
1770 #endif
1771 
handle_aiocb_rw_vector(RawPosixAIOData * aiocb)1772 static ssize_t handle_aiocb_rw_vector(RawPosixAIOData *aiocb)
1773 {
1774     ssize_t len;
1775 
1776     len = RETRY_ON_EINTR(
1777         (aiocb->aio_type & (QEMU_AIO_WRITE | QEMU_AIO_ZONE_APPEND)) ?
1778             qemu_pwritev(aiocb->aio_fildes,
1779                            aiocb->io.iov,
1780                            aiocb->io.niov,
1781                            aiocb->aio_offset) :
1782             qemu_preadv(aiocb->aio_fildes,
1783                           aiocb->io.iov,
1784                           aiocb->io.niov,
1785                           aiocb->aio_offset)
1786     );
1787 
1788     if (len == -1) {
1789         return -errno;
1790     }
1791     return len;
1792 }
1793 
1794 /*
1795  * Read/writes the data to/from a given linear buffer.
1796  *
1797  * Returns the number of bytes handles or -errno in case of an error. Short
1798  * reads are only returned if the end of the file is reached.
1799  */
handle_aiocb_rw_linear(RawPosixAIOData * aiocb,char * buf)1800 static ssize_t handle_aiocb_rw_linear(RawPosixAIOData *aiocb, char *buf)
1801 {
1802     ssize_t offset = 0;
1803     ssize_t len;
1804 
1805     while (offset < aiocb->aio_nbytes) {
1806         if (aiocb->aio_type & (QEMU_AIO_WRITE | QEMU_AIO_ZONE_APPEND)) {
1807             len = pwrite(aiocb->aio_fildes,
1808                          (const char *)buf + offset,
1809                          aiocb->aio_nbytes - offset,
1810                          aiocb->aio_offset + offset);
1811         } else {
1812             len = pread(aiocb->aio_fildes,
1813                         buf + offset,
1814                         aiocb->aio_nbytes - offset,
1815                         aiocb->aio_offset + offset);
1816         }
1817         if (len == -1 && errno == EINTR) {
1818             continue;
1819         } else if (len == -1 && errno == EINVAL &&
1820                    (aiocb->bs->open_flags & BDRV_O_NOCACHE) &&
1821                    !(aiocb->aio_type & QEMU_AIO_WRITE) &&
1822                    offset > 0) {
1823             /* O_DIRECT pread() may fail with EINVAL when offset is unaligned
1824              * after a short read.  Assume that O_DIRECT short reads only occur
1825              * at EOF.  Therefore this is a short read, not an I/O error.
1826              */
1827             break;
1828         } else if (len == -1) {
1829             offset = -errno;
1830             break;
1831         } else if (len == 0) {
1832             break;
1833         }
1834         offset += len;
1835     }
1836 
1837     return offset;
1838 }
1839 
handle_aiocb_rw(void * opaque)1840 static int handle_aiocb_rw(void *opaque)
1841 {
1842     RawPosixAIOData *aiocb = opaque;
1843     ssize_t nbytes;
1844     char *buf;
1845 
1846     if (!(aiocb->aio_type & QEMU_AIO_MISALIGNED)) {
1847         /*
1848          * If there is just a single buffer, and it is properly aligned
1849          * we can just use plain pread/pwrite without any problems.
1850          */
1851         if (aiocb->io.niov == 1) {
1852             nbytes = handle_aiocb_rw_linear(aiocb, aiocb->io.iov->iov_base);
1853             goto out;
1854         }
1855         /*
1856          * We have more than one iovec, and all are properly aligned.
1857          *
1858          * Try preadv/pwritev first and fall back to linearizing the
1859          * buffer if it's not supported.
1860          */
1861         if (preadv_present) {
1862             nbytes = handle_aiocb_rw_vector(aiocb);
1863             if (nbytes == aiocb->aio_nbytes ||
1864                 (nbytes < 0 && nbytes != -ENOSYS)) {
1865                 goto out;
1866             }
1867             preadv_present = false;
1868         }
1869 
1870         /*
1871          * XXX(hch): short read/write.  no easy way to handle the reminder
1872          * using these interfaces.  For now retry using plain
1873          * pread/pwrite?
1874          */
1875     }
1876 
1877     /*
1878      * Ok, we have to do it the hard way, copy all segments into
1879      * a single aligned buffer.
1880      */
1881     buf = qemu_try_blockalign(aiocb->bs, aiocb->aio_nbytes);
1882     if (buf == NULL) {
1883         nbytes = -ENOMEM;
1884         goto out;
1885     }
1886 
1887     if (aiocb->aio_type & QEMU_AIO_WRITE) {
1888         char *p = buf;
1889         int i;
1890 
1891         for (i = 0; i < aiocb->io.niov; ++i) {
1892             memcpy(p, aiocb->io.iov[i].iov_base, aiocb->io.iov[i].iov_len);
1893             p += aiocb->io.iov[i].iov_len;
1894         }
1895         assert(p - buf == aiocb->aio_nbytes);
1896     }
1897 
1898     nbytes = handle_aiocb_rw_linear(aiocb, buf);
1899     if (!(aiocb->aio_type & (QEMU_AIO_WRITE | QEMU_AIO_ZONE_APPEND))) {
1900         char *p = buf;
1901         size_t count = aiocb->aio_nbytes, copy;
1902         int i;
1903 
1904         for (i = 0; i < aiocb->io.niov && count; ++i) {
1905             copy = count;
1906             if (copy > aiocb->io.iov[i].iov_len) {
1907                 copy = aiocb->io.iov[i].iov_len;
1908             }
1909             memcpy(aiocb->io.iov[i].iov_base, p, copy);
1910             assert(count >= copy);
1911             p     += copy;
1912             count -= copy;
1913         }
1914         assert(count == 0);
1915     }
1916     qemu_vfree(buf);
1917 
1918 out:
1919     if (nbytes == aiocb->aio_nbytes) {
1920         return 0;
1921     } else if (nbytes >= 0 && nbytes < aiocb->aio_nbytes) {
1922         if (aiocb->aio_type & QEMU_AIO_WRITE) {
1923             return -EINVAL;
1924         } else {
1925             iov_memset(aiocb->io.iov, aiocb->io.niov, nbytes,
1926                       0, aiocb->aio_nbytes - nbytes);
1927             return 0;
1928         }
1929     } else {
1930         assert(nbytes < 0);
1931         return nbytes;
1932     }
1933 }
1934 
1935 #if defined(CONFIG_FALLOCATE) || defined(BLKZEROOUT) || defined(BLKDISCARD)
translate_err(int err)1936 static int translate_err(int err)
1937 {
1938     if (err == -ENODEV || err == -ENOSYS || err == -EOPNOTSUPP ||
1939         err == -ENOTTY) {
1940         err = -ENOTSUP;
1941     }
1942     return err;
1943 }
1944 #endif
1945 
1946 #ifdef CONFIG_FALLOCATE
do_fallocate(int fd,int mode,off_t offset,off_t len)1947 static int do_fallocate(int fd, int mode, off_t offset, off_t len)
1948 {
1949     do {
1950         if (fallocate(fd, mode, offset, len) == 0) {
1951             return 0;
1952         }
1953     } while (errno == EINTR);
1954     return translate_err(-errno);
1955 }
1956 #endif
1957 
handle_aiocb_write_zeroes_block(RawPosixAIOData * aiocb)1958 static ssize_t handle_aiocb_write_zeroes_block(RawPosixAIOData *aiocb)
1959 {
1960     int ret = -ENOTSUP;
1961     BDRVRawState *s = aiocb->bs->opaque;
1962 
1963     if (!s->has_write_zeroes) {
1964         return -ENOTSUP;
1965     }
1966 
1967 #ifdef BLKZEROOUT
1968     /* The BLKZEROOUT implementation in the kernel doesn't set
1969      * BLKDEV_ZERO_NOFALLBACK, so we can't call this if we have to avoid slow
1970      * fallbacks. */
1971     if (!(aiocb->aio_type & QEMU_AIO_NO_FALLBACK)) {
1972         do {
1973             uint64_t range[2] = { aiocb->aio_offset, aiocb->aio_nbytes };
1974             if (ioctl(aiocb->aio_fildes, BLKZEROOUT, range) == 0) {
1975                 return 0;
1976             }
1977         } while (errno == EINTR);
1978 
1979         ret = translate_err(-errno);
1980         if (ret == -ENOTSUP) {
1981             s->has_write_zeroes = false;
1982         }
1983     }
1984 #endif
1985 
1986     return ret;
1987 }
1988 
handle_aiocb_write_zeroes(void * opaque)1989 static int handle_aiocb_write_zeroes(void *opaque)
1990 {
1991     RawPosixAIOData *aiocb = opaque;
1992 #ifdef CONFIG_FALLOCATE
1993     BDRVRawState *s = aiocb->bs->opaque;
1994     int64_t len;
1995 #endif
1996 
1997     if (aiocb->aio_type & QEMU_AIO_BLKDEV) {
1998         return handle_aiocb_write_zeroes_block(aiocb);
1999     }
2000 
2001 #ifdef CONFIG_FALLOCATE_ZERO_RANGE
2002     if (s->has_write_zeroes) {
2003         int ret = do_fallocate(s->fd, FALLOC_FL_ZERO_RANGE,
2004                                aiocb->aio_offset, aiocb->aio_nbytes);
2005         if (ret == -ENOTSUP) {
2006             s->has_write_zeroes = false;
2007         } else if (ret == 0 || ret != -EINVAL) {
2008             return ret;
2009         }
2010         /*
2011          * Note: Some file systems do not like unaligned byte ranges, and
2012          * return EINVAL in such a case, though they should not do it according
2013          * to the man-page of fallocate(). Thus we simply ignore this return
2014          * value and try the other fallbacks instead.
2015          */
2016     }
2017 #endif
2018 
2019 #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
2020     if (s->has_discard && s->has_fallocate) {
2021         int ret = do_fallocate(s->fd,
2022                                FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
2023                                aiocb->aio_offset, aiocb->aio_nbytes);
2024         if (ret == 0) {
2025             ret = do_fallocate(s->fd, 0, aiocb->aio_offset, aiocb->aio_nbytes);
2026             if (ret == 0 || ret != -ENOTSUP) {
2027                 return ret;
2028             }
2029             s->has_fallocate = false;
2030         } else if (ret == -EINVAL) {
2031             /*
2032              * Some file systems like older versions of GPFS do not like un-
2033              * aligned byte ranges, and return EINVAL in such a case, though
2034              * they should not do it according to the man-page of fallocate().
2035              * Warn about the bad filesystem and try the final fallback instead.
2036              */
2037             warn_report_once("Your file system is misbehaving: "
2038                              "fallocate(FALLOC_FL_PUNCH_HOLE) returned EINVAL. "
2039                              "Please report this bug to your file system "
2040                              "vendor.");
2041         } else if (ret != -ENOTSUP) {
2042             return ret;
2043         } else {
2044             s->has_discard = false;
2045         }
2046     }
2047 #endif
2048 
2049 #ifdef CONFIG_FALLOCATE
2050     /* Last resort: we are trying to extend the file with zeroed data. This
2051      * can be done via fallocate(fd, 0) */
2052     len = raw_getlength(aiocb->bs);
2053     if (s->has_fallocate && len >= 0 && aiocb->aio_offset >= len) {
2054         int ret = do_fallocate(s->fd, 0, aiocb->aio_offset, aiocb->aio_nbytes);
2055         if (ret == 0 || ret != -ENOTSUP) {
2056             return ret;
2057         }
2058         s->has_fallocate = false;
2059     }
2060 #endif
2061 
2062     return -ENOTSUP;
2063 }
2064 
handle_aiocb_write_zeroes_unmap(void * opaque)2065 static int handle_aiocb_write_zeroes_unmap(void *opaque)
2066 {
2067     RawPosixAIOData *aiocb = opaque;
2068     BDRVRawState *s G_GNUC_UNUSED = aiocb->bs->opaque;
2069 
2070     /* First try to write zeros and unmap at the same time */
2071 
2072 #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
2073     int ret = do_fallocate(s->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
2074                            aiocb->aio_offset, aiocb->aio_nbytes);
2075     switch (ret) {
2076     case -ENOTSUP:
2077     case -EINVAL:
2078     case -EBUSY:
2079         break;
2080     default:
2081         return ret;
2082     }
2083 #endif
2084 
2085     /* If we couldn't manage to unmap while guaranteed that the area reads as
2086      * all-zero afterwards, just write zeroes without unmapping */
2087     return handle_aiocb_write_zeroes(aiocb);
2088 }
2089 
2090 #ifndef HAVE_COPY_FILE_RANGE
2091 #ifndef EMSCRIPTEN
2092 static
2093 #endif
copy_file_range(int in_fd,off_t * in_off,int out_fd,off_t * out_off,size_t len,unsigned int flags)2094 ssize_t copy_file_range(int in_fd, off_t *in_off, int out_fd,
2095                         off_t *out_off, size_t len, unsigned int flags)
2096 {
2097 #ifdef __NR_copy_file_range
2098     return syscall(__NR_copy_file_range, in_fd, in_off, out_fd,
2099                    out_off, len, flags);
2100 #else
2101     errno = ENOSYS;
2102     return -1;
2103 #endif
2104 }
2105 #endif
2106 
2107 /*
2108  * parse_zone - Fill a zone descriptor
2109  */
2110 #if defined(CONFIG_BLKZONED)
parse_zone(struct BlockZoneDescriptor * zone,const struct blk_zone * blkz)2111 static inline int parse_zone(struct BlockZoneDescriptor *zone,
2112                               const struct blk_zone *blkz) {
2113     zone->start = blkz->start << BDRV_SECTOR_BITS;
2114     zone->length = blkz->len << BDRV_SECTOR_BITS;
2115     zone->wp = blkz->wp << BDRV_SECTOR_BITS;
2116 
2117 #ifdef HAVE_BLK_ZONE_REP_CAPACITY
2118     zone->cap = blkz->capacity << BDRV_SECTOR_BITS;
2119 #else
2120     zone->cap = blkz->len << BDRV_SECTOR_BITS;
2121 #endif
2122 
2123     switch (blkz->type) {
2124     case BLK_ZONE_TYPE_SEQWRITE_REQ:
2125         zone->type = BLK_ZT_SWR;
2126         break;
2127     case BLK_ZONE_TYPE_SEQWRITE_PREF:
2128         zone->type = BLK_ZT_SWP;
2129         break;
2130     case BLK_ZONE_TYPE_CONVENTIONAL:
2131         zone->type = BLK_ZT_CONV;
2132         break;
2133     default:
2134         error_report("Unsupported zone type: 0x%x", blkz->type);
2135         return -ENOTSUP;
2136     }
2137 
2138     switch (blkz->cond) {
2139     case BLK_ZONE_COND_NOT_WP:
2140         zone->state = BLK_ZS_NOT_WP;
2141         break;
2142     case BLK_ZONE_COND_EMPTY:
2143         zone->state = BLK_ZS_EMPTY;
2144         break;
2145     case BLK_ZONE_COND_IMP_OPEN:
2146         zone->state = BLK_ZS_IOPEN;
2147         break;
2148     case BLK_ZONE_COND_EXP_OPEN:
2149         zone->state = BLK_ZS_EOPEN;
2150         break;
2151     case BLK_ZONE_COND_CLOSED:
2152         zone->state = BLK_ZS_CLOSED;
2153         break;
2154     case BLK_ZONE_COND_READONLY:
2155         zone->state = BLK_ZS_RDONLY;
2156         break;
2157     case BLK_ZONE_COND_FULL:
2158         zone->state = BLK_ZS_FULL;
2159         break;
2160     case BLK_ZONE_COND_OFFLINE:
2161         zone->state = BLK_ZS_OFFLINE;
2162         break;
2163     default:
2164         error_report("Unsupported zone state: 0x%x", blkz->cond);
2165         return -ENOTSUP;
2166     }
2167     return 0;
2168 }
2169 #endif
2170 
2171 #if defined(CONFIG_BLKZONED)
handle_aiocb_zone_report(void * opaque)2172 static int handle_aiocb_zone_report(void *opaque)
2173 {
2174     RawPosixAIOData *aiocb = opaque;
2175     int fd = aiocb->aio_fildes;
2176     unsigned int *nr_zones = aiocb->zone_report.nr_zones;
2177     BlockZoneDescriptor *zones = aiocb->zone_report.zones;
2178     /* zoned block devices use 512-byte sectors */
2179     uint64_t sector = aiocb->aio_offset / 512;
2180 
2181     struct blk_zone *blkz;
2182     size_t rep_size;
2183     unsigned int nrz;
2184     int ret;
2185     unsigned int n = 0, i = 0;
2186 
2187     nrz = *nr_zones;
2188     rep_size = sizeof(struct blk_zone_report) + nrz * sizeof(struct blk_zone);
2189     g_autofree struct blk_zone_report *rep = NULL;
2190     rep = g_malloc(rep_size);
2191 
2192     blkz = (struct blk_zone *)(rep + 1);
2193     while (n < nrz) {
2194         memset(rep, 0, rep_size);
2195         rep->sector = sector;
2196         rep->nr_zones = nrz - n;
2197 
2198         do {
2199             ret = ioctl(fd, BLKREPORTZONE, rep);
2200         } while (ret != 0 && errno == EINTR);
2201         if (ret != 0) {
2202             error_report("%d: ioctl BLKREPORTZONE at %" PRId64 " failed %d",
2203                          fd, sector, errno);
2204             return -errno;
2205         }
2206 
2207         if (!rep->nr_zones) {
2208             break;
2209         }
2210 
2211         for (i = 0; i < rep->nr_zones; i++, n++) {
2212             ret = parse_zone(&zones[n], &blkz[i]);
2213             if (ret != 0) {
2214                 return ret;
2215             }
2216 
2217             /* The next report should start after the last zone reported */
2218             sector = blkz[i].start + blkz[i].len;
2219         }
2220     }
2221 
2222     *nr_zones = n;
2223     return 0;
2224 }
2225 #endif
2226 
2227 #if defined(CONFIG_BLKZONED)
handle_aiocb_zone_mgmt(void * opaque)2228 static int handle_aiocb_zone_mgmt(void *opaque)
2229 {
2230     RawPosixAIOData *aiocb = opaque;
2231     int fd = aiocb->aio_fildes;
2232     uint64_t sector = aiocb->aio_offset / 512;
2233     int64_t nr_sectors = aiocb->aio_nbytes / 512;
2234     struct blk_zone_range range;
2235     int ret;
2236 
2237     /* Execute the operation */
2238     range.sector = sector;
2239     range.nr_sectors = nr_sectors;
2240     do {
2241         ret = ioctl(fd, aiocb->zone_mgmt.op, &range);
2242     } while (ret != 0 && errno == EINTR);
2243 
2244     return ret < 0 ? -errno : ret;
2245 }
2246 #endif
2247 
handle_aiocb_copy_range(void * opaque)2248 static int handle_aiocb_copy_range(void *opaque)
2249 {
2250     RawPosixAIOData *aiocb = opaque;
2251     uint64_t bytes = aiocb->aio_nbytes;
2252     off_t in_off = aiocb->aio_offset;
2253     off_t out_off = aiocb->copy_range.aio_offset2;
2254 
2255     while (bytes) {
2256         ssize_t ret = copy_file_range(aiocb->aio_fildes, &in_off,
2257                                       aiocb->copy_range.aio_fd2, &out_off,
2258                                       bytes, 0);
2259         trace_file_copy_file_range(aiocb->bs, aiocb->aio_fildes, in_off,
2260                                    aiocb->copy_range.aio_fd2, out_off, bytes,
2261                                    0, ret);
2262         if (ret == 0) {
2263             /* No progress (e.g. when beyond EOF), let the caller fall back to
2264              * buffer I/O. */
2265             return -ENOSPC;
2266         }
2267         if (ret < 0) {
2268             switch (errno) {
2269             case ENOSYS:
2270                 return -ENOTSUP;
2271             case EINTR:
2272                 continue;
2273             default:
2274                 return -errno;
2275             }
2276         }
2277         bytes -= ret;
2278     }
2279     return 0;
2280 }
2281 
handle_aiocb_discard(void * opaque)2282 static int handle_aiocb_discard(void *opaque)
2283 {
2284     RawPosixAIOData *aiocb = opaque;
2285     int ret = -ENOTSUP;
2286     BDRVRawState *s = aiocb->bs->opaque;
2287 
2288     if (!s->has_discard) {
2289         return -ENOTSUP;
2290     }
2291 
2292     if (aiocb->aio_type & QEMU_AIO_BLKDEV) {
2293 #ifdef BLKDISCARD
2294         do {
2295             uint64_t range[2] = { aiocb->aio_offset, aiocb->aio_nbytes };
2296             if (ioctl(aiocb->aio_fildes, BLKDISCARD, range) == 0) {
2297                 return 0;
2298             }
2299         } while (errno == EINTR);
2300 
2301         ret = translate_err(-errno);
2302 #endif
2303     } else {
2304 #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
2305         ret = do_fallocate(s->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
2306                            aiocb->aio_offset, aiocb->aio_nbytes);
2307         ret = translate_err(ret);
2308 #elif defined(__APPLE__) && (__MACH__)
2309         fpunchhole_t fpunchhole;
2310         fpunchhole.fp_flags = 0;
2311         fpunchhole.reserved = 0;
2312         fpunchhole.fp_offset = aiocb->aio_offset;
2313         fpunchhole.fp_length = aiocb->aio_nbytes;
2314         if (fcntl(s->fd, F_PUNCHHOLE, &fpunchhole) == -1) {
2315             ret = errno == ENODEV ? -ENOTSUP : -errno;
2316         } else {
2317             ret = 0;
2318         }
2319 #endif
2320     }
2321 
2322     if (ret == -ENOTSUP) {
2323         s->has_discard = false;
2324     }
2325     return ret;
2326 }
2327 
2328 /*
2329  * Help alignment probing by allocating the first block.
2330  *
2331  * When reading with direct I/O from unallocated area on Gluster backed by XFS,
2332  * reading succeeds regardless of request length. In this case we fallback to
2333  * safe alignment which is not optimal. Allocating the first block avoids this
2334  * fallback.
2335  *
2336  * fd may be opened with O_DIRECT, but we don't know the buffer alignment or
2337  * request alignment, so we use safe values.
2338  *
2339  * Returns: 0 on success, -errno on failure. Since this is an optimization,
2340  * caller may ignore failures.
2341  */
allocate_first_block(int fd,size_t max_size)2342 static int allocate_first_block(int fd, size_t max_size)
2343 {
2344     size_t write_size = (max_size < MAX_BLOCKSIZE)
2345         ? BDRV_SECTOR_SIZE
2346         : MAX_BLOCKSIZE;
2347     size_t max_align = MAX(MAX_BLOCKSIZE, qemu_real_host_page_size());
2348     void *buf;
2349     ssize_t n;
2350     int ret;
2351 
2352     buf = qemu_memalign(max_align, write_size);
2353     memset(buf, 0, write_size);
2354 
2355     n = RETRY_ON_EINTR(pwrite(fd, buf, write_size, 0));
2356 
2357     ret = (n == -1) ? -errno : 0;
2358 
2359     qemu_vfree(buf);
2360     return ret;
2361 }
2362 
handle_aiocb_truncate(void * opaque)2363 static int handle_aiocb_truncate(void *opaque)
2364 {
2365     RawPosixAIOData *aiocb = opaque;
2366     int result = 0;
2367     int64_t current_length = 0;
2368     char *buf = NULL;
2369     struct stat st;
2370     int fd = aiocb->aio_fildes;
2371     int64_t offset = aiocb->aio_offset;
2372     PreallocMode prealloc = aiocb->truncate.prealloc;
2373     Error **errp = aiocb->truncate.errp;
2374 
2375     if (fstat(fd, &st) < 0) {
2376         result = -errno;
2377         error_setg_errno(errp, -result, "Could not stat file");
2378         return result;
2379     }
2380 
2381     current_length = st.st_size;
2382     if (current_length > offset && prealloc != PREALLOC_MODE_OFF) {
2383         error_setg(errp, "Cannot use preallocation for shrinking files");
2384         return -ENOTSUP;
2385     }
2386 
2387     switch (prealloc) {
2388 #ifdef CONFIG_POSIX_FALLOCATE
2389     case PREALLOC_MODE_FALLOC:
2390         /*
2391          * Truncating before posix_fallocate() makes it about twice slower on
2392          * file systems that do not support fallocate(), trying to check if a
2393          * block is allocated before allocating it, so don't do that here.
2394          */
2395         if (offset != current_length) {
2396             result = -posix_fallocate(fd, current_length,
2397                                       offset - current_length);
2398             if (result != 0) {
2399                 /* posix_fallocate() doesn't set errno. */
2400                 error_setg_errno(errp, -result,
2401                                  "Could not preallocate new data");
2402             } else if (current_length == 0) {
2403                 /*
2404                  * posix_fallocate() uses fallocate() if the filesystem
2405                  * supports it, or fallback to manually writing zeroes. If
2406                  * fallocate() was used, unaligned reads from the fallocated
2407                  * area in raw_probe_alignment() will succeed, hence we need to
2408                  * allocate the first block.
2409                  *
2410                  * Optimize future alignment probing; ignore failures.
2411                  */
2412                 allocate_first_block(fd, offset);
2413             }
2414         } else {
2415             result = 0;
2416         }
2417         goto out;
2418 #endif
2419     case PREALLOC_MODE_FULL:
2420     {
2421         int64_t num = 0, left = offset - current_length;
2422         off_t seek_result;
2423 
2424         /*
2425          * Knowing the final size from the beginning could allow the file
2426          * system driver to do less allocations and possibly avoid
2427          * fragmentation of the file.
2428          */
2429         if (ftruncate(fd, offset) != 0) {
2430             result = -errno;
2431             error_setg_errno(errp, -result, "Could not resize file");
2432             goto out;
2433         }
2434 
2435         buf = g_malloc0(65536);
2436 
2437         seek_result = lseek(fd, current_length, SEEK_SET);
2438         if (seek_result < 0) {
2439             result = -errno;
2440             error_setg_errno(errp, -result,
2441                              "Failed to seek to the old end of file");
2442             goto out;
2443         }
2444 
2445         while (left > 0) {
2446             num = MIN(left, 65536);
2447             result = write(fd, buf, num);
2448             if (result < 0) {
2449                 if (errno == EINTR) {
2450                     continue;
2451                 }
2452                 result = -errno;
2453                 error_setg_errno(errp, -result,
2454                                  "Could not write zeros for preallocation");
2455                 goto out;
2456             }
2457             left -= result;
2458         }
2459         if (result >= 0) {
2460             result = fsync(fd);
2461             if (result < 0) {
2462                 result = -errno;
2463                 error_setg_errno(errp, -result,
2464                                  "Could not flush file to disk");
2465                 goto out;
2466             }
2467         }
2468         goto out;
2469     }
2470     case PREALLOC_MODE_OFF:
2471         if (ftruncate(fd, offset) != 0) {
2472             result = -errno;
2473             error_setg_errno(errp, -result, "Could not resize file");
2474         } else if (current_length == 0 && offset > current_length) {
2475             /* Optimize future alignment probing; ignore failures. */
2476             allocate_first_block(fd, offset);
2477         }
2478         return result;
2479     default:
2480         result = -ENOTSUP;
2481         error_setg(errp, "Unsupported preallocation mode: %s",
2482                    PreallocMode_str(prealloc));
2483         return result;
2484     }
2485 
2486 out:
2487     if (result < 0) {
2488         if (ftruncate(fd, current_length) < 0) {
2489             error_report("Failed to restore old file length: %s",
2490                          strerror(errno));
2491         }
2492     }
2493 
2494     g_free(buf);
2495     return result;
2496 }
2497 
raw_thread_pool_submit(ThreadPoolFunc func,void * arg)2498 static int coroutine_fn raw_thread_pool_submit(ThreadPoolFunc func, void *arg)
2499 {
2500     return thread_pool_submit_co(func, arg);
2501 }
2502 
2503 /*
2504  * Check if all memory in this vector is sector aligned.
2505  */
bdrv_qiov_is_aligned(BlockDriverState * bs,QEMUIOVector * qiov)2506 static bool bdrv_qiov_is_aligned(BlockDriverState *bs, QEMUIOVector *qiov)
2507 {
2508     int i;
2509     size_t alignment = bdrv_min_mem_align(bs);
2510     size_t len = bs->bl.request_alignment;
2511     IO_CODE();
2512 
2513     for (i = 0; i < qiov->niov; i++) {
2514         if ((uintptr_t) qiov->iov[i].iov_base % alignment) {
2515             return false;
2516         }
2517         if (qiov->iov[i].iov_len % len) {
2518             return false;
2519         }
2520     }
2521 
2522     return true;
2523 }
2524 
2525 #ifdef CONFIG_LINUX_IO_URING
raw_check_linux_io_uring(BDRVRawState * s)2526 static inline bool raw_check_linux_io_uring(BDRVRawState *s)
2527 {
2528     Error *local_err = NULL;
2529     AioContext *ctx;
2530 
2531     if (!s->use_linux_io_uring) {
2532         return false;
2533     }
2534 
2535     ctx = qemu_get_current_aio_context();
2536     if (unlikely(!aio_setup_linux_io_uring(ctx, &local_err))) {
2537         error_reportf_err(local_err, "Unable to use linux io_uring, "
2538                                      "falling back to thread pool: ");
2539         s->use_linux_io_uring = false;
2540         return false;
2541     }
2542     return true;
2543 }
2544 #endif
2545 
2546 #ifdef CONFIG_LINUX_AIO
raw_check_linux_aio(BDRVRawState * s)2547 static inline bool raw_check_linux_aio(BDRVRawState *s)
2548 {
2549     Error *local_err = NULL;
2550     AioContext *ctx;
2551 
2552     if (!s->use_linux_aio) {
2553         return false;
2554     }
2555 
2556     ctx = qemu_get_current_aio_context();
2557     if (unlikely(!aio_setup_linux_aio(ctx, &local_err))) {
2558         error_reportf_err(local_err, "Unable to use Linux AIO, "
2559                                      "falling back to thread pool: ");
2560         s->use_linux_aio = false;
2561         return false;
2562     }
2563     return true;
2564 }
2565 #endif
2566 
raw_co_prw(BlockDriverState * bs,int64_t * offset_ptr,uint64_t bytes,QEMUIOVector * qiov,int type,int flags)2567 static int coroutine_fn raw_co_prw(BlockDriverState *bs, int64_t *offset_ptr,
2568                                    uint64_t bytes, QEMUIOVector *qiov, int type,
2569                                    int flags)
2570 {
2571     BDRVRawState *s = bs->opaque;
2572     RawPosixAIOData acb;
2573     int ret;
2574     uint64_t offset = *offset_ptr;
2575 
2576     if (fd_open(bs) < 0)
2577         return -EIO;
2578 #if defined(CONFIG_BLKZONED)
2579     if ((type & (QEMU_AIO_WRITE | QEMU_AIO_ZONE_APPEND)) &&
2580         bs->bl.zoned != BLK_Z_NONE) {
2581         qemu_co_mutex_lock(&bs->wps->colock);
2582         if (type & QEMU_AIO_ZONE_APPEND) {
2583             int index = offset / bs->bl.zone_size;
2584             offset = bs->wps->wp[index];
2585         }
2586     }
2587 #endif
2588 
2589     /*
2590      * When using O_DIRECT, the request must be aligned to be able to use
2591      * either libaio or io_uring interface. If not fail back to regular thread
2592      * pool read/write code which emulates this for us if we
2593      * set QEMU_AIO_MISALIGNED.
2594      */
2595     if (s->needs_alignment && !bdrv_qiov_is_aligned(bs, qiov)) {
2596         type |= QEMU_AIO_MISALIGNED;
2597 #ifdef CONFIG_LINUX_IO_URING
2598     } else if (raw_check_linux_io_uring(s)) {
2599         assert(qiov->size == bytes);
2600         ret = luring_co_submit(bs, s->fd, offset, qiov, type, flags);
2601         goto out;
2602 #endif
2603 #ifdef CONFIG_LINUX_AIO
2604     } else if (raw_check_linux_aio(s)) {
2605         assert(qiov->size == bytes);
2606         ret = laio_co_submit(s->fd, offset, qiov, type, flags,
2607                               s->aio_max_batch);
2608         goto out;
2609 #endif
2610     }
2611 
2612     acb = (RawPosixAIOData) {
2613         .bs             = bs,
2614         .aio_fildes     = s->fd,
2615         .aio_type       = type,
2616         .aio_offset     = offset,
2617         .aio_nbytes     = bytes,
2618         .io             = {
2619             .iov            = qiov->iov,
2620             .niov           = qiov->niov,
2621         },
2622     };
2623 
2624     assert(qiov->size == bytes);
2625     ret = raw_thread_pool_submit(handle_aiocb_rw, &acb);
2626     if (ret == 0 && (flags & BDRV_REQ_FUA)) {
2627         /* TODO Use pwritev2() instead if it's available */
2628         ret = raw_co_flush_to_disk(bs);
2629     }
2630     goto out; /* Avoid the compiler err of unused label */
2631 
2632 out:
2633 #if defined(CONFIG_BLKZONED)
2634     if ((type & (QEMU_AIO_WRITE | QEMU_AIO_ZONE_APPEND)) &&
2635         bs->bl.zoned != BLK_Z_NONE) {
2636         BlockZoneWps *wps = bs->wps;
2637         if (ret == 0) {
2638             uint64_t *wp = &wps->wp[offset / bs->bl.zone_size];
2639             if (!BDRV_ZT_IS_CONV(*wp)) {
2640                 if (type & QEMU_AIO_ZONE_APPEND) {
2641                     *offset_ptr = *wp;
2642                     trace_zbd_zone_append_complete(bs, *offset_ptr
2643                         >> BDRV_SECTOR_BITS);
2644                 }
2645                 /* Advance the wp if needed */
2646                 if (offset + bytes > *wp) {
2647                     *wp = offset + bytes;
2648                 }
2649             }
2650         } else {
2651             /*
2652              * write and append write are not allowed to cross zone boundaries
2653              */
2654             update_zones_wp(bs, s->fd, offset, 1);
2655         }
2656 
2657         qemu_co_mutex_unlock(&wps->colock);
2658     }
2659 #endif
2660     return ret;
2661 }
2662 
raw_co_preadv(BlockDriverState * bs,int64_t offset,int64_t bytes,QEMUIOVector * qiov,BdrvRequestFlags flags)2663 static int coroutine_fn raw_co_preadv(BlockDriverState *bs, int64_t offset,
2664                                       int64_t bytes, QEMUIOVector *qiov,
2665                                       BdrvRequestFlags flags)
2666 {
2667     return raw_co_prw(bs, &offset, bytes, qiov, QEMU_AIO_READ, flags);
2668 }
2669 
raw_co_pwritev(BlockDriverState * bs,int64_t offset,int64_t bytes,QEMUIOVector * qiov,BdrvRequestFlags flags)2670 static int coroutine_fn raw_co_pwritev(BlockDriverState *bs, int64_t offset,
2671                                        int64_t bytes, QEMUIOVector *qiov,
2672                                        BdrvRequestFlags flags)
2673 {
2674     return raw_co_prw(bs, &offset, bytes, qiov, QEMU_AIO_WRITE, flags);
2675 }
2676 
raw_co_flush_to_disk(BlockDriverState * bs)2677 static int coroutine_fn raw_co_flush_to_disk(BlockDriverState *bs)
2678 {
2679     BDRVRawState *s = bs->opaque;
2680     RawPosixAIOData acb;
2681     int ret;
2682 
2683     ret = fd_open(bs);
2684     if (ret < 0) {
2685         return ret;
2686     }
2687 
2688     acb = (RawPosixAIOData) {
2689         .bs             = bs,
2690         .aio_fildes     = s->fd,
2691         .aio_type       = QEMU_AIO_FLUSH,
2692     };
2693 
2694 #ifdef CONFIG_LINUX_IO_URING
2695     if (raw_check_linux_io_uring(s)) {
2696         return luring_co_submit(bs, s->fd, 0, NULL, QEMU_AIO_FLUSH, 0);
2697     }
2698 #endif
2699 #ifdef CONFIG_LINUX_AIO
2700     if (s->has_laio_fdsync && raw_check_linux_aio(s)) {
2701         return laio_co_submit(s->fd, 0, NULL, QEMU_AIO_FLUSH, 0, 0);
2702     }
2703 #endif
2704     return raw_thread_pool_submit(handle_aiocb_flush, &acb);
2705 }
2706 
raw_close(BlockDriverState * bs)2707 static void raw_close(BlockDriverState *bs)
2708 {
2709     BDRVRawState *s = bs->opaque;
2710 
2711     if (s->fd >= 0) {
2712 #if defined(CONFIG_BLKZONED)
2713         g_free(bs->wps);
2714 #endif
2715         qemu_close(s->fd);
2716         s->fd = -1;
2717     }
2718 }
2719 
2720 /**
2721  * Truncates the given regular file @fd to @offset and, when growing, fills the
2722  * new space according to @prealloc.
2723  *
2724  * Returns: 0 on success, -errno on failure.
2725  */
2726 static int coroutine_fn
raw_regular_truncate(BlockDriverState * bs,int fd,int64_t offset,PreallocMode prealloc,Error ** errp)2727 raw_regular_truncate(BlockDriverState *bs, int fd, int64_t offset,
2728                      PreallocMode prealloc, Error **errp)
2729 {
2730     RawPosixAIOData acb;
2731 
2732     acb = (RawPosixAIOData) {
2733         .bs             = bs,
2734         .aio_fildes     = fd,
2735         .aio_type       = QEMU_AIO_TRUNCATE,
2736         .aio_offset     = offset,
2737         .truncate       = {
2738             .prealloc       = prealloc,
2739             .errp           = errp,
2740         },
2741     };
2742 
2743     return raw_thread_pool_submit(handle_aiocb_truncate, &acb);
2744 }
2745 
raw_co_truncate(BlockDriverState * bs,int64_t offset,bool exact,PreallocMode prealloc,BdrvRequestFlags flags,Error ** errp)2746 static int coroutine_fn raw_co_truncate(BlockDriverState *bs, int64_t offset,
2747                                         bool exact, PreallocMode prealloc,
2748                                         BdrvRequestFlags flags, Error **errp)
2749 {
2750     BDRVRawState *s = bs->opaque;
2751     struct stat st;
2752     int ret;
2753 
2754     if (fstat(s->fd, &st)) {
2755         ret = -errno;
2756         error_setg_errno(errp, -ret, "Failed to fstat() the file");
2757         return ret;
2758     }
2759 
2760     if (S_ISREG(st.st_mode)) {
2761         /* Always resizes to the exact @offset */
2762         return raw_regular_truncate(bs, s->fd, offset, prealloc, errp);
2763     }
2764 
2765     if (prealloc != PREALLOC_MODE_OFF) {
2766         error_setg(errp, "Preallocation mode '%s' unsupported for this "
2767                    "non-regular file", PreallocMode_str(prealloc));
2768         return -ENOTSUP;
2769     }
2770 
2771     if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
2772         int64_t cur_length = raw_getlength(bs);
2773 
2774         if (offset != cur_length && exact) {
2775             error_setg(errp, "Cannot resize device files");
2776             return -ENOTSUP;
2777         } else if (offset > cur_length) {
2778             error_setg(errp, "Cannot grow device files");
2779             return -EINVAL;
2780         }
2781     } else {
2782         error_setg(errp, "Resizing this file is not supported");
2783         return -ENOTSUP;
2784     }
2785 
2786     return 0;
2787 }
2788 
2789 #ifdef __OpenBSD__
raw_getlength(BlockDriverState * bs)2790 static int64_t raw_getlength(BlockDriverState *bs)
2791 {
2792     BDRVRawState *s = bs->opaque;
2793     int fd = s->fd;
2794     struct stat st;
2795 
2796     if (fstat(fd, &st))
2797         return -errno;
2798     if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
2799         struct disklabel dl;
2800 
2801         if (ioctl(fd, DIOCGDINFO, &dl))
2802             return -errno;
2803         return (uint64_t)dl.d_secsize *
2804             dl.d_partitions[DISKPART(st.st_rdev)].p_size;
2805     } else
2806         return st.st_size;
2807 }
2808 #elif defined(__NetBSD__)
raw_getlength(BlockDriverState * bs)2809 static int64_t raw_getlength(BlockDriverState *bs)
2810 {
2811     BDRVRawState *s = bs->opaque;
2812     int fd = s->fd;
2813     struct stat st;
2814 
2815     if (fstat(fd, &st))
2816         return -errno;
2817     if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
2818         struct dkwedge_info dkw;
2819 
2820         if (ioctl(fd, DIOCGWEDGEINFO, &dkw) != -1) {
2821             return dkw.dkw_size * 512;
2822         } else {
2823             struct disklabel dl;
2824 
2825             if (ioctl(fd, DIOCGDINFO, &dl))
2826                 return -errno;
2827             return (uint64_t)dl.d_secsize *
2828                 dl.d_partitions[DISKPART(st.st_rdev)].p_size;
2829         }
2830     } else
2831         return st.st_size;
2832 }
2833 #elif defined(__sun__)
raw_getlength(BlockDriverState * bs)2834 static int64_t raw_getlength(BlockDriverState *bs)
2835 {
2836     BDRVRawState *s = bs->opaque;
2837     struct dk_minfo minfo;
2838     int ret;
2839     int64_t size;
2840 
2841     ret = fd_open(bs);
2842     if (ret < 0) {
2843         return ret;
2844     }
2845 
2846     /*
2847      * Use the DKIOCGMEDIAINFO ioctl to read the size.
2848      */
2849     ret = ioctl(s->fd, DKIOCGMEDIAINFO, &minfo);
2850     if (ret != -1) {
2851         return minfo.dki_lbsize * minfo.dki_capacity;
2852     }
2853 
2854     /*
2855      * There are reports that lseek on some devices fails, but
2856      * irc discussion said that contingency on contingency was overkill.
2857      */
2858     size = lseek(s->fd, 0, SEEK_END);
2859     if (size < 0) {
2860         return -errno;
2861     }
2862     return size;
2863 }
2864 #elif defined(CONFIG_BSD)
raw_getlength(BlockDriverState * bs)2865 static int64_t raw_getlength(BlockDriverState *bs)
2866 {
2867     BDRVRawState *s = bs->opaque;
2868     int fd = s->fd;
2869     int64_t size;
2870     struct stat sb;
2871 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
2872     int reopened = 0;
2873 #endif
2874     int ret;
2875 
2876     ret = fd_open(bs);
2877     if (ret < 0)
2878         return ret;
2879 
2880 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
2881 again:
2882 #endif
2883     if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
2884         size = 0;
2885 #ifdef DIOCGMEDIASIZE
2886         if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size)) {
2887             size = 0;
2888         }
2889 #endif
2890 #ifdef DIOCGPART
2891         if (size == 0) {
2892             struct partinfo pi;
2893             if (ioctl(fd, DIOCGPART, &pi) == 0) {
2894                 size = pi.media_size;
2895             }
2896         }
2897 #endif
2898 #if defined(DKIOCGETBLOCKCOUNT) && defined(DKIOCGETBLOCKSIZE)
2899         if (size == 0) {
2900             uint64_t sectors = 0;
2901             uint32_t sector_size = 0;
2902 
2903             if (ioctl(fd, DKIOCGETBLOCKCOUNT, &sectors) == 0
2904                && ioctl(fd, DKIOCGETBLOCKSIZE, &sector_size) == 0) {
2905                 size = sectors * sector_size;
2906             }
2907         }
2908 #endif
2909         if (size == 0) {
2910             size = lseek(fd, 0LL, SEEK_END);
2911         }
2912         if (size < 0) {
2913             return -errno;
2914         }
2915 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
2916         switch(s->type) {
2917         case FTYPE_CD:
2918             /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
2919             if (size == 2048LL * (unsigned)-1)
2920                 size = 0;
2921             /* XXX no disc?  maybe we need to reopen... */
2922             if (size <= 0 && !reopened && cdrom_reopen(bs) >= 0) {
2923                 reopened = 1;
2924                 goto again;
2925             }
2926         }
2927 #endif
2928     } else {
2929         size = lseek(fd, 0, SEEK_END);
2930         if (size < 0) {
2931             return -errno;
2932         }
2933     }
2934     return size;
2935 }
2936 #else
raw_getlength(BlockDriverState * bs)2937 static int64_t raw_getlength(BlockDriverState *bs)
2938 {
2939     BDRVRawState *s = bs->opaque;
2940     int ret;
2941     int64_t size;
2942 
2943     ret = fd_open(bs);
2944     if (ret < 0) {
2945         return ret;
2946     }
2947 
2948     size = lseek(s->fd, 0, SEEK_END);
2949     if (size < 0) {
2950         return -errno;
2951     }
2952     return size;
2953 }
2954 #endif
2955 
raw_co_getlength(BlockDriverState * bs)2956 static int64_t coroutine_fn raw_co_getlength(BlockDriverState *bs)
2957 {
2958     return raw_getlength(bs);
2959 }
2960 
raw_co_get_allocated_file_size(BlockDriverState * bs)2961 static int64_t coroutine_fn raw_co_get_allocated_file_size(BlockDriverState *bs)
2962 {
2963     struct stat st;
2964     BDRVRawState *s = bs->opaque;
2965 
2966     if (fstat(s->fd, &st) < 0) {
2967         return -errno;
2968     }
2969     return (int64_t)st.st_blocks * 512;
2970 }
2971 
2972 static int coroutine_fn
raw_co_create(BlockdevCreateOptions * options,Error ** errp)2973 raw_co_create(BlockdevCreateOptions *options, Error **errp)
2974 {
2975     BlockdevCreateOptionsFile *file_opts;
2976     Error *local_err = NULL;
2977     int fd;
2978     uint64_t perm, shared;
2979     int result = 0;
2980 
2981     /* Validate options and set default values */
2982     assert(options->driver == BLOCKDEV_DRIVER_FILE);
2983     file_opts = &options->u.file;
2984 
2985     if (!file_opts->has_nocow) {
2986         file_opts->nocow = false;
2987     }
2988     if (!file_opts->has_preallocation) {
2989         file_opts->preallocation = PREALLOC_MODE_OFF;
2990     }
2991     if (!file_opts->has_extent_size_hint) {
2992         file_opts->extent_size_hint = 1 * MiB;
2993     }
2994     if (file_opts->extent_size_hint > UINT32_MAX) {
2995         result = -EINVAL;
2996         error_setg(errp, "Extent size hint is too large");
2997         goto out;
2998     }
2999 
3000     /* Create file */
3001     fd = qemu_create(file_opts->filename, O_RDWR | O_BINARY, 0644, errp);
3002     if (fd < 0) {
3003         result = -errno;
3004         goto out;
3005     }
3006 
3007     /* Take permissions: We want to discard everything, so we need
3008      * BLK_PERM_WRITE; and truncation to the desired size requires
3009      * BLK_PERM_RESIZE.
3010      * On the other hand, we cannot share the RESIZE permission
3011      * because we promise that after this function, the file has the
3012      * size given in the options.  If someone else were to resize it
3013      * concurrently, we could not guarantee that.
3014      * Note that after this function, we can no longer guarantee that
3015      * the file is not touched by a third party, so it may be resized
3016      * then. */
3017     perm = BLK_PERM_WRITE | BLK_PERM_RESIZE;
3018     shared = BLK_PERM_ALL & ~BLK_PERM_RESIZE;
3019 
3020     /* Step one: Take locks */
3021     result = raw_apply_lock_bytes(NULL, fd, perm, ~shared, false, errp);
3022     if (result < 0) {
3023         goto out_close;
3024     }
3025 
3026     /* Step two: Check that nobody else has taken conflicting locks */
3027     result = raw_check_lock_bytes(fd, perm, shared, errp);
3028     if (result < 0) {
3029         error_append_hint(errp,
3030                           "Is another process using the image [%s]?\n",
3031                           file_opts->filename);
3032         goto out_unlock;
3033     }
3034 
3035     /* Clear the file by truncating it to 0 */
3036     result = raw_regular_truncate(NULL, fd, 0, PREALLOC_MODE_OFF, errp);
3037     if (result < 0) {
3038         goto out_unlock;
3039     }
3040 
3041     if (file_opts->nocow) {
3042 #ifdef __linux__
3043         /* Set NOCOW flag to solve performance issue on fs like btrfs.
3044          * This is an optimisation. The FS_IOC_SETFLAGS ioctl return value
3045          * will be ignored since any failure of this operation should not
3046          * block the left work.
3047          */
3048         int attr;
3049         if (ioctl(fd, FS_IOC_GETFLAGS, &attr) == 0) {
3050             attr |= FS_NOCOW_FL;
3051             ioctl(fd, FS_IOC_SETFLAGS, &attr);
3052         }
3053 #endif
3054     }
3055 #ifdef FS_IOC_FSSETXATTR
3056     /*
3057      * Try to set the extent size hint. Failure is not fatal, and a warning is
3058      * only printed if the option was explicitly specified.
3059      */
3060     {
3061         struct fsxattr attr;
3062         result = ioctl(fd, FS_IOC_FSGETXATTR, &attr);
3063         if (result == 0) {
3064             attr.fsx_xflags |= FS_XFLAG_EXTSIZE;
3065             attr.fsx_extsize = file_opts->extent_size_hint;
3066             result = ioctl(fd, FS_IOC_FSSETXATTR, &attr);
3067         }
3068         if (result < 0 && file_opts->has_extent_size_hint &&
3069             file_opts->extent_size_hint)
3070         {
3071             warn_report("Failed to set extent size hint: %s",
3072                         strerror(errno));
3073         }
3074     }
3075 #endif
3076 
3077     /* Resize and potentially preallocate the file to the desired
3078      * final size */
3079     result = raw_regular_truncate(NULL, fd, file_opts->size,
3080                                   file_opts->preallocation, errp);
3081     if (result < 0) {
3082         goto out_unlock;
3083     }
3084 
3085 out_unlock:
3086     raw_apply_lock_bytes(NULL, fd, 0, 0, true, &local_err);
3087     if (local_err) {
3088         /* The above call should not fail, and if it does, that does
3089          * not mean the whole creation operation has failed.  So
3090          * report it the user for their convenience, but do not report
3091          * it to the caller. */
3092         warn_report_err(local_err);
3093     }
3094 
3095 out_close:
3096     if (qemu_close(fd) != 0 && result == 0) {
3097         result = -errno;
3098         error_setg_errno(errp, -result, "Could not close the new file");
3099     }
3100 out:
3101     return result;
3102 }
3103 
3104 static int coroutine_fn GRAPH_RDLOCK
raw_co_create_opts(BlockDriver * drv,const char * filename,QemuOpts * opts,Error ** errp)3105 raw_co_create_opts(BlockDriver *drv, const char *filename,
3106                    QemuOpts *opts, Error **errp)
3107 {
3108     BlockdevCreateOptions options;
3109     int64_t total_size = 0;
3110     int64_t extent_size_hint = 0;
3111     bool has_extent_size_hint = false;
3112     bool nocow = false;
3113     PreallocMode prealloc;
3114     char *buf = NULL;
3115     Error *local_err = NULL;
3116 
3117     /* Skip file: protocol prefix */
3118     strstart(filename, "file:", &filename);
3119 
3120     /* Read out options */
3121     total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
3122                           BDRV_SECTOR_SIZE);
3123     if (qemu_opt_get(opts, BLOCK_OPT_EXTENT_SIZE_HINT)) {
3124         has_extent_size_hint = true;
3125         extent_size_hint =
3126             qemu_opt_get_size_del(opts, BLOCK_OPT_EXTENT_SIZE_HINT, -1);
3127     }
3128     nocow = qemu_opt_get_bool(opts, BLOCK_OPT_NOCOW, false);
3129     buf = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC);
3130     prealloc = qapi_enum_parse(&PreallocMode_lookup, buf,
3131                                PREALLOC_MODE_OFF, &local_err);
3132     g_free(buf);
3133     if (local_err) {
3134         error_propagate(errp, local_err);
3135         return -EINVAL;
3136     }
3137 
3138     options = (BlockdevCreateOptions) {
3139         .driver     = BLOCKDEV_DRIVER_FILE,
3140         .u.file     = {
3141             .filename           = (char *) filename,
3142             .size               = total_size,
3143             .has_preallocation  = true,
3144             .preallocation      = prealloc,
3145             .has_nocow          = true,
3146             .nocow              = nocow,
3147             .has_extent_size_hint = has_extent_size_hint,
3148             .extent_size_hint   = extent_size_hint,
3149         },
3150     };
3151     return raw_co_create(&options, errp);
3152 }
3153 
raw_co_delete_file(BlockDriverState * bs,Error ** errp)3154 static int coroutine_fn raw_co_delete_file(BlockDriverState *bs,
3155                                            Error **errp)
3156 {
3157     struct stat st;
3158     int ret;
3159 
3160     if (!(stat(bs->filename, &st) == 0) || !S_ISREG(st.st_mode)) {
3161         error_setg_errno(errp, ENOENT, "%s is not a regular file",
3162                          bs->filename);
3163         return -ENOENT;
3164     }
3165 
3166     ret = unlink(bs->filename);
3167     if (ret < 0) {
3168         ret = -errno;
3169         error_setg_errno(errp, -ret, "Error when deleting file %s",
3170                          bs->filename);
3171     }
3172 
3173     return ret;
3174 }
3175 
3176 /*
3177  * Find allocation range in @bs around offset @start.
3178  * May change underlying file descriptor's file offset.
3179  * If @start is not in a hole, store @start in @data, and the
3180  * beginning of the next hole in @hole, and return 0.
3181  * If @start is in a non-trailing hole, store @start in @hole and the
3182  * beginning of the next non-hole in @data, and return 0.
3183  * If @start is in a trailing hole or beyond EOF, return -ENXIO.
3184  * If we can't find out, return a negative errno other than -ENXIO.
3185  */
find_allocation(BlockDriverState * bs,off_t start,off_t * data,off_t * hole)3186 static int find_allocation(BlockDriverState *bs, off_t start,
3187                            off_t *data, off_t *hole)
3188 {
3189 #if defined SEEK_HOLE && defined SEEK_DATA
3190     BDRVRawState *s = bs->opaque;
3191     off_t offs;
3192 
3193     /*
3194      * SEEK_DATA cases:
3195      * D1. offs == start: start is in data
3196      * D2. offs > start: start is in a hole, next data at offs
3197      * D3. offs < 0, errno = ENXIO: either start is in a trailing hole
3198      *                              or start is beyond EOF
3199      *     If the latter happens, the file has been truncated behind
3200      *     our back since we opened it.  All bets are off then.
3201      *     Treating like a trailing hole is simplest.
3202      * D4. offs < 0, errno != ENXIO: we learned nothing
3203      */
3204     offs = lseek(s->fd, start, SEEK_DATA);
3205     if (offs < 0) {
3206         return -errno;          /* D3 or D4 */
3207     }
3208 
3209     if (offs < start) {
3210         /* This is not a valid return by lseek().  We are safe to just return
3211          * -EIO in this case, and we'll treat it like D4. */
3212         return -EIO;
3213     }
3214 
3215     if (offs > start) {
3216         /* D2: in hole, next data at offs */
3217         *hole = start;
3218         *data = offs;
3219         return 0;
3220     }
3221 
3222     /* D1: in data, end not yet known */
3223 
3224     /*
3225      * SEEK_HOLE cases:
3226      * H1. offs == start: start is in a hole
3227      *     If this happens here, a hole has been dug behind our back
3228      *     since the previous lseek().
3229      * H2. offs > start: either start is in data, next hole at offs,
3230      *                   or start is in trailing hole, EOF at offs
3231      *     Linux treats trailing holes like any other hole: offs ==
3232      *     start.  Solaris seeks to EOF instead: offs > start (blech).
3233      *     If that happens here, a hole has been dug behind our back
3234      *     since the previous lseek().
3235      * H3. offs < 0, errno = ENXIO: start is beyond EOF
3236      *     If this happens, the file has been truncated behind our
3237      *     back since we opened it.  Treat it like a trailing hole.
3238      * H4. offs < 0, errno != ENXIO: we learned nothing
3239      *     Pretend we know nothing at all, i.e. "forget" about D1.
3240      */
3241     offs = lseek(s->fd, start, SEEK_HOLE);
3242     if (offs < 0) {
3243         return -errno;          /* D1 and (H3 or H4) */
3244     }
3245 
3246     if (offs < start) {
3247         /* This is not a valid return by lseek().  We are safe to just return
3248          * -EIO in this case, and we'll treat it like H4. */
3249         return -EIO;
3250     }
3251 
3252     if (offs > start) {
3253         /*
3254          * D1 and H2: either in data, next hole at offs, or it was in
3255          * data but is now in a trailing hole.  In the latter case,
3256          * all bets are off.  Treating it as if it there was data all
3257          * the way to EOF is safe, so simply do that.
3258          */
3259         *data = start;
3260         *hole = offs;
3261         return 0;
3262     }
3263 
3264     /* D1 and H1 */
3265     return -EBUSY;
3266 #else
3267     return -ENOTSUP;
3268 #endif
3269 }
3270 
3271 /*
3272  * Returns the allocation status of the specified offset.
3273  *
3274  * The block layer guarantees 'offset' and 'bytes' are within bounds.
3275  *
3276  * 'pnum' is set to the number of bytes (including and immediately following
3277  * the specified offset) that are known to be in the same
3278  * allocated/unallocated state.
3279  *
3280  * 'bytes' is a soft cap for 'pnum'.  If the information is free, 'pnum' may
3281  * well exceed it.
3282  */
raw_co_block_status(BlockDriverState * bs,unsigned int mode,int64_t offset,int64_t bytes,int64_t * pnum,int64_t * map,BlockDriverState ** file)3283 static int coroutine_fn raw_co_block_status(BlockDriverState *bs,
3284                                             unsigned int mode,
3285                                             int64_t offset,
3286                                             int64_t bytes, int64_t *pnum,
3287                                             int64_t *map,
3288                                             BlockDriverState **file)
3289 {
3290     off_t data = 0, hole = 0;
3291     int ret;
3292 
3293     assert(QEMU_IS_ALIGNED(offset | bytes, bs->bl.request_alignment));
3294 
3295     ret = fd_open(bs);
3296     if (ret < 0) {
3297         return ret;
3298     }
3299 
3300     if (!(mode & BDRV_WANT_ZERO)) {
3301         /* There is no backing file - all bytes are allocated in this file.  */
3302         *pnum = bytes;
3303         *map = offset;
3304         *file = bs;
3305         return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID;
3306     }
3307 
3308     ret = find_allocation(bs, offset, &data, &hole);
3309     if (ret == -ENXIO) {
3310         /* Trailing hole */
3311         *pnum = bytes;
3312         ret = BDRV_BLOCK_ZERO;
3313     } else if (ret < 0) {
3314         /* No info available, so pretend there are no holes */
3315         *pnum = bytes;
3316         ret = BDRV_BLOCK_DATA;
3317     } else if (data == offset) {
3318         /* On a data extent, compute bytes to the end of the extent,
3319          * possibly including a partial sector at EOF. */
3320         *pnum = hole - offset;
3321 
3322         /*
3323          * We are not allowed to return partial sectors, though, so
3324          * round up if necessary.
3325          */
3326         if (!QEMU_IS_ALIGNED(*pnum, bs->bl.request_alignment)) {
3327             int64_t file_length = raw_getlength(bs);
3328             if (file_length > 0) {
3329                 /* Ignore errors, this is just a safeguard */
3330                 assert(hole == file_length);
3331             }
3332             *pnum = ROUND_UP(*pnum, bs->bl.request_alignment);
3333         }
3334 
3335         ret = BDRV_BLOCK_DATA;
3336     } else {
3337         /* On a hole, compute bytes to the beginning of the next extent.  */
3338         assert(hole == offset);
3339         *pnum = data - offset;
3340         ret = BDRV_BLOCK_ZERO;
3341     }
3342     *map = offset;
3343     *file = bs;
3344     return ret | BDRV_BLOCK_OFFSET_VALID;
3345 }
3346 
3347 #if defined(__linux__)
3348 /* Verify that the file is not in the page cache */
check_cache_dropped(BlockDriverState * bs,Error ** errp)3349 static void check_cache_dropped(BlockDriverState *bs, Error **errp)
3350 {
3351     const size_t window_size = 128 * 1024 * 1024;
3352     BDRVRawState *s = bs->opaque;
3353     void *window = NULL;
3354     size_t length = 0;
3355     unsigned char *vec;
3356     size_t page_size;
3357     off_t offset;
3358     off_t end;
3359 
3360     /* mincore(2) page status information requires 1 byte per page */
3361     page_size = sysconf(_SC_PAGESIZE);
3362     vec = g_malloc(DIV_ROUND_UP(window_size, page_size));
3363 
3364     end = raw_getlength(bs);
3365 
3366     for (offset = 0; offset < end; offset += window_size) {
3367         void *new_window;
3368         size_t new_length;
3369         size_t vec_end;
3370         size_t i;
3371         int ret;
3372 
3373         /* Unmap previous window if size has changed */
3374         new_length = MIN(end - offset, window_size);
3375         if (new_length != length) {
3376             munmap(window, length);
3377             window = NULL;
3378             length = 0;
3379         }
3380 
3381         new_window = mmap(window, new_length, PROT_NONE, MAP_PRIVATE,
3382                           s->fd, offset);
3383         if (new_window == MAP_FAILED) {
3384             error_setg_errno(errp, errno, "mmap failed");
3385             break;
3386         }
3387 
3388         window = new_window;
3389         length = new_length;
3390 
3391         ret = mincore(window, length, vec);
3392         if (ret < 0) {
3393             error_setg_errno(errp, errno, "mincore failed");
3394             break;
3395         }
3396 
3397         vec_end = DIV_ROUND_UP(length, page_size);
3398         for (i = 0; i < vec_end; i++) {
3399             if (vec[i] & 0x1) {
3400                 break;
3401             }
3402         }
3403         if (i < vec_end) {
3404             error_setg(errp, "page cache still in use!");
3405             break;
3406         }
3407     }
3408 
3409     if (window) {
3410         munmap(window, length);
3411     }
3412 
3413     g_free(vec);
3414 }
3415 #endif /* __linux__ */
3416 
3417 static void coroutine_fn GRAPH_RDLOCK
raw_co_invalidate_cache(BlockDriverState * bs,Error ** errp)3418 raw_co_invalidate_cache(BlockDriverState *bs, Error **errp)
3419 {
3420     BDRVRawState *s = bs->opaque;
3421     int ret;
3422 
3423     ret = fd_open(bs);
3424     if (ret < 0) {
3425         error_setg_errno(errp, -ret, "The file descriptor is not open");
3426         return;
3427     }
3428 
3429     if (!s->drop_cache) {
3430         return;
3431     }
3432 
3433     if (s->open_flags & O_DIRECT) {
3434         return; /* No host kernel page cache */
3435     }
3436 
3437 #if defined(__linux__)
3438     /* This sets the scene for the next syscall... */
3439     ret = bdrv_co_flush(bs);
3440     if (ret < 0) {
3441         error_setg_errno(errp, -ret, "flush failed");
3442         return;
3443     }
3444 
3445     /* Linux does not invalidate pages that are dirty, locked, or mmapped by a
3446      * process.  These limitations are okay because we just fsynced the file,
3447      * we don't use mmap, and the file should not be in use by other processes.
3448      */
3449     ret = posix_fadvise(s->fd, 0, 0, POSIX_FADV_DONTNEED);
3450     if (ret != 0) { /* the return value is a positive errno */
3451         error_setg_errno(errp, ret, "fadvise failed");
3452         return;
3453     }
3454 
3455     if (s->check_cache_dropped) {
3456         check_cache_dropped(bs, errp);
3457     }
3458 #else /* __linux__ */
3459     /* Do nothing.  Live migration to a remote host with cache.direct=off is
3460      * unsupported on other host operating systems.  Cache consistency issues
3461      * may occur but no error is reported here, partly because that's the
3462      * historical behavior and partly because it's hard to differentiate valid
3463      * configurations that should not cause errors.
3464      */
3465 #endif /* !__linux__ */
3466 }
3467 
raw_account_discard(BDRVRawState * s,uint64_t nbytes,int ret)3468 static void raw_account_discard(BDRVRawState *s, uint64_t nbytes, int ret)
3469 {
3470     if (ret) {
3471         s->stats.discard_nb_failed++;
3472     } else {
3473         s->stats.discard_nb_ok++;
3474         s->stats.discard_bytes_ok += nbytes;
3475     }
3476 }
3477 
3478 /*
3479  * zone report - Get a zone block device's information in the form
3480  * of an array of zone descriptors.
3481  * zones is an array of zone descriptors to hold zone information on reply;
3482  * offset can be any byte within the entire size of the device;
3483  * nr_zones is the maximum number of sectors the command should operate on.
3484  */
3485 #if defined(CONFIG_BLKZONED)
raw_co_zone_report(BlockDriverState * bs,int64_t offset,unsigned int * nr_zones,BlockZoneDescriptor * zones)3486 static int coroutine_fn raw_co_zone_report(BlockDriverState *bs, int64_t offset,
3487                                            unsigned int *nr_zones,
3488                                            BlockZoneDescriptor *zones) {
3489     BDRVRawState *s = bs->opaque;
3490     RawPosixAIOData acb = (RawPosixAIOData) {
3491         .bs         = bs,
3492         .aio_fildes = s->fd,
3493         .aio_type   = QEMU_AIO_ZONE_REPORT,
3494         .aio_offset = offset,
3495         .zone_report    = {
3496             .nr_zones       = nr_zones,
3497             .zones          = zones,
3498         },
3499     };
3500 
3501     trace_zbd_zone_report(bs, *nr_zones, offset >> BDRV_SECTOR_BITS);
3502     return raw_thread_pool_submit(handle_aiocb_zone_report, &acb);
3503 }
3504 #endif
3505 
3506 /*
3507  * zone management operations - Execute an operation on a zone
3508  */
3509 #if defined(CONFIG_BLKZONED)
raw_co_zone_mgmt(BlockDriverState * bs,BlockZoneOp op,int64_t offset,int64_t len)3510 static int coroutine_fn raw_co_zone_mgmt(BlockDriverState *bs, BlockZoneOp op,
3511         int64_t offset, int64_t len) {
3512     BDRVRawState *s = bs->opaque;
3513     RawPosixAIOData acb;
3514     int64_t zone_size, zone_size_mask;
3515     const char *op_name;
3516     unsigned long zo;
3517     int ret;
3518     BlockZoneWps *wps = bs->wps;
3519     int64_t capacity = bs->total_sectors << BDRV_SECTOR_BITS;
3520 
3521     zone_size = bs->bl.zone_size;
3522     zone_size_mask = zone_size - 1;
3523     if (offset & zone_size_mask) {
3524         error_report("sector offset %" PRId64 " is not aligned to zone size "
3525                      "%" PRId64 "", offset / 512, zone_size / 512);
3526         return -EINVAL;
3527     }
3528 
3529     if (((offset + len) < capacity && len & zone_size_mask) ||
3530         offset + len > capacity) {
3531         error_report("number of sectors %" PRId64 " is not aligned to zone size"
3532                       " %" PRId64 "", len / 512, zone_size / 512);
3533         return -EINVAL;
3534     }
3535 
3536     uint32_t i = offset / bs->bl.zone_size;
3537     uint32_t nrz = len / bs->bl.zone_size;
3538     uint64_t *wp = &wps->wp[i];
3539     if (BDRV_ZT_IS_CONV(*wp) && len != capacity) {
3540         error_report("zone mgmt operations are not allowed for conventional zones");
3541         return -EIO;
3542     }
3543 
3544     switch (op) {
3545     case BLK_ZO_OPEN:
3546         op_name = "BLKOPENZONE";
3547         zo = BLKOPENZONE;
3548         break;
3549     case BLK_ZO_CLOSE:
3550         op_name = "BLKCLOSEZONE";
3551         zo = BLKCLOSEZONE;
3552         break;
3553     case BLK_ZO_FINISH:
3554         op_name = "BLKFINISHZONE";
3555         zo = BLKFINISHZONE;
3556         break;
3557     case BLK_ZO_RESET:
3558         op_name = "BLKRESETZONE";
3559         zo = BLKRESETZONE;
3560         break;
3561     default:
3562         error_report("Unsupported zone op: 0x%x", op);
3563         return -ENOTSUP;
3564     }
3565 
3566     acb = (RawPosixAIOData) {
3567         .bs             = bs,
3568         .aio_fildes     = s->fd,
3569         .aio_type       = QEMU_AIO_ZONE_MGMT,
3570         .aio_offset     = offset,
3571         .aio_nbytes     = len,
3572         .zone_mgmt  = {
3573             .op = zo,
3574         },
3575     };
3576 
3577     trace_zbd_zone_mgmt(bs, op_name, offset >> BDRV_SECTOR_BITS,
3578                         len >> BDRV_SECTOR_BITS);
3579     ret = raw_thread_pool_submit(handle_aiocb_zone_mgmt, &acb);
3580     if (ret != 0) {
3581         update_zones_wp(bs, s->fd, offset, nrz);
3582         error_report("ioctl %s failed %d", op_name, ret);
3583         return ret;
3584     }
3585 
3586     if (zo == BLKRESETZONE && len == capacity) {
3587         ret = get_zones_wp(bs, s->fd, 0, bs->bl.nr_zones, 1);
3588         if (ret < 0) {
3589             error_report("reporting single wp failed");
3590             return ret;
3591         }
3592     } else if (zo == BLKRESETZONE) {
3593         for (unsigned int j = 0; j < nrz; ++j) {
3594             wp[j] = offset + j * zone_size;
3595         }
3596     } else if (zo == BLKFINISHZONE) {
3597         for (unsigned int j = 0; j < nrz; ++j) {
3598             /* The zoned device allows the last zone smaller that the
3599              * zone size. */
3600             wp[j] = MIN(offset + (j + 1) * zone_size, offset + len);
3601         }
3602     }
3603 
3604     return ret;
3605 }
3606 #endif
3607 
3608 #if defined(CONFIG_BLKZONED)
raw_co_zone_append(BlockDriverState * bs,int64_t * offset,QEMUIOVector * qiov,BdrvRequestFlags flags)3609 static int coroutine_fn raw_co_zone_append(BlockDriverState *bs,
3610                                            int64_t *offset,
3611                                            QEMUIOVector *qiov,
3612                                            BdrvRequestFlags flags) {
3613     assert(flags == 0);
3614     int64_t zone_size_mask = bs->bl.zone_size - 1;
3615     int64_t iov_len = 0;
3616     int64_t len = 0;
3617 
3618     if (*offset & zone_size_mask) {
3619         error_report("sector offset %" PRId64 " is not aligned to zone size "
3620                      "%" PRId32 "", *offset / 512, bs->bl.zone_size / 512);
3621         return -EINVAL;
3622     }
3623 
3624     int64_t wg = bs->bl.write_granularity;
3625     int64_t wg_mask = wg - 1;
3626     for (int i = 0; i < qiov->niov; i++) {
3627         iov_len = qiov->iov[i].iov_len;
3628         if (iov_len & wg_mask) {
3629             error_report("len of IOVector[%d] %" PRId64 " is not aligned to "
3630                          "block size %" PRId64 "", i, iov_len, wg);
3631             return -EINVAL;
3632         }
3633         len += iov_len;
3634     }
3635 
3636     trace_zbd_zone_append(bs, *offset >> BDRV_SECTOR_BITS);
3637     return raw_co_prw(bs, offset, len, qiov, QEMU_AIO_ZONE_APPEND, 0);
3638 }
3639 #endif
3640 
3641 static coroutine_fn int
raw_do_pdiscard(BlockDriverState * bs,int64_t offset,int64_t bytes,bool blkdev)3642 raw_do_pdiscard(BlockDriverState *bs, int64_t offset, int64_t bytes,
3643                 bool blkdev)
3644 {
3645     BDRVRawState *s = bs->opaque;
3646     RawPosixAIOData acb;
3647     int ret;
3648 
3649     acb = (RawPosixAIOData) {
3650         .bs             = bs,
3651         .aio_fildes     = s->fd,
3652         .aio_type       = QEMU_AIO_DISCARD,
3653         .aio_offset     = offset,
3654         .aio_nbytes     = bytes,
3655     };
3656 
3657     if (blkdev) {
3658         acb.aio_type |= QEMU_AIO_BLKDEV;
3659     }
3660 
3661     ret = raw_thread_pool_submit(handle_aiocb_discard, &acb);
3662     raw_account_discard(s, bytes, ret);
3663     return ret;
3664 }
3665 
3666 static coroutine_fn int
raw_co_pdiscard(BlockDriverState * bs,int64_t offset,int64_t bytes)3667 raw_co_pdiscard(BlockDriverState *bs, int64_t offset, int64_t bytes)
3668 {
3669     return raw_do_pdiscard(bs, offset, bytes, false);
3670 }
3671 
3672 static int coroutine_fn
raw_do_pwrite_zeroes(BlockDriverState * bs,int64_t offset,int64_t bytes,BdrvRequestFlags flags,bool blkdev)3673 raw_do_pwrite_zeroes(BlockDriverState *bs, int64_t offset, int64_t bytes,
3674                      BdrvRequestFlags flags, bool blkdev)
3675 {
3676     BDRVRawState *s = bs->opaque;
3677     RawPosixAIOData acb;
3678     ThreadPoolFunc *handler;
3679 
3680 #ifdef CONFIG_FALLOCATE
3681     if (offset + bytes > bs->total_sectors * BDRV_SECTOR_SIZE) {
3682         BdrvTrackedRequest *req;
3683 
3684         /*
3685          * This is a workaround for a bug in the Linux XFS driver,
3686          * where writes submitted through the AIO interface will be
3687          * discarded if they happen beyond a concurrently running
3688          * fallocate() that increases the file length (i.e., both the
3689          * write and the fallocate() happen beyond the EOF).
3690          *
3691          * To work around it, we extend the tracked request for this
3692          * zero write until INT64_MAX (effectively infinity), and mark
3693          * it as serializing.
3694          *
3695          * We have to enable this workaround for all filesystems and
3696          * AIO modes (not just XFS with aio=native), because for
3697          * remote filesystems we do not know the host configuration.
3698          */
3699 
3700         req = bdrv_co_get_self_request(bs);
3701         assert(req);
3702         assert(req->type == BDRV_TRACKED_WRITE);
3703         assert(req->offset <= offset);
3704         assert(req->offset + req->bytes >= offset + bytes);
3705 
3706         req->bytes = BDRV_MAX_LENGTH - req->offset;
3707 
3708         bdrv_check_request(req->offset, req->bytes, &error_abort);
3709 
3710         bdrv_make_request_serialising(req, bs->bl.request_alignment);
3711     }
3712 #endif
3713 
3714     acb = (RawPosixAIOData) {
3715         .bs             = bs,
3716         .aio_fildes     = s->fd,
3717         .aio_type       = QEMU_AIO_WRITE_ZEROES,
3718         .aio_offset     = offset,
3719         .aio_nbytes     = bytes,
3720     };
3721 
3722     if (blkdev) {
3723         acb.aio_type |= QEMU_AIO_BLKDEV;
3724     }
3725     if (flags & BDRV_REQ_NO_FALLBACK) {
3726         acb.aio_type |= QEMU_AIO_NO_FALLBACK;
3727     }
3728 
3729     if (flags & BDRV_REQ_MAY_UNMAP) {
3730         acb.aio_type |= QEMU_AIO_DISCARD;
3731         handler = handle_aiocb_write_zeroes_unmap;
3732     } else {
3733         handler = handle_aiocb_write_zeroes;
3734     }
3735 
3736     return raw_thread_pool_submit(handler, &acb);
3737 }
3738 
raw_co_pwrite_zeroes(BlockDriverState * bs,int64_t offset,int64_t bytes,BdrvRequestFlags flags)3739 static int coroutine_fn raw_co_pwrite_zeroes(
3740     BlockDriverState *bs, int64_t offset,
3741     int64_t bytes, BdrvRequestFlags flags)
3742 {
3743     return raw_do_pwrite_zeroes(bs, offset, bytes, flags, false);
3744 }
3745 
3746 static int coroutine_fn
raw_co_get_info(BlockDriverState * bs,BlockDriverInfo * bdi)3747 raw_co_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
3748 {
3749     return 0;
3750 }
3751 
raw_get_specific_info(BlockDriverState * bs,Error ** errp)3752 static ImageInfoSpecific *raw_get_specific_info(BlockDriverState *bs,
3753                                                 Error **errp)
3754 {
3755     ImageInfoSpecificFile *file_info = g_new0(ImageInfoSpecificFile, 1);
3756     ImageInfoSpecific *spec_info = g_new(ImageInfoSpecific, 1);
3757 
3758     *spec_info = (ImageInfoSpecific){
3759         .type = IMAGE_INFO_SPECIFIC_KIND_FILE,
3760         .u.file.data = file_info,
3761     };
3762 
3763 #ifdef FS_IOC_FSGETXATTR
3764     {
3765         BDRVRawState *s = bs->opaque;
3766         struct fsxattr attr;
3767         int ret;
3768 
3769         ret = ioctl(s->fd, FS_IOC_FSGETXATTR, &attr);
3770         if (!ret && attr.fsx_extsize != 0) {
3771             file_info->has_extent_size_hint = true;
3772             file_info->extent_size_hint = attr.fsx_extsize;
3773         }
3774     }
3775 #endif
3776 
3777     return spec_info;
3778 }
3779 
get_blockstats_specific_file(BlockDriverState * bs)3780 static BlockStatsSpecificFile get_blockstats_specific_file(BlockDriverState *bs)
3781 {
3782     BDRVRawState *s = bs->opaque;
3783     return (BlockStatsSpecificFile) {
3784         .discard_nb_ok = s->stats.discard_nb_ok,
3785         .discard_nb_failed = s->stats.discard_nb_failed,
3786         .discard_bytes_ok = s->stats.discard_bytes_ok,
3787     };
3788 }
3789 
raw_get_specific_stats(BlockDriverState * bs)3790 static BlockStatsSpecific *raw_get_specific_stats(BlockDriverState *bs)
3791 {
3792     BlockStatsSpecific *stats = g_new(BlockStatsSpecific, 1);
3793 
3794     stats->driver = BLOCKDEV_DRIVER_FILE;
3795     stats->u.file = get_blockstats_specific_file(bs);
3796 
3797     return stats;
3798 }
3799 
3800 #if defined(HAVE_HOST_BLOCK_DEVICE)
hdev_get_specific_stats(BlockDriverState * bs)3801 static BlockStatsSpecific *hdev_get_specific_stats(BlockDriverState *bs)
3802 {
3803     BlockStatsSpecific *stats = g_new(BlockStatsSpecific, 1);
3804 
3805     stats->driver = BLOCKDEV_DRIVER_HOST_DEVICE;
3806     stats->u.host_device = get_blockstats_specific_file(bs);
3807 
3808     return stats;
3809 }
3810 #endif /* HAVE_HOST_BLOCK_DEVICE */
3811 
3812 static QemuOptsList raw_create_opts = {
3813     .name = "raw-create-opts",
3814     .head = QTAILQ_HEAD_INITIALIZER(raw_create_opts.head),
3815     .desc = {
3816         {
3817             .name = BLOCK_OPT_SIZE,
3818             .type = QEMU_OPT_SIZE,
3819             .help = "Virtual disk size"
3820         },
3821         {
3822             .name = BLOCK_OPT_NOCOW,
3823             .type = QEMU_OPT_BOOL,
3824             .help = "Turn off copy-on-write (valid only on btrfs)"
3825         },
3826         {
3827             .name = BLOCK_OPT_PREALLOC,
3828             .type = QEMU_OPT_STRING,
3829             .help = "Preallocation mode (allowed values: off"
3830 #ifdef CONFIG_POSIX_FALLOCATE
3831                     ", falloc"
3832 #endif
3833                     ", full)"
3834         },
3835         {
3836             .name = BLOCK_OPT_EXTENT_SIZE_HINT,
3837             .type = QEMU_OPT_SIZE,
3838             .help = "Extent size hint for the image file, 0 to disable"
3839         },
3840         { /* end of list */ }
3841     }
3842 };
3843 
raw_check_perm(BlockDriverState * bs,uint64_t perm,uint64_t shared,Error ** errp)3844 static int raw_check_perm(BlockDriverState *bs, uint64_t perm, uint64_t shared,
3845                           Error **errp)
3846 {
3847     BDRVRawState *s = bs->opaque;
3848     int input_flags = s->reopen_state ? s->reopen_state->flags : bs->open_flags;
3849     int open_flags;
3850     int ret;
3851 
3852     /* We may need a new fd if auto-read-only switches the mode */
3853     ret = raw_reconfigure_getfd(bs, input_flags, &open_flags, perm, errp);
3854     if (ret < 0) {
3855         return ret;
3856     } else if (ret != s->fd) {
3857         Error *local_err = NULL;
3858 
3859         /*
3860          * Fail already check_perm() if we can't get a working O_DIRECT
3861          * alignment with the new fd.
3862          */
3863         raw_probe_alignment(bs, ret, &local_err);
3864         if (local_err) {
3865             error_propagate(errp, local_err);
3866             return -EINVAL;
3867         }
3868 
3869         s->perm_change_fd = ret;
3870         s->perm_change_flags = open_flags;
3871     }
3872 
3873     /* Prepare permissions on old fd to avoid conflicts between old and new,
3874      * but keep everything locked that new will need. */
3875     ret = raw_handle_perm_lock(bs, RAW_PL_PREPARE, perm, shared, errp);
3876     if (ret < 0) {
3877         goto fail;
3878     }
3879 
3880     /* Copy locks to the new fd */
3881     if (s->perm_change_fd && s->use_lock) {
3882         ret = raw_apply_lock_bytes(NULL, s->perm_change_fd, perm, ~shared,
3883                                    false, errp);
3884         if (ret < 0) {
3885             raw_handle_perm_lock(bs, RAW_PL_ABORT, 0, 0, NULL);
3886             goto fail;
3887         }
3888     }
3889     return 0;
3890 
3891 fail:
3892     if (s->perm_change_fd) {
3893         qemu_close(s->perm_change_fd);
3894     }
3895     s->perm_change_fd = 0;
3896     return ret;
3897 }
3898 
raw_set_perm(BlockDriverState * bs,uint64_t perm,uint64_t shared)3899 static void raw_set_perm(BlockDriverState *bs, uint64_t perm, uint64_t shared)
3900 {
3901     BDRVRawState *s = bs->opaque;
3902 
3903     /* For reopen, we have already switched to the new fd (.bdrv_set_perm is
3904      * called after .bdrv_reopen_commit) */
3905     if (s->perm_change_fd && s->fd != s->perm_change_fd) {
3906         qemu_close(s->fd);
3907         s->fd = s->perm_change_fd;
3908         s->open_flags = s->perm_change_flags;
3909     }
3910     s->perm_change_fd = 0;
3911 
3912     raw_handle_perm_lock(bs, RAW_PL_COMMIT, perm, shared, NULL);
3913     s->perm = perm;
3914     s->shared_perm = shared;
3915 }
3916 
raw_abort_perm_update(BlockDriverState * bs)3917 static void raw_abort_perm_update(BlockDriverState *bs)
3918 {
3919     BDRVRawState *s = bs->opaque;
3920 
3921     /* For reopen, .bdrv_reopen_abort is called afterwards and will close
3922      * the file descriptor. */
3923     if (s->perm_change_fd) {
3924         qemu_close(s->perm_change_fd);
3925     }
3926     s->perm_change_fd = 0;
3927 
3928     raw_handle_perm_lock(bs, RAW_PL_ABORT, 0, 0, NULL);
3929 }
3930 
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)3931 static int coroutine_fn GRAPH_RDLOCK raw_co_copy_range_from(
3932         BlockDriverState *bs, BdrvChild *src, int64_t src_offset,
3933         BdrvChild *dst, int64_t dst_offset, int64_t bytes,
3934         BdrvRequestFlags read_flags, BdrvRequestFlags write_flags)
3935 {
3936     return bdrv_co_copy_range_to(src, src_offset, dst, dst_offset, bytes,
3937                                  read_flags, write_flags);
3938 }
3939 
3940 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)3941 raw_co_copy_range_to(BlockDriverState *bs,
3942                      BdrvChild *src, int64_t src_offset,
3943                      BdrvChild *dst, int64_t dst_offset,
3944                      int64_t bytes, BdrvRequestFlags read_flags,
3945                      BdrvRequestFlags write_flags)
3946 {
3947     RawPosixAIOData acb;
3948     BDRVRawState *s = bs->opaque;
3949     BDRVRawState *src_s;
3950 
3951     assert(dst->bs == bs);
3952     if (src->bs->drv->bdrv_co_copy_range_to != raw_co_copy_range_to) {
3953         return -ENOTSUP;
3954     }
3955 
3956     src_s = src->bs->opaque;
3957     if (fd_open(src->bs) < 0 || fd_open(dst->bs) < 0) {
3958         return -EIO;
3959     }
3960 
3961     acb = (RawPosixAIOData) {
3962         .bs             = bs,
3963         .aio_type       = QEMU_AIO_COPY_RANGE,
3964         .aio_fildes     = src_s->fd,
3965         .aio_offset     = src_offset,
3966         .aio_nbytes     = bytes,
3967         .copy_range     = {
3968             .aio_fd2        = s->fd,
3969             .aio_offset2    = dst_offset,
3970         },
3971     };
3972 
3973     return raw_thread_pool_submit(handle_aiocb_copy_range, &acb);
3974 }
3975 
3976 BlockDriver bdrv_file = {
3977     .format_name = "file",
3978     .protocol_name = "file",
3979     .instance_size = sizeof(BDRVRawState),
3980     .bdrv_needs_filename = true,
3981     .bdrv_probe = NULL, /* no probe for protocols */
3982     .bdrv_parse_filename = raw_parse_filename,
3983     .bdrv_open      = raw_open,
3984     .bdrv_reopen_prepare = raw_reopen_prepare,
3985     .bdrv_reopen_commit = raw_reopen_commit,
3986     .bdrv_reopen_abort = raw_reopen_abort,
3987     .bdrv_close = raw_close,
3988     .bdrv_co_create = raw_co_create,
3989     .bdrv_co_create_opts = raw_co_create_opts,
3990     .bdrv_has_zero_init = bdrv_has_zero_init_1,
3991     .bdrv_co_block_status = raw_co_block_status,
3992     .bdrv_co_invalidate_cache = raw_co_invalidate_cache,
3993     .bdrv_co_pwrite_zeroes = raw_co_pwrite_zeroes,
3994     .bdrv_co_delete_file = raw_co_delete_file,
3995 
3996     .bdrv_co_preadv         = raw_co_preadv,
3997     .bdrv_co_pwritev        = raw_co_pwritev,
3998     .bdrv_co_flush_to_disk  = raw_co_flush_to_disk,
3999     .bdrv_co_pdiscard       = raw_co_pdiscard,
4000     .bdrv_co_copy_range_from = raw_co_copy_range_from,
4001     .bdrv_co_copy_range_to  = raw_co_copy_range_to,
4002     .bdrv_refresh_limits = raw_refresh_limits,
4003 
4004     .bdrv_co_truncate                   = raw_co_truncate,
4005     .bdrv_co_getlength                  = raw_co_getlength,
4006     .bdrv_co_get_info                   = raw_co_get_info,
4007     .bdrv_get_specific_info             = raw_get_specific_info,
4008     .bdrv_co_get_allocated_file_size    = raw_co_get_allocated_file_size,
4009     .bdrv_get_specific_stats = raw_get_specific_stats,
4010     .bdrv_check_perm = raw_check_perm,
4011     .bdrv_set_perm   = raw_set_perm,
4012     .bdrv_abort_perm_update = raw_abort_perm_update,
4013     .create_opts = &raw_create_opts,
4014     .mutable_opts = mutable_opts,
4015 };
4016 
4017 /***********************************************/
4018 /* host device */
4019 
4020 #if defined(HAVE_HOST_BLOCK_DEVICE)
4021 
4022 #if defined(__APPLE__) && defined(__MACH__)
4023 static kern_return_t GetBSDPath(io_iterator_t mediaIterator, char *bsdPath,
4024                                 CFIndex maxPathSize, int flags);
4025 
FindEjectableOpticalMedia(io_iterator_t * mediaIterator)4026 static char *FindEjectableOpticalMedia(io_iterator_t *mediaIterator)
4027 {
4028     kern_return_t kernResult = KERN_FAILURE;
4029     mach_port_t mainPort;
4030     CFMutableDictionaryRef  classesToMatch;
4031     const char *matching_array[] = {kIODVDMediaClass, kIOCDMediaClass};
4032     char *mediaType = NULL;
4033 
4034     kernResult = IOMainPort(MACH_PORT_NULL, &mainPort);
4035     if ( KERN_SUCCESS != kernResult ) {
4036         printf("IOMainPort returned %d\n", kernResult);
4037     }
4038 
4039     int index;
4040     for (index = 0; index < ARRAY_SIZE(matching_array); index++) {
4041         classesToMatch = IOServiceMatching(matching_array[index]);
4042         if (classesToMatch == NULL) {
4043             error_report("IOServiceMatching returned NULL for %s",
4044                          matching_array[index]);
4045             continue;
4046         }
4047         CFDictionarySetValue(classesToMatch, CFSTR(kIOMediaEjectableKey),
4048                              kCFBooleanTrue);
4049         kernResult = IOServiceGetMatchingServices(mainPort, classesToMatch,
4050                                                   mediaIterator);
4051         if (kernResult != KERN_SUCCESS) {
4052             error_report("Note: IOServiceGetMatchingServices returned %d",
4053                          kernResult);
4054             continue;
4055         }
4056 
4057         /* If a match was found, leave the loop */
4058         if (*mediaIterator != 0) {
4059             trace_file_FindEjectableOpticalMedia(matching_array[index]);
4060             mediaType = g_strdup(matching_array[index]);
4061             break;
4062         }
4063     }
4064     return mediaType;
4065 }
4066 
GetBSDPath(io_iterator_t mediaIterator,char * bsdPath,CFIndex maxPathSize,int flags)4067 kern_return_t GetBSDPath(io_iterator_t mediaIterator, char *bsdPath,
4068                          CFIndex maxPathSize, int flags)
4069 {
4070     io_object_t     nextMedia;
4071     kern_return_t   kernResult = KERN_FAILURE;
4072     *bsdPath = '\0';
4073     nextMedia = IOIteratorNext( mediaIterator );
4074     if ( nextMedia )
4075     {
4076         CFTypeRef   bsdPathAsCFString;
4077     bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
4078         if ( bsdPathAsCFString ) {
4079             size_t devPathLength;
4080             strcpy( bsdPath, _PATH_DEV );
4081             if (flags & BDRV_O_NOCACHE) {
4082                 strcat(bsdPath, "r");
4083             }
4084             devPathLength = strlen( bsdPath );
4085             if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
4086                 kernResult = KERN_SUCCESS;
4087             }
4088             CFRelease( bsdPathAsCFString );
4089         }
4090         IOObjectRelease( nextMedia );
4091     }
4092 
4093     return kernResult;
4094 }
4095 
4096 /* Sets up a real cdrom for use in QEMU */
setup_cdrom(char * bsd_path,Error ** errp)4097 static bool setup_cdrom(char *bsd_path, Error **errp)
4098 {
4099     int index, num_of_test_partitions = 2, fd;
4100     char test_partition[MAXPATHLEN];
4101     bool partition_found = false;
4102 
4103     /* look for a working partition */
4104     for (index = 0; index < num_of_test_partitions; index++) {
4105         snprintf(test_partition, sizeof(test_partition), "%ss%d", bsd_path,
4106                  index);
4107         fd = qemu_open(test_partition, O_RDONLY | O_BINARY | O_LARGEFILE, NULL);
4108         if (fd >= 0) {
4109             partition_found = true;
4110             qemu_close(fd);
4111             break;
4112         }
4113     }
4114 
4115     /* if a working partition on the device was not found */
4116     if (partition_found == false) {
4117         error_setg(errp, "Failed to find a working partition on disc");
4118     } else {
4119         trace_file_setup_cdrom(test_partition);
4120         pstrcpy(bsd_path, MAXPATHLEN, test_partition);
4121     }
4122     return partition_found;
4123 }
4124 
4125 /* Prints directions on mounting and unmounting a device */
print_unmounting_directions(const char * file_name)4126 static void print_unmounting_directions(const char *file_name)
4127 {
4128     error_report("If device %s is mounted on the desktop, unmount"
4129                  " it first before using it in QEMU", file_name);
4130     error_report("Command to unmount device: diskutil unmountDisk %s",
4131                  file_name);
4132     error_report("Command to mount device: diskutil mountDisk %s", file_name);
4133 }
4134 
4135 #endif /* defined(__APPLE__) && defined(__MACH__) */
4136 
hdev_probe_device(const char * filename)4137 static int hdev_probe_device(const char *filename)
4138 {
4139     struct stat st;
4140 
4141     /* allow a dedicated CD-ROM driver to match with a higher priority */
4142     if (strstart(filename, "/dev/cdrom", NULL))
4143         return 50;
4144 
4145     if (stat(filename, &st) >= 0 &&
4146             (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
4147         return 100;
4148     }
4149 
4150     return 0;
4151 }
4152 
hdev_parse_filename(const char * filename,QDict * options,Error ** errp)4153 static void hdev_parse_filename(const char *filename, QDict *options,
4154                                 Error **errp)
4155 {
4156     bdrv_parse_filename_strip_prefix(filename, "host_device:", options);
4157 }
4158 
hdev_is_sg(BlockDriverState * bs)4159 static bool hdev_is_sg(BlockDriverState *bs)
4160 {
4161 
4162 #if defined(__linux__)
4163 
4164     BDRVRawState *s = bs->opaque;
4165     struct stat st;
4166     struct sg_scsi_id scsiid;
4167     int sg_version;
4168     int ret;
4169 
4170     if (stat(bs->filename, &st) < 0 || !S_ISCHR(st.st_mode)) {
4171         return false;
4172     }
4173 
4174     ret = ioctl(s->fd, SG_GET_VERSION_NUM, &sg_version);
4175     if (ret < 0) {
4176         return false;
4177     }
4178 
4179     ret = ioctl(s->fd, SG_GET_SCSI_ID, &scsiid);
4180     if (ret >= 0) {
4181         trace_file_hdev_is_sg(scsiid.scsi_type, sg_version);
4182         return true;
4183     }
4184 
4185 #endif
4186 
4187     return false;
4188 }
4189 
hdev_open(BlockDriverState * bs,QDict * options,int flags,Error ** errp)4190 static int hdev_open(BlockDriverState *bs, QDict *options, int flags,
4191                      Error **errp)
4192 {
4193     BDRVRawState *s = bs->opaque;
4194     int ret;
4195 
4196 #if defined(__APPLE__) && defined(__MACH__)
4197     /*
4198      * Caution: while qdict_get_str() is fine, getting non-string types
4199      * would require more care.  When @options come from -blockdev or
4200      * blockdev_add, its members are typed according to the QAPI
4201      * schema, but when they come from -drive, they're all QString.
4202      */
4203     const char *filename = qdict_get_str(options, "filename");
4204     char bsd_path[MAXPATHLEN] = "";
4205     bool error_occurred = false;
4206 
4207     /* If using a real cdrom */
4208     if (strcmp(filename, "/dev/cdrom") == 0) {
4209         char *mediaType = NULL;
4210         kern_return_t ret_val;
4211         io_iterator_t mediaIterator = 0;
4212 
4213         mediaType = FindEjectableOpticalMedia(&mediaIterator);
4214         if (mediaType == NULL) {
4215             error_setg(errp, "Please make sure your CD/DVD is in the optical"
4216                        " drive");
4217             error_occurred = true;
4218             goto hdev_open_Mac_error;
4219         }
4220 
4221         ret_val = GetBSDPath(mediaIterator, bsd_path, sizeof(bsd_path), flags);
4222         if (ret_val != KERN_SUCCESS) {
4223             error_setg(errp, "Could not get BSD path for optical drive");
4224             error_occurred = true;
4225             goto hdev_open_Mac_error;
4226         }
4227 
4228         /* If a real optical drive was not found */
4229         if (bsd_path[0] == '\0') {
4230             error_setg(errp, "Failed to obtain bsd path for optical drive");
4231             error_occurred = true;
4232             goto hdev_open_Mac_error;
4233         }
4234 
4235         /* If using a cdrom disc and finding a partition on the disc failed */
4236         if (strncmp(mediaType, kIOCDMediaClass, 9) == 0 &&
4237             setup_cdrom(bsd_path, errp) == false) {
4238             print_unmounting_directions(bsd_path);
4239             error_occurred = true;
4240             goto hdev_open_Mac_error;
4241         }
4242 
4243         qdict_put_str(options, "filename", bsd_path);
4244 
4245 hdev_open_Mac_error:
4246         g_free(mediaType);
4247         if (mediaIterator) {
4248             IOObjectRelease(mediaIterator);
4249         }
4250         if (error_occurred) {
4251             return -ENOENT;
4252         }
4253     }
4254 #endif /* defined(__APPLE__) && defined(__MACH__) */
4255 
4256     s->type = FTYPE_FILE;
4257 
4258     ret = raw_open_common(bs, options, flags, 0, true, errp);
4259     if (ret < 0) {
4260 #if defined(__APPLE__) && defined(__MACH__)
4261         if (*bsd_path) {
4262             filename = bsd_path;
4263         }
4264         /* if a physical device experienced an error while being opened */
4265         if (strncmp(filename, "/dev/", 5) == 0) {
4266             print_unmounting_directions(filename);
4267         }
4268 #endif /* defined(__APPLE__) && defined(__MACH__) */
4269         return ret;
4270     }
4271 
4272     /* Since this does ioctl the device must be already opened */
4273     bs->sg = hdev_is_sg(bs);
4274 
4275     /* sg devices aren't even block devices and can't use dm-mpath */
4276     s->use_mpath = !bs->sg;
4277 
4278     return ret;
4279 }
4280 
4281 #if defined(__linux__)
4282 #if defined(DM_MPATH_PROBE_PATHS)
sgio_path_error(int ret,sg_io_hdr_t * io_hdr)4283 static bool coroutine_fn sgio_path_error(int ret, sg_io_hdr_t *io_hdr)
4284 {
4285     if (ret < 0) {
4286         switch (ret) {
4287         case -ENODEV:
4288             return true;
4289         case -EAGAIN:
4290             /*
4291              * The device is probably suspended. This happens while the dm table
4292              * is reloaded, e.g. because a path is added or removed. This is an
4293              * operation that should complete within 1ms, so just wait a bit and
4294              * retry.
4295              *
4296              * If the device was suspended for another reason, we'll wait and
4297              * retry SG_IO_MAX_RETRIES times. This is a tolerable delay before
4298              * we return an error and potentially stop the VM.
4299              */
4300             qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, 1000000);
4301             return true;
4302         default:
4303             return false;
4304         }
4305     }
4306 
4307     if (io_hdr->host_status != SCSI_HOST_OK) {
4308         return true;
4309     }
4310 
4311     switch (io_hdr->status) {
4312     case GOOD:
4313     case CONDITION_GOOD:
4314     case INTERMEDIATE_GOOD:
4315     case INTERMEDIATE_C_GOOD:
4316     case RESERVATION_CONFLICT:
4317     case COMMAND_TERMINATED:
4318         return false;
4319     case CHECK_CONDITION:
4320         return !scsi_sense_buf_is_guest_recoverable(io_hdr->sbp,
4321                                                     io_hdr->mx_sb_len);
4322     default:
4323         return true;
4324     }
4325 }
4326 
hdev_co_ioctl_sgio_retry(RawPosixAIOData * acb,int ret)4327 static bool coroutine_fn hdev_co_ioctl_sgio_retry(RawPosixAIOData *acb, int ret)
4328 {
4329     BDRVRawState *s = acb->bs->opaque;
4330     RawPosixAIOData probe_acb;
4331 
4332     if (!s->use_mpath) {
4333         return false;
4334     }
4335 
4336     if (!sgio_path_error(ret, acb->ioctl.buf)) {
4337         return false;
4338     }
4339 
4340     probe_acb = (RawPosixAIOData) {
4341         .bs         = acb->bs,
4342         .aio_type   = QEMU_AIO_IOCTL,
4343         .aio_fildes = s->fd,
4344         .aio_offset = 0,
4345         .ioctl      = {
4346             .buf        = NULL,
4347             .cmd        = DM_MPATH_PROBE_PATHS,
4348         },
4349     };
4350 
4351     ret = raw_thread_pool_submit(handle_aiocb_ioctl, &probe_acb);
4352     if (ret == -ENOTTY) {
4353         s->use_mpath = false;
4354     } else if (ret == -EAGAIN) {
4355         /* The device might be suspended for a table reload, worth retrying */
4356         return true;
4357     }
4358 
4359     return ret == 0;
4360 }
4361 #else
hdev_co_ioctl_sgio_retry(RawPosixAIOData * acb,int ret)4362 static bool coroutine_fn hdev_co_ioctl_sgio_retry(RawPosixAIOData *acb, int ret)
4363 {
4364     return false;
4365 }
4366 #endif /* DM_MPATH_PROBE_PATHS */
4367 
4368 static int coroutine_fn
hdev_co_ioctl(BlockDriverState * bs,unsigned long int req,void * buf)4369 hdev_co_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
4370 {
4371     BDRVRawState *s = bs->opaque;
4372     RawPosixAIOData acb;
4373     int retries = SG_IO_MAX_RETRIES;
4374     int ret;
4375 
4376     ret = fd_open(bs);
4377     if (ret < 0) {
4378         return ret;
4379     }
4380 
4381     if (req == SG_IO && s->pr_mgr) {
4382         struct sg_io_hdr *io_hdr = buf;
4383         if (io_hdr->cmdp[0] == PERSISTENT_RESERVE_OUT ||
4384             io_hdr->cmdp[0] == PERSISTENT_RESERVE_IN) {
4385             return pr_manager_execute(s->pr_mgr, qemu_get_current_aio_context(),
4386                                       s->fd, io_hdr);
4387         }
4388     }
4389 
4390     acb = (RawPosixAIOData) {
4391         .bs         = bs,
4392         .aio_type   = QEMU_AIO_IOCTL,
4393         .aio_fildes = s->fd,
4394         .aio_offset = 0,
4395         .ioctl      = {
4396             .buf        = buf,
4397             .cmd        = req,
4398         },
4399     };
4400 
4401     do {
4402         ret = raw_thread_pool_submit(handle_aiocb_ioctl, &acb);
4403     } while (req == SG_IO && retries-- && hdev_co_ioctl_sgio_retry(&acb, ret));
4404 
4405     return ret;
4406 }
4407 #endif /* linux */
4408 
4409 static coroutine_fn int
hdev_co_pdiscard(BlockDriverState * bs,int64_t offset,int64_t bytes)4410 hdev_co_pdiscard(BlockDriverState *bs, int64_t offset, int64_t bytes)
4411 {
4412     BDRVRawState *s = bs->opaque;
4413     int ret;
4414 
4415     ret = fd_open(bs);
4416     if (ret < 0) {
4417         raw_account_discard(s, bytes, ret);
4418         return ret;
4419     }
4420     return raw_do_pdiscard(bs, offset, bytes, true);
4421 }
4422 
hdev_co_pwrite_zeroes(BlockDriverState * bs,int64_t offset,int64_t bytes,BdrvRequestFlags flags)4423 static coroutine_fn int hdev_co_pwrite_zeroes(BlockDriverState *bs,
4424     int64_t offset, int64_t bytes, BdrvRequestFlags flags)
4425 {
4426     int rc;
4427 
4428     rc = fd_open(bs);
4429     if (rc < 0) {
4430         return rc;
4431     }
4432 
4433     return raw_do_pwrite_zeroes(bs, offset, bytes, flags, true);
4434 }
4435 
4436 static BlockDriver bdrv_host_device = {
4437     .format_name        = "host_device",
4438     .protocol_name        = "host_device",
4439     .instance_size      = sizeof(BDRVRawState),
4440     .bdrv_needs_filename = true,
4441     .bdrv_probe_device  = hdev_probe_device,
4442     .bdrv_parse_filename = hdev_parse_filename,
4443     .bdrv_open          = hdev_open,
4444     .bdrv_close         = raw_close,
4445     .bdrv_reopen_prepare = raw_reopen_prepare,
4446     .bdrv_reopen_commit  = raw_reopen_commit,
4447     .bdrv_reopen_abort   = raw_reopen_abort,
4448     .bdrv_co_create_opts = bdrv_co_create_opts_simple,
4449     .create_opts         = &bdrv_create_opts_simple,
4450     .mutable_opts        = mutable_opts,
4451     .bdrv_co_invalidate_cache = raw_co_invalidate_cache,
4452     .bdrv_co_pwrite_zeroes = hdev_co_pwrite_zeroes,
4453 
4454     .bdrv_co_preadv         = raw_co_preadv,
4455     .bdrv_co_pwritev        = raw_co_pwritev,
4456     .bdrv_co_flush_to_disk  = raw_co_flush_to_disk,
4457     .bdrv_co_pdiscard       = hdev_co_pdiscard,
4458     .bdrv_co_copy_range_from = raw_co_copy_range_from,
4459     .bdrv_co_copy_range_to  = raw_co_copy_range_to,
4460     .bdrv_refresh_limits = raw_refresh_limits,
4461 
4462     .bdrv_co_truncate                   = raw_co_truncate,
4463     .bdrv_co_getlength                  = raw_co_getlength,
4464     .bdrv_co_get_info                   = raw_co_get_info,
4465     .bdrv_get_specific_info             = raw_get_specific_info,
4466     .bdrv_co_get_allocated_file_size    = raw_co_get_allocated_file_size,
4467     .bdrv_get_specific_stats = hdev_get_specific_stats,
4468     .bdrv_check_perm = raw_check_perm,
4469     .bdrv_set_perm   = raw_set_perm,
4470     .bdrv_abort_perm_update = raw_abort_perm_update,
4471     .bdrv_probe_blocksizes = hdev_probe_blocksizes,
4472     .bdrv_probe_geometry = hdev_probe_geometry,
4473 
4474     /* generic scsi device */
4475 #ifdef __linux__
4476     .bdrv_co_ioctl          = hdev_co_ioctl,
4477 #endif
4478 
4479     /* zoned device */
4480 #if defined(CONFIG_BLKZONED)
4481     /* zone management operations */
4482     .bdrv_co_zone_report = raw_co_zone_report,
4483     .bdrv_co_zone_mgmt = raw_co_zone_mgmt,
4484     .bdrv_co_zone_append = raw_co_zone_append,
4485 #endif
4486 };
4487 
4488 #if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
cdrom_parse_filename(const char * filename,QDict * options,Error ** errp)4489 static void cdrom_parse_filename(const char *filename, QDict *options,
4490                                  Error **errp)
4491 {
4492     bdrv_parse_filename_strip_prefix(filename, "host_cdrom:", options);
4493 }
4494 
cdrom_refresh_limits(BlockDriverState * bs,Error ** errp)4495 static void cdrom_refresh_limits(BlockDriverState *bs, Error **errp)
4496 {
4497     bs->bl.has_variable_length = true;
4498     raw_refresh_limits(bs, errp);
4499 }
4500 #endif
4501 
4502 #ifdef __linux__
cdrom_open(BlockDriverState * bs,QDict * options,int flags,Error ** errp)4503 static int cdrom_open(BlockDriverState *bs, QDict *options, int flags,
4504                       Error **errp)
4505 {
4506     BDRVRawState *s = bs->opaque;
4507 
4508     s->type = FTYPE_CD;
4509 
4510     /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
4511     return raw_open_common(bs, options, flags, O_NONBLOCK, true, errp);
4512 }
4513 
cdrom_probe_device(const char * filename)4514 static int cdrom_probe_device(const char *filename)
4515 {
4516     int fd, ret;
4517     int prio = 0;
4518     struct stat st;
4519 
4520     fd = qemu_open(filename, O_RDONLY | O_NONBLOCK, NULL);
4521     if (fd < 0) {
4522         goto out;
4523     }
4524     ret = fstat(fd, &st);
4525     if (ret == -1 || !S_ISBLK(st.st_mode)) {
4526         goto outc;
4527     }
4528 
4529     /* Attempt to detect via a CDROM specific ioctl */
4530     ret = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
4531     if (ret >= 0)
4532         prio = 100;
4533 
4534 outc:
4535     qemu_close(fd);
4536 out:
4537     return prio;
4538 }
4539 
cdrom_co_is_inserted(BlockDriverState * bs)4540 static bool coroutine_fn cdrom_co_is_inserted(BlockDriverState *bs)
4541 {
4542     BDRVRawState *s = bs->opaque;
4543     int ret;
4544 
4545     ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
4546     return ret == CDS_DISC_OK;
4547 }
4548 
cdrom_co_eject(BlockDriverState * bs,bool eject_flag)4549 static void coroutine_fn cdrom_co_eject(BlockDriverState *bs, bool eject_flag)
4550 {
4551     BDRVRawState *s = bs->opaque;
4552 
4553     if (eject_flag) {
4554         if (ioctl(s->fd, CDROMEJECT, NULL) < 0)
4555             perror("CDROMEJECT");
4556     } else {
4557         if (ioctl(s->fd, CDROMCLOSETRAY, NULL) < 0)
4558             perror("CDROMEJECT");
4559     }
4560 }
4561 
cdrom_co_lock_medium(BlockDriverState * bs,bool locked)4562 static void coroutine_fn cdrom_co_lock_medium(BlockDriverState *bs, bool locked)
4563 {
4564     BDRVRawState *s = bs->opaque;
4565 
4566     if (ioctl(s->fd, CDROM_LOCKDOOR, locked) < 0) {
4567         /*
4568          * Note: an error can happen if the distribution automatically
4569          * mounts the CD-ROM
4570          */
4571         /* perror("CDROM_LOCKDOOR"); */
4572     }
4573 }
4574 
4575 static BlockDriver bdrv_host_cdrom = {
4576     .format_name        = "host_cdrom",
4577     .protocol_name      = "host_cdrom",
4578     .instance_size      = sizeof(BDRVRawState),
4579     .bdrv_needs_filename = true,
4580     .bdrv_probe_device	= cdrom_probe_device,
4581     .bdrv_parse_filename = cdrom_parse_filename,
4582     .bdrv_open          = cdrom_open,
4583     .bdrv_close         = raw_close,
4584     .bdrv_reopen_prepare = raw_reopen_prepare,
4585     .bdrv_reopen_commit  = raw_reopen_commit,
4586     .bdrv_reopen_abort   = raw_reopen_abort,
4587     .bdrv_co_create_opts = bdrv_co_create_opts_simple,
4588     .create_opts         = &bdrv_create_opts_simple,
4589     .mutable_opts        = mutable_opts,
4590     .bdrv_co_invalidate_cache = raw_co_invalidate_cache,
4591 
4592     .bdrv_co_preadv         = raw_co_preadv,
4593     .bdrv_co_pwritev        = raw_co_pwritev,
4594     .bdrv_co_flush_to_disk  = raw_co_flush_to_disk,
4595     .bdrv_refresh_limits    = cdrom_refresh_limits,
4596 
4597     .bdrv_co_truncate                   = raw_co_truncate,
4598     .bdrv_co_getlength                  = raw_co_getlength,
4599     .bdrv_co_get_allocated_file_size    = raw_co_get_allocated_file_size,
4600 
4601     /* removable device support */
4602     .bdrv_co_is_inserted    = cdrom_co_is_inserted,
4603     .bdrv_co_eject          = cdrom_co_eject,
4604     .bdrv_co_lock_medium    = cdrom_co_lock_medium,
4605 
4606     /* generic scsi device */
4607     .bdrv_co_ioctl      = hdev_co_ioctl,
4608 };
4609 #endif /* __linux__ */
4610 
4611 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
cdrom_open(BlockDriverState * bs,QDict * options,int flags,Error ** errp)4612 static int cdrom_open(BlockDriverState *bs, QDict *options, int flags,
4613                       Error **errp)
4614 {
4615     BDRVRawState *s = bs->opaque;
4616     int ret;
4617 
4618     s->type = FTYPE_CD;
4619 
4620     ret = raw_open_common(bs, options, flags, 0, true, errp);
4621     if (ret) {
4622         return ret;
4623     }
4624 
4625     /* make sure the door isn't locked at this time */
4626     ioctl(s->fd, CDIOCALLOW);
4627     return 0;
4628 }
4629 
cdrom_probe_device(const char * filename)4630 static int cdrom_probe_device(const char *filename)
4631 {
4632     if (strstart(filename, "/dev/cd", NULL) ||
4633             strstart(filename, "/dev/acd", NULL))
4634         return 100;
4635     return 0;
4636 }
4637 
cdrom_reopen(BlockDriverState * bs)4638 static int cdrom_reopen(BlockDriverState *bs)
4639 {
4640     BDRVRawState *s = bs->opaque;
4641     int fd;
4642 
4643     /*
4644      * Force reread of possibly changed/newly loaded disc,
4645      * FreeBSD seems to not notice sometimes...
4646      */
4647     if (s->fd >= 0)
4648         qemu_close(s->fd);
4649     fd = qemu_open(bs->filename, s->open_flags, NULL);
4650     if (fd < 0) {
4651         s->fd = -1;
4652         return -EIO;
4653     }
4654     s->fd = fd;
4655 
4656     /* make sure the door isn't locked at this time */
4657     ioctl(s->fd, CDIOCALLOW);
4658     return 0;
4659 }
4660 
cdrom_co_is_inserted(BlockDriverState * bs)4661 static bool coroutine_fn cdrom_co_is_inserted(BlockDriverState *bs)
4662 {
4663     return raw_getlength(bs) > 0;
4664 }
4665 
cdrom_co_eject(BlockDriverState * bs,bool eject_flag)4666 static void coroutine_fn cdrom_co_eject(BlockDriverState *bs, bool eject_flag)
4667 {
4668     BDRVRawState *s = bs->opaque;
4669 
4670     if (s->fd < 0)
4671         return;
4672 
4673     (void) ioctl(s->fd, CDIOCALLOW);
4674 
4675     if (eject_flag) {
4676         if (ioctl(s->fd, CDIOCEJECT) < 0)
4677             perror("CDIOCEJECT");
4678     } else {
4679         if (ioctl(s->fd, CDIOCCLOSE) < 0)
4680             perror("CDIOCCLOSE");
4681     }
4682 
4683     cdrom_reopen(bs);
4684 }
4685 
cdrom_co_lock_medium(BlockDriverState * bs,bool locked)4686 static void coroutine_fn cdrom_co_lock_medium(BlockDriverState *bs, bool locked)
4687 {
4688     BDRVRawState *s = bs->opaque;
4689 
4690     if (s->fd < 0)
4691         return;
4692     if (ioctl(s->fd, (locked ? CDIOCPREVENT : CDIOCALLOW)) < 0) {
4693         /*
4694          * Note: an error can happen if the distribution automatically
4695          * mounts the CD-ROM
4696          */
4697         /* perror("CDROM_LOCKDOOR"); */
4698     }
4699 }
4700 
4701 static BlockDriver bdrv_host_cdrom = {
4702     .format_name        = "host_cdrom",
4703     .protocol_name      = "host_cdrom",
4704     .instance_size      = sizeof(BDRVRawState),
4705     .bdrv_needs_filename = true,
4706     .bdrv_probe_device	= cdrom_probe_device,
4707     .bdrv_parse_filename = cdrom_parse_filename,
4708     .bdrv_open          = cdrom_open,
4709     .bdrv_close         = raw_close,
4710     .bdrv_reopen_prepare = raw_reopen_prepare,
4711     .bdrv_reopen_commit  = raw_reopen_commit,
4712     .bdrv_reopen_abort   = raw_reopen_abort,
4713     .bdrv_co_create_opts = bdrv_co_create_opts_simple,
4714     .create_opts         = &bdrv_create_opts_simple,
4715     .mutable_opts       = mutable_opts,
4716 
4717     .bdrv_co_preadv         = raw_co_preadv,
4718     .bdrv_co_pwritev        = raw_co_pwritev,
4719     .bdrv_co_flush_to_disk  = raw_co_flush_to_disk,
4720     .bdrv_refresh_limits    = cdrom_refresh_limits,
4721 
4722     .bdrv_co_truncate                   = raw_co_truncate,
4723     .bdrv_co_getlength                  = raw_co_getlength,
4724     .bdrv_co_get_allocated_file_size    = raw_co_get_allocated_file_size,
4725 
4726     /* removable device support */
4727     .bdrv_co_is_inserted     = cdrom_co_is_inserted,
4728     .bdrv_co_eject           = cdrom_co_eject,
4729     .bdrv_co_lock_medium     = cdrom_co_lock_medium,
4730 };
4731 #endif /* __FreeBSD__ */
4732 
4733 #endif /* HAVE_HOST_BLOCK_DEVICE */
4734 
bdrv_file_init(void)4735 static void bdrv_file_init(void)
4736 {
4737     /*
4738      * Register all the drivers.  Note that order is important, the driver
4739      * registered last will get probed first.
4740      */
4741     bdrv_register(&bdrv_file);
4742 #if defined(HAVE_HOST_BLOCK_DEVICE)
4743     bdrv_register(&bdrv_host_device);
4744 #ifdef __linux__
4745     bdrv_register(&bdrv_host_cdrom);
4746 #endif
4747 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
4748     bdrv_register(&bdrv_host_cdrom);
4749 #endif
4750 #endif /* HAVE_HOST_BLOCK_DEVICE */
4751 }
4752 
4753 block_init(bdrv_file_init);
4754