1c1bb86cdSEric Blake /* 2c1bb86cdSEric Blake * Block driver for RAW files (posix) 3c1bb86cdSEric Blake * 4c1bb86cdSEric Blake * Copyright (c) 2006 Fabrice Bellard 5c1bb86cdSEric Blake * 6c1bb86cdSEric Blake * Permission is hereby granted, free of charge, to any person obtaining a copy 7c1bb86cdSEric Blake * of this software and associated documentation files (the "Software"), to deal 8c1bb86cdSEric Blake * in the Software without restriction, including without limitation the rights 9c1bb86cdSEric Blake * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10c1bb86cdSEric Blake * copies of the Software, and to permit persons to whom the Software is 11c1bb86cdSEric Blake * furnished to do so, subject to the following conditions: 12c1bb86cdSEric Blake * 13c1bb86cdSEric Blake * The above copyright notice and this permission notice shall be included in 14c1bb86cdSEric Blake * all copies or substantial portions of the Software. 15c1bb86cdSEric Blake * 16c1bb86cdSEric Blake * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17c1bb86cdSEric Blake * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18c1bb86cdSEric Blake * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19c1bb86cdSEric Blake * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20c1bb86cdSEric Blake * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21c1bb86cdSEric Blake * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22c1bb86cdSEric Blake * THE SOFTWARE. 23c1bb86cdSEric Blake */ 24922a01a0SMarkus Armbruster 25c1bb86cdSEric Blake #include "qemu/osdep.h" 26a8d25326SMarkus Armbruster #include "qemu-common.h" 27c1bb86cdSEric Blake #include "qapi/error.h" 28c1bb86cdSEric Blake #include "qemu/cutils.h" 29c1bb86cdSEric Blake #include "qemu/error-report.h" 30c1bb86cdSEric Blake #include "block/block_int.h" 31c1bb86cdSEric Blake #include "qemu/module.h" 32922a01a0SMarkus Armbruster #include "qemu/option.h" 33c1bb86cdSEric Blake #include "trace.h" 34c1bb86cdSEric Blake #include "block/thread-pool.h" 35c1bb86cdSEric Blake #include "qemu/iov.h" 36c1bb86cdSEric Blake #include "block/raw-aio.h" 37452fcdbcSMarkus Armbruster #include "qapi/qmp/qdict.h" 38c1bb86cdSEric Blake #include "qapi/qmp/qstring.h" 39c1bb86cdSEric Blake 407c9e5276SPaolo Bonzini #include "scsi/pr-manager.h" 417c9e5276SPaolo Bonzini #include "scsi/constants.h" 427c9e5276SPaolo Bonzini 43c1bb86cdSEric Blake #if defined(__APPLE__) && (__MACH__) 44c1bb86cdSEric Blake #include <paths.h> 45c1bb86cdSEric Blake #include <sys/param.h> 46c1bb86cdSEric Blake #include <IOKit/IOKitLib.h> 47c1bb86cdSEric Blake #include <IOKit/IOBSD.h> 48c1bb86cdSEric Blake #include <IOKit/storage/IOMediaBSDClient.h> 49c1bb86cdSEric Blake #include <IOKit/storage/IOMedia.h> 50c1bb86cdSEric Blake #include <IOKit/storage/IOCDMedia.h> 51c1bb86cdSEric Blake //#include <IOKit/storage/IOCDTypes.h> 52c1bb86cdSEric Blake #include <IOKit/storage/IODVDMedia.h> 53c1bb86cdSEric Blake #include <CoreFoundation/CoreFoundation.h> 54c1bb86cdSEric Blake #endif 55c1bb86cdSEric Blake 56c1bb86cdSEric Blake #ifdef __sun__ 57c1bb86cdSEric Blake #define _POSIX_PTHREAD_SEMANTICS 1 58c1bb86cdSEric Blake #include <sys/dkio.h> 59c1bb86cdSEric Blake #endif 60c1bb86cdSEric Blake #ifdef __linux__ 61c1bb86cdSEric Blake #include <sys/ioctl.h> 62c1bb86cdSEric Blake #include <sys/param.h> 631efad060SFam Zheng #include <sys/syscall.h> 64c1bb86cdSEric Blake #include <linux/cdrom.h> 65c1bb86cdSEric Blake #include <linux/fd.h> 66c1bb86cdSEric Blake #include <linux/fs.h> 67c1bb86cdSEric Blake #include <linux/hdreg.h> 68c1bb86cdSEric Blake #include <scsi/sg.h> 69c1bb86cdSEric Blake #ifdef __s390__ 70c1bb86cdSEric Blake #include <asm/dasd.h> 71c1bb86cdSEric Blake #endif 72c1bb86cdSEric Blake #ifndef FS_NOCOW_FL 73c1bb86cdSEric Blake #define FS_NOCOW_FL 0x00800000 /* Do not cow file */ 74c1bb86cdSEric Blake #endif 75c1bb86cdSEric Blake #endif 76c1bb86cdSEric Blake #if defined(CONFIG_FALLOCATE_PUNCH_HOLE) || defined(CONFIG_FALLOCATE_ZERO_RANGE) 77c1bb86cdSEric Blake #include <linux/falloc.h> 78c1bb86cdSEric Blake #endif 79c1bb86cdSEric Blake #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__) 80c1bb86cdSEric Blake #include <sys/disk.h> 81c1bb86cdSEric Blake #include <sys/cdio.h> 82c1bb86cdSEric Blake #endif 83c1bb86cdSEric Blake 84c1bb86cdSEric Blake #ifdef __OpenBSD__ 85c1bb86cdSEric Blake #include <sys/ioctl.h> 86c1bb86cdSEric Blake #include <sys/disklabel.h> 87c1bb86cdSEric Blake #include <sys/dkio.h> 88c1bb86cdSEric Blake #endif 89c1bb86cdSEric Blake 90c1bb86cdSEric Blake #ifdef __NetBSD__ 91c1bb86cdSEric Blake #include <sys/ioctl.h> 92c1bb86cdSEric Blake #include <sys/disklabel.h> 93c1bb86cdSEric Blake #include <sys/dkio.h> 94c1bb86cdSEric Blake #include <sys/disk.h> 95c1bb86cdSEric Blake #endif 96c1bb86cdSEric Blake 97c1bb86cdSEric Blake #ifdef __DragonFly__ 98c1bb86cdSEric Blake #include <sys/ioctl.h> 99c1bb86cdSEric Blake #include <sys/diskslice.h> 100c1bb86cdSEric Blake #endif 101c1bb86cdSEric Blake 102c1bb86cdSEric Blake #ifdef CONFIG_XFS 103c1bb86cdSEric Blake #include <xfs/xfs.h> 104c1bb86cdSEric Blake #endif 105c1bb86cdSEric Blake 1064f7d28d7SLaurent Vivier #include "trace.h" 107c1bb86cdSEric Blake 108c1bb86cdSEric Blake /* OS X does not have O_DSYNC */ 109c1bb86cdSEric Blake #ifndef O_DSYNC 110c1bb86cdSEric Blake #ifdef O_SYNC 111c1bb86cdSEric Blake #define O_DSYNC O_SYNC 112c1bb86cdSEric Blake #elif defined(O_FSYNC) 113c1bb86cdSEric Blake #define O_DSYNC O_FSYNC 114c1bb86cdSEric Blake #endif 115c1bb86cdSEric Blake #endif 116c1bb86cdSEric Blake 117c1bb86cdSEric Blake /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */ 118c1bb86cdSEric Blake #ifndef O_DIRECT 119c1bb86cdSEric Blake #define O_DIRECT O_DSYNC 120c1bb86cdSEric Blake #endif 121c1bb86cdSEric Blake 122c1bb86cdSEric Blake #define FTYPE_FILE 0 123c1bb86cdSEric Blake #define FTYPE_CD 1 124c1bb86cdSEric Blake 125c1bb86cdSEric Blake #define MAX_BLOCKSIZE 4096 126c1bb86cdSEric Blake 127244a5668SFam Zheng /* Posix file locking bytes. Libvirt takes byte 0, we start from higher bytes, 128244a5668SFam Zheng * leaving a few more bytes for its future use. */ 129244a5668SFam Zheng #define RAW_LOCK_PERM_BASE 100 130244a5668SFam Zheng #define RAW_LOCK_SHARED_BASE 200 131244a5668SFam Zheng 132c1bb86cdSEric Blake typedef struct BDRVRawState { 133c1bb86cdSEric Blake int fd; 134244a5668SFam Zheng bool use_lock; 135c1bb86cdSEric Blake int type; 136c1bb86cdSEric Blake int open_flags; 137c1bb86cdSEric Blake size_t buf_align; 138c1bb86cdSEric Blake 139244a5668SFam Zheng /* The current permissions. */ 140244a5668SFam Zheng uint64_t perm; 141244a5668SFam Zheng uint64_t shared_perm; 142244a5668SFam Zheng 1432996ffadSFam Zheng /* The perms bits whose corresponding bytes are already locked in 144f2e3af29SFam Zheng * s->fd. */ 1452996ffadSFam Zheng uint64_t locked_perm; 1462996ffadSFam Zheng uint64_t locked_shared_perm; 1472996ffadSFam Zheng 1486ceabe6fSKevin Wolf int perm_change_fd; 149094e3639SMax Reitz int perm_change_flags; 150e0c9cf3aSKevin Wolf BDRVReopenState *reopen_state; 151e0c9cf3aSKevin Wolf 152c1bb86cdSEric Blake #ifdef CONFIG_XFS 153c1bb86cdSEric Blake bool is_xfs:1; 154c1bb86cdSEric Blake #endif 155c1bb86cdSEric Blake bool has_discard:1; 156c1bb86cdSEric Blake bool has_write_zeroes:1; 157c1bb86cdSEric Blake bool discard_zeroes:1; 158c1bb86cdSEric Blake bool use_linux_aio:1; 159e5bcf967SKevin Wolf bool page_cache_inconsistent:1; 160c1bb86cdSEric Blake bool has_fallocate; 161c1bb86cdSEric Blake bool needs_alignment; 162f357fcd8SStefan Hajnoczi bool drop_cache; 16331be8a2aSStefan Hajnoczi bool check_cache_dropped; 1647c9e5276SPaolo Bonzini 1657c9e5276SPaolo Bonzini PRManager *pr_mgr; 166c1bb86cdSEric Blake } BDRVRawState; 167c1bb86cdSEric Blake 168c1bb86cdSEric Blake typedef struct BDRVRawReopenState { 169c1bb86cdSEric Blake int fd; 170c1bb86cdSEric Blake int open_flags; 171f357fcd8SStefan Hajnoczi bool drop_cache; 17231be8a2aSStefan Hajnoczi bool check_cache_dropped; 173c1bb86cdSEric Blake } BDRVRawReopenState; 174c1bb86cdSEric Blake 175c1bb86cdSEric Blake static int fd_open(BlockDriverState *bs); 176c1bb86cdSEric Blake static int64_t raw_getlength(BlockDriverState *bs); 177c1bb86cdSEric Blake 178c1bb86cdSEric Blake typedef struct RawPosixAIOData { 179c1bb86cdSEric Blake BlockDriverState *bs; 180c1bb86cdSEric Blake int aio_type; 181d57c44d0SKevin Wolf int aio_fildes; 182d57c44d0SKevin Wolf 183d57c44d0SKevin Wolf off_t aio_offset; 184d57c44d0SKevin Wolf uint64_t aio_nbytes; 185d57c44d0SKevin Wolf 18693f4e2ffSKevin Wolf union { 18793f4e2ffSKevin Wolf struct { 188d57c44d0SKevin Wolf struct iovec *iov; 189d57c44d0SKevin Wolf int niov; 190d57c44d0SKevin Wolf } io; 191d57c44d0SKevin Wolf struct { 192d57c44d0SKevin Wolf uint64_t cmd; 193d57c44d0SKevin Wolf void *buf; 194d57c44d0SKevin Wolf } ioctl; 195d57c44d0SKevin Wolf struct { 1961efad060SFam Zheng int aio_fd2; 1971efad060SFam Zheng off_t aio_offset2; 198d57c44d0SKevin Wolf } copy_range; 19993f4e2ffSKevin Wolf struct { 20093f4e2ffSKevin Wolf PreallocMode prealloc; 20193f4e2ffSKevin Wolf Error **errp; 202d57c44d0SKevin Wolf } truncate; 20393f4e2ffSKevin Wolf }; 204c1bb86cdSEric Blake } RawPosixAIOData; 205c1bb86cdSEric Blake 206c1bb86cdSEric Blake #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) 207c1bb86cdSEric Blake static int cdrom_reopen(BlockDriverState *bs); 208c1bb86cdSEric Blake #endif 209c1bb86cdSEric Blake 210c1bb86cdSEric Blake #if defined(__NetBSD__) 211db0754dfSFam Zheng static int raw_normalize_devicepath(const char **filename, Error **errp) 212c1bb86cdSEric Blake { 213c1bb86cdSEric Blake static char namebuf[PATH_MAX]; 214c1bb86cdSEric Blake const char *dp, *fname; 215c1bb86cdSEric Blake struct stat sb; 216c1bb86cdSEric Blake 217c1bb86cdSEric Blake fname = *filename; 218c1bb86cdSEric Blake dp = strrchr(fname, '/'); 219c1bb86cdSEric Blake if (lstat(fname, &sb) < 0) { 220f6fc1e30SPaolo Bonzini error_setg_file_open(errp, errno, fname); 221c1bb86cdSEric Blake return -errno; 222c1bb86cdSEric Blake } 223c1bb86cdSEric Blake 224c1bb86cdSEric Blake if (!S_ISBLK(sb.st_mode)) { 225c1bb86cdSEric Blake return 0; 226c1bb86cdSEric Blake } 227c1bb86cdSEric Blake 228c1bb86cdSEric Blake if (dp == NULL) { 229c1bb86cdSEric Blake snprintf(namebuf, PATH_MAX, "r%s", fname); 230c1bb86cdSEric Blake } else { 231c1bb86cdSEric Blake snprintf(namebuf, PATH_MAX, "%.*s/r%s", 232c1bb86cdSEric Blake (int)(dp - fname), fname, dp + 1); 233c1bb86cdSEric Blake } 234c1bb86cdSEric Blake *filename = namebuf; 235db0754dfSFam Zheng warn_report("%s is a block device, using %s", fname, *filename); 236c1bb86cdSEric Blake 237c1bb86cdSEric Blake return 0; 238c1bb86cdSEric Blake } 239c1bb86cdSEric Blake #else 240db0754dfSFam Zheng static int raw_normalize_devicepath(const char **filename, Error **errp) 241c1bb86cdSEric Blake { 242c1bb86cdSEric Blake return 0; 243c1bb86cdSEric Blake } 244c1bb86cdSEric Blake #endif 245c1bb86cdSEric Blake 246c1bb86cdSEric Blake /* 247c1bb86cdSEric Blake * Get logical block size via ioctl. On success store it in @sector_size_p. 248c1bb86cdSEric Blake */ 249c1bb86cdSEric Blake static int probe_logical_blocksize(int fd, unsigned int *sector_size_p) 250c1bb86cdSEric Blake { 251c1bb86cdSEric Blake unsigned int sector_size; 252c1bb86cdSEric Blake bool success = false; 253700f9ce0SPeter Maydell int i; 254c1bb86cdSEric Blake 255c1bb86cdSEric Blake errno = ENOTSUP; 256700f9ce0SPeter Maydell static const unsigned long ioctl_list[] = { 257c1bb86cdSEric Blake #ifdef BLKSSZGET 258700f9ce0SPeter Maydell BLKSSZGET, 259c1bb86cdSEric Blake #endif 260c1bb86cdSEric Blake #ifdef DKIOCGETBLOCKSIZE 261700f9ce0SPeter Maydell DKIOCGETBLOCKSIZE, 262c1bb86cdSEric Blake #endif 263c1bb86cdSEric Blake #ifdef DIOCGSECTORSIZE 264700f9ce0SPeter Maydell DIOCGSECTORSIZE, 265700f9ce0SPeter Maydell #endif 266700f9ce0SPeter Maydell }; 267700f9ce0SPeter Maydell 268700f9ce0SPeter Maydell /* Try a few ioctls to get the right size */ 269700f9ce0SPeter Maydell for (i = 0; i < (int)ARRAY_SIZE(ioctl_list); i++) { 270700f9ce0SPeter Maydell if (ioctl(fd, ioctl_list[i], §or_size) >= 0) { 271c1bb86cdSEric Blake *sector_size_p = sector_size; 272c1bb86cdSEric Blake success = true; 273c1bb86cdSEric Blake } 274700f9ce0SPeter Maydell } 275c1bb86cdSEric Blake 276c1bb86cdSEric Blake return success ? 0 : -errno; 277c1bb86cdSEric Blake } 278c1bb86cdSEric Blake 279c1bb86cdSEric Blake /** 280c1bb86cdSEric Blake * Get physical block size of @fd. 281c1bb86cdSEric Blake * On success, store it in @blk_size and return 0. 282c1bb86cdSEric Blake * On failure, return -errno. 283c1bb86cdSEric Blake */ 284c1bb86cdSEric Blake static int probe_physical_blocksize(int fd, unsigned int *blk_size) 285c1bb86cdSEric Blake { 286c1bb86cdSEric Blake #ifdef BLKPBSZGET 287c1bb86cdSEric Blake if (ioctl(fd, BLKPBSZGET, blk_size) < 0) { 288c1bb86cdSEric Blake return -errno; 289c1bb86cdSEric Blake } 290c1bb86cdSEric Blake return 0; 291c1bb86cdSEric Blake #else 292c1bb86cdSEric Blake return -ENOTSUP; 293c1bb86cdSEric Blake #endif 294c1bb86cdSEric Blake } 295c1bb86cdSEric Blake 296c1bb86cdSEric Blake /* Check if read is allowed with given memory buffer and length. 297c1bb86cdSEric Blake * 298c1bb86cdSEric Blake * This function is used to check O_DIRECT memory buffer and request alignment. 299c1bb86cdSEric Blake */ 300c1bb86cdSEric Blake static bool raw_is_io_aligned(int fd, void *buf, size_t len) 301c1bb86cdSEric Blake { 302c1bb86cdSEric Blake ssize_t ret = pread(fd, buf, len, 0); 303c1bb86cdSEric Blake 304c1bb86cdSEric Blake if (ret >= 0) { 305c1bb86cdSEric Blake return true; 306c1bb86cdSEric Blake } 307c1bb86cdSEric Blake 308c1bb86cdSEric Blake #ifdef __linux__ 309c1bb86cdSEric Blake /* The Linux kernel returns EINVAL for misaligned O_DIRECT reads. Ignore 310c1bb86cdSEric Blake * other errors (e.g. real I/O error), which could happen on a failed 311c1bb86cdSEric Blake * drive, since we only care about probing alignment. 312c1bb86cdSEric Blake */ 313c1bb86cdSEric Blake if (errno != EINVAL) { 314c1bb86cdSEric Blake return true; 315c1bb86cdSEric Blake } 316c1bb86cdSEric Blake #endif 317c1bb86cdSEric Blake 318c1bb86cdSEric Blake return false; 319c1bb86cdSEric Blake } 320c1bb86cdSEric Blake 321c1bb86cdSEric Blake static void raw_probe_alignment(BlockDriverState *bs, int fd, Error **errp) 322c1bb86cdSEric Blake { 323c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 324c1bb86cdSEric Blake char *buf; 325c1bb86cdSEric Blake size_t max_align = MAX(MAX_BLOCKSIZE, getpagesize()); 326a6b257a0SNir Soffer size_t alignments[] = {1, 512, 1024, 2048, 4096}; 327c1bb86cdSEric Blake 328c1bb86cdSEric Blake /* For SCSI generic devices the alignment is not really used. 329c1bb86cdSEric Blake With buffered I/O, we don't have any restrictions. */ 330c1bb86cdSEric Blake if (bdrv_is_sg(bs) || !s->needs_alignment) { 331c1bb86cdSEric Blake bs->bl.request_alignment = 1; 332c1bb86cdSEric Blake s->buf_align = 1; 333c1bb86cdSEric Blake return; 334c1bb86cdSEric Blake } 335c1bb86cdSEric Blake 336c1bb86cdSEric Blake bs->bl.request_alignment = 0; 337c1bb86cdSEric Blake s->buf_align = 0; 338c1bb86cdSEric Blake /* Let's try to use the logical blocksize for the alignment. */ 339c1bb86cdSEric Blake if (probe_logical_blocksize(fd, &bs->bl.request_alignment) < 0) { 340c1bb86cdSEric Blake bs->bl.request_alignment = 0; 341c1bb86cdSEric Blake } 342c1bb86cdSEric Blake #ifdef CONFIG_XFS 343c1bb86cdSEric Blake if (s->is_xfs) { 344c1bb86cdSEric Blake struct dioattr da; 345c1bb86cdSEric Blake if (xfsctl(NULL, fd, XFS_IOC_DIOINFO, &da) >= 0) { 346c1bb86cdSEric Blake bs->bl.request_alignment = da.d_miniosz; 347c1bb86cdSEric Blake /* The kernel returns wrong information for d_mem */ 348c1bb86cdSEric Blake /* s->buf_align = da.d_mem; */ 349c1bb86cdSEric Blake } 350c1bb86cdSEric Blake } 351c1bb86cdSEric Blake #endif 352c1bb86cdSEric Blake 353a6b257a0SNir Soffer /* 354a6b257a0SNir Soffer * If we could not get the sizes so far, we can only guess them. First try 355a6b257a0SNir Soffer * to detect request alignment, since it is more likely to succeed. Then 356a6b257a0SNir Soffer * try to detect buf_align, which cannot be detected in some cases (e.g. 357a6b257a0SNir Soffer * Gluster). If buf_align cannot be detected, we fallback to the value of 358a6b257a0SNir Soffer * request_alignment. 359a6b257a0SNir Soffer */ 360a6b257a0SNir Soffer 361a6b257a0SNir Soffer if (!bs->bl.request_alignment) { 362a6b257a0SNir Soffer int i; 363c1bb86cdSEric Blake size_t align; 364a6b257a0SNir Soffer buf = qemu_memalign(max_align, max_align); 365a6b257a0SNir Soffer for (i = 0; i < ARRAY_SIZE(alignments); i++) { 366a6b257a0SNir Soffer align = alignments[i]; 367a6b257a0SNir Soffer if (raw_is_io_aligned(fd, buf, align)) { 368a6b257a0SNir Soffer /* Fallback to safe value. */ 369a6b257a0SNir Soffer bs->bl.request_alignment = (align != 1) ? align : max_align; 370c1bb86cdSEric Blake break; 371c1bb86cdSEric Blake } 372c1bb86cdSEric Blake } 373c1bb86cdSEric Blake qemu_vfree(buf); 374c1bb86cdSEric Blake } 375c1bb86cdSEric Blake 376a6b257a0SNir Soffer if (!s->buf_align) { 377a6b257a0SNir Soffer int i; 378c1bb86cdSEric Blake size_t align; 379a6b257a0SNir Soffer buf = qemu_memalign(max_align, 2 * max_align); 380a6b257a0SNir Soffer for (i = 0; i < ARRAY_SIZE(alignments); i++) { 381a6b257a0SNir Soffer align = alignments[i]; 382a6b257a0SNir Soffer if (raw_is_io_aligned(fd, buf + align, max_align)) { 383236094c7SStefan Hajnoczi /* Fallback to request_alignment. */ 384a6b257a0SNir Soffer s->buf_align = (align != 1) ? align : bs->bl.request_alignment; 385c1bb86cdSEric Blake break; 386c1bb86cdSEric Blake } 387c1bb86cdSEric Blake } 388c1bb86cdSEric Blake qemu_vfree(buf); 389c1bb86cdSEric Blake } 390c1bb86cdSEric Blake 391c1bb86cdSEric Blake if (!s->buf_align || !bs->bl.request_alignment) { 392c1bb86cdSEric Blake error_setg(errp, "Could not find working O_DIRECT alignment"); 393c1bb86cdSEric Blake error_append_hint(errp, "Try cache.direct=off\n"); 394c1bb86cdSEric Blake } 395c1bb86cdSEric Blake } 396c1bb86cdSEric Blake 39723dece19SKevin Wolf static void raw_parse_flags(int bdrv_flags, int *open_flags, bool has_writers) 398c1bb86cdSEric Blake { 39923dece19SKevin Wolf bool read_write = false; 400c1bb86cdSEric Blake assert(open_flags != NULL); 401c1bb86cdSEric Blake 402c1bb86cdSEric Blake *open_flags |= O_BINARY; 403c1bb86cdSEric Blake *open_flags &= ~O_ACCMODE; 40423dece19SKevin Wolf 40523dece19SKevin Wolf if (bdrv_flags & BDRV_O_AUTO_RDONLY) { 40623dece19SKevin Wolf read_write = has_writers; 40723dece19SKevin Wolf } else if (bdrv_flags & BDRV_O_RDWR) { 40823dece19SKevin Wolf read_write = true; 40923dece19SKevin Wolf } 41023dece19SKevin Wolf 41123dece19SKevin Wolf if (read_write) { 412c1bb86cdSEric Blake *open_flags |= O_RDWR; 413c1bb86cdSEric Blake } else { 414c1bb86cdSEric Blake *open_flags |= O_RDONLY; 415c1bb86cdSEric Blake } 416c1bb86cdSEric Blake 417c1bb86cdSEric Blake /* Use O_DSYNC for write-through caching, no flags for write-back caching, 418c1bb86cdSEric Blake * and O_DIRECT for no caching. */ 419c1bb86cdSEric Blake if ((bdrv_flags & BDRV_O_NOCACHE)) { 420c1bb86cdSEric Blake *open_flags |= O_DIRECT; 421c1bb86cdSEric Blake } 422c1bb86cdSEric Blake } 423c1bb86cdSEric Blake 424c1bb86cdSEric Blake static void raw_parse_filename(const char *filename, QDict *options, 425c1bb86cdSEric Blake Error **errp) 426c1bb86cdSEric Blake { 42703c320d8SMax Reitz bdrv_parse_filename_strip_prefix(filename, "file:", options); 428c1bb86cdSEric Blake } 429c1bb86cdSEric Blake 430c1bb86cdSEric Blake static QemuOptsList raw_runtime_opts = { 431c1bb86cdSEric Blake .name = "raw", 432c1bb86cdSEric Blake .head = QTAILQ_HEAD_INITIALIZER(raw_runtime_opts.head), 433c1bb86cdSEric Blake .desc = { 434c1bb86cdSEric Blake { 435c1bb86cdSEric Blake .name = "filename", 436c1bb86cdSEric Blake .type = QEMU_OPT_STRING, 437c1bb86cdSEric Blake .help = "File name of the image", 438c1bb86cdSEric Blake }, 439c1bb86cdSEric Blake { 440c1bb86cdSEric Blake .name = "aio", 441c1bb86cdSEric Blake .type = QEMU_OPT_STRING, 442c1bb86cdSEric Blake .help = "host AIO implementation (threads, native)", 443c1bb86cdSEric Blake }, 44416b48d5dSFam Zheng { 44516b48d5dSFam Zheng .name = "locking", 44616b48d5dSFam Zheng .type = QEMU_OPT_STRING, 44716b48d5dSFam Zheng .help = "file locking mode (on/off/auto, default: auto)", 44816b48d5dSFam Zheng }, 4497c9e5276SPaolo Bonzini { 4507c9e5276SPaolo Bonzini .name = "pr-manager", 4517c9e5276SPaolo Bonzini .type = QEMU_OPT_STRING, 4527c9e5276SPaolo Bonzini .help = "id of persistent reservation manager object (default: none)", 4537c9e5276SPaolo Bonzini }, 454f357fcd8SStefan Hajnoczi #if defined(__linux__) 455f357fcd8SStefan Hajnoczi { 456f357fcd8SStefan Hajnoczi .name = "drop-cache", 457f357fcd8SStefan Hajnoczi .type = QEMU_OPT_BOOL, 458f357fcd8SStefan Hajnoczi .help = "invalidate page cache during live migration (default: on)", 459f357fcd8SStefan Hajnoczi }, 460f357fcd8SStefan Hajnoczi #endif 46131be8a2aSStefan Hajnoczi { 46231be8a2aSStefan Hajnoczi .name = "x-check-cache-dropped", 46331be8a2aSStefan Hajnoczi .type = QEMU_OPT_BOOL, 46431be8a2aSStefan Hajnoczi .help = "check that page cache was dropped on live migration (default: off)" 46531be8a2aSStefan Hajnoczi }, 466c1bb86cdSEric Blake { /* end of list */ } 467c1bb86cdSEric Blake }, 468c1bb86cdSEric Blake }; 469c1bb86cdSEric Blake 4708a2ce0bcSAlberto Garcia static const char *const mutable_opts[] = { "x-check-cache-dropped", NULL }; 4718a2ce0bcSAlberto Garcia 472c1bb86cdSEric Blake static int raw_open_common(BlockDriverState *bs, QDict *options, 473230ff739SJohn Snow int bdrv_flags, int open_flags, 474230ff739SJohn Snow bool device, Error **errp) 475c1bb86cdSEric Blake { 476c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 477c1bb86cdSEric Blake QemuOpts *opts; 478c1bb86cdSEric Blake Error *local_err = NULL; 479c1bb86cdSEric Blake const char *filename = NULL; 4807c9e5276SPaolo Bonzini const char *str; 481c1bb86cdSEric Blake BlockdevAioOptions aio, aio_default; 482c1bb86cdSEric Blake int fd, ret; 483c1bb86cdSEric Blake struct stat st; 484244a5668SFam Zheng OnOffAuto locking; 485c1bb86cdSEric Blake 486c1bb86cdSEric Blake opts = qemu_opts_create(&raw_runtime_opts, NULL, 0, &error_abort); 487c1bb86cdSEric Blake qemu_opts_absorb_qdict(opts, options, &local_err); 488c1bb86cdSEric Blake if (local_err) { 489c1bb86cdSEric Blake error_propagate(errp, local_err); 490c1bb86cdSEric Blake ret = -EINVAL; 491c1bb86cdSEric Blake goto fail; 492c1bb86cdSEric Blake } 493c1bb86cdSEric Blake 494c1bb86cdSEric Blake filename = qemu_opt_get(opts, "filename"); 495c1bb86cdSEric Blake 496db0754dfSFam Zheng ret = raw_normalize_devicepath(&filename, errp); 497c1bb86cdSEric Blake if (ret != 0) { 498c1bb86cdSEric Blake goto fail; 499c1bb86cdSEric Blake } 500c1bb86cdSEric Blake 501c1bb86cdSEric Blake aio_default = (bdrv_flags & BDRV_O_NATIVE_AIO) 502c1bb86cdSEric Blake ? BLOCKDEV_AIO_OPTIONS_NATIVE 503c1bb86cdSEric Blake : BLOCKDEV_AIO_OPTIONS_THREADS; 504f7abe0ecSMarc-André Lureau aio = qapi_enum_parse(&BlockdevAioOptions_lookup, 505f7abe0ecSMarc-André Lureau qemu_opt_get(opts, "aio"), 50606c60b6cSMarkus Armbruster aio_default, &local_err); 507c1bb86cdSEric Blake if (local_err) { 508c1bb86cdSEric Blake error_propagate(errp, local_err); 509c1bb86cdSEric Blake ret = -EINVAL; 510c1bb86cdSEric Blake goto fail; 511c1bb86cdSEric Blake } 512c1bb86cdSEric Blake s->use_linux_aio = (aio == BLOCKDEV_AIO_OPTIONS_NATIVE); 513c1bb86cdSEric Blake 514f7abe0ecSMarc-André Lureau locking = qapi_enum_parse(&OnOffAuto_lookup, 515f7abe0ecSMarc-André Lureau qemu_opt_get(opts, "locking"), 51606c60b6cSMarkus Armbruster ON_OFF_AUTO_AUTO, &local_err); 517244a5668SFam Zheng if (local_err) { 518244a5668SFam Zheng error_propagate(errp, local_err); 519244a5668SFam Zheng ret = -EINVAL; 520244a5668SFam Zheng goto fail; 521244a5668SFam Zheng } 522244a5668SFam Zheng switch (locking) { 523244a5668SFam Zheng case ON_OFF_AUTO_ON: 524244a5668SFam Zheng s->use_lock = true; 5252b218f5dSFam Zheng if (!qemu_has_ofd_lock()) { 526db0754dfSFam Zheng warn_report("File lock requested but OFD locking syscall is " 527db0754dfSFam Zheng "unavailable, falling back to POSIX file locks"); 528db0754dfSFam Zheng error_printf("Due to the implementation, locks can be lost " 5292b218f5dSFam Zheng "unexpectedly.\n"); 5302b218f5dSFam Zheng } 531244a5668SFam Zheng break; 532244a5668SFam Zheng case ON_OFF_AUTO_OFF: 533244a5668SFam Zheng s->use_lock = false; 534244a5668SFam Zheng break; 535244a5668SFam Zheng case ON_OFF_AUTO_AUTO: 5362b218f5dSFam Zheng s->use_lock = qemu_has_ofd_lock(); 537244a5668SFam Zheng break; 538244a5668SFam Zheng default: 539244a5668SFam Zheng abort(); 540244a5668SFam Zheng } 541244a5668SFam Zheng 5427c9e5276SPaolo Bonzini str = qemu_opt_get(opts, "pr-manager"); 5437c9e5276SPaolo Bonzini if (str) { 5447c9e5276SPaolo Bonzini s->pr_mgr = pr_manager_lookup(str, &local_err); 5457c9e5276SPaolo Bonzini if (local_err) { 5467c9e5276SPaolo Bonzini error_propagate(errp, local_err); 5477c9e5276SPaolo Bonzini ret = -EINVAL; 5487c9e5276SPaolo Bonzini goto fail; 5497c9e5276SPaolo Bonzini } 5507c9e5276SPaolo Bonzini } 5517c9e5276SPaolo Bonzini 552f357fcd8SStefan Hajnoczi s->drop_cache = qemu_opt_get_bool(opts, "drop-cache", true); 55331be8a2aSStefan Hajnoczi s->check_cache_dropped = qemu_opt_get_bool(opts, "x-check-cache-dropped", 55431be8a2aSStefan Hajnoczi false); 55531be8a2aSStefan Hajnoczi 556c1bb86cdSEric Blake s->open_flags = open_flags; 55723dece19SKevin Wolf raw_parse_flags(bdrv_flags, &s->open_flags, false); 558c1bb86cdSEric Blake 559c1bb86cdSEric Blake s->fd = -1; 560c1bb86cdSEric Blake fd = qemu_open(filename, s->open_flags, 0644); 56164107dc0SKevin Wolf ret = fd < 0 ? -errno : 0; 56264107dc0SKevin Wolf 56364107dc0SKevin Wolf if (ret < 0) { 564f6fc1e30SPaolo Bonzini error_setg_file_open(errp, -ret, filename); 565c1bb86cdSEric Blake if (ret == -EROFS) { 566c1bb86cdSEric Blake ret = -EACCES; 567c1bb86cdSEric Blake } 568c1bb86cdSEric Blake goto fail; 569c1bb86cdSEric Blake } 570c1bb86cdSEric Blake s->fd = fd; 571c1bb86cdSEric Blake 572244a5668SFam Zheng s->perm = 0; 573244a5668SFam Zheng s->shared_perm = BLK_PERM_ALL; 574244a5668SFam Zheng 575c1bb86cdSEric Blake #ifdef CONFIG_LINUX_AIO 576c1bb86cdSEric Blake /* Currently Linux does AIO only for files opened with O_DIRECT */ 577ed6e2161SNishanth Aravamudan if (s->use_linux_aio) { 578ed6e2161SNishanth Aravamudan if (!(s->open_flags & O_DIRECT)) { 579c1bb86cdSEric Blake error_setg(errp, "aio=native was specified, but it requires " 580c1bb86cdSEric Blake "cache.direct=on, which was not specified."); 581c1bb86cdSEric Blake ret = -EINVAL; 582c1bb86cdSEric Blake goto fail; 583c1bb86cdSEric Blake } 584ed6e2161SNishanth Aravamudan if (!aio_setup_linux_aio(bdrv_get_aio_context(bs), errp)) { 585ed6e2161SNishanth Aravamudan error_prepend(errp, "Unable to use native AIO: "); 586ed6e2161SNishanth Aravamudan goto fail; 587ed6e2161SNishanth Aravamudan } 588ed6e2161SNishanth Aravamudan } 589c1bb86cdSEric Blake #else 590c1bb86cdSEric Blake if (s->use_linux_aio) { 591c1bb86cdSEric Blake error_setg(errp, "aio=native was specified, but is not supported " 592c1bb86cdSEric Blake "in this build."); 593c1bb86cdSEric Blake ret = -EINVAL; 594c1bb86cdSEric Blake goto fail; 595c1bb86cdSEric Blake } 596c1bb86cdSEric Blake #endif /* !defined(CONFIG_LINUX_AIO) */ 597c1bb86cdSEric Blake 598c1bb86cdSEric Blake s->has_discard = true; 599c1bb86cdSEric Blake s->has_write_zeroes = true; 600c1bb86cdSEric Blake if ((bs->open_flags & BDRV_O_NOCACHE) != 0) { 601c1bb86cdSEric Blake s->needs_alignment = true; 602c1bb86cdSEric Blake } 603c1bb86cdSEric Blake 604c1bb86cdSEric Blake if (fstat(s->fd, &st) < 0) { 605c1bb86cdSEric Blake ret = -errno; 606c1bb86cdSEric Blake error_setg_errno(errp, errno, "Could not stat file"); 607c1bb86cdSEric Blake goto fail; 608c1bb86cdSEric Blake } 609230ff739SJohn Snow 610230ff739SJohn Snow if (!device) { 611230ff739SJohn Snow if (S_ISBLK(st.st_mode)) { 612230ff739SJohn Snow warn_report("Opening a block device as a file using the '%s' " 613230ff739SJohn Snow "driver is deprecated", bs->drv->format_name); 614230ff739SJohn Snow } else if (S_ISCHR(st.st_mode)) { 615230ff739SJohn Snow warn_report("Opening a character device as a file using the '%s' " 616230ff739SJohn Snow "driver is deprecated", bs->drv->format_name); 617230ff739SJohn Snow } else if (!S_ISREG(st.st_mode)) { 618230ff739SJohn Snow error_setg(errp, "A regular file was expected by the '%s' driver, " 619230ff739SJohn Snow "but something else was given", bs->drv->format_name); 620230ff739SJohn Snow ret = -EINVAL; 621230ff739SJohn Snow goto fail; 622230ff739SJohn Snow } else { 623c1bb86cdSEric Blake s->discard_zeroes = true; 624c1bb86cdSEric Blake s->has_fallocate = true; 625c1bb86cdSEric Blake } 626230ff739SJohn Snow } else { 627230ff739SJohn Snow if (!(S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) { 628230ff739SJohn Snow error_setg(errp, "'%s' driver expects either " 629230ff739SJohn Snow "a character or block device", bs->drv->format_name); 630230ff739SJohn Snow ret = -EINVAL; 631230ff739SJohn Snow goto fail; 632230ff739SJohn Snow } 633230ff739SJohn Snow } 634230ff739SJohn Snow 635c1bb86cdSEric Blake if (S_ISBLK(st.st_mode)) { 636c1bb86cdSEric Blake #ifdef BLKDISCARDZEROES 637c1bb86cdSEric Blake unsigned int arg; 638c1bb86cdSEric Blake if (ioctl(s->fd, BLKDISCARDZEROES, &arg) == 0 && arg) { 639c1bb86cdSEric Blake s->discard_zeroes = true; 640c1bb86cdSEric Blake } 641c1bb86cdSEric Blake #endif 642c1bb86cdSEric Blake #ifdef __linux__ 643c1bb86cdSEric Blake /* On Linux 3.10, BLKDISCARD leaves stale data in the page cache. Do 644c1bb86cdSEric Blake * not rely on the contents of discarded blocks unless using O_DIRECT. 645c1bb86cdSEric Blake * Same for BLKZEROOUT. 646c1bb86cdSEric Blake */ 647c1bb86cdSEric Blake if (!(bs->open_flags & BDRV_O_NOCACHE)) { 648c1bb86cdSEric Blake s->discard_zeroes = false; 649c1bb86cdSEric Blake s->has_write_zeroes = false; 650c1bb86cdSEric Blake } 651c1bb86cdSEric Blake #endif 652c1bb86cdSEric Blake } 653c1bb86cdSEric Blake #ifdef __FreeBSD__ 654c1bb86cdSEric Blake if (S_ISCHR(st.st_mode)) { 655c1bb86cdSEric Blake /* 656c1bb86cdSEric Blake * The file is a char device (disk), which on FreeBSD isn't behind 657c1bb86cdSEric Blake * a pager, so force all requests to be aligned. This is needed 658c1bb86cdSEric Blake * so QEMU makes sure all IO operations on the device are aligned 659c1bb86cdSEric Blake * to sector size, or else FreeBSD will reject them with EINVAL. 660c1bb86cdSEric Blake */ 661c1bb86cdSEric Blake s->needs_alignment = true; 662c1bb86cdSEric Blake } 663c1bb86cdSEric Blake #endif 664c1bb86cdSEric Blake 665c1bb86cdSEric Blake #ifdef CONFIG_XFS 666c1bb86cdSEric Blake if (platform_test_xfs_fd(s->fd)) { 667c1bb86cdSEric Blake s->is_xfs = true; 668c1bb86cdSEric Blake } 669c1bb86cdSEric Blake #endif 670c1bb86cdSEric Blake 671738301e1SKevin Wolf bs->supported_zero_flags = BDRV_REQ_MAY_UNMAP | BDRV_REQ_NO_FALLBACK; 672c1bb86cdSEric Blake ret = 0; 673c1bb86cdSEric Blake fail: 674c1bb86cdSEric Blake if (filename && (bdrv_flags & BDRV_O_TEMPORARY)) { 675c1bb86cdSEric Blake unlink(filename); 676c1bb86cdSEric Blake } 677c1bb86cdSEric Blake qemu_opts_del(opts); 678c1bb86cdSEric Blake return ret; 679c1bb86cdSEric Blake } 680c1bb86cdSEric Blake 681c1bb86cdSEric Blake static int raw_open(BlockDriverState *bs, QDict *options, int flags, 682c1bb86cdSEric Blake Error **errp) 683c1bb86cdSEric Blake { 684c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 685c1bb86cdSEric Blake 686c1bb86cdSEric Blake s->type = FTYPE_FILE; 687230ff739SJohn Snow return raw_open_common(bs, options, flags, 0, false, errp); 688c1bb86cdSEric Blake } 689c1bb86cdSEric Blake 690244a5668SFam Zheng typedef enum { 691244a5668SFam Zheng RAW_PL_PREPARE, 692244a5668SFam Zheng RAW_PL_COMMIT, 693244a5668SFam Zheng RAW_PL_ABORT, 694244a5668SFam Zheng } RawPermLockOp; 695244a5668SFam Zheng 696244a5668SFam Zheng #define PERM_FOREACH(i) \ 697244a5668SFam Zheng for ((i) = 0; (1ULL << (i)) <= BLK_PERM_ALL; i++) 698244a5668SFam Zheng 699244a5668SFam Zheng /* Lock bytes indicated by @perm_lock_bits and @shared_perm_lock_bits in the 700244a5668SFam Zheng * file; if @unlock == true, also unlock the unneeded bytes. 701244a5668SFam Zheng * @shared_perm_lock_bits is the mask of all permissions that are NOT shared. 702244a5668SFam Zheng */ 7032996ffadSFam Zheng static int raw_apply_lock_bytes(BDRVRawState *s, int fd, 704244a5668SFam Zheng uint64_t perm_lock_bits, 705244a5668SFam Zheng uint64_t shared_perm_lock_bits, 706244a5668SFam Zheng bool unlock, Error **errp) 707244a5668SFam Zheng { 708244a5668SFam Zheng int ret; 709244a5668SFam Zheng int i; 7102996ffadSFam Zheng uint64_t locked_perm, locked_shared_perm; 7112996ffadSFam Zheng 7122996ffadSFam Zheng if (s) { 7132996ffadSFam Zheng locked_perm = s->locked_perm; 7142996ffadSFam Zheng locked_shared_perm = s->locked_shared_perm; 7152996ffadSFam Zheng } else { 7162996ffadSFam Zheng /* 7172996ffadSFam Zheng * We don't have the previous bits, just lock/unlock for each of the 7182996ffadSFam Zheng * requested bits. 7192996ffadSFam Zheng */ 7202996ffadSFam Zheng if (unlock) { 7212996ffadSFam Zheng locked_perm = BLK_PERM_ALL; 7222996ffadSFam Zheng locked_shared_perm = BLK_PERM_ALL; 7232996ffadSFam Zheng } else { 7242996ffadSFam Zheng locked_perm = 0; 7252996ffadSFam Zheng locked_shared_perm = 0; 7262996ffadSFam Zheng } 7272996ffadSFam Zheng } 728244a5668SFam Zheng 729244a5668SFam Zheng PERM_FOREACH(i) { 730244a5668SFam Zheng int off = RAW_LOCK_PERM_BASE + i; 7312996ffadSFam Zheng uint64_t bit = (1ULL << i); 7322996ffadSFam Zheng if ((perm_lock_bits & bit) && !(locked_perm & bit)) { 733d0a96155SMax Reitz ret = qemu_lock_fd(fd, off, 1, false); 734244a5668SFam Zheng if (ret) { 735244a5668SFam Zheng error_setg(errp, "Failed to lock byte %d", off); 736244a5668SFam Zheng return ret; 7372996ffadSFam Zheng } else if (s) { 7382996ffadSFam Zheng s->locked_perm |= bit; 739244a5668SFam Zheng } 7402996ffadSFam Zheng } else if (unlock && (locked_perm & bit) && !(perm_lock_bits & bit)) { 741d0a96155SMax Reitz ret = qemu_unlock_fd(fd, off, 1); 742244a5668SFam Zheng if (ret) { 743244a5668SFam Zheng error_setg(errp, "Failed to unlock byte %d", off); 744244a5668SFam Zheng return ret; 7452996ffadSFam Zheng } else if (s) { 7462996ffadSFam Zheng s->locked_perm &= ~bit; 747244a5668SFam Zheng } 748244a5668SFam Zheng } 749244a5668SFam Zheng } 750244a5668SFam Zheng PERM_FOREACH(i) { 751244a5668SFam Zheng int off = RAW_LOCK_SHARED_BASE + i; 7522996ffadSFam Zheng uint64_t bit = (1ULL << i); 7532996ffadSFam Zheng if ((shared_perm_lock_bits & bit) && !(locked_shared_perm & bit)) { 754d0a96155SMax Reitz ret = qemu_lock_fd(fd, off, 1, false); 755244a5668SFam Zheng if (ret) { 756244a5668SFam Zheng error_setg(errp, "Failed to lock byte %d", off); 757244a5668SFam Zheng return ret; 7582996ffadSFam Zheng } else if (s) { 7592996ffadSFam Zheng s->locked_shared_perm |= bit; 760244a5668SFam Zheng } 7612996ffadSFam Zheng } else if (unlock && (locked_shared_perm & bit) && 7622996ffadSFam Zheng !(shared_perm_lock_bits & bit)) { 763d0a96155SMax Reitz ret = qemu_unlock_fd(fd, off, 1); 764244a5668SFam Zheng if (ret) { 765244a5668SFam Zheng error_setg(errp, "Failed to unlock byte %d", off); 766244a5668SFam Zheng return ret; 7672996ffadSFam Zheng } else if (s) { 7682996ffadSFam Zheng s->locked_shared_perm &= ~bit; 769244a5668SFam Zheng } 770244a5668SFam Zheng } 771244a5668SFam Zheng } 772244a5668SFam Zheng return 0; 773244a5668SFam Zheng } 774244a5668SFam Zheng 775244a5668SFam Zheng /* Check "unshared" bytes implied by @perm and ~@shared_perm in the file. */ 776d0a96155SMax Reitz static int raw_check_lock_bytes(int fd, uint64_t perm, uint64_t shared_perm, 777244a5668SFam Zheng Error **errp) 778244a5668SFam Zheng { 779244a5668SFam Zheng int ret; 780244a5668SFam Zheng int i; 781244a5668SFam Zheng 782244a5668SFam Zheng PERM_FOREACH(i) { 783244a5668SFam Zheng int off = RAW_LOCK_SHARED_BASE + i; 784244a5668SFam Zheng uint64_t p = 1ULL << i; 785244a5668SFam Zheng if (perm & p) { 786d0a96155SMax Reitz ret = qemu_lock_fd_test(fd, off, 1, true); 787244a5668SFam Zheng if (ret) { 788244a5668SFam Zheng char *perm_name = bdrv_perm_names(p); 789244a5668SFam Zheng error_setg(errp, 790244a5668SFam Zheng "Failed to get \"%s\" lock", 791244a5668SFam Zheng perm_name); 792244a5668SFam Zheng g_free(perm_name); 793244a5668SFam Zheng return ret; 794244a5668SFam Zheng } 795244a5668SFam Zheng } 796244a5668SFam Zheng } 797244a5668SFam Zheng PERM_FOREACH(i) { 798244a5668SFam Zheng int off = RAW_LOCK_PERM_BASE + i; 799244a5668SFam Zheng uint64_t p = 1ULL << i; 800244a5668SFam Zheng if (!(shared_perm & p)) { 801d0a96155SMax Reitz ret = qemu_lock_fd_test(fd, off, 1, true); 802244a5668SFam Zheng if (ret) { 803244a5668SFam Zheng char *perm_name = bdrv_perm_names(p); 804244a5668SFam Zheng error_setg(errp, 805244a5668SFam Zheng "Failed to get shared \"%s\" lock", 806244a5668SFam Zheng perm_name); 807244a5668SFam Zheng g_free(perm_name); 808244a5668SFam Zheng return ret; 809244a5668SFam Zheng } 810244a5668SFam Zheng } 811244a5668SFam Zheng } 812244a5668SFam Zheng return 0; 813244a5668SFam Zheng } 814244a5668SFam Zheng 815244a5668SFam Zheng static int raw_handle_perm_lock(BlockDriverState *bs, 816244a5668SFam Zheng RawPermLockOp op, 817244a5668SFam Zheng uint64_t new_perm, uint64_t new_shared, 818244a5668SFam Zheng Error **errp) 819244a5668SFam Zheng { 820244a5668SFam Zheng BDRVRawState *s = bs->opaque; 821244a5668SFam Zheng int ret = 0; 822244a5668SFam Zheng Error *local_err = NULL; 823244a5668SFam Zheng 824244a5668SFam Zheng if (!s->use_lock) { 825244a5668SFam Zheng return 0; 826244a5668SFam Zheng } 827244a5668SFam Zheng 828244a5668SFam Zheng if (bdrv_get_flags(bs) & BDRV_O_INACTIVE) { 829244a5668SFam Zheng return 0; 830244a5668SFam Zheng } 831244a5668SFam Zheng 832244a5668SFam Zheng switch (op) { 833244a5668SFam Zheng case RAW_PL_PREPARE: 834696aaaedSVladimir Sementsov-Ogievskiy if ((s->perm | new_perm) == s->perm && 835696aaaedSVladimir Sementsov-Ogievskiy (s->shared_perm & new_shared) == s->shared_perm) 836696aaaedSVladimir Sementsov-Ogievskiy { 837696aaaedSVladimir Sementsov-Ogievskiy /* 838696aaaedSVladimir Sementsov-Ogievskiy * We are going to unlock bytes, it should not fail. If it fail due 839696aaaedSVladimir Sementsov-Ogievskiy * to some fs-dependent permission-unrelated reasons (which occurs 840696aaaedSVladimir Sementsov-Ogievskiy * sometimes on NFS and leads to abort in bdrv_replace_child) we 841696aaaedSVladimir Sementsov-Ogievskiy * can't prevent such errors by any check here. And we ignore them 842696aaaedSVladimir Sementsov-Ogievskiy * anyway in ABORT and COMMIT. 843696aaaedSVladimir Sementsov-Ogievskiy */ 844696aaaedSVladimir Sementsov-Ogievskiy return 0; 845696aaaedSVladimir Sementsov-Ogievskiy } 846f2e3af29SFam Zheng ret = raw_apply_lock_bytes(s, s->fd, s->perm | new_perm, 847244a5668SFam Zheng ~s->shared_perm | ~new_shared, 848244a5668SFam Zheng false, errp); 849244a5668SFam Zheng if (!ret) { 850f2e3af29SFam Zheng ret = raw_check_lock_bytes(s->fd, new_perm, new_shared, errp); 851244a5668SFam Zheng if (!ret) { 852244a5668SFam Zheng return 0; 853244a5668SFam Zheng } 854b857431dSFam Zheng error_append_hint(errp, 855b857431dSFam Zheng "Is another process using the image [%s]?\n", 856b857431dSFam Zheng bs->filename); 857244a5668SFam Zheng } 858244a5668SFam Zheng op = RAW_PL_ABORT; 859244a5668SFam Zheng /* fall through to unlock bytes. */ 860244a5668SFam Zheng case RAW_PL_ABORT: 861f2e3af29SFam Zheng raw_apply_lock_bytes(s, s->fd, s->perm, ~s->shared_perm, 862d0a96155SMax Reitz true, &local_err); 863244a5668SFam Zheng if (local_err) { 864244a5668SFam Zheng /* Theoretically the above call only unlocks bytes and it cannot 865244a5668SFam Zheng * fail. Something weird happened, report it. 866244a5668SFam Zheng */ 867db0754dfSFam Zheng warn_report_err(local_err); 868244a5668SFam Zheng } 869244a5668SFam Zheng break; 870244a5668SFam Zheng case RAW_PL_COMMIT: 871f2e3af29SFam Zheng raw_apply_lock_bytes(s, s->fd, new_perm, ~new_shared, 872d0a96155SMax Reitz true, &local_err); 873244a5668SFam Zheng if (local_err) { 874244a5668SFam Zheng /* Theoretically the above call only unlocks bytes and it cannot 875244a5668SFam Zheng * fail. Something weird happened, report it. 876244a5668SFam Zheng */ 877db0754dfSFam Zheng warn_report_err(local_err); 878244a5668SFam Zheng } 879244a5668SFam Zheng break; 880244a5668SFam Zheng } 881244a5668SFam Zheng return ret; 882244a5668SFam Zheng } 883244a5668SFam Zheng 8845cec2870SKevin Wolf static int raw_reconfigure_getfd(BlockDriverState *bs, int flags, 88523dece19SKevin Wolf int *open_flags, uint64_t perm, bool force_dup, 8866ceabe6fSKevin Wolf Error **errp) 8875cec2870SKevin Wolf { 8885cec2870SKevin Wolf BDRVRawState *s = bs->opaque; 8895cec2870SKevin Wolf int fd = -1; 8905cec2870SKevin Wolf int ret; 89123dece19SKevin Wolf bool has_writers = perm & 89223dece19SKevin Wolf (BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED | BLK_PERM_RESIZE); 8935cec2870SKevin Wolf int fcntl_flags = O_APPEND | O_NONBLOCK; 8945cec2870SKevin Wolf #ifdef O_NOATIME 8955cec2870SKevin Wolf fcntl_flags |= O_NOATIME; 8965cec2870SKevin Wolf #endif 8975cec2870SKevin Wolf 8985cec2870SKevin Wolf *open_flags = 0; 8995cec2870SKevin Wolf if (s->type == FTYPE_CD) { 9005cec2870SKevin Wolf *open_flags |= O_NONBLOCK; 9015cec2870SKevin Wolf } 9025cec2870SKevin Wolf 90323dece19SKevin Wolf raw_parse_flags(flags, open_flags, has_writers); 9045cec2870SKevin Wolf 9055cec2870SKevin Wolf #ifdef O_ASYNC 9065cec2870SKevin Wolf /* Not all operating systems have O_ASYNC, and those that don't 9075cec2870SKevin Wolf * will not let us track the state into rs->open_flags (typically 9085cec2870SKevin Wolf * you achieve the same effect with an ioctl, for example I_SETSIG 9095cec2870SKevin Wolf * on Solaris). But we do not use O_ASYNC, so that's fine. 9105cec2870SKevin Wolf */ 9115cec2870SKevin Wolf assert((s->open_flags & O_ASYNC) == 0); 9125cec2870SKevin Wolf #endif 9135cec2870SKevin Wolf 9146ceabe6fSKevin Wolf if (!force_dup && *open_flags == s->open_flags) { 9156ceabe6fSKevin Wolf /* We're lucky, the existing fd is fine */ 9166ceabe6fSKevin Wolf return s->fd; 9176ceabe6fSKevin Wolf } 9186ceabe6fSKevin Wolf 9195cec2870SKevin Wolf if ((*open_flags & ~fcntl_flags) == (s->open_flags & ~fcntl_flags)) { 9205cec2870SKevin Wolf /* dup the original fd */ 9215cec2870SKevin Wolf fd = qemu_dup(s->fd); 9225cec2870SKevin Wolf if (fd >= 0) { 9235cec2870SKevin Wolf ret = fcntl_setfl(fd, *open_flags); 9245cec2870SKevin Wolf if (ret) { 9255cec2870SKevin Wolf qemu_close(fd); 9265cec2870SKevin Wolf fd = -1; 9275cec2870SKevin Wolf } 9285cec2870SKevin Wolf } 9295cec2870SKevin Wolf } 9305cec2870SKevin Wolf 9315cec2870SKevin Wolf /* If we cannot use fcntl, or fcntl failed, fall back to qemu_open() */ 9325cec2870SKevin Wolf if (fd == -1) { 9335cec2870SKevin Wolf const char *normalized_filename = bs->filename; 9345cec2870SKevin Wolf ret = raw_normalize_devicepath(&normalized_filename, errp); 9355cec2870SKevin Wolf if (ret >= 0) { 9365cec2870SKevin Wolf assert(!(*open_flags & O_CREAT)); 9375cec2870SKevin Wolf fd = qemu_open(normalized_filename, *open_flags); 9385cec2870SKevin Wolf if (fd == -1) { 9395cec2870SKevin Wolf error_setg_errno(errp, errno, "Could not reopen file"); 9405cec2870SKevin Wolf return -1; 9415cec2870SKevin Wolf } 9425cec2870SKevin Wolf } 9435cec2870SKevin Wolf } 9445cec2870SKevin Wolf 9455cec2870SKevin Wolf return fd; 9465cec2870SKevin Wolf } 9475cec2870SKevin Wolf 948c1bb86cdSEric Blake static int raw_reopen_prepare(BDRVReopenState *state, 949c1bb86cdSEric Blake BlockReopenQueue *queue, Error **errp) 950c1bb86cdSEric Blake { 951c1bb86cdSEric Blake BDRVRawState *s; 952c1bb86cdSEric Blake BDRVRawReopenState *rs; 95331be8a2aSStefan Hajnoczi QemuOpts *opts; 954a6aeca0cSKevin Wolf int ret; 955c1bb86cdSEric Blake Error *local_err = NULL; 956c1bb86cdSEric Blake 957c1bb86cdSEric Blake assert(state != NULL); 958c1bb86cdSEric Blake assert(state->bs != NULL); 959c1bb86cdSEric Blake 960c1bb86cdSEric Blake s = state->bs->opaque; 961c1bb86cdSEric Blake 962c1bb86cdSEric Blake state->opaque = g_new0(BDRVRawReopenState, 1); 963c1bb86cdSEric Blake rs = state->opaque; 96431be8a2aSStefan Hajnoczi 96531be8a2aSStefan Hajnoczi /* Handle options changes */ 96631be8a2aSStefan Hajnoczi opts = qemu_opts_create(&raw_runtime_opts, NULL, 0, &error_abort); 96731be8a2aSStefan Hajnoczi qemu_opts_absorb_qdict(opts, state->options, &local_err); 96831be8a2aSStefan Hajnoczi if (local_err) { 96931be8a2aSStefan Hajnoczi error_propagate(errp, local_err); 97031be8a2aSStefan Hajnoczi ret = -EINVAL; 97131be8a2aSStefan Hajnoczi goto out; 97231be8a2aSStefan Hajnoczi } 97331be8a2aSStefan Hajnoczi 974f357fcd8SStefan Hajnoczi rs->drop_cache = qemu_opt_get_bool_del(opts, "drop-cache", true); 9758d324575SAlberto Garcia rs->check_cache_dropped = 9768d324575SAlberto Garcia qemu_opt_get_bool_del(opts, "x-check-cache-dropped", false); 9778d324575SAlberto Garcia 9788d324575SAlberto Garcia /* This driver's reopen function doesn't currently allow changing 9798d324575SAlberto Garcia * other options, so let's put them back in the original QDict and 9808d324575SAlberto Garcia * bdrv_reopen_prepare() will detect changes and complain. */ 9818d324575SAlberto Garcia qemu_opts_to_qdict(opts, state->options); 982c1bb86cdSEric Blake 9835cec2870SKevin Wolf rs->fd = raw_reconfigure_getfd(state->bs, state->flags, &rs->open_flags, 98423dece19SKevin Wolf state->perm, true, &local_err); 9855cec2870SKevin Wolf if (local_err) { 9865cec2870SKevin Wolf error_propagate(errp, local_err); 987c1bb86cdSEric Blake ret = -1; 9885cec2870SKevin Wolf goto out; 989c1bb86cdSEric Blake } 990c1bb86cdSEric Blake 991c1bb86cdSEric Blake /* Fail already reopen_prepare() if we can't get a working O_DIRECT 992c1bb86cdSEric Blake * alignment with the new fd. */ 993c1bb86cdSEric Blake if (rs->fd != -1) { 994c1bb86cdSEric Blake raw_probe_alignment(state->bs, rs->fd, &local_err); 995c1bb86cdSEric Blake if (local_err) { 996c1bb86cdSEric Blake error_propagate(errp, local_err); 997c1bb86cdSEric Blake ret = -EINVAL; 998a6aeca0cSKevin Wolf goto out_fd; 999a6aeca0cSKevin Wolf } 1000c1bb86cdSEric Blake } 1001c1bb86cdSEric Blake 1002e0c9cf3aSKevin Wolf s->reopen_state = state; 1003a6aeca0cSKevin Wolf ret = 0; 1004a6aeca0cSKevin Wolf out_fd: 1005a6aeca0cSKevin Wolf if (ret < 0) { 1006a6aeca0cSKevin Wolf qemu_close(rs->fd); 1007a6aeca0cSKevin Wolf rs->fd = -1; 1008a6aeca0cSKevin Wolf } 100931be8a2aSStefan Hajnoczi out: 101031be8a2aSStefan Hajnoczi qemu_opts_del(opts); 1011c1bb86cdSEric Blake return ret; 1012c1bb86cdSEric Blake } 1013c1bb86cdSEric Blake 1014c1bb86cdSEric Blake static void raw_reopen_commit(BDRVReopenState *state) 1015c1bb86cdSEric Blake { 1016c1bb86cdSEric Blake BDRVRawReopenState *rs = state->opaque; 1017c1bb86cdSEric Blake BDRVRawState *s = state->bs->opaque; 1018c1bb86cdSEric Blake 1019f357fcd8SStefan Hajnoczi s->drop_cache = rs->drop_cache; 102031be8a2aSStefan Hajnoczi s->check_cache_dropped = rs->check_cache_dropped; 1021c1bb86cdSEric Blake s->open_flags = rs->open_flags; 1022c1bb86cdSEric Blake 1023c1bb86cdSEric Blake qemu_close(s->fd); 1024c1bb86cdSEric Blake s->fd = rs->fd; 1025c1bb86cdSEric Blake 1026c1bb86cdSEric Blake g_free(state->opaque); 1027c1bb86cdSEric Blake state->opaque = NULL; 1028e0c9cf3aSKevin Wolf 1029e0c9cf3aSKevin Wolf assert(s->reopen_state == state); 1030e0c9cf3aSKevin Wolf s->reopen_state = NULL; 1031c1bb86cdSEric Blake } 1032c1bb86cdSEric Blake 1033c1bb86cdSEric Blake 1034c1bb86cdSEric Blake static void raw_reopen_abort(BDRVReopenState *state) 1035c1bb86cdSEric Blake { 1036c1bb86cdSEric Blake BDRVRawReopenState *rs = state->opaque; 1037e0c9cf3aSKevin Wolf BDRVRawState *s = state->bs->opaque; 1038c1bb86cdSEric Blake 1039c1bb86cdSEric Blake /* nothing to do if NULL, we didn't get far enough */ 1040c1bb86cdSEric Blake if (rs == NULL) { 1041c1bb86cdSEric Blake return; 1042c1bb86cdSEric Blake } 1043c1bb86cdSEric Blake 1044c1bb86cdSEric Blake if (rs->fd >= 0) { 1045c1bb86cdSEric Blake qemu_close(rs->fd); 1046c1bb86cdSEric Blake rs->fd = -1; 1047c1bb86cdSEric Blake } 1048c1bb86cdSEric Blake g_free(state->opaque); 1049c1bb86cdSEric Blake state->opaque = NULL; 1050e0c9cf3aSKevin Wolf 1051e0c9cf3aSKevin Wolf assert(s->reopen_state == state); 1052e0c9cf3aSKevin Wolf s->reopen_state = NULL; 1053c1bb86cdSEric Blake } 1054c1bb86cdSEric Blake 1055867eccfeSMaxim Levitsky static int sg_get_max_transfer_length(int fd) 1056c1bb86cdSEric Blake { 1057c1bb86cdSEric Blake #ifdef BLKSECTGET 105848265250SEric Farman int max_bytes = 0; 1059867eccfeSMaxim Levitsky 1060867eccfeSMaxim Levitsky if (ioctl(fd, BLKSECTGET, &max_bytes) == 0) { 106148265250SEric Farman return max_bytes; 1062c1bb86cdSEric Blake } else { 1063c1bb86cdSEric Blake return -errno; 1064c1bb86cdSEric Blake } 1065c1bb86cdSEric Blake #else 1066c1bb86cdSEric Blake return -ENOSYS; 1067c1bb86cdSEric Blake #endif 1068c1bb86cdSEric Blake } 1069c1bb86cdSEric Blake 1070867eccfeSMaxim Levitsky static int sg_get_max_segments(int fd) 10719103f1ceSFam Zheng { 10729103f1ceSFam Zheng #ifdef CONFIG_LINUX 10739103f1ceSFam Zheng char buf[32]; 10749103f1ceSFam Zheng const char *end; 1075867eccfeSMaxim Levitsky char *sysfspath = NULL; 10769103f1ceSFam Zheng int ret; 1077867eccfeSMaxim Levitsky int sysfd = -1; 10789103f1ceSFam Zheng long max_segments; 1079867eccfeSMaxim Levitsky struct stat st; 1080867eccfeSMaxim Levitsky 1081867eccfeSMaxim Levitsky if (fstat(fd, &st)) { 1082867eccfeSMaxim Levitsky ret = -errno; 1083867eccfeSMaxim Levitsky goto out; 1084867eccfeSMaxim Levitsky } 10859103f1ceSFam Zheng 10869103f1ceSFam Zheng sysfspath = g_strdup_printf("/sys/dev/block/%u:%u/queue/max_segments", 1087867eccfeSMaxim Levitsky major(st.st_rdev), minor(st.st_rdev)); 1088867eccfeSMaxim Levitsky sysfd = open(sysfspath, O_RDONLY); 1089867eccfeSMaxim Levitsky if (sysfd == -1) { 10909103f1ceSFam Zheng ret = -errno; 10919103f1ceSFam Zheng goto out; 10929103f1ceSFam Zheng } 10939103f1ceSFam Zheng do { 1094867eccfeSMaxim Levitsky ret = read(sysfd, buf, sizeof(buf) - 1); 10959103f1ceSFam Zheng } while (ret == -1 && errno == EINTR); 10969103f1ceSFam Zheng if (ret < 0) { 10979103f1ceSFam Zheng ret = -errno; 10989103f1ceSFam Zheng goto out; 10999103f1ceSFam Zheng } else if (ret == 0) { 11009103f1ceSFam Zheng ret = -EIO; 11019103f1ceSFam Zheng goto out; 11029103f1ceSFam Zheng } 11039103f1ceSFam Zheng buf[ret] = 0; 11049103f1ceSFam Zheng /* The file is ended with '\n', pass 'end' to accept that. */ 11059103f1ceSFam Zheng ret = qemu_strtol(buf, &end, 10, &max_segments); 11069103f1ceSFam Zheng if (ret == 0 && end && *end == '\n') { 11079103f1ceSFam Zheng ret = max_segments; 11089103f1ceSFam Zheng } 11099103f1ceSFam Zheng 11109103f1ceSFam Zheng out: 1111867eccfeSMaxim Levitsky if (sysfd != -1) { 1112867eccfeSMaxim Levitsky close(sysfd); 1113fed414dfSFam Zheng } 11149103f1ceSFam Zheng g_free(sysfspath); 11159103f1ceSFam Zheng return ret; 11169103f1ceSFam Zheng #else 11179103f1ceSFam Zheng return -ENOTSUP; 11189103f1ceSFam Zheng #endif 11199103f1ceSFam Zheng } 11209103f1ceSFam Zheng 1121c1bb86cdSEric Blake static void raw_refresh_limits(BlockDriverState *bs, Error **errp) 1122c1bb86cdSEric Blake { 1123c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 1124c1bb86cdSEric Blake 1125867eccfeSMaxim Levitsky if (bs->sg) { 1126867eccfeSMaxim Levitsky int ret = sg_get_max_transfer_length(s->fd); 1127867eccfeSMaxim Levitsky 112848265250SEric Farman if (ret > 0 && ret <= BDRV_REQUEST_MAX_BYTES) { 112948265250SEric Farman bs->bl.max_transfer = pow2floor(ret); 1130c1bb86cdSEric Blake } 1131867eccfeSMaxim Levitsky 1132867eccfeSMaxim Levitsky ret = sg_get_max_segments(s->fd); 11339103f1ceSFam Zheng if (ret > 0) { 1134867eccfeSMaxim Levitsky bs->bl.max_transfer = MIN(bs->bl.max_transfer, ret * getpagesize()); 1135c1bb86cdSEric Blake } 1136c1bb86cdSEric Blake } 1137c1bb86cdSEric Blake 1138c1bb86cdSEric Blake raw_probe_alignment(bs, s->fd, errp); 1139c1bb86cdSEric Blake bs->bl.min_mem_alignment = s->buf_align; 1140c1bb86cdSEric Blake bs->bl.opt_mem_alignment = MAX(s->buf_align, getpagesize()); 1141c1bb86cdSEric Blake } 1142c1bb86cdSEric Blake 1143c1bb86cdSEric Blake static int check_for_dasd(int fd) 1144c1bb86cdSEric Blake { 1145c1bb86cdSEric Blake #ifdef BIODASDINFO2 1146c1bb86cdSEric Blake struct dasd_information2_t info = {0}; 1147c1bb86cdSEric Blake 1148c1bb86cdSEric Blake return ioctl(fd, BIODASDINFO2, &info); 1149c1bb86cdSEric Blake #else 1150c1bb86cdSEric Blake return -1; 1151c1bb86cdSEric Blake #endif 1152c1bb86cdSEric Blake } 1153c1bb86cdSEric Blake 1154c1bb86cdSEric Blake /** 1155c1bb86cdSEric Blake * Try to get @bs's logical and physical block size. 1156c1bb86cdSEric Blake * On success, store them in @bsz and return zero. 1157c1bb86cdSEric Blake * On failure, return negative errno. 1158c1bb86cdSEric Blake */ 1159c1bb86cdSEric Blake static int hdev_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz) 1160c1bb86cdSEric Blake { 1161c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 1162c1bb86cdSEric Blake int ret; 1163c1bb86cdSEric Blake 1164c1bb86cdSEric Blake /* If DASD, get blocksizes */ 1165c1bb86cdSEric Blake if (check_for_dasd(s->fd) < 0) { 1166c1bb86cdSEric Blake return -ENOTSUP; 1167c1bb86cdSEric Blake } 1168c1bb86cdSEric Blake ret = probe_logical_blocksize(s->fd, &bsz->log); 1169c1bb86cdSEric Blake if (ret < 0) { 1170c1bb86cdSEric Blake return ret; 1171c1bb86cdSEric Blake } 1172c1bb86cdSEric Blake return probe_physical_blocksize(s->fd, &bsz->phys); 1173c1bb86cdSEric Blake } 1174c1bb86cdSEric Blake 1175c1bb86cdSEric Blake /** 1176c1bb86cdSEric Blake * Try to get @bs's geometry: cyls, heads, sectors. 1177c1bb86cdSEric Blake * On success, store them in @geo and return 0. 1178c1bb86cdSEric Blake * On failure return -errno. 1179c1bb86cdSEric Blake * (Allows block driver to assign default geometry values that guest sees) 1180c1bb86cdSEric Blake */ 1181c1bb86cdSEric Blake #ifdef __linux__ 1182c1bb86cdSEric Blake static int hdev_probe_geometry(BlockDriverState *bs, HDGeometry *geo) 1183c1bb86cdSEric Blake { 1184c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 1185c1bb86cdSEric Blake struct hd_geometry ioctl_geo = {0}; 1186c1bb86cdSEric Blake 1187c1bb86cdSEric Blake /* If DASD, get its geometry */ 1188c1bb86cdSEric Blake if (check_for_dasd(s->fd) < 0) { 1189c1bb86cdSEric Blake return -ENOTSUP; 1190c1bb86cdSEric Blake } 1191c1bb86cdSEric Blake if (ioctl(s->fd, HDIO_GETGEO, &ioctl_geo) < 0) { 1192c1bb86cdSEric Blake return -errno; 1193c1bb86cdSEric Blake } 1194c1bb86cdSEric Blake /* HDIO_GETGEO may return success even though geo contains zeros 1195c1bb86cdSEric Blake (e.g. certain multipath setups) */ 1196c1bb86cdSEric Blake if (!ioctl_geo.heads || !ioctl_geo.sectors || !ioctl_geo.cylinders) { 1197c1bb86cdSEric Blake return -ENOTSUP; 1198c1bb86cdSEric Blake } 1199c1bb86cdSEric Blake /* Do not return a geometry for partition */ 1200c1bb86cdSEric Blake if (ioctl_geo.start != 0) { 1201c1bb86cdSEric Blake return -ENOTSUP; 1202c1bb86cdSEric Blake } 1203c1bb86cdSEric Blake geo->heads = ioctl_geo.heads; 1204c1bb86cdSEric Blake geo->sectors = ioctl_geo.sectors; 1205c1bb86cdSEric Blake geo->cylinders = ioctl_geo.cylinders; 1206c1bb86cdSEric Blake 1207c1bb86cdSEric Blake return 0; 1208c1bb86cdSEric Blake } 1209c1bb86cdSEric Blake #else /* __linux__ */ 1210c1bb86cdSEric Blake static int hdev_probe_geometry(BlockDriverState *bs, HDGeometry *geo) 1211c1bb86cdSEric Blake { 1212c1bb86cdSEric Blake return -ENOTSUP; 1213c1bb86cdSEric Blake } 1214c1bb86cdSEric Blake #endif 1215c1bb86cdSEric Blake 121603425671SKevin Wolf #if defined(__linux__) 121703425671SKevin Wolf static int handle_aiocb_ioctl(void *opaque) 1218c1bb86cdSEric Blake { 121903425671SKevin Wolf RawPosixAIOData *aiocb = opaque; 1220c1bb86cdSEric Blake int ret; 1221c1bb86cdSEric Blake 1222d57c44d0SKevin Wolf ret = ioctl(aiocb->aio_fildes, aiocb->ioctl.cmd, aiocb->ioctl.buf); 1223c1bb86cdSEric Blake if (ret == -1) { 1224c1bb86cdSEric Blake return -errno; 1225c1bb86cdSEric Blake } 1226c1bb86cdSEric Blake 1227c1bb86cdSEric Blake return 0; 1228c1bb86cdSEric Blake } 122903425671SKevin Wolf #endif /* linux */ 1230c1bb86cdSEric Blake 123106dc9bd5SKevin Wolf static int handle_aiocb_flush(void *opaque) 1232c1bb86cdSEric Blake { 123306dc9bd5SKevin Wolf RawPosixAIOData *aiocb = opaque; 1234e5bcf967SKevin Wolf BDRVRawState *s = aiocb->bs->opaque; 1235c1bb86cdSEric Blake int ret; 1236c1bb86cdSEric Blake 1237e5bcf967SKevin Wolf if (s->page_cache_inconsistent) { 1238e5bcf967SKevin Wolf return -EIO; 1239e5bcf967SKevin Wolf } 1240e5bcf967SKevin Wolf 1241c1bb86cdSEric Blake ret = qemu_fdatasync(aiocb->aio_fildes); 1242c1bb86cdSEric Blake if (ret == -1) { 1243e5bcf967SKevin Wolf /* There is no clear definition of the semantics of a failing fsync(), 1244e5bcf967SKevin Wolf * so we may have to assume the worst. The sad truth is that this 1245e5bcf967SKevin Wolf * assumption is correct for Linux. Some pages are now probably marked 1246e5bcf967SKevin Wolf * clean in the page cache even though they are inconsistent with the 1247e5bcf967SKevin Wolf * on-disk contents. The next fdatasync() call would succeed, but no 1248e5bcf967SKevin Wolf * further writeback attempt will be made. We can't get back to a state 1249e5bcf967SKevin Wolf * in which we know what is on disk (we would have to rewrite 1250e5bcf967SKevin Wolf * everything that was touched since the last fdatasync() at least), so 1251e5bcf967SKevin Wolf * make bdrv_flush() fail permanently. Given that the behaviour isn't 1252e5bcf967SKevin Wolf * really defined, I have little hope that other OSes are doing better. 1253e5bcf967SKevin Wolf * 1254e5bcf967SKevin Wolf * Obviously, this doesn't affect O_DIRECT, which bypasses the page 1255e5bcf967SKevin Wolf * cache. */ 1256e5bcf967SKevin Wolf if ((s->open_flags & O_DIRECT) == 0) { 1257e5bcf967SKevin Wolf s->page_cache_inconsistent = true; 1258e5bcf967SKevin Wolf } 1259c1bb86cdSEric Blake return -errno; 1260c1bb86cdSEric Blake } 1261c1bb86cdSEric Blake return 0; 1262c1bb86cdSEric Blake } 1263c1bb86cdSEric Blake 1264c1bb86cdSEric Blake #ifdef CONFIG_PREADV 1265c1bb86cdSEric Blake 1266c1bb86cdSEric Blake static bool preadv_present = true; 1267c1bb86cdSEric Blake 1268c1bb86cdSEric Blake static ssize_t 1269c1bb86cdSEric Blake qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset) 1270c1bb86cdSEric Blake { 1271c1bb86cdSEric Blake return preadv(fd, iov, nr_iov, offset); 1272c1bb86cdSEric Blake } 1273c1bb86cdSEric Blake 1274c1bb86cdSEric Blake static ssize_t 1275c1bb86cdSEric Blake qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset) 1276c1bb86cdSEric Blake { 1277c1bb86cdSEric Blake return pwritev(fd, iov, nr_iov, offset); 1278c1bb86cdSEric Blake } 1279c1bb86cdSEric Blake 1280c1bb86cdSEric Blake #else 1281c1bb86cdSEric Blake 1282c1bb86cdSEric Blake static bool preadv_present = false; 1283c1bb86cdSEric Blake 1284c1bb86cdSEric Blake static ssize_t 1285c1bb86cdSEric Blake qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset) 1286c1bb86cdSEric Blake { 1287c1bb86cdSEric Blake return -ENOSYS; 1288c1bb86cdSEric Blake } 1289c1bb86cdSEric Blake 1290c1bb86cdSEric Blake static ssize_t 1291c1bb86cdSEric Blake qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset) 1292c1bb86cdSEric Blake { 1293c1bb86cdSEric Blake return -ENOSYS; 1294c1bb86cdSEric Blake } 1295c1bb86cdSEric Blake 1296c1bb86cdSEric Blake #endif 1297c1bb86cdSEric Blake 1298c1bb86cdSEric Blake static ssize_t handle_aiocb_rw_vector(RawPosixAIOData *aiocb) 1299c1bb86cdSEric Blake { 1300c1bb86cdSEric Blake ssize_t len; 1301c1bb86cdSEric Blake 1302c1bb86cdSEric Blake do { 1303c1bb86cdSEric Blake if (aiocb->aio_type & QEMU_AIO_WRITE) 1304c1bb86cdSEric Blake len = qemu_pwritev(aiocb->aio_fildes, 1305d57c44d0SKevin Wolf aiocb->io.iov, 1306d57c44d0SKevin Wolf aiocb->io.niov, 1307c1bb86cdSEric Blake aiocb->aio_offset); 1308c1bb86cdSEric Blake else 1309c1bb86cdSEric Blake len = qemu_preadv(aiocb->aio_fildes, 1310d57c44d0SKevin Wolf aiocb->io.iov, 1311d57c44d0SKevin Wolf aiocb->io.niov, 1312c1bb86cdSEric Blake aiocb->aio_offset); 1313c1bb86cdSEric Blake } while (len == -1 && errno == EINTR); 1314c1bb86cdSEric Blake 1315c1bb86cdSEric Blake if (len == -1) { 1316c1bb86cdSEric Blake return -errno; 1317c1bb86cdSEric Blake } 1318c1bb86cdSEric Blake return len; 1319c1bb86cdSEric Blake } 1320c1bb86cdSEric Blake 1321c1bb86cdSEric Blake /* 1322c1bb86cdSEric Blake * Read/writes the data to/from a given linear buffer. 1323c1bb86cdSEric Blake * 1324c1bb86cdSEric Blake * Returns the number of bytes handles or -errno in case of an error. Short 1325c1bb86cdSEric Blake * reads are only returned if the end of the file is reached. 1326c1bb86cdSEric Blake */ 1327c1bb86cdSEric Blake static ssize_t handle_aiocb_rw_linear(RawPosixAIOData *aiocb, char *buf) 1328c1bb86cdSEric Blake { 1329c1bb86cdSEric Blake ssize_t offset = 0; 1330c1bb86cdSEric Blake ssize_t len; 1331c1bb86cdSEric Blake 1332c1bb86cdSEric Blake while (offset < aiocb->aio_nbytes) { 1333c1bb86cdSEric Blake if (aiocb->aio_type & QEMU_AIO_WRITE) { 1334c1bb86cdSEric Blake len = pwrite(aiocb->aio_fildes, 1335c1bb86cdSEric Blake (const char *)buf + offset, 1336c1bb86cdSEric Blake aiocb->aio_nbytes - offset, 1337c1bb86cdSEric Blake aiocb->aio_offset + offset); 1338c1bb86cdSEric Blake } else { 1339c1bb86cdSEric Blake len = pread(aiocb->aio_fildes, 1340c1bb86cdSEric Blake buf + offset, 1341c1bb86cdSEric Blake aiocb->aio_nbytes - offset, 1342c1bb86cdSEric Blake aiocb->aio_offset + offset); 1343c1bb86cdSEric Blake } 1344c1bb86cdSEric Blake if (len == -1 && errno == EINTR) { 1345c1bb86cdSEric Blake continue; 1346c1bb86cdSEric Blake } else if (len == -1 && errno == EINVAL && 1347c1bb86cdSEric Blake (aiocb->bs->open_flags & BDRV_O_NOCACHE) && 1348c1bb86cdSEric Blake !(aiocb->aio_type & QEMU_AIO_WRITE) && 1349c1bb86cdSEric Blake offset > 0) { 1350c1bb86cdSEric Blake /* O_DIRECT pread() may fail with EINVAL when offset is unaligned 1351c1bb86cdSEric Blake * after a short read. Assume that O_DIRECT short reads only occur 1352c1bb86cdSEric Blake * at EOF. Therefore this is a short read, not an I/O error. 1353c1bb86cdSEric Blake */ 1354c1bb86cdSEric Blake break; 1355c1bb86cdSEric Blake } else if (len == -1) { 1356c1bb86cdSEric Blake offset = -errno; 1357c1bb86cdSEric Blake break; 1358c1bb86cdSEric Blake } else if (len == 0) { 1359c1bb86cdSEric Blake break; 1360c1bb86cdSEric Blake } 1361c1bb86cdSEric Blake offset += len; 1362c1bb86cdSEric Blake } 1363c1bb86cdSEric Blake 1364c1bb86cdSEric Blake return offset; 1365c1bb86cdSEric Blake } 1366c1bb86cdSEric Blake 1367999e6b69SKevin Wolf static int handle_aiocb_rw(void *opaque) 1368c1bb86cdSEric Blake { 1369999e6b69SKevin Wolf RawPosixAIOData *aiocb = opaque; 1370c1bb86cdSEric Blake ssize_t nbytes; 1371c1bb86cdSEric Blake char *buf; 1372c1bb86cdSEric Blake 1373c1bb86cdSEric Blake if (!(aiocb->aio_type & QEMU_AIO_MISALIGNED)) { 1374c1bb86cdSEric Blake /* 1375c1bb86cdSEric Blake * If there is just a single buffer, and it is properly aligned 1376c1bb86cdSEric Blake * we can just use plain pread/pwrite without any problems. 1377c1bb86cdSEric Blake */ 1378d57c44d0SKevin Wolf if (aiocb->io.niov == 1) { 137954c7ca1bSKevin Wolf nbytes = handle_aiocb_rw_linear(aiocb, aiocb->io.iov->iov_base); 138054c7ca1bSKevin Wolf goto out; 1381c1bb86cdSEric Blake } 1382c1bb86cdSEric Blake /* 1383c1bb86cdSEric Blake * We have more than one iovec, and all are properly aligned. 1384c1bb86cdSEric Blake * 1385c1bb86cdSEric Blake * Try preadv/pwritev first and fall back to linearizing the 1386c1bb86cdSEric Blake * buffer if it's not supported. 1387c1bb86cdSEric Blake */ 1388c1bb86cdSEric Blake if (preadv_present) { 1389c1bb86cdSEric Blake nbytes = handle_aiocb_rw_vector(aiocb); 1390c1bb86cdSEric Blake if (nbytes == aiocb->aio_nbytes || 1391c1bb86cdSEric Blake (nbytes < 0 && nbytes != -ENOSYS)) { 139254c7ca1bSKevin Wolf goto out; 1393c1bb86cdSEric Blake } 1394c1bb86cdSEric Blake preadv_present = false; 1395c1bb86cdSEric Blake } 1396c1bb86cdSEric Blake 1397c1bb86cdSEric Blake /* 1398c1bb86cdSEric Blake * XXX(hch): short read/write. no easy way to handle the reminder 1399c1bb86cdSEric Blake * using these interfaces. For now retry using plain 1400c1bb86cdSEric Blake * pread/pwrite? 1401c1bb86cdSEric Blake */ 1402c1bb86cdSEric Blake } 1403c1bb86cdSEric Blake 1404c1bb86cdSEric Blake /* 1405c1bb86cdSEric Blake * Ok, we have to do it the hard way, copy all segments into 1406c1bb86cdSEric Blake * a single aligned buffer. 1407c1bb86cdSEric Blake */ 1408c1bb86cdSEric Blake buf = qemu_try_blockalign(aiocb->bs, aiocb->aio_nbytes); 1409c1bb86cdSEric Blake if (buf == NULL) { 141054c7ca1bSKevin Wolf nbytes = -ENOMEM; 141154c7ca1bSKevin Wolf goto out; 1412c1bb86cdSEric Blake } 1413c1bb86cdSEric Blake 1414c1bb86cdSEric Blake if (aiocb->aio_type & QEMU_AIO_WRITE) { 1415c1bb86cdSEric Blake char *p = buf; 1416c1bb86cdSEric Blake int i; 1417c1bb86cdSEric Blake 1418d57c44d0SKevin Wolf for (i = 0; i < aiocb->io.niov; ++i) { 1419d57c44d0SKevin Wolf memcpy(p, aiocb->io.iov[i].iov_base, aiocb->io.iov[i].iov_len); 1420d57c44d0SKevin Wolf p += aiocb->io.iov[i].iov_len; 1421c1bb86cdSEric Blake } 1422c1bb86cdSEric Blake assert(p - buf == aiocb->aio_nbytes); 1423c1bb86cdSEric Blake } 1424c1bb86cdSEric Blake 1425c1bb86cdSEric Blake nbytes = handle_aiocb_rw_linear(aiocb, buf); 1426c1bb86cdSEric Blake if (!(aiocb->aio_type & QEMU_AIO_WRITE)) { 1427c1bb86cdSEric Blake char *p = buf; 1428c1bb86cdSEric Blake size_t count = aiocb->aio_nbytes, copy; 1429c1bb86cdSEric Blake int i; 1430c1bb86cdSEric Blake 1431d57c44d0SKevin Wolf for (i = 0; i < aiocb->io.niov && count; ++i) { 1432c1bb86cdSEric Blake copy = count; 1433d57c44d0SKevin Wolf if (copy > aiocb->io.iov[i].iov_len) { 1434d57c44d0SKevin Wolf copy = aiocb->io.iov[i].iov_len; 1435c1bb86cdSEric Blake } 1436d57c44d0SKevin Wolf memcpy(aiocb->io.iov[i].iov_base, p, copy); 1437c1bb86cdSEric Blake assert(count >= copy); 1438c1bb86cdSEric Blake p += copy; 1439c1bb86cdSEric Blake count -= copy; 1440c1bb86cdSEric Blake } 1441c1bb86cdSEric Blake assert(count == 0); 1442c1bb86cdSEric Blake } 1443c1bb86cdSEric Blake qemu_vfree(buf); 1444c1bb86cdSEric Blake 144554c7ca1bSKevin Wolf out: 144654c7ca1bSKevin Wolf if (nbytes == aiocb->aio_nbytes) { 144754c7ca1bSKevin Wolf return 0; 144854c7ca1bSKevin Wolf } else if (nbytes >= 0 && nbytes < aiocb->aio_nbytes) { 144954c7ca1bSKevin Wolf if (aiocb->aio_type & QEMU_AIO_WRITE) { 145054c7ca1bSKevin Wolf return -EINVAL; 145154c7ca1bSKevin Wolf } else { 145254c7ca1bSKevin Wolf iov_memset(aiocb->io.iov, aiocb->io.niov, nbytes, 145354c7ca1bSKevin Wolf 0, aiocb->aio_nbytes - nbytes); 145454c7ca1bSKevin Wolf return 0; 145554c7ca1bSKevin Wolf } 145654c7ca1bSKevin Wolf } else { 145754c7ca1bSKevin Wolf assert(nbytes < 0); 1458c1bb86cdSEric Blake return nbytes; 1459c1bb86cdSEric Blake } 146054c7ca1bSKevin Wolf } 1461c1bb86cdSEric Blake 1462c1bb86cdSEric Blake static int translate_err(int err) 1463c1bb86cdSEric Blake { 1464c1bb86cdSEric Blake if (err == -ENODEV || err == -ENOSYS || err == -EOPNOTSUPP || 1465c1bb86cdSEric Blake err == -ENOTTY) { 1466c1bb86cdSEric Blake err = -ENOTSUP; 1467c1bb86cdSEric Blake } 1468c1bb86cdSEric Blake return err; 1469c1bb86cdSEric Blake } 1470c1bb86cdSEric Blake 1471c1bb86cdSEric Blake #ifdef CONFIG_FALLOCATE 1472c1bb86cdSEric Blake static int do_fallocate(int fd, int mode, off_t offset, off_t len) 1473c1bb86cdSEric Blake { 1474c1bb86cdSEric Blake do { 1475c1bb86cdSEric Blake if (fallocate(fd, mode, offset, len) == 0) { 1476c1bb86cdSEric Blake return 0; 1477c1bb86cdSEric Blake } 1478c1bb86cdSEric Blake } while (errno == EINTR); 1479c1bb86cdSEric Blake return translate_err(-errno); 1480c1bb86cdSEric Blake } 1481c1bb86cdSEric Blake #endif 1482c1bb86cdSEric Blake 1483c1bb86cdSEric Blake static ssize_t handle_aiocb_write_zeroes_block(RawPosixAIOData *aiocb) 1484c1bb86cdSEric Blake { 1485c1bb86cdSEric Blake int ret = -ENOTSUP; 1486c1bb86cdSEric Blake BDRVRawState *s = aiocb->bs->opaque; 1487c1bb86cdSEric Blake 1488c1bb86cdSEric Blake if (!s->has_write_zeroes) { 1489c1bb86cdSEric Blake return -ENOTSUP; 1490c1bb86cdSEric Blake } 1491c1bb86cdSEric Blake 1492c1bb86cdSEric Blake #ifdef BLKZEROOUT 1493738301e1SKevin Wolf /* The BLKZEROOUT implementation in the kernel doesn't set 1494738301e1SKevin Wolf * BLKDEV_ZERO_NOFALLBACK, so we can't call this if we have to avoid slow 1495738301e1SKevin Wolf * fallbacks. */ 1496738301e1SKevin Wolf if (!(aiocb->aio_type & QEMU_AIO_NO_FALLBACK)) { 1497c1bb86cdSEric Blake do { 1498c1bb86cdSEric Blake uint64_t range[2] = { aiocb->aio_offset, aiocb->aio_nbytes }; 1499c1bb86cdSEric Blake if (ioctl(aiocb->aio_fildes, BLKZEROOUT, range) == 0) { 1500c1bb86cdSEric Blake return 0; 1501c1bb86cdSEric Blake } 1502c1bb86cdSEric Blake } while (errno == EINTR); 1503c1bb86cdSEric Blake 1504c1bb86cdSEric Blake ret = translate_err(-errno); 1505c1bb86cdSEric Blake if (ret == -ENOTSUP) { 1506c1bb86cdSEric Blake s->has_write_zeroes = false; 1507c1bb86cdSEric Blake } 1508*effecce6SKevin Wolf } 1509*effecce6SKevin Wolf #endif 1510*effecce6SKevin Wolf 1511c1bb86cdSEric Blake return ret; 1512c1bb86cdSEric Blake } 1513c1bb86cdSEric Blake 15147154d8aeSKevin Wolf static int handle_aiocb_write_zeroes(void *opaque) 1515c1bb86cdSEric Blake { 15167154d8aeSKevin Wolf RawPosixAIOData *aiocb = opaque; 151770d9110bSDenis V. Lunev #ifdef CONFIG_FALLOCATE 1518b2c6f23fSMax Reitz BDRVRawState *s = aiocb->bs->opaque; 151970d9110bSDenis V. Lunev int64_t len; 152070d9110bSDenis V. Lunev #endif 1521c1bb86cdSEric Blake 1522c1bb86cdSEric Blake if (aiocb->aio_type & QEMU_AIO_BLKDEV) { 1523c1bb86cdSEric Blake return handle_aiocb_write_zeroes_block(aiocb); 1524c1bb86cdSEric Blake } 1525c1bb86cdSEric Blake 1526c1bb86cdSEric Blake #ifdef CONFIG_FALLOCATE_ZERO_RANGE 1527c1bb86cdSEric Blake if (s->has_write_zeroes) { 1528c1bb86cdSEric Blake int ret = do_fallocate(s->fd, FALLOC_FL_ZERO_RANGE, 1529c1bb86cdSEric Blake aiocb->aio_offset, aiocb->aio_nbytes); 1530294682ccSAndrey Shinkevich if (ret == -EINVAL) { 1531294682ccSAndrey Shinkevich /* 1532294682ccSAndrey Shinkevich * Allow falling back to pwrite for file systems that 1533294682ccSAndrey Shinkevich * do not support fallocate() for an unaligned byte range. 1534294682ccSAndrey Shinkevich */ 1535294682ccSAndrey Shinkevich return -ENOTSUP; 1536294682ccSAndrey Shinkevich } 1537c1bb86cdSEric Blake if (ret == 0 || ret != -ENOTSUP) { 1538c1bb86cdSEric Blake return ret; 1539c1bb86cdSEric Blake } 1540c1bb86cdSEric Blake s->has_write_zeroes = false; 1541c1bb86cdSEric Blake } 1542c1bb86cdSEric Blake #endif 1543c1bb86cdSEric Blake 1544c1bb86cdSEric Blake #ifdef CONFIG_FALLOCATE_PUNCH_HOLE 1545c1bb86cdSEric Blake if (s->has_discard && s->has_fallocate) { 1546c1bb86cdSEric Blake int ret = do_fallocate(s->fd, 1547c1bb86cdSEric Blake FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 1548c1bb86cdSEric Blake aiocb->aio_offset, aiocb->aio_nbytes); 1549c1bb86cdSEric Blake if (ret == 0) { 1550c1bb86cdSEric Blake ret = do_fallocate(s->fd, 0, aiocb->aio_offset, aiocb->aio_nbytes); 1551c1bb86cdSEric Blake if (ret == 0 || ret != -ENOTSUP) { 1552c1bb86cdSEric Blake return ret; 1553c1bb86cdSEric Blake } 1554c1bb86cdSEric Blake s->has_fallocate = false; 1555c1bb86cdSEric Blake } else if (ret != -ENOTSUP) { 1556c1bb86cdSEric Blake return ret; 1557c1bb86cdSEric Blake } else { 1558c1bb86cdSEric Blake s->has_discard = false; 1559c1bb86cdSEric Blake } 1560c1bb86cdSEric Blake } 1561c1bb86cdSEric Blake #endif 1562c1bb86cdSEric Blake 1563c1bb86cdSEric Blake #ifdef CONFIG_FALLOCATE 156470d9110bSDenis V. Lunev /* Last resort: we are trying to extend the file with zeroed data. This 156570d9110bSDenis V. Lunev * can be done via fallocate(fd, 0) */ 156670d9110bSDenis V. Lunev len = bdrv_getlength(aiocb->bs); 156770d9110bSDenis V. Lunev if (s->has_fallocate && len >= 0 && aiocb->aio_offset >= len) { 1568c1bb86cdSEric Blake int ret = do_fallocate(s->fd, 0, aiocb->aio_offset, aiocb->aio_nbytes); 1569c1bb86cdSEric Blake if (ret == 0 || ret != -ENOTSUP) { 1570c1bb86cdSEric Blake return ret; 1571c1bb86cdSEric Blake } 1572c1bb86cdSEric Blake s->has_fallocate = false; 1573c1bb86cdSEric Blake } 1574c1bb86cdSEric Blake #endif 1575c1bb86cdSEric Blake 1576c1bb86cdSEric Blake return -ENOTSUP; 1577c1bb86cdSEric Blake } 1578c1bb86cdSEric Blake 15797154d8aeSKevin Wolf static int handle_aiocb_write_zeroes_unmap(void *opaque) 158034fa110eSKevin Wolf { 15817154d8aeSKevin Wolf RawPosixAIOData *aiocb = opaque; 158234fa110eSKevin Wolf BDRVRawState *s G_GNUC_UNUSED = aiocb->bs->opaque; 158334fa110eSKevin Wolf int ret; 158434fa110eSKevin Wolf 158534fa110eSKevin Wolf /* First try to write zeros and unmap at the same time */ 158634fa110eSKevin Wolf 158734fa110eSKevin Wolf #ifdef CONFIG_FALLOCATE_PUNCH_HOLE 158834fa110eSKevin Wolf ret = do_fallocate(s->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 158934fa110eSKevin Wolf aiocb->aio_offset, aiocb->aio_nbytes); 159034fa110eSKevin Wolf if (ret != -ENOTSUP) { 159134fa110eSKevin Wolf return ret; 159234fa110eSKevin Wolf } 159334fa110eSKevin Wolf #endif 159434fa110eSKevin Wolf 159534fa110eSKevin Wolf /* If we couldn't manage to unmap while guaranteed that the area reads as 159634fa110eSKevin Wolf * all-zero afterwards, just write zeroes without unmapping */ 159734fa110eSKevin Wolf ret = handle_aiocb_write_zeroes(aiocb); 159834fa110eSKevin Wolf return ret; 159934fa110eSKevin Wolf } 160034fa110eSKevin Wolf 16011efad060SFam Zheng #ifndef HAVE_COPY_FILE_RANGE 16021efad060SFam Zheng static off_t copy_file_range(int in_fd, off_t *in_off, int out_fd, 16031efad060SFam Zheng off_t *out_off, size_t len, unsigned int flags) 16041efad060SFam Zheng { 16051efad060SFam Zheng #ifdef __NR_copy_file_range 16061efad060SFam Zheng return syscall(__NR_copy_file_range, in_fd, in_off, out_fd, 16071efad060SFam Zheng out_off, len, flags); 16081efad060SFam Zheng #else 16091efad060SFam Zheng errno = ENOSYS; 16101efad060SFam Zheng return -1; 16111efad060SFam Zheng #endif 16121efad060SFam Zheng } 16131efad060SFam Zheng #endif 16141efad060SFam Zheng 161558a209c4SKevin Wolf static int handle_aiocb_copy_range(void *opaque) 16161efad060SFam Zheng { 161758a209c4SKevin Wolf RawPosixAIOData *aiocb = opaque; 16181efad060SFam Zheng uint64_t bytes = aiocb->aio_nbytes; 16191efad060SFam Zheng off_t in_off = aiocb->aio_offset; 1620d57c44d0SKevin Wolf off_t out_off = aiocb->copy_range.aio_offset2; 16211efad060SFam Zheng 16221efad060SFam Zheng while (bytes) { 16231efad060SFam Zheng ssize_t ret = copy_file_range(aiocb->aio_fildes, &in_off, 1624d57c44d0SKevin Wolf aiocb->copy_range.aio_fd2, &out_off, 16251efad060SFam Zheng bytes, 0); 1626ecc983a5SFam Zheng trace_file_copy_file_range(aiocb->bs, aiocb->aio_fildes, in_off, 1627d57c44d0SKevin Wolf aiocb->copy_range.aio_fd2, out_off, bytes, 1628d57c44d0SKevin Wolf 0, ret); 1629c436e3d0SFam Zheng if (ret == 0) { 1630c436e3d0SFam Zheng /* No progress (e.g. when beyond EOF), let the caller fall back to 1631c436e3d0SFam Zheng * buffer I/O. */ 1632c436e3d0SFam Zheng return -ENOSPC; 16331efad060SFam Zheng } 16341efad060SFam Zheng if (ret < 0) { 1635c436e3d0SFam Zheng switch (errno) { 1636c436e3d0SFam Zheng case ENOSYS: 16371efad060SFam Zheng return -ENOTSUP; 1638c436e3d0SFam Zheng case EINTR: 1639c436e3d0SFam Zheng continue; 1640c436e3d0SFam Zheng default: 16411efad060SFam Zheng return -errno; 16421efad060SFam Zheng } 16431efad060SFam Zheng } 16441efad060SFam Zheng bytes -= ret; 16451efad060SFam Zheng } 16461efad060SFam Zheng return 0; 16471efad060SFam Zheng } 16481efad060SFam Zheng 164946ee0f46SKevin Wolf static int handle_aiocb_discard(void *opaque) 1650c1bb86cdSEric Blake { 165146ee0f46SKevin Wolf RawPosixAIOData *aiocb = opaque; 1652c1bb86cdSEric Blake int ret = -EOPNOTSUPP; 1653c1bb86cdSEric Blake BDRVRawState *s = aiocb->bs->opaque; 1654c1bb86cdSEric Blake 1655c1bb86cdSEric Blake if (!s->has_discard) { 1656c1bb86cdSEric Blake return -ENOTSUP; 1657c1bb86cdSEric Blake } 1658c1bb86cdSEric Blake 1659c1bb86cdSEric Blake if (aiocb->aio_type & QEMU_AIO_BLKDEV) { 1660c1bb86cdSEric Blake #ifdef BLKDISCARD 1661c1bb86cdSEric Blake do { 1662c1bb86cdSEric Blake uint64_t range[2] = { aiocb->aio_offset, aiocb->aio_nbytes }; 1663c1bb86cdSEric Blake if (ioctl(aiocb->aio_fildes, BLKDISCARD, range) == 0) { 1664c1bb86cdSEric Blake return 0; 1665c1bb86cdSEric Blake } 1666c1bb86cdSEric Blake } while (errno == EINTR); 1667c1bb86cdSEric Blake 1668c1bb86cdSEric Blake ret = -errno; 1669c1bb86cdSEric Blake #endif 1670c1bb86cdSEric Blake } else { 1671c1bb86cdSEric Blake #ifdef CONFIG_FALLOCATE_PUNCH_HOLE 1672c1bb86cdSEric Blake ret = do_fallocate(s->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 1673c1bb86cdSEric Blake aiocb->aio_offset, aiocb->aio_nbytes); 1674c1bb86cdSEric Blake #endif 1675c1bb86cdSEric Blake } 1676c1bb86cdSEric Blake 1677c1bb86cdSEric Blake ret = translate_err(ret); 1678c1bb86cdSEric Blake if (ret == -ENOTSUP) { 1679c1bb86cdSEric Blake s->has_discard = false; 1680c1bb86cdSEric Blake } 1681c1bb86cdSEric Blake return ret; 1682c1bb86cdSEric Blake } 1683c1bb86cdSEric Blake 16843a20013fSNir Soffer /* 16853a20013fSNir Soffer * Help alignment probing by allocating the first block. 16863a20013fSNir Soffer * 16873a20013fSNir Soffer * When reading with direct I/O from unallocated area on Gluster backed by XFS, 16883a20013fSNir Soffer * reading succeeds regardless of request length. In this case we fallback to 16893a20013fSNir Soffer * safe alignment which is not optimal. Allocating the first block avoids this 16903a20013fSNir Soffer * fallback. 16913a20013fSNir Soffer * 16923a20013fSNir Soffer * fd may be opened with O_DIRECT, but we don't know the buffer alignment or 16933a20013fSNir Soffer * request alignment, so we use safe values. 16943a20013fSNir Soffer * 16953a20013fSNir Soffer * Returns: 0 on success, -errno on failure. Since this is an optimization, 16963a20013fSNir Soffer * caller may ignore failures. 16973a20013fSNir Soffer */ 16983a20013fSNir Soffer static int allocate_first_block(int fd, size_t max_size) 16993a20013fSNir Soffer { 17003a20013fSNir Soffer size_t write_size = (max_size < MAX_BLOCKSIZE) 17013a20013fSNir Soffer ? BDRV_SECTOR_SIZE 17023a20013fSNir Soffer : MAX_BLOCKSIZE; 17033a20013fSNir Soffer size_t max_align = MAX(MAX_BLOCKSIZE, getpagesize()); 17043a20013fSNir Soffer void *buf; 17053a20013fSNir Soffer ssize_t n; 17063a20013fSNir Soffer int ret; 17073a20013fSNir Soffer 17083a20013fSNir Soffer buf = qemu_memalign(max_align, write_size); 17093a20013fSNir Soffer memset(buf, 0, write_size); 17103a20013fSNir Soffer 17113a20013fSNir Soffer do { 17123a20013fSNir Soffer n = pwrite(fd, buf, write_size, 0); 17133a20013fSNir Soffer } while (n == -1 && errno == EINTR); 17143a20013fSNir Soffer 17153a20013fSNir Soffer ret = (n == -1) ? -errno : 0; 17163a20013fSNir Soffer 17173a20013fSNir Soffer qemu_vfree(buf); 17183a20013fSNir Soffer return ret; 17193a20013fSNir Soffer } 17203a20013fSNir Soffer 172129cb4c01SKevin Wolf static int handle_aiocb_truncate(void *opaque) 172293f4e2ffSKevin Wolf { 172329cb4c01SKevin Wolf RawPosixAIOData *aiocb = opaque; 172493f4e2ffSKevin Wolf int result = 0; 172593f4e2ffSKevin Wolf int64_t current_length = 0; 172693f4e2ffSKevin Wolf char *buf = NULL; 172793f4e2ffSKevin Wolf struct stat st; 172893f4e2ffSKevin Wolf int fd = aiocb->aio_fildes; 172993f4e2ffSKevin Wolf int64_t offset = aiocb->aio_offset; 1730d57c44d0SKevin Wolf PreallocMode prealloc = aiocb->truncate.prealloc; 1731d57c44d0SKevin Wolf Error **errp = aiocb->truncate.errp; 173293f4e2ffSKevin Wolf 173393f4e2ffSKevin Wolf if (fstat(fd, &st) < 0) { 173493f4e2ffSKevin Wolf result = -errno; 173593f4e2ffSKevin Wolf error_setg_errno(errp, -result, "Could not stat file"); 173693f4e2ffSKevin Wolf return result; 173793f4e2ffSKevin Wolf } 173893f4e2ffSKevin Wolf 173993f4e2ffSKevin Wolf current_length = st.st_size; 1740d57c44d0SKevin Wolf if (current_length > offset && prealloc != PREALLOC_MODE_OFF) { 174193f4e2ffSKevin Wolf error_setg(errp, "Cannot use preallocation for shrinking files"); 174293f4e2ffSKevin Wolf return -ENOTSUP; 174393f4e2ffSKevin Wolf } 174493f4e2ffSKevin Wolf 1745d57c44d0SKevin Wolf switch (prealloc) { 174693f4e2ffSKevin Wolf #ifdef CONFIG_POSIX_FALLOCATE 174793f4e2ffSKevin Wolf case PREALLOC_MODE_FALLOC: 174893f4e2ffSKevin Wolf /* 174993f4e2ffSKevin Wolf * Truncating before posix_fallocate() makes it about twice slower on 175093f4e2ffSKevin Wolf * file systems that do not support fallocate(), trying to check if a 175193f4e2ffSKevin Wolf * block is allocated before allocating it, so don't do that here. 175293f4e2ffSKevin Wolf */ 175393f4e2ffSKevin Wolf if (offset != current_length) { 175493f4e2ffSKevin Wolf result = -posix_fallocate(fd, current_length, 175593f4e2ffSKevin Wolf offset - current_length); 175693f4e2ffSKevin Wolf if (result != 0) { 175793f4e2ffSKevin Wolf /* posix_fallocate() doesn't set errno. */ 175893f4e2ffSKevin Wolf error_setg_errno(errp, -result, 175993f4e2ffSKevin Wolf "Could not preallocate new data"); 17603a20013fSNir Soffer } else if (current_length == 0) { 17613a20013fSNir Soffer /* 17623a20013fSNir Soffer * posix_fallocate() uses fallocate() if the filesystem 17633a20013fSNir Soffer * supports it, or fallback to manually writing zeroes. If 17643a20013fSNir Soffer * fallocate() was used, unaligned reads from the fallocated 17653a20013fSNir Soffer * area in raw_probe_alignment() will succeed, hence we need to 17663a20013fSNir Soffer * allocate the first block. 17673a20013fSNir Soffer * 17683a20013fSNir Soffer * Optimize future alignment probing; ignore failures. 17693a20013fSNir Soffer */ 17703a20013fSNir Soffer allocate_first_block(fd, offset); 177193f4e2ffSKevin Wolf } 177293f4e2ffSKevin Wolf } else { 177393f4e2ffSKevin Wolf result = 0; 177493f4e2ffSKevin Wolf } 177593f4e2ffSKevin Wolf goto out; 177693f4e2ffSKevin Wolf #endif 177793f4e2ffSKevin Wolf case PREALLOC_MODE_FULL: 177893f4e2ffSKevin Wolf { 177993f4e2ffSKevin Wolf int64_t num = 0, left = offset - current_length; 178093f4e2ffSKevin Wolf off_t seek_result; 178193f4e2ffSKevin Wolf 178293f4e2ffSKevin Wolf /* 178393f4e2ffSKevin Wolf * Knowing the final size from the beginning could allow the file 178493f4e2ffSKevin Wolf * system driver to do less allocations and possibly avoid 178593f4e2ffSKevin Wolf * fragmentation of the file. 178693f4e2ffSKevin Wolf */ 178793f4e2ffSKevin Wolf if (ftruncate(fd, offset) != 0) { 178893f4e2ffSKevin Wolf result = -errno; 178993f4e2ffSKevin Wolf error_setg_errno(errp, -result, "Could not resize file"); 179093f4e2ffSKevin Wolf goto out; 179193f4e2ffSKevin Wolf } 179293f4e2ffSKevin Wolf 179393f4e2ffSKevin Wolf buf = g_malloc0(65536); 179493f4e2ffSKevin Wolf 179593f4e2ffSKevin Wolf seek_result = lseek(fd, current_length, SEEK_SET); 179693f4e2ffSKevin Wolf if (seek_result < 0) { 179793f4e2ffSKevin Wolf result = -errno; 179893f4e2ffSKevin Wolf error_setg_errno(errp, -result, 179993f4e2ffSKevin Wolf "Failed to seek to the old end of file"); 180093f4e2ffSKevin Wolf goto out; 180193f4e2ffSKevin Wolf } 180293f4e2ffSKevin Wolf 180393f4e2ffSKevin Wolf while (left > 0) { 180493f4e2ffSKevin Wolf num = MIN(left, 65536); 180593f4e2ffSKevin Wolf result = write(fd, buf, num); 180693f4e2ffSKevin Wolf if (result < 0) { 1807a1c81f4fSFam Zheng if (errno == EINTR) { 1808a1c81f4fSFam Zheng continue; 1809a1c81f4fSFam Zheng } 181093f4e2ffSKevin Wolf result = -errno; 181193f4e2ffSKevin Wolf error_setg_errno(errp, -result, 181293f4e2ffSKevin Wolf "Could not write zeros for preallocation"); 181393f4e2ffSKevin Wolf goto out; 181493f4e2ffSKevin Wolf } 181593f4e2ffSKevin Wolf left -= result; 181693f4e2ffSKevin Wolf } 181793f4e2ffSKevin Wolf if (result >= 0) { 181893f4e2ffSKevin Wolf result = fsync(fd); 181993f4e2ffSKevin Wolf if (result < 0) { 182093f4e2ffSKevin Wolf result = -errno; 182193f4e2ffSKevin Wolf error_setg_errno(errp, -result, 182293f4e2ffSKevin Wolf "Could not flush file to disk"); 182393f4e2ffSKevin Wolf goto out; 182493f4e2ffSKevin Wolf } 182593f4e2ffSKevin Wolf } 182693f4e2ffSKevin Wolf goto out; 182793f4e2ffSKevin Wolf } 182893f4e2ffSKevin Wolf case PREALLOC_MODE_OFF: 182993f4e2ffSKevin Wolf if (ftruncate(fd, offset) != 0) { 183093f4e2ffSKevin Wolf result = -errno; 183193f4e2ffSKevin Wolf error_setg_errno(errp, -result, "Could not resize file"); 18323a20013fSNir Soffer } else if (current_length == 0 && offset > current_length) { 18333a20013fSNir Soffer /* Optimize future alignment probing; ignore failures. */ 18343a20013fSNir Soffer allocate_first_block(fd, offset); 183593f4e2ffSKevin Wolf } 183693f4e2ffSKevin Wolf return result; 183793f4e2ffSKevin Wolf default: 183893f4e2ffSKevin Wolf result = -ENOTSUP; 183993f4e2ffSKevin Wolf error_setg(errp, "Unsupported preallocation mode: %s", 1840d57c44d0SKevin Wolf PreallocMode_str(prealloc)); 184193f4e2ffSKevin Wolf return result; 184293f4e2ffSKevin Wolf } 184393f4e2ffSKevin Wolf 184493f4e2ffSKevin Wolf out: 184593f4e2ffSKevin Wolf if (result < 0) { 184693f4e2ffSKevin Wolf if (ftruncate(fd, current_length) < 0) { 184793f4e2ffSKevin Wolf error_report("Failed to restore old file length: %s", 184893f4e2ffSKevin Wolf strerror(errno)); 184993f4e2ffSKevin Wolf } 185093f4e2ffSKevin Wolf } 185193f4e2ffSKevin Wolf 185293f4e2ffSKevin Wolf g_free(buf); 185393f4e2ffSKevin Wolf return result; 185493f4e2ffSKevin Wolf } 185593f4e2ffSKevin Wolf 18565d5de250SKevin Wolf static int coroutine_fn raw_thread_pool_submit(BlockDriverState *bs, 18575d5de250SKevin Wolf ThreadPoolFunc func, void *arg) 18585d5de250SKevin Wolf { 18595d5de250SKevin Wolf /* @bs can be NULL, bdrv_get_aio_context() returns the main context then */ 18605d5de250SKevin Wolf ThreadPool *pool = aio_get_thread_pool(bdrv_get_aio_context(bs)); 18615d5de250SKevin Wolf return thread_pool_submit_co(pool, func, arg); 18625d5de250SKevin Wolf } 18635d5de250SKevin Wolf 1864c1bb86cdSEric Blake static int coroutine_fn raw_co_prw(BlockDriverState *bs, uint64_t offset, 1865c1bb86cdSEric Blake uint64_t bytes, QEMUIOVector *qiov, int type) 1866c1bb86cdSEric Blake { 1867c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 1868999e6b69SKevin Wolf RawPosixAIOData acb; 1869c1bb86cdSEric Blake 1870c1bb86cdSEric Blake if (fd_open(bs) < 0) 1871c1bb86cdSEric Blake return -EIO; 1872c1bb86cdSEric Blake 1873c1bb86cdSEric Blake /* 1874c1bb86cdSEric Blake * Check if the underlying device requires requests to be aligned, 1875c1bb86cdSEric Blake * and if the request we are trying to submit is aligned or not. 1876c1bb86cdSEric Blake * If this is the case tell the low-level driver that it needs 1877c1bb86cdSEric Blake * to copy the buffer. 1878c1bb86cdSEric Blake */ 1879c1bb86cdSEric Blake if (s->needs_alignment) { 1880c1bb86cdSEric Blake if (!bdrv_qiov_is_aligned(bs, qiov)) { 1881c1bb86cdSEric Blake type |= QEMU_AIO_MISALIGNED; 1882c1bb86cdSEric Blake #ifdef CONFIG_LINUX_AIO 1883c1bb86cdSEric Blake } else if (s->use_linux_aio) { 1884c1bb86cdSEric Blake LinuxAioState *aio = aio_get_linux_aio(bdrv_get_aio_context(bs)); 1885c1bb86cdSEric Blake assert(qiov->size == bytes); 1886c1bb86cdSEric Blake return laio_co_submit(bs, aio, s->fd, offset, qiov, type); 1887c1bb86cdSEric Blake #endif 1888c1bb86cdSEric Blake } 1889c1bb86cdSEric Blake } 1890c1bb86cdSEric Blake 1891999e6b69SKevin Wolf acb = (RawPosixAIOData) { 1892999e6b69SKevin Wolf .bs = bs, 1893999e6b69SKevin Wolf .aio_fildes = s->fd, 1894999e6b69SKevin Wolf .aio_type = type, 1895999e6b69SKevin Wolf .aio_offset = offset, 1896999e6b69SKevin Wolf .aio_nbytes = bytes, 1897999e6b69SKevin Wolf .io = { 1898999e6b69SKevin Wolf .iov = qiov->iov, 1899999e6b69SKevin Wolf .niov = qiov->niov, 1900999e6b69SKevin Wolf }, 1901999e6b69SKevin Wolf }; 1902999e6b69SKevin Wolf 1903999e6b69SKevin Wolf assert(qiov->size == bytes); 1904999e6b69SKevin Wolf return raw_thread_pool_submit(bs, handle_aiocb_rw, &acb); 1905c1bb86cdSEric Blake } 1906c1bb86cdSEric Blake 1907c1bb86cdSEric Blake static int coroutine_fn raw_co_preadv(BlockDriverState *bs, uint64_t offset, 1908c1bb86cdSEric Blake uint64_t bytes, QEMUIOVector *qiov, 1909c1bb86cdSEric Blake int flags) 1910c1bb86cdSEric Blake { 1911c1bb86cdSEric Blake return raw_co_prw(bs, offset, bytes, qiov, QEMU_AIO_READ); 1912c1bb86cdSEric Blake } 1913c1bb86cdSEric Blake 1914c1bb86cdSEric Blake static int coroutine_fn raw_co_pwritev(BlockDriverState *bs, uint64_t offset, 1915c1bb86cdSEric Blake uint64_t bytes, QEMUIOVector *qiov, 1916c1bb86cdSEric Blake int flags) 1917c1bb86cdSEric Blake { 1918c1bb86cdSEric Blake assert(flags == 0); 1919c1bb86cdSEric Blake return raw_co_prw(bs, offset, bytes, qiov, QEMU_AIO_WRITE); 1920c1bb86cdSEric Blake } 1921c1bb86cdSEric Blake 1922c1bb86cdSEric Blake static void raw_aio_plug(BlockDriverState *bs) 1923c1bb86cdSEric Blake { 1924c1bb86cdSEric Blake #ifdef CONFIG_LINUX_AIO 1925c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 1926c1bb86cdSEric Blake if (s->use_linux_aio) { 1927c1bb86cdSEric Blake LinuxAioState *aio = aio_get_linux_aio(bdrv_get_aio_context(bs)); 1928c1bb86cdSEric Blake laio_io_plug(bs, aio); 1929c1bb86cdSEric Blake } 1930c1bb86cdSEric Blake #endif 1931c1bb86cdSEric Blake } 1932c1bb86cdSEric Blake 1933c1bb86cdSEric Blake static void raw_aio_unplug(BlockDriverState *bs) 1934c1bb86cdSEric Blake { 1935c1bb86cdSEric Blake #ifdef CONFIG_LINUX_AIO 1936c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 1937c1bb86cdSEric Blake if (s->use_linux_aio) { 1938c1bb86cdSEric Blake LinuxAioState *aio = aio_get_linux_aio(bdrv_get_aio_context(bs)); 1939c1bb86cdSEric Blake laio_io_unplug(bs, aio); 1940c1bb86cdSEric Blake } 1941c1bb86cdSEric Blake #endif 1942c1bb86cdSEric Blake } 1943c1bb86cdSEric Blake 194433d70fb6SKevin Wolf static int raw_co_flush_to_disk(BlockDriverState *bs) 1945c1bb86cdSEric Blake { 1946c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 194706dc9bd5SKevin Wolf RawPosixAIOData acb; 194833d70fb6SKevin Wolf int ret; 1949c1bb86cdSEric Blake 195033d70fb6SKevin Wolf ret = fd_open(bs); 195133d70fb6SKevin Wolf if (ret < 0) { 195233d70fb6SKevin Wolf return ret; 195333d70fb6SKevin Wolf } 1954c1bb86cdSEric Blake 195506dc9bd5SKevin Wolf acb = (RawPosixAIOData) { 195606dc9bd5SKevin Wolf .bs = bs, 195706dc9bd5SKevin Wolf .aio_fildes = s->fd, 195806dc9bd5SKevin Wolf .aio_type = QEMU_AIO_FLUSH, 195906dc9bd5SKevin Wolf }; 196006dc9bd5SKevin Wolf 196106dc9bd5SKevin Wolf return raw_thread_pool_submit(bs, handle_aiocb_flush, &acb); 1962c1bb86cdSEric Blake } 1963c1bb86cdSEric Blake 1964ed6e2161SNishanth Aravamudan static void raw_aio_attach_aio_context(BlockDriverState *bs, 1965ed6e2161SNishanth Aravamudan AioContext *new_context) 1966ed6e2161SNishanth Aravamudan { 1967ed6e2161SNishanth Aravamudan #ifdef CONFIG_LINUX_AIO 1968ed6e2161SNishanth Aravamudan BDRVRawState *s = bs->opaque; 1969ed6e2161SNishanth Aravamudan if (s->use_linux_aio) { 1970ed6e2161SNishanth Aravamudan Error *local_err; 1971ed6e2161SNishanth Aravamudan if (!aio_setup_linux_aio(new_context, &local_err)) { 1972ed6e2161SNishanth Aravamudan error_reportf_err(local_err, "Unable to use native AIO, " 1973ed6e2161SNishanth Aravamudan "falling back to thread pool: "); 1974ed6e2161SNishanth Aravamudan s->use_linux_aio = false; 1975ed6e2161SNishanth Aravamudan } 1976ed6e2161SNishanth Aravamudan } 1977ed6e2161SNishanth Aravamudan #endif 1978ed6e2161SNishanth Aravamudan } 1979ed6e2161SNishanth Aravamudan 1980c1bb86cdSEric Blake static void raw_close(BlockDriverState *bs) 1981c1bb86cdSEric Blake { 1982c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 1983c1bb86cdSEric Blake 1984c1bb86cdSEric Blake if (s->fd >= 0) { 1985c1bb86cdSEric Blake qemu_close(s->fd); 1986c1bb86cdSEric Blake s->fd = -1; 1987c1bb86cdSEric Blake } 1988c1bb86cdSEric Blake } 1989c1bb86cdSEric Blake 1990d0bc9e5dSMax Reitz /** 1991d0bc9e5dSMax Reitz * Truncates the given regular file @fd to @offset and, when growing, fills the 1992d0bc9e5dSMax Reitz * new space according to @prealloc. 1993d0bc9e5dSMax Reitz * 1994d0bc9e5dSMax Reitz * Returns: 0 on success, -errno on failure. 1995d0bc9e5dSMax Reitz */ 199693f4e2ffSKevin Wolf static int coroutine_fn 199793f4e2ffSKevin Wolf raw_regular_truncate(BlockDriverState *bs, int fd, int64_t offset, 199893f4e2ffSKevin Wolf PreallocMode prealloc, Error **errp) 19999f63b07eSMax Reitz { 200029cb4c01SKevin Wolf RawPosixAIOData acb; 2001d0bc9e5dSMax Reitz 200229cb4c01SKevin Wolf acb = (RawPosixAIOData) { 200393f4e2ffSKevin Wolf .bs = bs, 200493f4e2ffSKevin Wolf .aio_fildes = fd, 200593f4e2ffSKevin Wolf .aio_type = QEMU_AIO_TRUNCATE, 200693f4e2ffSKevin Wolf .aio_offset = offset, 2007d57c44d0SKevin Wolf .truncate = { 200893f4e2ffSKevin Wolf .prealloc = prealloc, 200993f4e2ffSKevin Wolf .errp = errp, 2010d57c44d0SKevin Wolf }, 201193f4e2ffSKevin Wolf }; 2012d0bc9e5dSMax Reitz 201329cb4c01SKevin Wolf return raw_thread_pool_submit(bs, handle_aiocb_truncate, &acb); 20149f63b07eSMax Reitz } 20159f63b07eSMax Reitz 2016061ca8a3SKevin Wolf static int coroutine_fn raw_co_truncate(BlockDriverState *bs, int64_t offset, 20178243ccb7SMax Reitz PreallocMode prealloc, Error **errp) 2018c1bb86cdSEric Blake { 2019c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2020c1bb86cdSEric Blake struct stat st; 2021f59adb32SMax Reitz int ret; 2022c1bb86cdSEric Blake 2023c1bb86cdSEric Blake if (fstat(s->fd, &st)) { 2024f59adb32SMax Reitz ret = -errno; 2025f59adb32SMax Reitz error_setg_errno(errp, -ret, "Failed to fstat() the file"); 2026f59adb32SMax Reitz return ret; 2027c1bb86cdSEric Blake } 2028c1bb86cdSEric Blake 2029c1bb86cdSEric Blake if (S_ISREG(st.st_mode)) { 203093f4e2ffSKevin Wolf return raw_regular_truncate(bs, s->fd, offset, prealloc, errp); 2031c1bb86cdSEric Blake } 203235d72602SMax Reitz 203335d72602SMax Reitz if (prealloc != PREALLOC_MODE_OFF) { 203435d72602SMax Reitz error_setg(errp, "Preallocation mode '%s' unsupported for this " 2035977c736fSMarkus Armbruster "non-regular file", PreallocMode_str(prealloc)); 203635d72602SMax Reitz return -ENOTSUP; 203735d72602SMax Reitz } 203835d72602SMax Reitz 203935d72602SMax Reitz if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) { 2040c1bb86cdSEric Blake if (offset > raw_getlength(bs)) { 2041f59adb32SMax Reitz error_setg(errp, "Cannot grow device files"); 2042c1bb86cdSEric Blake return -EINVAL; 2043c1bb86cdSEric Blake } 2044c1bb86cdSEric Blake } else { 2045f59adb32SMax Reitz error_setg(errp, "Resizing this file is not supported"); 2046c1bb86cdSEric Blake return -ENOTSUP; 2047c1bb86cdSEric Blake } 2048c1bb86cdSEric Blake 2049c1bb86cdSEric Blake return 0; 2050c1bb86cdSEric Blake } 2051c1bb86cdSEric Blake 2052c1bb86cdSEric Blake #ifdef __OpenBSD__ 2053c1bb86cdSEric Blake static int64_t raw_getlength(BlockDriverState *bs) 2054c1bb86cdSEric Blake { 2055c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2056c1bb86cdSEric Blake int fd = s->fd; 2057c1bb86cdSEric Blake struct stat st; 2058c1bb86cdSEric Blake 2059c1bb86cdSEric Blake if (fstat(fd, &st)) 2060c1bb86cdSEric Blake return -errno; 2061c1bb86cdSEric Blake if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) { 2062c1bb86cdSEric Blake struct disklabel dl; 2063c1bb86cdSEric Blake 2064c1bb86cdSEric Blake if (ioctl(fd, DIOCGDINFO, &dl)) 2065c1bb86cdSEric Blake return -errno; 2066c1bb86cdSEric Blake return (uint64_t)dl.d_secsize * 2067c1bb86cdSEric Blake dl.d_partitions[DISKPART(st.st_rdev)].p_size; 2068c1bb86cdSEric Blake } else 2069c1bb86cdSEric Blake return st.st_size; 2070c1bb86cdSEric Blake } 2071c1bb86cdSEric Blake #elif defined(__NetBSD__) 2072c1bb86cdSEric Blake static int64_t raw_getlength(BlockDriverState *bs) 2073c1bb86cdSEric Blake { 2074c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2075c1bb86cdSEric Blake int fd = s->fd; 2076c1bb86cdSEric Blake struct stat st; 2077c1bb86cdSEric Blake 2078c1bb86cdSEric Blake if (fstat(fd, &st)) 2079c1bb86cdSEric Blake return -errno; 2080c1bb86cdSEric Blake if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) { 2081c1bb86cdSEric Blake struct dkwedge_info dkw; 2082c1bb86cdSEric Blake 2083c1bb86cdSEric Blake if (ioctl(fd, DIOCGWEDGEINFO, &dkw) != -1) { 2084c1bb86cdSEric Blake return dkw.dkw_size * 512; 2085c1bb86cdSEric Blake } else { 2086c1bb86cdSEric Blake struct disklabel dl; 2087c1bb86cdSEric Blake 2088c1bb86cdSEric Blake if (ioctl(fd, DIOCGDINFO, &dl)) 2089c1bb86cdSEric Blake return -errno; 2090c1bb86cdSEric Blake return (uint64_t)dl.d_secsize * 2091c1bb86cdSEric Blake dl.d_partitions[DISKPART(st.st_rdev)].p_size; 2092c1bb86cdSEric Blake } 2093c1bb86cdSEric Blake } else 2094c1bb86cdSEric Blake return st.st_size; 2095c1bb86cdSEric Blake } 2096c1bb86cdSEric Blake #elif defined(__sun__) 2097c1bb86cdSEric Blake static int64_t raw_getlength(BlockDriverState *bs) 2098c1bb86cdSEric Blake { 2099c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2100c1bb86cdSEric Blake struct dk_minfo minfo; 2101c1bb86cdSEric Blake int ret; 2102c1bb86cdSEric Blake int64_t size; 2103c1bb86cdSEric Blake 2104c1bb86cdSEric Blake ret = fd_open(bs); 2105c1bb86cdSEric Blake if (ret < 0) { 2106c1bb86cdSEric Blake return ret; 2107c1bb86cdSEric Blake } 2108c1bb86cdSEric Blake 2109c1bb86cdSEric Blake /* 2110c1bb86cdSEric Blake * Use the DKIOCGMEDIAINFO ioctl to read the size. 2111c1bb86cdSEric Blake */ 2112c1bb86cdSEric Blake ret = ioctl(s->fd, DKIOCGMEDIAINFO, &minfo); 2113c1bb86cdSEric Blake if (ret != -1) { 2114c1bb86cdSEric Blake return minfo.dki_lbsize * minfo.dki_capacity; 2115c1bb86cdSEric Blake } 2116c1bb86cdSEric Blake 2117c1bb86cdSEric Blake /* 2118c1bb86cdSEric Blake * There are reports that lseek on some devices fails, but 2119c1bb86cdSEric Blake * irc discussion said that contingency on contingency was overkill. 2120c1bb86cdSEric Blake */ 2121c1bb86cdSEric Blake size = lseek(s->fd, 0, SEEK_END); 2122c1bb86cdSEric Blake if (size < 0) { 2123c1bb86cdSEric Blake return -errno; 2124c1bb86cdSEric Blake } 2125c1bb86cdSEric Blake return size; 2126c1bb86cdSEric Blake } 2127c1bb86cdSEric Blake #elif defined(CONFIG_BSD) 2128c1bb86cdSEric Blake static int64_t raw_getlength(BlockDriverState *bs) 2129c1bb86cdSEric Blake { 2130c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2131c1bb86cdSEric Blake int fd = s->fd; 2132c1bb86cdSEric Blake int64_t size; 2133c1bb86cdSEric Blake struct stat sb; 2134c1bb86cdSEric Blake #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__) 2135c1bb86cdSEric Blake int reopened = 0; 2136c1bb86cdSEric Blake #endif 2137c1bb86cdSEric Blake int ret; 2138c1bb86cdSEric Blake 2139c1bb86cdSEric Blake ret = fd_open(bs); 2140c1bb86cdSEric Blake if (ret < 0) 2141c1bb86cdSEric Blake return ret; 2142c1bb86cdSEric Blake 2143c1bb86cdSEric Blake #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__) 2144c1bb86cdSEric Blake again: 2145c1bb86cdSEric Blake #endif 2146c1bb86cdSEric Blake if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) { 2147c1bb86cdSEric Blake #ifdef DIOCGMEDIASIZE 2148c1bb86cdSEric Blake if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size)) 2149c1bb86cdSEric Blake #elif defined(DIOCGPART) 2150c1bb86cdSEric Blake { 2151c1bb86cdSEric Blake struct partinfo pi; 2152c1bb86cdSEric Blake if (ioctl(fd, DIOCGPART, &pi) == 0) 2153c1bb86cdSEric Blake size = pi.media_size; 2154c1bb86cdSEric Blake else 2155c1bb86cdSEric Blake size = 0; 2156c1bb86cdSEric Blake } 2157c1bb86cdSEric Blake if (size == 0) 2158c1bb86cdSEric Blake #endif 2159c1bb86cdSEric Blake #if defined(__APPLE__) && defined(__MACH__) 2160c1bb86cdSEric Blake { 2161c1bb86cdSEric Blake uint64_t sectors = 0; 2162c1bb86cdSEric Blake uint32_t sector_size = 0; 2163c1bb86cdSEric Blake 2164c1bb86cdSEric Blake if (ioctl(fd, DKIOCGETBLOCKCOUNT, §ors) == 0 2165c1bb86cdSEric Blake && ioctl(fd, DKIOCGETBLOCKSIZE, §or_size) == 0) { 2166c1bb86cdSEric Blake size = sectors * sector_size; 2167c1bb86cdSEric Blake } else { 2168c1bb86cdSEric Blake size = lseek(fd, 0LL, SEEK_END); 2169c1bb86cdSEric Blake if (size < 0) { 2170c1bb86cdSEric Blake return -errno; 2171c1bb86cdSEric Blake } 2172c1bb86cdSEric Blake } 2173c1bb86cdSEric Blake } 2174c1bb86cdSEric Blake #else 2175c1bb86cdSEric Blake size = lseek(fd, 0LL, SEEK_END); 2176c1bb86cdSEric Blake if (size < 0) { 2177c1bb86cdSEric Blake return -errno; 2178c1bb86cdSEric Blake } 2179c1bb86cdSEric Blake #endif 2180c1bb86cdSEric Blake #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) 2181c1bb86cdSEric Blake switch(s->type) { 2182c1bb86cdSEric Blake case FTYPE_CD: 2183c1bb86cdSEric Blake /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */ 2184c1bb86cdSEric Blake if (size == 2048LL * (unsigned)-1) 2185c1bb86cdSEric Blake size = 0; 2186c1bb86cdSEric Blake /* XXX no disc? maybe we need to reopen... */ 2187c1bb86cdSEric Blake if (size <= 0 && !reopened && cdrom_reopen(bs) >= 0) { 2188c1bb86cdSEric Blake reopened = 1; 2189c1bb86cdSEric Blake goto again; 2190c1bb86cdSEric Blake } 2191c1bb86cdSEric Blake } 2192c1bb86cdSEric Blake #endif 2193c1bb86cdSEric Blake } else { 2194c1bb86cdSEric Blake size = lseek(fd, 0, SEEK_END); 2195c1bb86cdSEric Blake if (size < 0) { 2196c1bb86cdSEric Blake return -errno; 2197c1bb86cdSEric Blake } 2198c1bb86cdSEric Blake } 2199c1bb86cdSEric Blake return size; 2200c1bb86cdSEric Blake } 2201c1bb86cdSEric Blake #else 2202c1bb86cdSEric Blake static int64_t raw_getlength(BlockDriverState *bs) 2203c1bb86cdSEric Blake { 2204c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2205c1bb86cdSEric Blake int ret; 2206c1bb86cdSEric Blake int64_t size; 2207c1bb86cdSEric Blake 2208c1bb86cdSEric Blake ret = fd_open(bs); 2209c1bb86cdSEric Blake if (ret < 0) { 2210c1bb86cdSEric Blake return ret; 2211c1bb86cdSEric Blake } 2212c1bb86cdSEric Blake 2213c1bb86cdSEric Blake size = lseek(s->fd, 0, SEEK_END); 2214c1bb86cdSEric Blake if (size < 0) { 2215c1bb86cdSEric Blake return -errno; 2216c1bb86cdSEric Blake } 2217c1bb86cdSEric Blake return size; 2218c1bb86cdSEric Blake } 2219c1bb86cdSEric Blake #endif 2220c1bb86cdSEric Blake 2221c1bb86cdSEric Blake static int64_t raw_get_allocated_file_size(BlockDriverState *bs) 2222c1bb86cdSEric Blake { 2223c1bb86cdSEric Blake struct stat st; 2224c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2225c1bb86cdSEric Blake 2226c1bb86cdSEric Blake if (fstat(s->fd, &st) < 0) { 2227c1bb86cdSEric Blake return -errno; 2228c1bb86cdSEric Blake } 2229c1bb86cdSEric Blake return (int64_t)st.st_blocks * 512; 2230c1bb86cdSEric Blake } 2231c1bb86cdSEric Blake 223293f4e2ffSKevin Wolf static int coroutine_fn 223393f4e2ffSKevin Wolf raw_co_create(BlockdevCreateOptions *options, Error **errp) 2234c1bb86cdSEric Blake { 2235927f11e1SKevin Wolf BlockdevCreateOptionsFile *file_opts; 22367c20c808SMax Reitz Error *local_err = NULL; 2237c1bb86cdSEric Blake int fd; 2238d815efcaSMax Reitz uint64_t perm, shared; 2239c1bb86cdSEric Blake int result = 0; 2240c1bb86cdSEric Blake 2241927f11e1SKevin Wolf /* Validate options and set default values */ 2242927f11e1SKevin Wolf assert(options->driver == BLOCKDEV_DRIVER_FILE); 2243927f11e1SKevin Wolf file_opts = &options->u.file; 2244c1bb86cdSEric Blake 2245927f11e1SKevin Wolf if (!file_opts->has_nocow) { 2246927f11e1SKevin Wolf file_opts->nocow = false; 2247927f11e1SKevin Wolf } 2248927f11e1SKevin Wolf if (!file_opts->has_preallocation) { 2249927f11e1SKevin Wolf file_opts->preallocation = PREALLOC_MODE_OFF; 2250c1bb86cdSEric Blake } 2251c1bb86cdSEric Blake 2252927f11e1SKevin Wolf /* Create file */ 2253b8cf1913SMax Reitz fd = qemu_open(file_opts->filename, O_RDWR | O_CREAT | O_BINARY, 0644); 2254c1bb86cdSEric Blake if (fd < 0) { 2255c1bb86cdSEric Blake result = -errno; 2256c1bb86cdSEric Blake error_setg_errno(errp, -result, "Could not create file"); 2257c1bb86cdSEric Blake goto out; 2258c1bb86cdSEric Blake } 2259c1bb86cdSEric Blake 2260b8cf1913SMax Reitz /* Take permissions: We want to discard everything, so we need 2261b8cf1913SMax Reitz * BLK_PERM_WRITE; and truncation to the desired size requires 2262b8cf1913SMax Reitz * BLK_PERM_RESIZE. 2263b8cf1913SMax Reitz * On the other hand, we cannot share the RESIZE permission 2264b8cf1913SMax Reitz * because we promise that after this function, the file has the 2265b8cf1913SMax Reitz * size given in the options. If someone else were to resize it 2266b8cf1913SMax Reitz * concurrently, we could not guarantee that. 2267b8cf1913SMax Reitz * Note that after this function, we can no longer guarantee that 2268b8cf1913SMax Reitz * the file is not touched by a third party, so it may be resized 2269b8cf1913SMax Reitz * then. */ 2270b8cf1913SMax Reitz perm = BLK_PERM_WRITE | BLK_PERM_RESIZE; 2271b8cf1913SMax Reitz shared = BLK_PERM_ALL & ~BLK_PERM_RESIZE; 2272b8cf1913SMax Reitz 2273b8cf1913SMax Reitz /* Step one: Take locks */ 22742996ffadSFam Zheng result = raw_apply_lock_bytes(NULL, fd, perm, ~shared, false, errp); 2275b8cf1913SMax Reitz if (result < 0) { 2276b8cf1913SMax Reitz goto out_close; 2277b8cf1913SMax Reitz } 2278b8cf1913SMax Reitz 2279b8cf1913SMax Reitz /* Step two: Check that nobody else has taken conflicting locks */ 2280b8cf1913SMax Reitz result = raw_check_lock_bytes(fd, perm, shared, errp); 2281b8cf1913SMax Reitz if (result < 0) { 2282b857431dSFam Zheng error_append_hint(errp, 2283b857431dSFam Zheng "Is another process using the image [%s]?\n", 2284b857431dSFam Zheng file_opts->filename); 22857c20c808SMax Reitz goto out_unlock; 2286b8cf1913SMax Reitz } 2287b8cf1913SMax Reitz 2288b8cf1913SMax Reitz /* Clear the file by truncating it to 0 */ 228993f4e2ffSKevin Wolf result = raw_regular_truncate(NULL, fd, 0, PREALLOC_MODE_OFF, errp); 2290b8cf1913SMax Reitz if (result < 0) { 22917c20c808SMax Reitz goto out_unlock; 2292b8cf1913SMax Reitz } 2293b8cf1913SMax Reitz 2294927f11e1SKevin Wolf if (file_opts->nocow) { 2295c1bb86cdSEric Blake #ifdef __linux__ 2296c1bb86cdSEric Blake /* Set NOCOW flag to solve performance issue on fs like btrfs. 2297c1bb86cdSEric Blake * This is an optimisation. The FS_IOC_SETFLAGS ioctl return value 2298c1bb86cdSEric Blake * will be ignored since any failure of this operation should not 2299c1bb86cdSEric Blake * block the left work. 2300c1bb86cdSEric Blake */ 2301c1bb86cdSEric Blake int attr; 2302c1bb86cdSEric Blake if (ioctl(fd, FS_IOC_GETFLAGS, &attr) == 0) { 2303c1bb86cdSEric Blake attr |= FS_NOCOW_FL; 2304c1bb86cdSEric Blake ioctl(fd, FS_IOC_SETFLAGS, &attr); 2305c1bb86cdSEric Blake } 2306c1bb86cdSEric Blake #endif 2307c1bb86cdSEric Blake } 2308c1bb86cdSEric Blake 2309b8cf1913SMax Reitz /* Resize and potentially preallocate the file to the desired 2310b8cf1913SMax Reitz * final size */ 231193f4e2ffSKevin Wolf result = raw_regular_truncate(NULL, fd, file_opts->size, 231293f4e2ffSKevin Wolf file_opts->preallocation, errp); 23139f63b07eSMax Reitz if (result < 0) { 23147c20c808SMax Reitz goto out_unlock; 23157c20c808SMax Reitz } 23167c20c808SMax Reitz 23177c20c808SMax Reitz out_unlock: 23182996ffadSFam Zheng raw_apply_lock_bytes(NULL, fd, 0, 0, true, &local_err); 23197c20c808SMax Reitz if (local_err) { 23207c20c808SMax Reitz /* The above call should not fail, and if it does, that does 23217c20c808SMax Reitz * not mean the whole creation operation has failed. So 23227c20c808SMax Reitz * report it the user for their convenience, but do not report 23237c20c808SMax Reitz * it to the caller. */ 2324db0754dfSFam Zheng warn_report_err(local_err); 23255a1dad9dSNir Soffer } 23265a1dad9dSNir Soffer 23275a1dad9dSNir Soffer out_close: 2328c1bb86cdSEric Blake if (qemu_close(fd) != 0 && result == 0) { 2329c1bb86cdSEric Blake result = -errno; 2330c1bb86cdSEric Blake error_setg_errno(errp, -result, "Could not close the new file"); 2331c1bb86cdSEric Blake } 2332c1bb86cdSEric Blake out: 2333c1bb86cdSEric Blake return result; 2334c1bb86cdSEric Blake } 2335c1bb86cdSEric Blake 2336927f11e1SKevin Wolf static int coroutine_fn raw_co_create_opts(const char *filename, QemuOpts *opts, 2337927f11e1SKevin Wolf Error **errp) 2338927f11e1SKevin Wolf { 2339927f11e1SKevin Wolf BlockdevCreateOptions options; 2340927f11e1SKevin Wolf int64_t total_size = 0; 2341927f11e1SKevin Wolf bool nocow = false; 2342927f11e1SKevin Wolf PreallocMode prealloc; 2343927f11e1SKevin Wolf char *buf = NULL; 2344927f11e1SKevin Wolf Error *local_err = NULL; 2345927f11e1SKevin Wolf 2346927f11e1SKevin Wolf /* Skip file: protocol prefix */ 2347927f11e1SKevin Wolf strstart(filename, "file:", &filename); 2348927f11e1SKevin Wolf 2349927f11e1SKevin Wolf /* Read out options */ 2350927f11e1SKevin Wolf total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), 2351927f11e1SKevin Wolf BDRV_SECTOR_SIZE); 2352927f11e1SKevin Wolf nocow = qemu_opt_get_bool(opts, BLOCK_OPT_NOCOW, false); 2353927f11e1SKevin Wolf buf = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC); 2354927f11e1SKevin Wolf prealloc = qapi_enum_parse(&PreallocMode_lookup, buf, 2355927f11e1SKevin Wolf PREALLOC_MODE_OFF, &local_err); 2356927f11e1SKevin Wolf g_free(buf); 2357927f11e1SKevin Wolf if (local_err) { 2358927f11e1SKevin Wolf error_propagate(errp, local_err); 2359927f11e1SKevin Wolf return -EINVAL; 2360927f11e1SKevin Wolf } 2361927f11e1SKevin Wolf 2362927f11e1SKevin Wolf options = (BlockdevCreateOptions) { 2363927f11e1SKevin Wolf .driver = BLOCKDEV_DRIVER_FILE, 2364927f11e1SKevin Wolf .u.file = { 2365927f11e1SKevin Wolf .filename = (char *) filename, 2366927f11e1SKevin Wolf .size = total_size, 2367927f11e1SKevin Wolf .has_preallocation = true, 2368927f11e1SKevin Wolf .preallocation = prealloc, 2369927f11e1SKevin Wolf .has_nocow = true, 2370927f11e1SKevin Wolf .nocow = nocow, 2371927f11e1SKevin Wolf }, 2372927f11e1SKevin Wolf }; 2373927f11e1SKevin Wolf return raw_co_create(&options, errp); 2374927f11e1SKevin Wolf } 2375927f11e1SKevin Wolf 2376c1bb86cdSEric Blake /* 2377c1bb86cdSEric Blake * Find allocation range in @bs around offset @start. 2378c1bb86cdSEric Blake * May change underlying file descriptor's file offset. 2379c1bb86cdSEric Blake * If @start is not in a hole, store @start in @data, and the 2380c1bb86cdSEric Blake * beginning of the next hole in @hole, and return 0. 2381c1bb86cdSEric Blake * If @start is in a non-trailing hole, store @start in @hole and the 2382c1bb86cdSEric Blake * beginning of the next non-hole in @data, and return 0. 2383c1bb86cdSEric Blake * If @start is in a trailing hole or beyond EOF, return -ENXIO. 2384c1bb86cdSEric Blake * If we can't find out, return a negative errno other than -ENXIO. 2385c1bb86cdSEric Blake */ 2386c1bb86cdSEric Blake static int find_allocation(BlockDriverState *bs, off_t start, 2387c1bb86cdSEric Blake off_t *data, off_t *hole) 2388c1bb86cdSEric Blake { 2389c1bb86cdSEric Blake #if defined SEEK_HOLE && defined SEEK_DATA 2390c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2391c1bb86cdSEric Blake off_t offs; 2392c1bb86cdSEric Blake 2393c1bb86cdSEric Blake /* 2394c1bb86cdSEric Blake * SEEK_DATA cases: 2395c1bb86cdSEric Blake * D1. offs == start: start is in data 2396c1bb86cdSEric Blake * D2. offs > start: start is in a hole, next data at offs 2397c1bb86cdSEric Blake * D3. offs < 0, errno = ENXIO: either start is in a trailing hole 2398c1bb86cdSEric Blake * or start is beyond EOF 2399c1bb86cdSEric Blake * If the latter happens, the file has been truncated behind 2400c1bb86cdSEric Blake * our back since we opened it. All bets are off then. 2401c1bb86cdSEric Blake * Treating like a trailing hole is simplest. 2402c1bb86cdSEric Blake * D4. offs < 0, errno != ENXIO: we learned nothing 2403c1bb86cdSEric Blake */ 2404c1bb86cdSEric Blake offs = lseek(s->fd, start, SEEK_DATA); 2405c1bb86cdSEric Blake if (offs < 0) { 2406c1bb86cdSEric Blake return -errno; /* D3 or D4 */ 2407c1bb86cdSEric Blake } 2408a03083a0SJeff Cody 2409a03083a0SJeff Cody if (offs < start) { 2410a03083a0SJeff Cody /* This is not a valid return by lseek(). We are safe to just return 2411a03083a0SJeff Cody * -EIO in this case, and we'll treat it like D4. */ 2412a03083a0SJeff Cody return -EIO; 2413a03083a0SJeff Cody } 2414c1bb86cdSEric Blake 2415c1bb86cdSEric Blake if (offs > start) { 2416c1bb86cdSEric Blake /* D2: in hole, next data at offs */ 2417c1bb86cdSEric Blake *hole = start; 2418c1bb86cdSEric Blake *data = offs; 2419c1bb86cdSEric Blake return 0; 2420c1bb86cdSEric Blake } 2421c1bb86cdSEric Blake 2422c1bb86cdSEric Blake /* D1: in data, end not yet known */ 2423c1bb86cdSEric Blake 2424c1bb86cdSEric Blake /* 2425c1bb86cdSEric Blake * SEEK_HOLE cases: 2426c1bb86cdSEric Blake * H1. offs == start: start is in a hole 2427c1bb86cdSEric Blake * If this happens here, a hole has been dug behind our back 2428c1bb86cdSEric Blake * since the previous lseek(). 2429c1bb86cdSEric Blake * H2. offs > start: either start is in data, next hole at offs, 2430c1bb86cdSEric Blake * or start is in trailing hole, EOF at offs 2431c1bb86cdSEric Blake * Linux treats trailing holes like any other hole: offs == 2432c1bb86cdSEric Blake * start. Solaris seeks to EOF instead: offs > start (blech). 2433c1bb86cdSEric Blake * If that happens here, a hole has been dug behind our back 2434c1bb86cdSEric Blake * since the previous lseek(). 2435c1bb86cdSEric Blake * H3. offs < 0, errno = ENXIO: start is beyond EOF 2436c1bb86cdSEric Blake * If this happens, the file has been truncated behind our 2437c1bb86cdSEric Blake * back since we opened it. Treat it like a trailing hole. 2438c1bb86cdSEric Blake * H4. offs < 0, errno != ENXIO: we learned nothing 2439c1bb86cdSEric Blake * Pretend we know nothing at all, i.e. "forget" about D1. 2440c1bb86cdSEric Blake */ 2441c1bb86cdSEric Blake offs = lseek(s->fd, start, SEEK_HOLE); 2442c1bb86cdSEric Blake if (offs < 0) { 2443c1bb86cdSEric Blake return -errno; /* D1 and (H3 or H4) */ 2444c1bb86cdSEric Blake } 2445a03083a0SJeff Cody 2446a03083a0SJeff Cody if (offs < start) { 2447a03083a0SJeff Cody /* This is not a valid return by lseek(). We are safe to just return 2448a03083a0SJeff Cody * -EIO in this case, and we'll treat it like H4. */ 2449a03083a0SJeff Cody return -EIO; 2450a03083a0SJeff Cody } 2451c1bb86cdSEric Blake 2452c1bb86cdSEric Blake if (offs > start) { 2453c1bb86cdSEric Blake /* 2454c1bb86cdSEric Blake * D1 and H2: either in data, next hole at offs, or it was in 2455c1bb86cdSEric Blake * data but is now in a trailing hole. In the latter case, 2456c1bb86cdSEric Blake * all bets are off. Treating it as if it there was data all 2457c1bb86cdSEric Blake * the way to EOF is safe, so simply do that. 2458c1bb86cdSEric Blake */ 2459c1bb86cdSEric Blake *data = start; 2460c1bb86cdSEric Blake *hole = offs; 2461c1bb86cdSEric Blake return 0; 2462c1bb86cdSEric Blake } 2463c1bb86cdSEric Blake 2464c1bb86cdSEric Blake /* D1 and H1 */ 2465c1bb86cdSEric Blake return -EBUSY; 2466c1bb86cdSEric Blake #else 2467c1bb86cdSEric Blake return -ENOTSUP; 2468c1bb86cdSEric Blake #endif 2469c1bb86cdSEric Blake } 2470c1bb86cdSEric Blake 2471c1bb86cdSEric Blake /* 2472a290f085SEric Blake * Returns the allocation status of the specified offset. 2473c1bb86cdSEric Blake * 2474a290f085SEric Blake * The block layer guarantees 'offset' and 'bytes' are within bounds. 2475c1bb86cdSEric Blake * 2476a290f085SEric Blake * 'pnum' is set to the number of bytes (including and immediately following 2477a290f085SEric Blake * the specified offset) that are known to be in the same 2478c1bb86cdSEric Blake * allocated/unallocated state. 2479c1bb86cdSEric Blake * 2480a290f085SEric Blake * 'bytes' is the max value 'pnum' should be set to. 2481c1bb86cdSEric Blake */ 2482a290f085SEric Blake static int coroutine_fn raw_co_block_status(BlockDriverState *bs, 2483a290f085SEric Blake bool want_zero, 2484a290f085SEric Blake int64_t offset, 2485a290f085SEric Blake int64_t bytes, int64_t *pnum, 2486a290f085SEric Blake int64_t *map, 2487c1bb86cdSEric Blake BlockDriverState **file) 2488c1bb86cdSEric Blake { 2489a290f085SEric Blake off_t data = 0, hole = 0; 2490c1bb86cdSEric Blake int ret; 2491c1bb86cdSEric Blake 24929c3db310SMax Reitz assert(QEMU_IS_ALIGNED(offset | bytes, bs->bl.request_alignment)); 24939c3db310SMax Reitz 2494c1bb86cdSEric Blake ret = fd_open(bs); 2495c1bb86cdSEric Blake if (ret < 0) { 2496c1bb86cdSEric Blake return ret; 2497c1bb86cdSEric Blake } 2498c1bb86cdSEric Blake 2499a290f085SEric Blake if (!want_zero) { 2500a290f085SEric Blake *pnum = bytes; 2501a290f085SEric Blake *map = offset; 2502a290f085SEric Blake *file = bs; 2503a290f085SEric Blake return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID; 2504c1bb86cdSEric Blake } 2505c1bb86cdSEric Blake 2506a290f085SEric Blake ret = find_allocation(bs, offset, &data, &hole); 2507c1bb86cdSEric Blake if (ret == -ENXIO) { 2508c1bb86cdSEric Blake /* Trailing hole */ 2509a290f085SEric Blake *pnum = bytes; 2510c1bb86cdSEric Blake ret = BDRV_BLOCK_ZERO; 2511c1bb86cdSEric Blake } else if (ret < 0) { 2512c1bb86cdSEric Blake /* No info available, so pretend there are no holes */ 2513a290f085SEric Blake *pnum = bytes; 2514c1bb86cdSEric Blake ret = BDRV_BLOCK_DATA; 2515a290f085SEric Blake } else if (data == offset) { 2516a290f085SEric Blake /* On a data extent, compute bytes to the end of the extent, 2517c1bb86cdSEric Blake * possibly including a partial sector at EOF. */ 2518a290f085SEric Blake *pnum = MIN(bytes, hole - offset); 25199c3db310SMax Reitz 25209c3db310SMax Reitz /* 25219c3db310SMax Reitz * We are not allowed to return partial sectors, though, so 25229c3db310SMax Reitz * round up if necessary. 25239c3db310SMax Reitz */ 25249c3db310SMax Reitz if (!QEMU_IS_ALIGNED(*pnum, bs->bl.request_alignment)) { 25259c3db310SMax Reitz int64_t file_length = raw_getlength(bs); 25269c3db310SMax Reitz if (file_length > 0) { 25279c3db310SMax Reitz /* Ignore errors, this is just a safeguard */ 25289c3db310SMax Reitz assert(hole == file_length); 25299c3db310SMax Reitz } 25309c3db310SMax Reitz *pnum = ROUND_UP(*pnum, bs->bl.request_alignment); 25319c3db310SMax Reitz } 25329c3db310SMax Reitz 2533c1bb86cdSEric Blake ret = BDRV_BLOCK_DATA; 2534c1bb86cdSEric Blake } else { 2535a290f085SEric Blake /* On a hole, compute bytes to the beginning of the next extent. */ 2536a290f085SEric Blake assert(hole == offset); 2537a290f085SEric Blake *pnum = MIN(bytes, data - offset); 2538c1bb86cdSEric Blake ret = BDRV_BLOCK_ZERO; 2539c1bb86cdSEric Blake } 2540a290f085SEric Blake *map = offset; 2541c1bb86cdSEric Blake *file = bs; 2542a290f085SEric Blake return ret | BDRV_BLOCK_OFFSET_VALID; 2543c1bb86cdSEric Blake } 2544c1bb86cdSEric Blake 254531be8a2aSStefan Hajnoczi #if defined(__linux__) 254631be8a2aSStefan Hajnoczi /* Verify that the file is not in the page cache */ 254731be8a2aSStefan Hajnoczi static void check_cache_dropped(BlockDriverState *bs, Error **errp) 254831be8a2aSStefan Hajnoczi { 254931be8a2aSStefan Hajnoczi const size_t window_size = 128 * 1024 * 1024; 255031be8a2aSStefan Hajnoczi BDRVRawState *s = bs->opaque; 255131be8a2aSStefan Hajnoczi void *window = NULL; 255231be8a2aSStefan Hajnoczi size_t length = 0; 255331be8a2aSStefan Hajnoczi unsigned char *vec; 255431be8a2aSStefan Hajnoczi size_t page_size; 255531be8a2aSStefan Hajnoczi off_t offset; 255631be8a2aSStefan Hajnoczi off_t end; 255731be8a2aSStefan Hajnoczi 255831be8a2aSStefan Hajnoczi /* mincore(2) page status information requires 1 byte per page */ 255931be8a2aSStefan Hajnoczi page_size = sysconf(_SC_PAGESIZE); 256031be8a2aSStefan Hajnoczi vec = g_malloc(DIV_ROUND_UP(window_size, page_size)); 256131be8a2aSStefan Hajnoczi 256231be8a2aSStefan Hajnoczi end = raw_getlength(bs); 256331be8a2aSStefan Hajnoczi 256431be8a2aSStefan Hajnoczi for (offset = 0; offset < end; offset += window_size) { 256531be8a2aSStefan Hajnoczi void *new_window; 256631be8a2aSStefan Hajnoczi size_t new_length; 256731be8a2aSStefan Hajnoczi size_t vec_end; 256831be8a2aSStefan Hajnoczi size_t i; 256931be8a2aSStefan Hajnoczi int ret; 257031be8a2aSStefan Hajnoczi 257131be8a2aSStefan Hajnoczi /* Unmap previous window if size has changed */ 257231be8a2aSStefan Hajnoczi new_length = MIN(end - offset, window_size); 257331be8a2aSStefan Hajnoczi if (new_length != length) { 257431be8a2aSStefan Hajnoczi munmap(window, length); 257531be8a2aSStefan Hajnoczi window = NULL; 257631be8a2aSStefan Hajnoczi length = 0; 257731be8a2aSStefan Hajnoczi } 257831be8a2aSStefan Hajnoczi 257931be8a2aSStefan Hajnoczi new_window = mmap(window, new_length, PROT_NONE, MAP_PRIVATE, 258031be8a2aSStefan Hajnoczi s->fd, offset); 258131be8a2aSStefan Hajnoczi if (new_window == MAP_FAILED) { 258231be8a2aSStefan Hajnoczi error_setg_errno(errp, errno, "mmap failed"); 258331be8a2aSStefan Hajnoczi break; 258431be8a2aSStefan Hajnoczi } 258531be8a2aSStefan Hajnoczi 258631be8a2aSStefan Hajnoczi window = new_window; 258731be8a2aSStefan Hajnoczi length = new_length; 258831be8a2aSStefan Hajnoczi 258931be8a2aSStefan Hajnoczi ret = mincore(window, length, vec); 259031be8a2aSStefan Hajnoczi if (ret < 0) { 259131be8a2aSStefan Hajnoczi error_setg_errno(errp, errno, "mincore failed"); 259231be8a2aSStefan Hajnoczi break; 259331be8a2aSStefan Hajnoczi } 259431be8a2aSStefan Hajnoczi 259531be8a2aSStefan Hajnoczi vec_end = DIV_ROUND_UP(length, page_size); 259631be8a2aSStefan Hajnoczi for (i = 0; i < vec_end; i++) { 259731be8a2aSStefan Hajnoczi if (vec[i] & 0x1) { 259831be8a2aSStefan Hajnoczi error_setg(errp, "page cache still in use!"); 259931be8a2aSStefan Hajnoczi break; 260031be8a2aSStefan Hajnoczi } 260131be8a2aSStefan Hajnoczi } 260231be8a2aSStefan Hajnoczi } 260331be8a2aSStefan Hajnoczi 260431be8a2aSStefan Hajnoczi if (window) { 260531be8a2aSStefan Hajnoczi munmap(window, length); 260631be8a2aSStefan Hajnoczi } 260731be8a2aSStefan Hajnoczi 260831be8a2aSStefan Hajnoczi g_free(vec); 260931be8a2aSStefan Hajnoczi } 261031be8a2aSStefan Hajnoczi #endif /* __linux__ */ 261131be8a2aSStefan Hajnoczi 2612dd577a26SStefan Hajnoczi static void coroutine_fn raw_co_invalidate_cache(BlockDriverState *bs, 2613dd577a26SStefan Hajnoczi Error **errp) 2614dd577a26SStefan Hajnoczi { 2615dd577a26SStefan Hajnoczi BDRVRawState *s = bs->opaque; 2616dd577a26SStefan Hajnoczi int ret; 2617dd577a26SStefan Hajnoczi 2618dd577a26SStefan Hajnoczi ret = fd_open(bs); 2619dd577a26SStefan Hajnoczi if (ret < 0) { 2620dd577a26SStefan Hajnoczi error_setg_errno(errp, -ret, "The file descriptor is not open"); 2621dd577a26SStefan Hajnoczi return; 2622dd577a26SStefan Hajnoczi } 2623dd577a26SStefan Hajnoczi 2624f357fcd8SStefan Hajnoczi if (!s->drop_cache) { 2625f357fcd8SStefan Hajnoczi return; 2626f357fcd8SStefan Hajnoczi } 2627f357fcd8SStefan Hajnoczi 2628dd577a26SStefan Hajnoczi if (s->open_flags & O_DIRECT) { 2629dd577a26SStefan Hajnoczi return; /* No host kernel page cache */ 2630dd577a26SStefan Hajnoczi } 2631dd577a26SStefan Hajnoczi 2632dd577a26SStefan Hajnoczi #if defined(__linux__) 2633dd577a26SStefan Hajnoczi /* This sets the scene for the next syscall... */ 2634dd577a26SStefan Hajnoczi ret = bdrv_co_flush(bs); 2635dd577a26SStefan Hajnoczi if (ret < 0) { 2636dd577a26SStefan Hajnoczi error_setg_errno(errp, -ret, "flush failed"); 2637dd577a26SStefan Hajnoczi return; 2638dd577a26SStefan Hajnoczi } 2639dd577a26SStefan Hajnoczi 2640dd577a26SStefan Hajnoczi /* Linux does not invalidate pages that are dirty, locked, or mmapped by a 2641dd577a26SStefan Hajnoczi * process. These limitations are okay because we just fsynced the file, 2642dd577a26SStefan Hajnoczi * we don't use mmap, and the file should not be in use by other processes. 2643dd577a26SStefan Hajnoczi */ 2644dd577a26SStefan Hajnoczi ret = posix_fadvise(s->fd, 0, 0, POSIX_FADV_DONTNEED); 2645dd577a26SStefan Hajnoczi if (ret != 0) { /* the return value is a positive errno */ 2646dd577a26SStefan Hajnoczi error_setg_errno(errp, ret, "fadvise failed"); 2647dd577a26SStefan Hajnoczi return; 2648dd577a26SStefan Hajnoczi } 264931be8a2aSStefan Hajnoczi 265031be8a2aSStefan Hajnoczi if (s->check_cache_dropped) { 265131be8a2aSStefan Hajnoczi check_cache_dropped(bs, errp); 265231be8a2aSStefan Hajnoczi } 2653dd577a26SStefan Hajnoczi #else /* __linux__ */ 2654dd577a26SStefan Hajnoczi /* Do nothing. Live migration to a remote host with cache.direct=off is 2655dd577a26SStefan Hajnoczi * unsupported on other host operating systems. Cache consistency issues 2656dd577a26SStefan Hajnoczi * may occur but no error is reported here, partly because that's the 2657dd577a26SStefan Hajnoczi * historical behavior and partly because it's hard to differentiate valid 2658dd577a26SStefan Hajnoczi * configurations that should not cause errors. 2659dd577a26SStefan Hajnoczi */ 2660dd577a26SStefan Hajnoczi #endif /* !__linux__ */ 2661dd577a26SStefan Hajnoczi } 2662dd577a26SStefan Hajnoczi 266333d70fb6SKevin Wolf static coroutine_fn int 266446ee0f46SKevin Wolf raw_do_pdiscard(BlockDriverState *bs, int64_t offset, int bytes, bool blkdev) 2665c1bb86cdSEric Blake { 2666c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 266746ee0f46SKevin Wolf RawPosixAIOData acb; 2668c1bb86cdSEric Blake 266946ee0f46SKevin Wolf acb = (RawPosixAIOData) { 267046ee0f46SKevin Wolf .bs = bs, 267146ee0f46SKevin Wolf .aio_fildes = s->fd, 267246ee0f46SKevin Wolf .aio_type = QEMU_AIO_DISCARD, 267346ee0f46SKevin Wolf .aio_offset = offset, 267446ee0f46SKevin Wolf .aio_nbytes = bytes, 267546ee0f46SKevin Wolf }; 267646ee0f46SKevin Wolf 267746ee0f46SKevin Wolf if (blkdev) { 267846ee0f46SKevin Wolf acb.aio_type |= QEMU_AIO_BLKDEV; 267946ee0f46SKevin Wolf } 268046ee0f46SKevin Wolf 268146ee0f46SKevin Wolf return raw_thread_pool_submit(bs, handle_aiocb_discard, &acb); 268246ee0f46SKevin Wolf } 268346ee0f46SKevin Wolf 268446ee0f46SKevin Wolf static coroutine_fn int 268546ee0f46SKevin Wolf raw_co_pdiscard(BlockDriverState *bs, int64_t offset, int bytes) 268646ee0f46SKevin Wolf { 268746ee0f46SKevin Wolf return raw_do_pdiscard(bs, offset, bytes, false); 2688c1bb86cdSEric Blake } 2689c1bb86cdSEric Blake 26907154d8aeSKevin Wolf static int coroutine_fn 26917154d8aeSKevin Wolf raw_do_pwrite_zeroes(BlockDriverState *bs, int64_t offset, int bytes, 26927154d8aeSKevin Wolf BdrvRequestFlags flags, bool blkdev) 26937154d8aeSKevin Wolf { 26947154d8aeSKevin Wolf BDRVRawState *s = bs->opaque; 26957154d8aeSKevin Wolf RawPosixAIOData acb; 26967154d8aeSKevin Wolf ThreadPoolFunc *handler; 26977154d8aeSKevin Wolf 26987154d8aeSKevin Wolf acb = (RawPosixAIOData) { 26997154d8aeSKevin Wolf .bs = bs, 27007154d8aeSKevin Wolf .aio_fildes = s->fd, 27017154d8aeSKevin Wolf .aio_type = QEMU_AIO_WRITE_ZEROES, 27027154d8aeSKevin Wolf .aio_offset = offset, 27037154d8aeSKevin Wolf .aio_nbytes = bytes, 27047154d8aeSKevin Wolf }; 27057154d8aeSKevin Wolf 27067154d8aeSKevin Wolf if (blkdev) { 27077154d8aeSKevin Wolf acb.aio_type |= QEMU_AIO_BLKDEV; 27087154d8aeSKevin Wolf } 2709738301e1SKevin Wolf if (flags & BDRV_REQ_NO_FALLBACK) { 2710738301e1SKevin Wolf acb.aio_type |= QEMU_AIO_NO_FALLBACK; 2711738301e1SKevin Wolf } 27127154d8aeSKevin Wolf 27137154d8aeSKevin Wolf if (flags & BDRV_REQ_MAY_UNMAP) { 27147154d8aeSKevin Wolf acb.aio_type |= QEMU_AIO_DISCARD; 27157154d8aeSKevin Wolf handler = handle_aiocb_write_zeroes_unmap; 27167154d8aeSKevin Wolf } else { 27177154d8aeSKevin Wolf handler = handle_aiocb_write_zeroes; 27187154d8aeSKevin Wolf } 27197154d8aeSKevin Wolf 27207154d8aeSKevin Wolf return raw_thread_pool_submit(bs, handler, &acb); 27217154d8aeSKevin Wolf } 27227154d8aeSKevin Wolf 2723c1bb86cdSEric Blake static int coroutine_fn raw_co_pwrite_zeroes( 2724c1bb86cdSEric Blake BlockDriverState *bs, int64_t offset, 2725f5a5ca79SManos Pitsidianakis int bytes, BdrvRequestFlags flags) 2726c1bb86cdSEric Blake { 27277154d8aeSKevin Wolf return raw_do_pwrite_zeroes(bs, offset, bytes, flags, false); 2728c1bb86cdSEric Blake } 2729c1bb86cdSEric Blake 2730c1bb86cdSEric Blake static int raw_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 2731c1bb86cdSEric Blake { 2732c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2733c1bb86cdSEric Blake 2734c1bb86cdSEric Blake bdi->unallocated_blocks_are_zero = s->discard_zeroes; 2735c1bb86cdSEric Blake return 0; 2736c1bb86cdSEric Blake } 2737c1bb86cdSEric Blake 2738c1bb86cdSEric Blake static QemuOptsList raw_create_opts = { 2739c1bb86cdSEric Blake .name = "raw-create-opts", 2740c1bb86cdSEric Blake .head = QTAILQ_HEAD_INITIALIZER(raw_create_opts.head), 2741c1bb86cdSEric Blake .desc = { 2742c1bb86cdSEric Blake { 2743c1bb86cdSEric Blake .name = BLOCK_OPT_SIZE, 2744c1bb86cdSEric Blake .type = QEMU_OPT_SIZE, 2745c1bb86cdSEric Blake .help = "Virtual disk size" 2746c1bb86cdSEric Blake }, 2747c1bb86cdSEric Blake { 2748c1bb86cdSEric Blake .name = BLOCK_OPT_NOCOW, 2749c1bb86cdSEric Blake .type = QEMU_OPT_BOOL, 2750c1bb86cdSEric Blake .help = "Turn off copy-on-write (valid only on btrfs)" 2751c1bb86cdSEric Blake }, 2752c1bb86cdSEric Blake { 2753c1bb86cdSEric Blake .name = BLOCK_OPT_PREALLOC, 2754c1bb86cdSEric Blake .type = QEMU_OPT_STRING, 2755abea0053SStefano Garzarella .help = "Preallocation mode (allowed values: off" 2756abea0053SStefano Garzarella #ifdef CONFIG_POSIX_FALLOCATE 2757abea0053SStefano Garzarella ", falloc" 2758abea0053SStefano Garzarella #endif 2759abea0053SStefano Garzarella ", full)" 2760c1bb86cdSEric Blake }, 2761c1bb86cdSEric Blake { /* end of list */ } 2762c1bb86cdSEric Blake } 2763c1bb86cdSEric Blake }; 2764c1bb86cdSEric Blake 2765244a5668SFam Zheng static int raw_check_perm(BlockDriverState *bs, uint64_t perm, uint64_t shared, 2766244a5668SFam Zheng Error **errp) 2767244a5668SFam Zheng { 27686ceabe6fSKevin Wolf BDRVRawState *s = bs->opaque; 27696ceabe6fSKevin Wolf BDRVRawReopenState *rs = NULL; 27706ceabe6fSKevin Wolf int open_flags; 27716ceabe6fSKevin Wolf int ret; 27726ceabe6fSKevin Wolf 27736ceabe6fSKevin Wolf if (s->perm_change_fd) { 27746ceabe6fSKevin Wolf /* 27756ceabe6fSKevin Wolf * In the context of reopen, this function may be called several times 27766ceabe6fSKevin Wolf * (directly and recursively while change permissions of the parent). 27776ceabe6fSKevin Wolf * This is even true for children that don't inherit from the original 27786ceabe6fSKevin Wolf * reopen node, so s->reopen_state is not set. 27796ceabe6fSKevin Wolf * 27806ceabe6fSKevin Wolf * Ignore all but the first call. 27816ceabe6fSKevin Wolf */ 27826ceabe6fSKevin Wolf return 0; 27836ceabe6fSKevin Wolf } 27846ceabe6fSKevin Wolf 27856ceabe6fSKevin Wolf if (s->reopen_state) { 27866ceabe6fSKevin Wolf /* We already have a new file descriptor to set permissions for */ 27876ceabe6fSKevin Wolf assert(s->reopen_state->perm == perm); 27886ceabe6fSKevin Wolf assert(s->reopen_state->shared_perm == shared); 27896ceabe6fSKevin Wolf rs = s->reopen_state->opaque; 27906ceabe6fSKevin Wolf s->perm_change_fd = rs->fd; 2791094e3639SMax Reitz s->perm_change_flags = rs->open_flags; 27926ceabe6fSKevin Wolf } else { 27936ceabe6fSKevin Wolf /* We may need a new fd if auto-read-only switches the mode */ 279423dece19SKevin Wolf ret = raw_reconfigure_getfd(bs, bs->open_flags, &open_flags, perm, 27956ceabe6fSKevin Wolf false, errp); 27966ceabe6fSKevin Wolf if (ret < 0) { 27976ceabe6fSKevin Wolf return ret; 27986ceabe6fSKevin Wolf } else if (ret != s->fd) { 27996ceabe6fSKevin Wolf s->perm_change_fd = ret; 2800094e3639SMax Reitz s->perm_change_flags = open_flags; 28016ceabe6fSKevin Wolf } 28026ceabe6fSKevin Wolf } 28036ceabe6fSKevin Wolf 28046ceabe6fSKevin Wolf /* Prepare permissions on old fd to avoid conflicts between old and new, 28056ceabe6fSKevin Wolf * but keep everything locked that new will need. */ 28066ceabe6fSKevin Wolf ret = raw_handle_perm_lock(bs, RAW_PL_PREPARE, perm, shared, errp); 28076ceabe6fSKevin Wolf if (ret < 0) { 28086ceabe6fSKevin Wolf goto fail; 28096ceabe6fSKevin Wolf } 28106ceabe6fSKevin Wolf 28116ceabe6fSKevin Wolf /* Copy locks to the new fd */ 28126ceabe6fSKevin Wolf if (s->perm_change_fd) { 28136ceabe6fSKevin Wolf ret = raw_apply_lock_bytes(NULL, s->perm_change_fd, perm, ~shared, 28146ceabe6fSKevin Wolf false, errp); 28156ceabe6fSKevin Wolf if (ret < 0) { 28166ceabe6fSKevin Wolf raw_handle_perm_lock(bs, RAW_PL_ABORT, 0, 0, NULL); 28176ceabe6fSKevin Wolf goto fail; 28186ceabe6fSKevin Wolf } 28196ceabe6fSKevin Wolf } 28206ceabe6fSKevin Wolf return 0; 28216ceabe6fSKevin Wolf 28226ceabe6fSKevin Wolf fail: 28236ceabe6fSKevin Wolf if (s->perm_change_fd && !s->reopen_state) { 28246ceabe6fSKevin Wolf qemu_close(s->perm_change_fd); 28256ceabe6fSKevin Wolf } 28266ceabe6fSKevin Wolf s->perm_change_fd = 0; 28276ceabe6fSKevin Wolf return ret; 2828244a5668SFam Zheng } 2829244a5668SFam Zheng 2830244a5668SFam Zheng static void raw_set_perm(BlockDriverState *bs, uint64_t perm, uint64_t shared) 2831244a5668SFam Zheng { 2832244a5668SFam Zheng BDRVRawState *s = bs->opaque; 28336ceabe6fSKevin Wolf 28346ceabe6fSKevin Wolf /* For reopen, we have already switched to the new fd (.bdrv_set_perm is 28356ceabe6fSKevin Wolf * called after .bdrv_reopen_commit) */ 28366ceabe6fSKevin Wolf if (s->perm_change_fd && s->fd != s->perm_change_fd) { 28376ceabe6fSKevin Wolf qemu_close(s->fd); 28386ceabe6fSKevin Wolf s->fd = s->perm_change_fd; 2839094e3639SMax Reitz s->open_flags = s->perm_change_flags; 28406ceabe6fSKevin Wolf } 28416ceabe6fSKevin Wolf s->perm_change_fd = 0; 28426ceabe6fSKevin Wolf 2843244a5668SFam Zheng raw_handle_perm_lock(bs, RAW_PL_COMMIT, perm, shared, NULL); 2844244a5668SFam Zheng s->perm = perm; 2845244a5668SFam Zheng s->shared_perm = shared; 2846244a5668SFam Zheng } 2847244a5668SFam Zheng 2848244a5668SFam Zheng static void raw_abort_perm_update(BlockDriverState *bs) 2849244a5668SFam Zheng { 28506ceabe6fSKevin Wolf BDRVRawState *s = bs->opaque; 28516ceabe6fSKevin Wolf 28526ceabe6fSKevin Wolf /* For reopen, .bdrv_reopen_abort is called afterwards and will close 28536ceabe6fSKevin Wolf * the file descriptor. */ 28546ceabe6fSKevin Wolf if (s->perm_change_fd && !s->reopen_state) { 28556ceabe6fSKevin Wolf qemu_close(s->perm_change_fd); 28566ceabe6fSKevin Wolf } 28576ceabe6fSKevin Wolf s->perm_change_fd = 0; 28586ceabe6fSKevin Wolf 2859244a5668SFam Zheng raw_handle_perm_lock(bs, RAW_PL_ABORT, 0, 0, NULL); 2860244a5668SFam Zheng } 2861244a5668SFam Zheng 286267b51fb9SVladimir Sementsov-Ogievskiy static int coroutine_fn raw_co_copy_range_from( 286367b51fb9SVladimir Sementsov-Ogievskiy BlockDriverState *bs, BdrvChild *src, uint64_t src_offset, 286467b51fb9SVladimir Sementsov-Ogievskiy BdrvChild *dst, uint64_t dst_offset, uint64_t bytes, 286567b51fb9SVladimir Sementsov-Ogievskiy BdrvRequestFlags read_flags, BdrvRequestFlags write_flags) 28661efad060SFam Zheng { 286767b51fb9SVladimir Sementsov-Ogievskiy return bdrv_co_copy_range_to(src, src_offset, dst, dst_offset, bytes, 286867b51fb9SVladimir Sementsov-Ogievskiy read_flags, write_flags); 28691efad060SFam Zheng } 28701efad060SFam Zheng 28711efad060SFam Zheng static int coroutine_fn raw_co_copy_range_to(BlockDriverState *bs, 287267b51fb9SVladimir Sementsov-Ogievskiy BdrvChild *src, 287367b51fb9SVladimir Sementsov-Ogievskiy uint64_t src_offset, 287467b51fb9SVladimir Sementsov-Ogievskiy BdrvChild *dst, 287567b51fb9SVladimir Sementsov-Ogievskiy uint64_t dst_offset, 287667b51fb9SVladimir Sementsov-Ogievskiy uint64_t bytes, 287767b51fb9SVladimir Sementsov-Ogievskiy BdrvRequestFlags read_flags, 287867b51fb9SVladimir Sementsov-Ogievskiy BdrvRequestFlags write_flags) 28791efad060SFam Zheng { 288058a209c4SKevin Wolf RawPosixAIOData acb; 28811efad060SFam Zheng BDRVRawState *s = bs->opaque; 28821efad060SFam Zheng BDRVRawState *src_s; 28831efad060SFam Zheng 28841efad060SFam Zheng assert(dst->bs == bs); 28851efad060SFam Zheng if (src->bs->drv->bdrv_co_copy_range_to != raw_co_copy_range_to) { 28861efad060SFam Zheng return -ENOTSUP; 28871efad060SFam Zheng } 28881efad060SFam Zheng 28891efad060SFam Zheng src_s = src->bs->opaque; 28909f850f67SFam Zheng if (fd_open(src->bs) < 0 || fd_open(dst->bs) < 0) { 28911efad060SFam Zheng return -EIO; 28921efad060SFam Zheng } 289358a209c4SKevin Wolf 289458a209c4SKevin Wolf acb = (RawPosixAIOData) { 289558a209c4SKevin Wolf .bs = bs, 289658a209c4SKevin Wolf .aio_type = QEMU_AIO_COPY_RANGE, 289758a209c4SKevin Wolf .aio_fildes = src_s->fd, 289858a209c4SKevin Wolf .aio_offset = src_offset, 289958a209c4SKevin Wolf .aio_nbytes = bytes, 290058a209c4SKevin Wolf .copy_range = { 290158a209c4SKevin Wolf .aio_fd2 = s->fd, 290258a209c4SKevin Wolf .aio_offset2 = dst_offset, 290358a209c4SKevin Wolf }, 290458a209c4SKevin Wolf }; 290558a209c4SKevin Wolf 290658a209c4SKevin Wolf return raw_thread_pool_submit(bs, handle_aiocb_copy_range, &acb); 29071efad060SFam Zheng } 29081efad060SFam Zheng 2909c1bb86cdSEric Blake BlockDriver bdrv_file = { 2910c1bb86cdSEric Blake .format_name = "file", 2911c1bb86cdSEric Blake .protocol_name = "file", 2912c1bb86cdSEric Blake .instance_size = sizeof(BDRVRawState), 2913c1bb86cdSEric Blake .bdrv_needs_filename = true, 2914c1bb86cdSEric Blake .bdrv_probe = NULL, /* no probe for protocols */ 2915c1bb86cdSEric Blake .bdrv_parse_filename = raw_parse_filename, 2916c1bb86cdSEric Blake .bdrv_file_open = raw_open, 2917c1bb86cdSEric Blake .bdrv_reopen_prepare = raw_reopen_prepare, 2918c1bb86cdSEric Blake .bdrv_reopen_commit = raw_reopen_commit, 2919c1bb86cdSEric Blake .bdrv_reopen_abort = raw_reopen_abort, 2920c1bb86cdSEric Blake .bdrv_close = raw_close, 2921927f11e1SKevin Wolf .bdrv_co_create = raw_co_create, 2922efc75e2aSStefan Hajnoczi .bdrv_co_create_opts = raw_co_create_opts, 2923c1bb86cdSEric Blake .bdrv_has_zero_init = bdrv_has_zero_init_1, 29241dcaf527SMax Reitz .bdrv_has_zero_init_truncate = bdrv_has_zero_init_1, 2925a290f085SEric Blake .bdrv_co_block_status = raw_co_block_status, 2926dd577a26SStefan Hajnoczi .bdrv_co_invalidate_cache = raw_co_invalidate_cache, 2927c1bb86cdSEric Blake .bdrv_co_pwrite_zeroes = raw_co_pwrite_zeroes, 2928c1bb86cdSEric Blake 2929c1bb86cdSEric Blake .bdrv_co_preadv = raw_co_preadv, 2930c1bb86cdSEric Blake .bdrv_co_pwritev = raw_co_pwritev, 293133d70fb6SKevin Wolf .bdrv_co_flush_to_disk = raw_co_flush_to_disk, 293233d70fb6SKevin Wolf .bdrv_co_pdiscard = raw_co_pdiscard, 29331efad060SFam Zheng .bdrv_co_copy_range_from = raw_co_copy_range_from, 29341efad060SFam Zheng .bdrv_co_copy_range_to = raw_co_copy_range_to, 2935c1bb86cdSEric Blake .bdrv_refresh_limits = raw_refresh_limits, 2936c1bb86cdSEric Blake .bdrv_io_plug = raw_aio_plug, 2937c1bb86cdSEric Blake .bdrv_io_unplug = raw_aio_unplug, 2938ed6e2161SNishanth Aravamudan .bdrv_attach_aio_context = raw_aio_attach_aio_context, 2939c1bb86cdSEric Blake 2940061ca8a3SKevin Wolf .bdrv_co_truncate = raw_co_truncate, 2941c1bb86cdSEric Blake .bdrv_getlength = raw_getlength, 2942c1bb86cdSEric Blake .bdrv_get_info = raw_get_info, 2943c1bb86cdSEric Blake .bdrv_get_allocated_file_size 2944c1bb86cdSEric Blake = raw_get_allocated_file_size, 2945244a5668SFam Zheng .bdrv_check_perm = raw_check_perm, 2946244a5668SFam Zheng .bdrv_set_perm = raw_set_perm, 2947244a5668SFam Zheng .bdrv_abort_perm_update = raw_abort_perm_update, 2948c1bb86cdSEric Blake .create_opts = &raw_create_opts, 29498a2ce0bcSAlberto Garcia .mutable_opts = mutable_opts, 2950c1bb86cdSEric Blake }; 2951c1bb86cdSEric Blake 2952c1bb86cdSEric Blake /***********************************************/ 2953c1bb86cdSEric Blake /* host device */ 2954c1bb86cdSEric Blake 2955c1bb86cdSEric Blake #if defined(__APPLE__) && defined(__MACH__) 2956c1bb86cdSEric Blake static kern_return_t GetBSDPath(io_iterator_t mediaIterator, char *bsdPath, 2957c1bb86cdSEric Blake CFIndex maxPathSize, int flags); 2958c1bb86cdSEric Blake static char *FindEjectableOpticalMedia(io_iterator_t *mediaIterator) 2959c1bb86cdSEric Blake { 2960c1bb86cdSEric Blake kern_return_t kernResult = KERN_FAILURE; 2961c1bb86cdSEric Blake mach_port_t masterPort; 2962c1bb86cdSEric Blake CFMutableDictionaryRef classesToMatch; 2963c1bb86cdSEric Blake const char *matching_array[] = {kIODVDMediaClass, kIOCDMediaClass}; 2964c1bb86cdSEric Blake char *mediaType = NULL; 2965c1bb86cdSEric Blake 2966c1bb86cdSEric Blake kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort ); 2967c1bb86cdSEric Blake if ( KERN_SUCCESS != kernResult ) { 2968c1bb86cdSEric Blake printf( "IOMasterPort returned %d\n", kernResult ); 2969c1bb86cdSEric Blake } 2970c1bb86cdSEric Blake 2971c1bb86cdSEric Blake int index; 2972c1bb86cdSEric Blake for (index = 0; index < ARRAY_SIZE(matching_array); index++) { 2973c1bb86cdSEric Blake classesToMatch = IOServiceMatching(matching_array[index]); 2974c1bb86cdSEric Blake if (classesToMatch == NULL) { 2975c1bb86cdSEric Blake error_report("IOServiceMatching returned NULL for %s", 2976c1bb86cdSEric Blake matching_array[index]); 2977c1bb86cdSEric Blake continue; 2978c1bb86cdSEric Blake } 2979c1bb86cdSEric Blake CFDictionarySetValue(classesToMatch, CFSTR(kIOMediaEjectableKey), 2980c1bb86cdSEric Blake kCFBooleanTrue); 2981c1bb86cdSEric Blake kernResult = IOServiceGetMatchingServices(masterPort, classesToMatch, 2982c1bb86cdSEric Blake mediaIterator); 2983c1bb86cdSEric Blake if (kernResult != KERN_SUCCESS) { 2984c1bb86cdSEric Blake error_report("Note: IOServiceGetMatchingServices returned %d", 2985c1bb86cdSEric Blake kernResult); 2986c1bb86cdSEric Blake continue; 2987c1bb86cdSEric Blake } 2988c1bb86cdSEric Blake 2989c1bb86cdSEric Blake /* If a match was found, leave the loop */ 2990c1bb86cdSEric Blake if (*mediaIterator != 0) { 29914f7d28d7SLaurent Vivier trace_file_FindEjectableOpticalMedia(matching_array[index]); 2992c1bb86cdSEric Blake mediaType = g_strdup(matching_array[index]); 2993c1bb86cdSEric Blake break; 2994c1bb86cdSEric Blake } 2995c1bb86cdSEric Blake } 2996c1bb86cdSEric Blake return mediaType; 2997c1bb86cdSEric Blake } 2998c1bb86cdSEric Blake 2999c1bb86cdSEric Blake kern_return_t GetBSDPath(io_iterator_t mediaIterator, char *bsdPath, 3000c1bb86cdSEric Blake CFIndex maxPathSize, int flags) 3001c1bb86cdSEric Blake { 3002c1bb86cdSEric Blake io_object_t nextMedia; 3003c1bb86cdSEric Blake kern_return_t kernResult = KERN_FAILURE; 3004c1bb86cdSEric Blake *bsdPath = '\0'; 3005c1bb86cdSEric Blake nextMedia = IOIteratorNext( mediaIterator ); 3006c1bb86cdSEric Blake if ( nextMedia ) 3007c1bb86cdSEric Blake { 3008c1bb86cdSEric Blake CFTypeRef bsdPathAsCFString; 3009c1bb86cdSEric Blake bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 ); 3010c1bb86cdSEric Blake if ( bsdPathAsCFString ) { 3011c1bb86cdSEric Blake size_t devPathLength; 3012c1bb86cdSEric Blake strcpy( bsdPath, _PATH_DEV ); 3013c1bb86cdSEric Blake if (flags & BDRV_O_NOCACHE) { 3014c1bb86cdSEric Blake strcat(bsdPath, "r"); 3015c1bb86cdSEric Blake } 3016c1bb86cdSEric Blake devPathLength = strlen( bsdPath ); 3017c1bb86cdSEric Blake if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) { 3018c1bb86cdSEric Blake kernResult = KERN_SUCCESS; 3019c1bb86cdSEric Blake } 3020c1bb86cdSEric Blake CFRelease( bsdPathAsCFString ); 3021c1bb86cdSEric Blake } 3022c1bb86cdSEric Blake IOObjectRelease( nextMedia ); 3023c1bb86cdSEric Blake } 3024c1bb86cdSEric Blake 3025c1bb86cdSEric Blake return kernResult; 3026c1bb86cdSEric Blake } 3027c1bb86cdSEric Blake 3028c1bb86cdSEric Blake /* Sets up a real cdrom for use in QEMU */ 3029c1bb86cdSEric Blake static bool setup_cdrom(char *bsd_path, Error **errp) 3030c1bb86cdSEric Blake { 3031c1bb86cdSEric Blake int index, num_of_test_partitions = 2, fd; 3032c1bb86cdSEric Blake char test_partition[MAXPATHLEN]; 3033c1bb86cdSEric Blake bool partition_found = false; 3034c1bb86cdSEric Blake 3035c1bb86cdSEric Blake /* look for a working partition */ 3036c1bb86cdSEric Blake for (index = 0; index < num_of_test_partitions; index++) { 3037c1bb86cdSEric Blake snprintf(test_partition, sizeof(test_partition), "%ss%d", bsd_path, 3038c1bb86cdSEric Blake index); 3039c1bb86cdSEric Blake fd = qemu_open(test_partition, O_RDONLY | O_BINARY | O_LARGEFILE); 3040c1bb86cdSEric Blake if (fd >= 0) { 3041c1bb86cdSEric Blake partition_found = true; 3042c1bb86cdSEric Blake qemu_close(fd); 3043c1bb86cdSEric Blake break; 3044c1bb86cdSEric Blake } 3045c1bb86cdSEric Blake } 3046c1bb86cdSEric Blake 3047c1bb86cdSEric Blake /* if a working partition on the device was not found */ 3048c1bb86cdSEric Blake if (partition_found == false) { 3049c1bb86cdSEric Blake error_setg(errp, "Failed to find a working partition on disc"); 3050c1bb86cdSEric Blake } else { 30514f7d28d7SLaurent Vivier trace_file_setup_cdrom(test_partition); 3052c1bb86cdSEric Blake pstrcpy(bsd_path, MAXPATHLEN, test_partition); 3053c1bb86cdSEric Blake } 3054c1bb86cdSEric Blake return partition_found; 3055c1bb86cdSEric Blake } 3056c1bb86cdSEric Blake 3057c1bb86cdSEric Blake /* Prints directions on mounting and unmounting a device */ 3058c1bb86cdSEric Blake static void print_unmounting_directions(const char *file_name) 3059c1bb86cdSEric Blake { 3060c1bb86cdSEric Blake error_report("If device %s is mounted on the desktop, unmount" 3061c1bb86cdSEric Blake " it first before using it in QEMU", file_name); 3062c1bb86cdSEric Blake error_report("Command to unmount device: diskutil unmountDisk %s", 3063c1bb86cdSEric Blake file_name); 3064c1bb86cdSEric Blake error_report("Command to mount device: diskutil mountDisk %s", file_name); 3065c1bb86cdSEric Blake } 3066c1bb86cdSEric Blake 3067c1bb86cdSEric Blake #endif /* defined(__APPLE__) && defined(__MACH__) */ 3068c1bb86cdSEric Blake 3069c1bb86cdSEric Blake static int hdev_probe_device(const char *filename) 3070c1bb86cdSEric Blake { 3071c1bb86cdSEric Blake struct stat st; 3072c1bb86cdSEric Blake 3073c1bb86cdSEric Blake /* allow a dedicated CD-ROM driver to match with a higher priority */ 3074c1bb86cdSEric Blake if (strstart(filename, "/dev/cdrom", NULL)) 3075c1bb86cdSEric Blake return 50; 3076c1bb86cdSEric Blake 3077c1bb86cdSEric Blake if (stat(filename, &st) >= 0 && 3078c1bb86cdSEric Blake (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) { 3079c1bb86cdSEric Blake return 100; 3080c1bb86cdSEric Blake } 3081c1bb86cdSEric Blake 3082c1bb86cdSEric Blake return 0; 3083c1bb86cdSEric Blake } 3084c1bb86cdSEric Blake 3085c1bb86cdSEric Blake static int check_hdev_writable(BDRVRawState *s) 3086c1bb86cdSEric Blake { 3087c1bb86cdSEric Blake #if defined(BLKROGET) 3088c1bb86cdSEric Blake /* Linux block devices can be configured "read-only" using blockdev(8). 3089c1bb86cdSEric Blake * This is independent of device node permissions and therefore open(2) 3090c1bb86cdSEric Blake * with O_RDWR succeeds. Actual writes fail with EPERM. 3091c1bb86cdSEric Blake * 3092c1bb86cdSEric Blake * bdrv_open() is supposed to fail if the disk is read-only. Explicitly 3093c1bb86cdSEric Blake * check for read-only block devices so that Linux block devices behave 3094c1bb86cdSEric Blake * properly. 3095c1bb86cdSEric Blake */ 3096c1bb86cdSEric Blake struct stat st; 3097c1bb86cdSEric Blake int readonly = 0; 3098c1bb86cdSEric Blake 3099c1bb86cdSEric Blake if (fstat(s->fd, &st)) { 3100c1bb86cdSEric Blake return -errno; 3101c1bb86cdSEric Blake } 3102c1bb86cdSEric Blake 3103c1bb86cdSEric Blake if (!S_ISBLK(st.st_mode)) { 3104c1bb86cdSEric Blake return 0; 3105c1bb86cdSEric Blake } 3106c1bb86cdSEric Blake 3107c1bb86cdSEric Blake if (ioctl(s->fd, BLKROGET, &readonly) < 0) { 3108c1bb86cdSEric Blake return -errno; 3109c1bb86cdSEric Blake } 3110c1bb86cdSEric Blake 3111c1bb86cdSEric Blake if (readonly) { 3112c1bb86cdSEric Blake return -EACCES; 3113c1bb86cdSEric Blake } 3114c1bb86cdSEric Blake #endif /* defined(BLKROGET) */ 3115c1bb86cdSEric Blake return 0; 3116c1bb86cdSEric Blake } 3117c1bb86cdSEric Blake 3118c1bb86cdSEric Blake static void hdev_parse_filename(const char *filename, QDict *options, 3119c1bb86cdSEric Blake Error **errp) 3120c1bb86cdSEric Blake { 312103c320d8SMax Reitz bdrv_parse_filename_strip_prefix(filename, "host_device:", options); 3122c1bb86cdSEric Blake } 3123c1bb86cdSEric Blake 3124c1bb86cdSEric Blake static bool hdev_is_sg(BlockDriverState *bs) 3125c1bb86cdSEric Blake { 3126c1bb86cdSEric Blake 3127c1bb86cdSEric Blake #if defined(__linux__) 3128c1bb86cdSEric Blake 3129c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 3130c1bb86cdSEric Blake struct stat st; 3131c1bb86cdSEric Blake struct sg_scsi_id scsiid; 3132c1bb86cdSEric Blake int sg_version; 3133c1bb86cdSEric Blake int ret; 3134c1bb86cdSEric Blake 3135c1bb86cdSEric Blake if (stat(bs->filename, &st) < 0 || !S_ISCHR(st.st_mode)) { 3136c1bb86cdSEric Blake return false; 3137c1bb86cdSEric Blake } 3138c1bb86cdSEric Blake 3139c1bb86cdSEric Blake ret = ioctl(s->fd, SG_GET_VERSION_NUM, &sg_version); 3140c1bb86cdSEric Blake if (ret < 0) { 3141c1bb86cdSEric Blake return false; 3142c1bb86cdSEric Blake } 3143c1bb86cdSEric Blake 3144c1bb86cdSEric Blake ret = ioctl(s->fd, SG_GET_SCSI_ID, &scsiid); 3145c1bb86cdSEric Blake if (ret >= 0) { 31464f7d28d7SLaurent Vivier trace_file_hdev_is_sg(scsiid.scsi_type, sg_version); 3147c1bb86cdSEric Blake return true; 3148c1bb86cdSEric Blake } 3149c1bb86cdSEric Blake 3150c1bb86cdSEric Blake #endif 3151c1bb86cdSEric Blake 3152c1bb86cdSEric Blake return false; 3153c1bb86cdSEric Blake } 3154c1bb86cdSEric Blake 3155c1bb86cdSEric Blake static int hdev_open(BlockDriverState *bs, QDict *options, int flags, 3156c1bb86cdSEric Blake Error **errp) 3157c1bb86cdSEric Blake { 3158c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 3159c1bb86cdSEric Blake Error *local_err = NULL; 3160c1bb86cdSEric Blake int ret; 3161c1bb86cdSEric Blake 3162c1bb86cdSEric Blake #if defined(__APPLE__) && defined(__MACH__) 3163129c7d1cSMarkus Armbruster /* 3164129c7d1cSMarkus Armbruster * Caution: while qdict_get_str() is fine, getting non-string types 3165129c7d1cSMarkus Armbruster * would require more care. When @options come from -blockdev or 3166129c7d1cSMarkus Armbruster * blockdev_add, its members are typed according to the QAPI 3167129c7d1cSMarkus Armbruster * schema, but when they come from -drive, they're all QString. 3168129c7d1cSMarkus Armbruster */ 3169c1bb86cdSEric Blake const char *filename = qdict_get_str(options, "filename"); 3170c1bb86cdSEric Blake char bsd_path[MAXPATHLEN] = ""; 3171c1bb86cdSEric Blake bool error_occurred = false; 3172c1bb86cdSEric Blake 3173c1bb86cdSEric Blake /* If using a real cdrom */ 3174c1bb86cdSEric Blake if (strcmp(filename, "/dev/cdrom") == 0) { 3175c1bb86cdSEric Blake char *mediaType = NULL; 3176c1bb86cdSEric Blake kern_return_t ret_val; 3177c1bb86cdSEric Blake io_iterator_t mediaIterator = 0; 3178c1bb86cdSEric Blake 3179c1bb86cdSEric Blake mediaType = FindEjectableOpticalMedia(&mediaIterator); 3180c1bb86cdSEric Blake if (mediaType == NULL) { 3181c1bb86cdSEric Blake error_setg(errp, "Please make sure your CD/DVD is in the optical" 3182c1bb86cdSEric Blake " drive"); 3183c1bb86cdSEric Blake error_occurred = true; 3184c1bb86cdSEric Blake goto hdev_open_Mac_error; 3185c1bb86cdSEric Blake } 3186c1bb86cdSEric Blake 3187c1bb86cdSEric Blake ret_val = GetBSDPath(mediaIterator, bsd_path, sizeof(bsd_path), flags); 3188c1bb86cdSEric Blake if (ret_val != KERN_SUCCESS) { 3189c1bb86cdSEric Blake error_setg(errp, "Could not get BSD path for optical drive"); 3190c1bb86cdSEric Blake error_occurred = true; 3191c1bb86cdSEric Blake goto hdev_open_Mac_error; 3192c1bb86cdSEric Blake } 3193c1bb86cdSEric Blake 3194c1bb86cdSEric Blake /* If a real optical drive was not found */ 3195c1bb86cdSEric Blake if (bsd_path[0] == '\0') { 3196c1bb86cdSEric Blake error_setg(errp, "Failed to obtain bsd path for optical drive"); 3197c1bb86cdSEric Blake error_occurred = true; 3198c1bb86cdSEric Blake goto hdev_open_Mac_error; 3199c1bb86cdSEric Blake } 3200c1bb86cdSEric Blake 3201c1bb86cdSEric Blake /* If using a cdrom disc and finding a partition on the disc failed */ 3202c1bb86cdSEric Blake if (strncmp(mediaType, kIOCDMediaClass, 9) == 0 && 3203c1bb86cdSEric Blake setup_cdrom(bsd_path, errp) == false) { 3204c1bb86cdSEric Blake print_unmounting_directions(bsd_path); 3205c1bb86cdSEric Blake error_occurred = true; 3206c1bb86cdSEric Blake goto hdev_open_Mac_error; 3207c1bb86cdSEric Blake } 3208c1bb86cdSEric Blake 320946f5ac20SEric Blake qdict_put_str(options, "filename", bsd_path); 3210c1bb86cdSEric Blake 3211c1bb86cdSEric Blake hdev_open_Mac_error: 3212c1bb86cdSEric Blake g_free(mediaType); 3213c1bb86cdSEric Blake if (mediaIterator) { 3214c1bb86cdSEric Blake IOObjectRelease(mediaIterator); 3215c1bb86cdSEric Blake } 3216c1bb86cdSEric Blake if (error_occurred) { 3217c1bb86cdSEric Blake return -ENOENT; 3218c1bb86cdSEric Blake } 3219c1bb86cdSEric Blake } 3220c1bb86cdSEric Blake #endif /* defined(__APPLE__) && defined(__MACH__) */ 3221c1bb86cdSEric Blake 3222c1bb86cdSEric Blake s->type = FTYPE_FILE; 3223c1bb86cdSEric Blake 3224230ff739SJohn Snow ret = raw_open_common(bs, options, flags, 0, true, &local_err); 3225c1bb86cdSEric Blake if (ret < 0) { 3226c1bb86cdSEric Blake error_propagate(errp, local_err); 3227c1bb86cdSEric Blake #if defined(__APPLE__) && defined(__MACH__) 3228c1bb86cdSEric Blake if (*bsd_path) { 3229c1bb86cdSEric Blake filename = bsd_path; 3230c1bb86cdSEric Blake } 3231c1bb86cdSEric Blake /* if a physical device experienced an error while being opened */ 3232c1bb86cdSEric Blake if (strncmp(filename, "/dev/", 5) == 0) { 3233c1bb86cdSEric Blake print_unmounting_directions(filename); 3234c1bb86cdSEric Blake } 3235c1bb86cdSEric Blake #endif /* defined(__APPLE__) && defined(__MACH__) */ 3236c1bb86cdSEric Blake return ret; 3237c1bb86cdSEric Blake } 3238c1bb86cdSEric Blake 3239c1bb86cdSEric Blake /* Since this does ioctl the device must be already opened */ 3240c1bb86cdSEric Blake bs->sg = hdev_is_sg(bs); 3241c1bb86cdSEric Blake 3242c1bb86cdSEric Blake if (flags & BDRV_O_RDWR) { 3243c1bb86cdSEric Blake ret = check_hdev_writable(s); 3244c1bb86cdSEric Blake if (ret < 0) { 3245c1bb86cdSEric Blake raw_close(bs); 3246c1bb86cdSEric Blake error_setg_errno(errp, -ret, "The device is not writable"); 3247c1bb86cdSEric Blake return ret; 3248c1bb86cdSEric Blake } 3249c1bb86cdSEric Blake } 3250c1bb86cdSEric Blake 3251c1bb86cdSEric Blake return ret; 3252c1bb86cdSEric Blake } 3253c1bb86cdSEric Blake 3254c1bb86cdSEric Blake #if defined(__linux__) 32552f3a7ab3SKevin Wolf static int coroutine_fn 32562f3a7ab3SKevin Wolf hdev_co_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) 3257c1bb86cdSEric Blake { 3258c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 325903425671SKevin Wolf RawPosixAIOData acb; 32602f3a7ab3SKevin Wolf int ret; 3261c1bb86cdSEric Blake 32622f3a7ab3SKevin Wolf ret = fd_open(bs); 32632f3a7ab3SKevin Wolf if (ret < 0) { 32642f3a7ab3SKevin Wolf return ret; 32652f3a7ab3SKevin Wolf } 3266c1bb86cdSEric Blake 32677c9e5276SPaolo Bonzini if (req == SG_IO && s->pr_mgr) { 32687c9e5276SPaolo Bonzini struct sg_io_hdr *io_hdr = buf; 32697c9e5276SPaolo Bonzini if (io_hdr->cmdp[0] == PERSISTENT_RESERVE_OUT || 32707c9e5276SPaolo Bonzini io_hdr->cmdp[0] == PERSISTENT_RESERVE_IN) { 32717c9e5276SPaolo Bonzini return pr_manager_execute(s->pr_mgr, bdrv_get_aio_context(bs), 32722f3a7ab3SKevin Wolf s->fd, io_hdr); 32737c9e5276SPaolo Bonzini } 32747c9e5276SPaolo Bonzini } 32757c9e5276SPaolo Bonzini 327603425671SKevin Wolf acb = (RawPosixAIOData) { 327703425671SKevin Wolf .bs = bs, 327803425671SKevin Wolf .aio_type = QEMU_AIO_IOCTL, 327903425671SKevin Wolf .aio_fildes = s->fd, 328003425671SKevin Wolf .aio_offset = 0, 328103425671SKevin Wolf .ioctl = { 328203425671SKevin Wolf .buf = buf, 328303425671SKevin Wolf .cmd = req, 328403425671SKevin Wolf }, 328503425671SKevin Wolf }; 328603425671SKevin Wolf 328703425671SKevin Wolf return raw_thread_pool_submit(bs, handle_aiocb_ioctl, &acb); 3288c1bb86cdSEric Blake } 3289c1bb86cdSEric Blake #endif /* linux */ 3290c1bb86cdSEric Blake 3291c1bb86cdSEric Blake static int fd_open(BlockDriverState *bs) 3292c1bb86cdSEric Blake { 3293c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 3294c1bb86cdSEric Blake 3295c1bb86cdSEric Blake /* this is just to ensure s->fd is sane (its called by io ops) */ 3296c1bb86cdSEric Blake if (s->fd >= 0) 3297c1bb86cdSEric Blake return 0; 3298c1bb86cdSEric Blake return -EIO; 3299c1bb86cdSEric Blake } 3300c1bb86cdSEric Blake 330133d70fb6SKevin Wolf static coroutine_fn int 330233d70fb6SKevin Wolf hdev_co_pdiscard(BlockDriverState *bs, int64_t offset, int bytes) 3303c1bb86cdSEric Blake { 330433d70fb6SKevin Wolf int ret; 3305c1bb86cdSEric Blake 330633d70fb6SKevin Wolf ret = fd_open(bs); 330733d70fb6SKevin Wolf if (ret < 0) { 330833d70fb6SKevin Wolf return ret; 3309c1bb86cdSEric Blake } 331046ee0f46SKevin Wolf return raw_do_pdiscard(bs, offset, bytes, true); 3311c1bb86cdSEric Blake } 3312c1bb86cdSEric Blake 3313c1bb86cdSEric Blake static coroutine_fn int hdev_co_pwrite_zeroes(BlockDriverState *bs, 3314f5a5ca79SManos Pitsidianakis int64_t offset, int bytes, BdrvRequestFlags flags) 3315c1bb86cdSEric Blake { 3316c1bb86cdSEric Blake int rc; 3317c1bb86cdSEric Blake 3318c1bb86cdSEric Blake rc = fd_open(bs); 3319c1bb86cdSEric Blake if (rc < 0) { 3320c1bb86cdSEric Blake return rc; 3321c1bb86cdSEric Blake } 332234fa110eSKevin Wolf 33237154d8aeSKevin Wolf return raw_do_pwrite_zeroes(bs, offset, bytes, flags, true); 3324c1bb86cdSEric Blake } 3325c1bb86cdSEric Blake 3326efc75e2aSStefan Hajnoczi static int coroutine_fn hdev_co_create_opts(const char *filename, QemuOpts *opts, 3327c1bb86cdSEric Blake Error **errp) 3328c1bb86cdSEric Blake { 3329c1bb86cdSEric Blake int fd; 3330c1bb86cdSEric Blake int ret = 0; 3331c1bb86cdSEric Blake struct stat stat_buf; 3332c1bb86cdSEric Blake int64_t total_size = 0; 3333c1bb86cdSEric Blake bool has_prefix; 3334c1bb86cdSEric Blake 3335c1bb86cdSEric Blake /* This function is used by both protocol block drivers and therefore either 3336c1bb86cdSEric Blake * of these prefixes may be given. 3337c1bb86cdSEric Blake * The return value has to be stored somewhere, otherwise this is an error 3338c1bb86cdSEric Blake * due to -Werror=unused-value. */ 3339c1bb86cdSEric Blake has_prefix = 3340c1bb86cdSEric Blake strstart(filename, "host_device:", &filename) || 3341c1bb86cdSEric Blake strstart(filename, "host_cdrom:" , &filename); 3342c1bb86cdSEric Blake 3343c1bb86cdSEric Blake (void)has_prefix; 3344c1bb86cdSEric Blake 3345db0754dfSFam Zheng ret = raw_normalize_devicepath(&filename, errp); 3346c1bb86cdSEric Blake if (ret < 0) { 3347c1bb86cdSEric Blake return ret; 3348c1bb86cdSEric Blake } 3349c1bb86cdSEric Blake 3350c1bb86cdSEric Blake /* Read out options */ 3351c1bb86cdSEric Blake total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), 3352c1bb86cdSEric Blake BDRV_SECTOR_SIZE); 3353c1bb86cdSEric Blake 3354c1bb86cdSEric Blake fd = qemu_open(filename, O_WRONLY | O_BINARY); 3355c1bb86cdSEric Blake if (fd < 0) { 3356c1bb86cdSEric Blake ret = -errno; 3357c1bb86cdSEric Blake error_setg_errno(errp, -ret, "Could not open device"); 3358c1bb86cdSEric Blake return ret; 3359c1bb86cdSEric Blake } 3360c1bb86cdSEric Blake 3361c1bb86cdSEric Blake if (fstat(fd, &stat_buf) < 0) { 3362c1bb86cdSEric Blake ret = -errno; 3363c1bb86cdSEric Blake error_setg_errno(errp, -ret, "Could not stat device"); 3364c1bb86cdSEric Blake } else if (!S_ISBLK(stat_buf.st_mode) && !S_ISCHR(stat_buf.st_mode)) { 3365c1bb86cdSEric Blake error_setg(errp, 3366c1bb86cdSEric Blake "The given file is neither a block nor a character device"); 3367c1bb86cdSEric Blake ret = -ENODEV; 3368c1bb86cdSEric Blake } else if (lseek(fd, 0, SEEK_END) < total_size) { 3369c1bb86cdSEric Blake error_setg(errp, "Device is too small"); 3370c1bb86cdSEric Blake ret = -ENOSPC; 3371c1bb86cdSEric Blake } 3372c1bb86cdSEric Blake 337397ec9117SFam Zheng if (!ret && total_size) { 337497ec9117SFam Zheng uint8_t buf[BDRV_SECTOR_SIZE] = { 0 }; 337597ec9117SFam Zheng int64_t zero_size = MIN(BDRV_SECTOR_SIZE, total_size); 337697ec9117SFam Zheng if (lseek(fd, 0, SEEK_SET) == -1) { 337797ec9117SFam Zheng ret = -errno; 337897ec9117SFam Zheng } else { 337997ec9117SFam Zheng ret = qemu_write_full(fd, buf, zero_size); 338097ec9117SFam Zheng ret = ret == zero_size ? 0 : -errno; 338197ec9117SFam Zheng } 338297ec9117SFam Zheng } 3383c1bb86cdSEric Blake qemu_close(fd); 3384c1bb86cdSEric Blake return ret; 3385c1bb86cdSEric Blake } 3386c1bb86cdSEric Blake 3387c1bb86cdSEric Blake static BlockDriver bdrv_host_device = { 3388c1bb86cdSEric Blake .format_name = "host_device", 3389c1bb86cdSEric Blake .protocol_name = "host_device", 3390c1bb86cdSEric Blake .instance_size = sizeof(BDRVRawState), 3391c1bb86cdSEric Blake .bdrv_needs_filename = true, 3392c1bb86cdSEric Blake .bdrv_probe_device = hdev_probe_device, 3393c1bb86cdSEric Blake .bdrv_parse_filename = hdev_parse_filename, 3394c1bb86cdSEric Blake .bdrv_file_open = hdev_open, 3395c1bb86cdSEric Blake .bdrv_close = raw_close, 3396c1bb86cdSEric Blake .bdrv_reopen_prepare = raw_reopen_prepare, 3397c1bb86cdSEric Blake .bdrv_reopen_commit = raw_reopen_commit, 3398c1bb86cdSEric Blake .bdrv_reopen_abort = raw_reopen_abort, 3399efc75e2aSStefan Hajnoczi .bdrv_co_create_opts = hdev_co_create_opts, 3400c1bb86cdSEric Blake .create_opts = &raw_create_opts, 34018a2ce0bcSAlberto Garcia .mutable_opts = mutable_opts, 3402dd577a26SStefan Hajnoczi .bdrv_co_invalidate_cache = raw_co_invalidate_cache, 3403c1bb86cdSEric Blake .bdrv_co_pwrite_zeroes = hdev_co_pwrite_zeroes, 3404c1bb86cdSEric Blake 3405c1bb86cdSEric Blake .bdrv_co_preadv = raw_co_preadv, 3406c1bb86cdSEric Blake .bdrv_co_pwritev = raw_co_pwritev, 340733d70fb6SKevin Wolf .bdrv_co_flush_to_disk = raw_co_flush_to_disk, 340833d70fb6SKevin Wolf .bdrv_co_pdiscard = hdev_co_pdiscard, 34091efad060SFam Zheng .bdrv_co_copy_range_from = raw_co_copy_range_from, 34101efad060SFam Zheng .bdrv_co_copy_range_to = raw_co_copy_range_to, 3411c1bb86cdSEric Blake .bdrv_refresh_limits = raw_refresh_limits, 3412c1bb86cdSEric Blake .bdrv_io_plug = raw_aio_plug, 3413c1bb86cdSEric Blake .bdrv_io_unplug = raw_aio_unplug, 3414042b757cSNishanth Aravamudan .bdrv_attach_aio_context = raw_aio_attach_aio_context, 3415c1bb86cdSEric Blake 3416061ca8a3SKevin Wolf .bdrv_co_truncate = raw_co_truncate, 3417c1bb86cdSEric Blake .bdrv_getlength = raw_getlength, 3418c1bb86cdSEric Blake .bdrv_get_info = raw_get_info, 3419c1bb86cdSEric Blake .bdrv_get_allocated_file_size 3420c1bb86cdSEric Blake = raw_get_allocated_file_size, 3421244a5668SFam Zheng .bdrv_check_perm = raw_check_perm, 3422244a5668SFam Zheng .bdrv_set_perm = raw_set_perm, 3423244a5668SFam Zheng .bdrv_abort_perm_update = raw_abort_perm_update, 3424c1bb86cdSEric Blake .bdrv_probe_blocksizes = hdev_probe_blocksizes, 3425c1bb86cdSEric Blake .bdrv_probe_geometry = hdev_probe_geometry, 3426c1bb86cdSEric Blake 3427c1bb86cdSEric Blake /* generic scsi device */ 3428c1bb86cdSEric Blake #ifdef __linux__ 34292f3a7ab3SKevin Wolf .bdrv_co_ioctl = hdev_co_ioctl, 3430c1bb86cdSEric Blake #endif 3431c1bb86cdSEric Blake }; 3432c1bb86cdSEric Blake 3433c1bb86cdSEric Blake #if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) 3434c1bb86cdSEric Blake static void cdrom_parse_filename(const char *filename, QDict *options, 3435c1bb86cdSEric Blake Error **errp) 3436c1bb86cdSEric Blake { 343703c320d8SMax Reitz bdrv_parse_filename_strip_prefix(filename, "host_cdrom:", options); 3438c1bb86cdSEric Blake } 3439c1bb86cdSEric Blake #endif 3440c1bb86cdSEric Blake 3441c1bb86cdSEric Blake #ifdef __linux__ 3442c1bb86cdSEric Blake static int cdrom_open(BlockDriverState *bs, QDict *options, int flags, 3443c1bb86cdSEric Blake Error **errp) 3444c1bb86cdSEric Blake { 3445c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 3446c1bb86cdSEric Blake 3447c1bb86cdSEric Blake s->type = FTYPE_CD; 3448c1bb86cdSEric Blake 3449c1bb86cdSEric Blake /* open will not fail even if no CD is inserted, so add O_NONBLOCK */ 3450230ff739SJohn Snow return raw_open_common(bs, options, flags, O_NONBLOCK, true, errp); 3451c1bb86cdSEric Blake } 3452c1bb86cdSEric Blake 3453c1bb86cdSEric Blake static int cdrom_probe_device(const char *filename) 3454c1bb86cdSEric Blake { 3455c1bb86cdSEric Blake int fd, ret; 3456c1bb86cdSEric Blake int prio = 0; 3457c1bb86cdSEric Blake struct stat st; 3458c1bb86cdSEric Blake 3459c1bb86cdSEric Blake fd = qemu_open(filename, O_RDONLY | O_NONBLOCK); 3460c1bb86cdSEric Blake if (fd < 0) { 3461c1bb86cdSEric Blake goto out; 3462c1bb86cdSEric Blake } 3463c1bb86cdSEric Blake ret = fstat(fd, &st); 3464c1bb86cdSEric Blake if (ret == -1 || !S_ISBLK(st.st_mode)) { 3465c1bb86cdSEric Blake goto outc; 3466c1bb86cdSEric Blake } 3467c1bb86cdSEric Blake 3468c1bb86cdSEric Blake /* Attempt to detect via a CDROM specific ioctl */ 3469c1bb86cdSEric Blake ret = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT); 3470c1bb86cdSEric Blake if (ret >= 0) 3471c1bb86cdSEric Blake prio = 100; 3472c1bb86cdSEric Blake 3473c1bb86cdSEric Blake outc: 3474c1bb86cdSEric Blake qemu_close(fd); 3475c1bb86cdSEric Blake out: 3476c1bb86cdSEric Blake return prio; 3477c1bb86cdSEric Blake } 3478c1bb86cdSEric Blake 3479c1bb86cdSEric Blake static bool cdrom_is_inserted(BlockDriverState *bs) 3480c1bb86cdSEric Blake { 3481c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 3482c1bb86cdSEric Blake int ret; 3483c1bb86cdSEric Blake 3484c1bb86cdSEric Blake ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT); 3485c1bb86cdSEric Blake return ret == CDS_DISC_OK; 3486c1bb86cdSEric Blake } 3487c1bb86cdSEric Blake 3488c1bb86cdSEric Blake static void cdrom_eject(BlockDriverState *bs, bool eject_flag) 3489c1bb86cdSEric Blake { 3490c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 3491c1bb86cdSEric Blake 3492c1bb86cdSEric Blake if (eject_flag) { 3493c1bb86cdSEric Blake if (ioctl(s->fd, CDROMEJECT, NULL) < 0) 3494c1bb86cdSEric Blake perror("CDROMEJECT"); 3495c1bb86cdSEric Blake } else { 3496c1bb86cdSEric Blake if (ioctl(s->fd, CDROMCLOSETRAY, NULL) < 0) 3497c1bb86cdSEric Blake perror("CDROMEJECT"); 3498c1bb86cdSEric Blake } 3499c1bb86cdSEric Blake } 3500c1bb86cdSEric Blake 3501c1bb86cdSEric Blake static void cdrom_lock_medium(BlockDriverState *bs, bool locked) 3502c1bb86cdSEric Blake { 3503c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 3504c1bb86cdSEric Blake 3505c1bb86cdSEric Blake if (ioctl(s->fd, CDROM_LOCKDOOR, locked) < 0) { 3506c1bb86cdSEric Blake /* 3507c1bb86cdSEric Blake * Note: an error can happen if the distribution automatically 3508c1bb86cdSEric Blake * mounts the CD-ROM 3509c1bb86cdSEric Blake */ 3510c1bb86cdSEric Blake /* perror("CDROM_LOCKDOOR"); */ 3511c1bb86cdSEric Blake } 3512c1bb86cdSEric Blake } 3513c1bb86cdSEric Blake 3514c1bb86cdSEric Blake static BlockDriver bdrv_host_cdrom = { 3515c1bb86cdSEric Blake .format_name = "host_cdrom", 3516c1bb86cdSEric Blake .protocol_name = "host_cdrom", 3517c1bb86cdSEric Blake .instance_size = sizeof(BDRVRawState), 3518c1bb86cdSEric Blake .bdrv_needs_filename = true, 3519c1bb86cdSEric Blake .bdrv_probe_device = cdrom_probe_device, 3520c1bb86cdSEric Blake .bdrv_parse_filename = cdrom_parse_filename, 3521c1bb86cdSEric Blake .bdrv_file_open = cdrom_open, 3522c1bb86cdSEric Blake .bdrv_close = raw_close, 3523c1bb86cdSEric Blake .bdrv_reopen_prepare = raw_reopen_prepare, 3524c1bb86cdSEric Blake .bdrv_reopen_commit = raw_reopen_commit, 3525c1bb86cdSEric Blake .bdrv_reopen_abort = raw_reopen_abort, 3526efc75e2aSStefan Hajnoczi .bdrv_co_create_opts = hdev_co_create_opts, 3527c1bb86cdSEric Blake .create_opts = &raw_create_opts, 35288a2ce0bcSAlberto Garcia .mutable_opts = mutable_opts, 3529dd577a26SStefan Hajnoczi .bdrv_co_invalidate_cache = raw_co_invalidate_cache, 3530c1bb86cdSEric Blake 3531c1bb86cdSEric Blake 3532c1bb86cdSEric Blake .bdrv_co_preadv = raw_co_preadv, 3533c1bb86cdSEric Blake .bdrv_co_pwritev = raw_co_pwritev, 353433d70fb6SKevin Wolf .bdrv_co_flush_to_disk = raw_co_flush_to_disk, 3535c1bb86cdSEric Blake .bdrv_refresh_limits = raw_refresh_limits, 3536c1bb86cdSEric Blake .bdrv_io_plug = raw_aio_plug, 3537c1bb86cdSEric Blake .bdrv_io_unplug = raw_aio_unplug, 3538042b757cSNishanth Aravamudan .bdrv_attach_aio_context = raw_aio_attach_aio_context, 3539c1bb86cdSEric Blake 3540061ca8a3SKevin Wolf .bdrv_co_truncate = raw_co_truncate, 3541c1bb86cdSEric Blake .bdrv_getlength = raw_getlength, 3542c1bb86cdSEric Blake .has_variable_length = true, 3543c1bb86cdSEric Blake .bdrv_get_allocated_file_size 3544c1bb86cdSEric Blake = raw_get_allocated_file_size, 3545c1bb86cdSEric Blake 3546c1bb86cdSEric Blake /* removable device support */ 3547c1bb86cdSEric Blake .bdrv_is_inserted = cdrom_is_inserted, 3548c1bb86cdSEric Blake .bdrv_eject = cdrom_eject, 3549c1bb86cdSEric Blake .bdrv_lock_medium = cdrom_lock_medium, 3550c1bb86cdSEric Blake 3551c1bb86cdSEric Blake /* generic scsi device */ 35522f3a7ab3SKevin Wolf .bdrv_co_ioctl = hdev_co_ioctl, 3553c1bb86cdSEric Blake }; 3554c1bb86cdSEric Blake #endif /* __linux__ */ 3555c1bb86cdSEric Blake 3556c1bb86cdSEric Blake #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__) 3557c1bb86cdSEric Blake static int cdrom_open(BlockDriverState *bs, QDict *options, int flags, 3558c1bb86cdSEric Blake Error **errp) 3559c1bb86cdSEric Blake { 3560c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 3561c1bb86cdSEric Blake Error *local_err = NULL; 3562c1bb86cdSEric Blake int ret; 3563c1bb86cdSEric Blake 3564c1bb86cdSEric Blake s->type = FTYPE_CD; 3565c1bb86cdSEric Blake 3566230ff739SJohn Snow ret = raw_open_common(bs, options, flags, 0, true, &local_err); 3567c1bb86cdSEric Blake if (ret) { 3568c1bb86cdSEric Blake error_propagate(errp, local_err); 3569c1bb86cdSEric Blake return ret; 3570c1bb86cdSEric Blake } 3571c1bb86cdSEric Blake 3572c1bb86cdSEric Blake /* make sure the door isn't locked at this time */ 3573c1bb86cdSEric Blake ioctl(s->fd, CDIOCALLOW); 3574c1bb86cdSEric Blake return 0; 3575c1bb86cdSEric Blake } 3576c1bb86cdSEric Blake 3577c1bb86cdSEric Blake static int cdrom_probe_device(const char *filename) 3578c1bb86cdSEric Blake { 3579c1bb86cdSEric Blake if (strstart(filename, "/dev/cd", NULL) || 3580c1bb86cdSEric Blake strstart(filename, "/dev/acd", NULL)) 3581c1bb86cdSEric Blake return 100; 3582c1bb86cdSEric Blake return 0; 3583c1bb86cdSEric Blake } 3584c1bb86cdSEric Blake 3585c1bb86cdSEric Blake static int cdrom_reopen(BlockDriverState *bs) 3586c1bb86cdSEric Blake { 3587c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 3588c1bb86cdSEric Blake int fd; 3589c1bb86cdSEric Blake 3590c1bb86cdSEric Blake /* 3591c1bb86cdSEric Blake * Force reread of possibly changed/newly loaded disc, 3592c1bb86cdSEric Blake * FreeBSD seems to not notice sometimes... 3593c1bb86cdSEric Blake */ 3594c1bb86cdSEric Blake if (s->fd >= 0) 3595c1bb86cdSEric Blake qemu_close(s->fd); 3596c1bb86cdSEric Blake fd = qemu_open(bs->filename, s->open_flags, 0644); 3597c1bb86cdSEric Blake if (fd < 0) { 3598c1bb86cdSEric Blake s->fd = -1; 3599c1bb86cdSEric Blake return -EIO; 3600c1bb86cdSEric Blake } 3601c1bb86cdSEric Blake s->fd = fd; 3602c1bb86cdSEric Blake 3603c1bb86cdSEric Blake /* make sure the door isn't locked at this time */ 3604c1bb86cdSEric Blake ioctl(s->fd, CDIOCALLOW); 3605c1bb86cdSEric Blake return 0; 3606c1bb86cdSEric Blake } 3607c1bb86cdSEric Blake 3608c1bb86cdSEric Blake static bool cdrom_is_inserted(BlockDriverState *bs) 3609c1bb86cdSEric Blake { 3610c1bb86cdSEric Blake return raw_getlength(bs) > 0; 3611c1bb86cdSEric Blake } 3612c1bb86cdSEric Blake 3613c1bb86cdSEric Blake static void cdrom_eject(BlockDriverState *bs, bool eject_flag) 3614c1bb86cdSEric Blake { 3615c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 3616c1bb86cdSEric Blake 3617c1bb86cdSEric Blake if (s->fd < 0) 3618c1bb86cdSEric Blake return; 3619c1bb86cdSEric Blake 3620c1bb86cdSEric Blake (void) ioctl(s->fd, CDIOCALLOW); 3621c1bb86cdSEric Blake 3622c1bb86cdSEric Blake if (eject_flag) { 3623c1bb86cdSEric Blake if (ioctl(s->fd, CDIOCEJECT) < 0) 3624c1bb86cdSEric Blake perror("CDIOCEJECT"); 3625c1bb86cdSEric Blake } else { 3626c1bb86cdSEric Blake if (ioctl(s->fd, CDIOCCLOSE) < 0) 3627c1bb86cdSEric Blake perror("CDIOCCLOSE"); 3628c1bb86cdSEric Blake } 3629c1bb86cdSEric Blake 3630c1bb86cdSEric Blake cdrom_reopen(bs); 3631c1bb86cdSEric Blake } 3632c1bb86cdSEric Blake 3633c1bb86cdSEric Blake static void cdrom_lock_medium(BlockDriverState *bs, bool locked) 3634c1bb86cdSEric Blake { 3635c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 3636c1bb86cdSEric Blake 3637c1bb86cdSEric Blake if (s->fd < 0) 3638c1bb86cdSEric Blake return; 3639c1bb86cdSEric Blake if (ioctl(s->fd, (locked ? CDIOCPREVENT : CDIOCALLOW)) < 0) { 3640c1bb86cdSEric Blake /* 3641c1bb86cdSEric Blake * Note: an error can happen if the distribution automatically 3642c1bb86cdSEric Blake * mounts the CD-ROM 3643c1bb86cdSEric Blake */ 3644c1bb86cdSEric Blake /* perror("CDROM_LOCKDOOR"); */ 3645c1bb86cdSEric Blake } 3646c1bb86cdSEric Blake } 3647c1bb86cdSEric Blake 3648c1bb86cdSEric Blake static BlockDriver bdrv_host_cdrom = { 3649c1bb86cdSEric Blake .format_name = "host_cdrom", 3650c1bb86cdSEric Blake .protocol_name = "host_cdrom", 3651c1bb86cdSEric Blake .instance_size = sizeof(BDRVRawState), 3652c1bb86cdSEric Blake .bdrv_needs_filename = true, 3653c1bb86cdSEric Blake .bdrv_probe_device = cdrom_probe_device, 3654c1bb86cdSEric Blake .bdrv_parse_filename = cdrom_parse_filename, 3655c1bb86cdSEric Blake .bdrv_file_open = cdrom_open, 3656c1bb86cdSEric Blake .bdrv_close = raw_close, 3657c1bb86cdSEric Blake .bdrv_reopen_prepare = raw_reopen_prepare, 3658c1bb86cdSEric Blake .bdrv_reopen_commit = raw_reopen_commit, 3659c1bb86cdSEric Blake .bdrv_reopen_abort = raw_reopen_abort, 3660efc75e2aSStefan Hajnoczi .bdrv_co_create_opts = hdev_co_create_opts, 3661c1bb86cdSEric Blake .create_opts = &raw_create_opts, 36628a2ce0bcSAlberto Garcia .mutable_opts = mutable_opts, 3663c1bb86cdSEric Blake 3664c1bb86cdSEric Blake .bdrv_co_preadv = raw_co_preadv, 3665c1bb86cdSEric Blake .bdrv_co_pwritev = raw_co_pwritev, 366633d70fb6SKevin Wolf .bdrv_co_flush_to_disk = raw_co_flush_to_disk, 3667c1bb86cdSEric Blake .bdrv_refresh_limits = raw_refresh_limits, 3668c1bb86cdSEric Blake .bdrv_io_plug = raw_aio_plug, 3669c1bb86cdSEric Blake .bdrv_io_unplug = raw_aio_unplug, 3670042b757cSNishanth Aravamudan .bdrv_attach_aio_context = raw_aio_attach_aio_context, 3671c1bb86cdSEric Blake 3672061ca8a3SKevin Wolf .bdrv_co_truncate = raw_co_truncate, 3673c1bb86cdSEric Blake .bdrv_getlength = raw_getlength, 3674c1bb86cdSEric Blake .has_variable_length = true, 3675c1bb86cdSEric Blake .bdrv_get_allocated_file_size 3676c1bb86cdSEric Blake = raw_get_allocated_file_size, 3677c1bb86cdSEric Blake 3678c1bb86cdSEric Blake /* removable device support */ 3679c1bb86cdSEric Blake .bdrv_is_inserted = cdrom_is_inserted, 3680c1bb86cdSEric Blake .bdrv_eject = cdrom_eject, 3681c1bb86cdSEric Blake .bdrv_lock_medium = cdrom_lock_medium, 3682c1bb86cdSEric Blake }; 3683c1bb86cdSEric Blake #endif /* __FreeBSD__ */ 3684c1bb86cdSEric Blake 3685c1bb86cdSEric Blake static void bdrv_file_init(void) 3686c1bb86cdSEric Blake { 3687c1bb86cdSEric Blake /* 3688c1bb86cdSEric Blake * Register all the drivers. Note that order is important, the driver 3689c1bb86cdSEric Blake * registered last will get probed first. 3690c1bb86cdSEric Blake */ 3691c1bb86cdSEric Blake bdrv_register(&bdrv_file); 3692c1bb86cdSEric Blake bdrv_register(&bdrv_host_device); 3693c1bb86cdSEric Blake #ifdef __linux__ 3694c1bb86cdSEric Blake bdrv_register(&bdrv_host_cdrom); 3695c1bb86cdSEric Blake #endif 3696c1bb86cdSEric Blake #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) 3697c1bb86cdSEric Blake bdrv_register(&bdrv_host_cdrom); 3698c1bb86cdSEric Blake #endif 3699c1bb86cdSEric Blake } 3700c1bb86cdSEric Blake 3701c1bb86cdSEric Blake block_init(bdrv_file_init); 3702