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" 26c1bb86cdSEric Blake #include "qapi/error.h" 27c1bb86cdSEric Blake #include "qemu/cutils.h" 28c1bb86cdSEric Blake #include "qemu/error-report.h" 29c1bb86cdSEric Blake #include "block/block_int.h" 30c1bb86cdSEric Blake #include "qemu/module.h" 31922a01a0SMarkus Armbruster #include "qemu/option.h" 32c1bb86cdSEric Blake #include "trace.h" 33c1bb86cdSEric Blake #include "block/thread-pool.h" 34c1bb86cdSEric Blake #include "qemu/iov.h" 35c1bb86cdSEric Blake #include "block/raw-aio.h" 36452fcdbcSMarkus Armbruster #include "qapi/qmp/qdict.h" 37c1bb86cdSEric Blake #include "qapi/qmp/qstring.h" 38c1bb86cdSEric Blake 397c9e5276SPaolo Bonzini #include "scsi/pr-manager.h" 407c9e5276SPaolo Bonzini #include "scsi/constants.h" 417c9e5276SPaolo Bonzini 42c1bb86cdSEric Blake #if defined(__APPLE__) && (__MACH__) 43c1bb86cdSEric Blake #include <paths.h> 44c1bb86cdSEric Blake #include <sys/param.h> 45c1bb86cdSEric Blake #include <IOKit/IOKitLib.h> 46c1bb86cdSEric Blake #include <IOKit/IOBSD.h> 47c1bb86cdSEric Blake #include <IOKit/storage/IOMediaBSDClient.h> 48c1bb86cdSEric Blake #include <IOKit/storage/IOMedia.h> 49c1bb86cdSEric Blake #include <IOKit/storage/IOCDMedia.h> 50c1bb86cdSEric Blake //#include <IOKit/storage/IOCDTypes.h> 51c1bb86cdSEric Blake #include <IOKit/storage/IODVDMedia.h> 52c1bb86cdSEric Blake #include <CoreFoundation/CoreFoundation.h> 53c1bb86cdSEric Blake #endif 54c1bb86cdSEric Blake 55c1bb86cdSEric Blake #ifdef __sun__ 56c1bb86cdSEric Blake #define _POSIX_PTHREAD_SEMANTICS 1 57c1bb86cdSEric Blake #include <sys/dkio.h> 58c1bb86cdSEric Blake #endif 59c1bb86cdSEric Blake #ifdef __linux__ 60c1bb86cdSEric Blake #include <sys/ioctl.h> 61c1bb86cdSEric Blake #include <sys/param.h> 621efad060SFam Zheng #include <sys/syscall.h> 63c1bb86cdSEric Blake #include <linux/cdrom.h> 64c1bb86cdSEric Blake #include <linux/fd.h> 65c1bb86cdSEric Blake #include <linux/fs.h> 66c1bb86cdSEric Blake #include <linux/hdreg.h> 67c1bb86cdSEric Blake #include <scsi/sg.h> 68c1bb86cdSEric Blake #ifdef __s390__ 69c1bb86cdSEric Blake #include <asm/dasd.h> 70c1bb86cdSEric Blake #endif 71c1bb86cdSEric Blake #ifndef FS_NOCOW_FL 72c1bb86cdSEric Blake #define FS_NOCOW_FL 0x00800000 /* Do not cow file */ 73c1bb86cdSEric Blake #endif 74c1bb86cdSEric Blake #endif 75c1bb86cdSEric Blake #if defined(CONFIG_FALLOCATE_PUNCH_HOLE) || defined(CONFIG_FALLOCATE_ZERO_RANGE) 76c1bb86cdSEric Blake #include <linux/falloc.h> 77c1bb86cdSEric Blake #endif 78c1bb86cdSEric Blake #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__) 79c1bb86cdSEric Blake #include <sys/disk.h> 80c1bb86cdSEric Blake #include <sys/cdio.h> 81c1bb86cdSEric Blake #endif 82c1bb86cdSEric Blake 83c1bb86cdSEric Blake #ifdef __OpenBSD__ 84c1bb86cdSEric Blake #include <sys/ioctl.h> 85c1bb86cdSEric Blake #include <sys/disklabel.h> 86c1bb86cdSEric Blake #include <sys/dkio.h> 87c1bb86cdSEric Blake #endif 88c1bb86cdSEric Blake 89c1bb86cdSEric Blake #ifdef __NetBSD__ 90c1bb86cdSEric Blake #include <sys/ioctl.h> 91c1bb86cdSEric Blake #include <sys/disklabel.h> 92c1bb86cdSEric Blake #include <sys/dkio.h> 93c1bb86cdSEric Blake #include <sys/disk.h> 94c1bb86cdSEric Blake #endif 95c1bb86cdSEric Blake 96c1bb86cdSEric Blake #ifdef __DragonFly__ 97c1bb86cdSEric Blake #include <sys/ioctl.h> 98c1bb86cdSEric Blake #include <sys/diskslice.h> 99c1bb86cdSEric Blake #endif 100c1bb86cdSEric Blake 101c1bb86cdSEric Blake #ifdef CONFIG_XFS 102c1bb86cdSEric Blake #include <xfs/xfs.h> 103c1bb86cdSEric Blake #endif 104c1bb86cdSEric Blake 1054f7d28d7SLaurent Vivier #include "trace.h" 106c1bb86cdSEric Blake 107c1bb86cdSEric Blake /* OS X does not have O_DSYNC */ 108c1bb86cdSEric Blake #ifndef O_DSYNC 109c1bb86cdSEric Blake #ifdef O_SYNC 110c1bb86cdSEric Blake #define O_DSYNC O_SYNC 111c1bb86cdSEric Blake #elif defined(O_FSYNC) 112c1bb86cdSEric Blake #define O_DSYNC O_FSYNC 113c1bb86cdSEric Blake #endif 114c1bb86cdSEric Blake #endif 115c1bb86cdSEric Blake 116c1bb86cdSEric Blake /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */ 117c1bb86cdSEric Blake #ifndef O_DIRECT 118c1bb86cdSEric Blake #define O_DIRECT O_DSYNC 119c1bb86cdSEric Blake #endif 120c1bb86cdSEric Blake 121c1bb86cdSEric Blake #define FTYPE_FILE 0 122c1bb86cdSEric Blake #define FTYPE_CD 1 123c1bb86cdSEric Blake 124c1bb86cdSEric Blake #define MAX_BLOCKSIZE 4096 125c1bb86cdSEric Blake 126244a5668SFam Zheng /* Posix file locking bytes. Libvirt takes byte 0, we start from higher bytes, 127244a5668SFam Zheng * leaving a few more bytes for its future use. */ 128244a5668SFam Zheng #define RAW_LOCK_PERM_BASE 100 129244a5668SFam Zheng #define RAW_LOCK_SHARED_BASE 200 130244a5668SFam Zheng 131c1bb86cdSEric Blake typedef struct BDRVRawState { 132c1bb86cdSEric Blake int fd; 133244a5668SFam Zheng bool use_lock; 134c1bb86cdSEric Blake int type; 135c1bb86cdSEric Blake int open_flags; 136c1bb86cdSEric Blake size_t buf_align; 137c1bb86cdSEric Blake 138244a5668SFam Zheng /* The current permissions. */ 139244a5668SFam Zheng uint64_t perm; 140244a5668SFam Zheng uint64_t shared_perm; 141244a5668SFam Zheng 1422996ffadSFam Zheng /* The perms bits whose corresponding bytes are already locked in 143f2e3af29SFam Zheng * s->fd. */ 1442996ffadSFam Zheng uint64_t locked_perm; 1452996ffadSFam Zheng uint64_t locked_shared_perm; 1462996ffadSFam Zheng 147*e0c9cf3aSKevin Wolf BDRVReopenState *reopen_state; 148*e0c9cf3aSKevin Wolf 149c1bb86cdSEric Blake #ifdef CONFIG_XFS 150c1bb86cdSEric Blake bool is_xfs:1; 151c1bb86cdSEric Blake #endif 152c1bb86cdSEric Blake bool has_discard:1; 153c1bb86cdSEric Blake bool has_write_zeroes:1; 154c1bb86cdSEric Blake bool discard_zeroes:1; 155c1bb86cdSEric Blake bool use_linux_aio:1; 156e5bcf967SKevin Wolf bool page_cache_inconsistent:1; 157c1bb86cdSEric Blake bool has_fallocate; 158c1bb86cdSEric Blake bool needs_alignment; 15931be8a2aSStefan Hajnoczi bool check_cache_dropped; 1607c9e5276SPaolo Bonzini 1617c9e5276SPaolo Bonzini PRManager *pr_mgr; 162c1bb86cdSEric Blake } BDRVRawState; 163c1bb86cdSEric Blake 164c1bb86cdSEric Blake typedef struct BDRVRawReopenState { 165c1bb86cdSEric Blake int fd; 166c1bb86cdSEric Blake int open_flags; 16731be8a2aSStefan Hajnoczi bool check_cache_dropped; 168c1bb86cdSEric Blake } BDRVRawReopenState; 169c1bb86cdSEric Blake 170c1bb86cdSEric Blake static int fd_open(BlockDriverState *bs); 171c1bb86cdSEric Blake static int64_t raw_getlength(BlockDriverState *bs); 172c1bb86cdSEric Blake 173c1bb86cdSEric Blake typedef struct RawPosixAIOData { 174c1bb86cdSEric Blake BlockDriverState *bs; 175c1bb86cdSEric Blake int aio_type; 176d57c44d0SKevin Wolf int aio_fildes; 177d57c44d0SKevin Wolf 178d57c44d0SKevin Wolf off_t aio_offset; 179d57c44d0SKevin Wolf uint64_t aio_nbytes; 180d57c44d0SKevin Wolf 18193f4e2ffSKevin Wolf union { 18293f4e2ffSKevin Wolf struct { 183d57c44d0SKevin Wolf struct iovec *iov; 184d57c44d0SKevin Wolf int niov; 185d57c44d0SKevin Wolf } io; 186d57c44d0SKevin Wolf struct { 187d57c44d0SKevin Wolf uint64_t cmd; 188d57c44d0SKevin Wolf void *buf; 189d57c44d0SKevin Wolf } ioctl; 190d57c44d0SKevin Wolf struct { 1911efad060SFam Zheng int aio_fd2; 1921efad060SFam Zheng off_t aio_offset2; 193d57c44d0SKevin Wolf } copy_range; 19493f4e2ffSKevin Wolf struct { 19593f4e2ffSKevin Wolf PreallocMode prealloc; 19693f4e2ffSKevin Wolf Error **errp; 197d57c44d0SKevin Wolf } truncate; 19893f4e2ffSKevin Wolf }; 199c1bb86cdSEric Blake } RawPosixAIOData; 200c1bb86cdSEric Blake 201c1bb86cdSEric Blake #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) 202c1bb86cdSEric Blake static int cdrom_reopen(BlockDriverState *bs); 203c1bb86cdSEric Blake #endif 204c1bb86cdSEric Blake 205c1bb86cdSEric Blake #if defined(__NetBSD__) 206db0754dfSFam Zheng static int raw_normalize_devicepath(const char **filename, Error **errp) 207c1bb86cdSEric Blake { 208c1bb86cdSEric Blake static char namebuf[PATH_MAX]; 209c1bb86cdSEric Blake const char *dp, *fname; 210c1bb86cdSEric Blake struct stat sb; 211c1bb86cdSEric Blake 212c1bb86cdSEric Blake fname = *filename; 213c1bb86cdSEric Blake dp = strrchr(fname, '/'); 214c1bb86cdSEric Blake if (lstat(fname, &sb) < 0) { 215db0754dfSFam Zheng error_setg_errno(errp, errno, "%s: stat failed", fname); 216c1bb86cdSEric Blake return -errno; 217c1bb86cdSEric Blake } 218c1bb86cdSEric Blake 219c1bb86cdSEric Blake if (!S_ISBLK(sb.st_mode)) { 220c1bb86cdSEric Blake return 0; 221c1bb86cdSEric Blake } 222c1bb86cdSEric Blake 223c1bb86cdSEric Blake if (dp == NULL) { 224c1bb86cdSEric Blake snprintf(namebuf, PATH_MAX, "r%s", fname); 225c1bb86cdSEric Blake } else { 226c1bb86cdSEric Blake snprintf(namebuf, PATH_MAX, "%.*s/r%s", 227c1bb86cdSEric Blake (int)(dp - fname), fname, dp + 1); 228c1bb86cdSEric Blake } 229c1bb86cdSEric Blake *filename = namebuf; 230db0754dfSFam Zheng warn_report("%s is a block device, using %s", fname, *filename); 231c1bb86cdSEric Blake 232c1bb86cdSEric Blake return 0; 233c1bb86cdSEric Blake } 234c1bb86cdSEric Blake #else 235db0754dfSFam Zheng static int raw_normalize_devicepath(const char **filename, Error **errp) 236c1bb86cdSEric Blake { 237c1bb86cdSEric Blake return 0; 238c1bb86cdSEric Blake } 239c1bb86cdSEric Blake #endif 240c1bb86cdSEric Blake 241c1bb86cdSEric Blake /* 242c1bb86cdSEric Blake * Get logical block size via ioctl. On success store it in @sector_size_p. 243c1bb86cdSEric Blake */ 244c1bb86cdSEric Blake static int probe_logical_blocksize(int fd, unsigned int *sector_size_p) 245c1bb86cdSEric Blake { 246c1bb86cdSEric Blake unsigned int sector_size; 247c1bb86cdSEric Blake bool success = false; 248700f9ce0SPeter Maydell int i; 249c1bb86cdSEric Blake 250c1bb86cdSEric Blake errno = ENOTSUP; 251700f9ce0SPeter Maydell static const unsigned long ioctl_list[] = { 252c1bb86cdSEric Blake #ifdef BLKSSZGET 253700f9ce0SPeter Maydell BLKSSZGET, 254c1bb86cdSEric Blake #endif 255c1bb86cdSEric Blake #ifdef DKIOCGETBLOCKSIZE 256700f9ce0SPeter Maydell DKIOCGETBLOCKSIZE, 257c1bb86cdSEric Blake #endif 258c1bb86cdSEric Blake #ifdef DIOCGSECTORSIZE 259700f9ce0SPeter Maydell DIOCGSECTORSIZE, 260700f9ce0SPeter Maydell #endif 261700f9ce0SPeter Maydell }; 262700f9ce0SPeter Maydell 263700f9ce0SPeter Maydell /* Try a few ioctls to get the right size */ 264700f9ce0SPeter Maydell for (i = 0; i < (int)ARRAY_SIZE(ioctl_list); i++) { 265700f9ce0SPeter Maydell if (ioctl(fd, ioctl_list[i], §or_size) >= 0) { 266c1bb86cdSEric Blake *sector_size_p = sector_size; 267c1bb86cdSEric Blake success = true; 268c1bb86cdSEric Blake } 269700f9ce0SPeter Maydell } 270c1bb86cdSEric Blake 271c1bb86cdSEric Blake return success ? 0 : -errno; 272c1bb86cdSEric Blake } 273c1bb86cdSEric Blake 274c1bb86cdSEric Blake /** 275c1bb86cdSEric Blake * Get physical block size of @fd. 276c1bb86cdSEric Blake * On success, store it in @blk_size and return 0. 277c1bb86cdSEric Blake * On failure, return -errno. 278c1bb86cdSEric Blake */ 279c1bb86cdSEric Blake static int probe_physical_blocksize(int fd, unsigned int *blk_size) 280c1bb86cdSEric Blake { 281c1bb86cdSEric Blake #ifdef BLKPBSZGET 282c1bb86cdSEric Blake if (ioctl(fd, BLKPBSZGET, blk_size) < 0) { 283c1bb86cdSEric Blake return -errno; 284c1bb86cdSEric Blake } 285c1bb86cdSEric Blake return 0; 286c1bb86cdSEric Blake #else 287c1bb86cdSEric Blake return -ENOTSUP; 288c1bb86cdSEric Blake #endif 289c1bb86cdSEric Blake } 290c1bb86cdSEric Blake 291c1bb86cdSEric Blake /* Check if read is allowed with given memory buffer and length. 292c1bb86cdSEric Blake * 293c1bb86cdSEric Blake * This function is used to check O_DIRECT memory buffer and request alignment. 294c1bb86cdSEric Blake */ 295c1bb86cdSEric Blake static bool raw_is_io_aligned(int fd, void *buf, size_t len) 296c1bb86cdSEric Blake { 297c1bb86cdSEric Blake ssize_t ret = pread(fd, buf, len, 0); 298c1bb86cdSEric Blake 299c1bb86cdSEric Blake if (ret >= 0) { 300c1bb86cdSEric Blake return true; 301c1bb86cdSEric Blake } 302c1bb86cdSEric Blake 303c1bb86cdSEric Blake #ifdef __linux__ 304c1bb86cdSEric Blake /* The Linux kernel returns EINVAL for misaligned O_DIRECT reads. Ignore 305c1bb86cdSEric Blake * other errors (e.g. real I/O error), which could happen on a failed 306c1bb86cdSEric Blake * drive, since we only care about probing alignment. 307c1bb86cdSEric Blake */ 308c1bb86cdSEric Blake if (errno != EINVAL) { 309c1bb86cdSEric Blake return true; 310c1bb86cdSEric Blake } 311c1bb86cdSEric Blake #endif 312c1bb86cdSEric Blake 313c1bb86cdSEric Blake return false; 314c1bb86cdSEric Blake } 315c1bb86cdSEric Blake 316c1bb86cdSEric Blake static void raw_probe_alignment(BlockDriverState *bs, int fd, Error **errp) 317c1bb86cdSEric Blake { 318c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 319c1bb86cdSEric Blake char *buf; 320c1bb86cdSEric Blake size_t max_align = MAX(MAX_BLOCKSIZE, getpagesize()); 321c1bb86cdSEric Blake 322c1bb86cdSEric Blake /* For SCSI generic devices the alignment is not really used. 323c1bb86cdSEric Blake With buffered I/O, we don't have any restrictions. */ 324c1bb86cdSEric Blake if (bdrv_is_sg(bs) || !s->needs_alignment) { 325c1bb86cdSEric Blake bs->bl.request_alignment = 1; 326c1bb86cdSEric Blake s->buf_align = 1; 327c1bb86cdSEric Blake return; 328c1bb86cdSEric Blake } 329c1bb86cdSEric Blake 330c1bb86cdSEric Blake bs->bl.request_alignment = 0; 331c1bb86cdSEric Blake s->buf_align = 0; 332c1bb86cdSEric Blake /* Let's try to use the logical blocksize for the alignment. */ 333c1bb86cdSEric Blake if (probe_logical_blocksize(fd, &bs->bl.request_alignment) < 0) { 334c1bb86cdSEric Blake bs->bl.request_alignment = 0; 335c1bb86cdSEric Blake } 336c1bb86cdSEric Blake #ifdef CONFIG_XFS 337c1bb86cdSEric Blake if (s->is_xfs) { 338c1bb86cdSEric Blake struct dioattr da; 339c1bb86cdSEric Blake if (xfsctl(NULL, fd, XFS_IOC_DIOINFO, &da) >= 0) { 340c1bb86cdSEric Blake bs->bl.request_alignment = da.d_miniosz; 341c1bb86cdSEric Blake /* The kernel returns wrong information for d_mem */ 342c1bb86cdSEric Blake /* s->buf_align = da.d_mem; */ 343c1bb86cdSEric Blake } 344c1bb86cdSEric Blake } 345c1bb86cdSEric Blake #endif 346c1bb86cdSEric Blake 347c1bb86cdSEric Blake /* If we could not get the sizes so far, we can only guess them */ 348c1bb86cdSEric Blake if (!s->buf_align) { 349c1bb86cdSEric Blake size_t align; 350c1bb86cdSEric Blake buf = qemu_memalign(max_align, 2 * max_align); 351c1bb86cdSEric Blake for (align = 512; align <= max_align; align <<= 1) { 352c1bb86cdSEric Blake if (raw_is_io_aligned(fd, buf + align, max_align)) { 353c1bb86cdSEric Blake s->buf_align = align; 354c1bb86cdSEric Blake break; 355c1bb86cdSEric Blake } 356c1bb86cdSEric Blake } 357c1bb86cdSEric Blake qemu_vfree(buf); 358c1bb86cdSEric Blake } 359c1bb86cdSEric Blake 360c1bb86cdSEric Blake if (!bs->bl.request_alignment) { 361c1bb86cdSEric Blake size_t align; 362c1bb86cdSEric Blake buf = qemu_memalign(s->buf_align, max_align); 363c1bb86cdSEric Blake for (align = 512; align <= max_align; align <<= 1) { 364c1bb86cdSEric Blake if (raw_is_io_aligned(fd, buf, align)) { 365c1bb86cdSEric Blake bs->bl.request_alignment = align; 366c1bb86cdSEric Blake break; 367c1bb86cdSEric Blake } 368c1bb86cdSEric Blake } 369c1bb86cdSEric Blake qemu_vfree(buf); 370c1bb86cdSEric Blake } 371c1bb86cdSEric Blake 372c1bb86cdSEric Blake if (!s->buf_align || !bs->bl.request_alignment) { 373c1bb86cdSEric Blake error_setg(errp, "Could not find working O_DIRECT alignment"); 374c1bb86cdSEric Blake error_append_hint(errp, "Try cache.direct=off\n"); 375c1bb86cdSEric Blake } 376c1bb86cdSEric Blake } 377c1bb86cdSEric Blake 378c1bb86cdSEric Blake static void raw_parse_flags(int bdrv_flags, int *open_flags) 379c1bb86cdSEric Blake { 380c1bb86cdSEric Blake assert(open_flags != NULL); 381c1bb86cdSEric Blake 382c1bb86cdSEric Blake *open_flags |= O_BINARY; 383c1bb86cdSEric Blake *open_flags &= ~O_ACCMODE; 384c1bb86cdSEric Blake if (bdrv_flags & BDRV_O_RDWR) { 385c1bb86cdSEric Blake *open_flags |= O_RDWR; 386c1bb86cdSEric Blake } else { 387c1bb86cdSEric Blake *open_flags |= O_RDONLY; 388c1bb86cdSEric Blake } 389c1bb86cdSEric Blake 390c1bb86cdSEric Blake /* Use O_DSYNC for write-through caching, no flags for write-back caching, 391c1bb86cdSEric Blake * and O_DIRECT for no caching. */ 392c1bb86cdSEric Blake if ((bdrv_flags & BDRV_O_NOCACHE)) { 393c1bb86cdSEric Blake *open_flags |= O_DIRECT; 394c1bb86cdSEric Blake } 395c1bb86cdSEric Blake } 396c1bb86cdSEric Blake 397c1bb86cdSEric Blake static void raw_parse_filename(const char *filename, QDict *options, 398c1bb86cdSEric Blake Error **errp) 399c1bb86cdSEric Blake { 40003c320d8SMax Reitz bdrv_parse_filename_strip_prefix(filename, "file:", options); 401c1bb86cdSEric Blake } 402c1bb86cdSEric Blake 403c1bb86cdSEric Blake static QemuOptsList raw_runtime_opts = { 404c1bb86cdSEric Blake .name = "raw", 405c1bb86cdSEric Blake .head = QTAILQ_HEAD_INITIALIZER(raw_runtime_opts.head), 406c1bb86cdSEric Blake .desc = { 407c1bb86cdSEric Blake { 408c1bb86cdSEric Blake .name = "filename", 409c1bb86cdSEric Blake .type = QEMU_OPT_STRING, 410c1bb86cdSEric Blake .help = "File name of the image", 411c1bb86cdSEric Blake }, 412c1bb86cdSEric Blake { 413c1bb86cdSEric Blake .name = "aio", 414c1bb86cdSEric Blake .type = QEMU_OPT_STRING, 415c1bb86cdSEric Blake .help = "host AIO implementation (threads, native)", 416c1bb86cdSEric Blake }, 41716b48d5dSFam Zheng { 41816b48d5dSFam Zheng .name = "locking", 41916b48d5dSFam Zheng .type = QEMU_OPT_STRING, 42016b48d5dSFam Zheng .help = "file locking mode (on/off/auto, default: auto)", 42116b48d5dSFam Zheng }, 4227c9e5276SPaolo Bonzini { 4237c9e5276SPaolo Bonzini .name = "pr-manager", 4247c9e5276SPaolo Bonzini .type = QEMU_OPT_STRING, 4257c9e5276SPaolo Bonzini .help = "id of persistent reservation manager object (default: none)", 4267c9e5276SPaolo Bonzini }, 42731be8a2aSStefan Hajnoczi { 42831be8a2aSStefan Hajnoczi .name = "x-check-cache-dropped", 42931be8a2aSStefan Hajnoczi .type = QEMU_OPT_BOOL, 43031be8a2aSStefan Hajnoczi .help = "check that page cache was dropped on live migration (default: off)" 43131be8a2aSStefan Hajnoczi }, 432c1bb86cdSEric Blake { /* end of list */ } 433c1bb86cdSEric Blake }, 434c1bb86cdSEric Blake }; 435c1bb86cdSEric Blake 436c1bb86cdSEric Blake static int raw_open_common(BlockDriverState *bs, QDict *options, 437230ff739SJohn Snow int bdrv_flags, int open_flags, 438230ff739SJohn Snow bool device, Error **errp) 439c1bb86cdSEric Blake { 440c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 441c1bb86cdSEric Blake QemuOpts *opts; 442c1bb86cdSEric Blake Error *local_err = NULL; 443c1bb86cdSEric Blake const char *filename = NULL; 4447c9e5276SPaolo Bonzini const char *str; 445c1bb86cdSEric Blake BlockdevAioOptions aio, aio_default; 446c1bb86cdSEric Blake int fd, ret; 447c1bb86cdSEric Blake struct stat st; 448244a5668SFam Zheng OnOffAuto locking; 449c1bb86cdSEric Blake 450c1bb86cdSEric Blake opts = qemu_opts_create(&raw_runtime_opts, NULL, 0, &error_abort); 451c1bb86cdSEric Blake qemu_opts_absorb_qdict(opts, options, &local_err); 452c1bb86cdSEric Blake if (local_err) { 453c1bb86cdSEric Blake error_propagate(errp, local_err); 454c1bb86cdSEric Blake ret = -EINVAL; 455c1bb86cdSEric Blake goto fail; 456c1bb86cdSEric Blake } 457c1bb86cdSEric Blake 458c1bb86cdSEric Blake filename = qemu_opt_get(opts, "filename"); 459c1bb86cdSEric Blake 460db0754dfSFam Zheng ret = raw_normalize_devicepath(&filename, errp); 461c1bb86cdSEric Blake if (ret != 0) { 462c1bb86cdSEric Blake goto fail; 463c1bb86cdSEric Blake } 464c1bb86cdSEric Blake 465c1bb86cdSEric Blake aio_default = (bdrv_flags & BDRV_O_NATIVE_AIO) 466c1bb86cdSEric Blake ? BLOCKDEV_AIO_OPTIONS_NATIVE 467c1bb86cdSEric Blake : BLOCKDEV_AIO_OPTIONS_THREADS; 468f7abe0ecSMarc-André Lureau aio = qapi_enum_parse(&BlockdevAioOptions_lookup, 469f7abe0ecSMarc-André Lureau qemu_opt_get(opts, "aio"), 47006c60b6cSMarkus Armbruster aio_default, &local_err); 471c1bb86cdSEric Blake if (local_err) { 472c1bb86cdSEric Blake error_propagate(errp, local_err); 473c1bb86cdSEric Blake ret = -EINVAL; 474c1bb86cdSEric Blake goto fail; 475c1bb86cdSEric Blake } 476c1bb86cdSEric Blake s->use_linux_aio = (aio == BLOCKDEV_AIO_OPTIONS_NATIVE); 477c1bb86cdSEric Blake 478f7abe0ecSMarc-André Lureau locking = qapi_enum_parse(&OnOffAuto_lookup, 479f7abe0ecSMarc-André Lureau qemu_opt_get(opts, "locking"), 48006c60b6cSMarkus Armbruster ON_OFF_AUTO_AUTO, &local_err); 481244a5668SFam Zheng if (local_err) { 482244a5668SFam Zheng error_propagate(errp, local_err); 483244a5668SFam Zheng ret = -EINVAL; 484244a5668SFam Zheng goto fail; 485244a5668SFam Zheng } 486244a5668SFam Zheng switch (locking) { 487244a5668SFam Zheng case ON_OFF_AUTO_ON: 488244a5668SFam Zheng s->use_lock = true; 4892b218f5dSFam Zheng if (!qemu_has_ofd_lock()) { 490db0754dfSFam Zheng warn_report("File lock requested but OFD locking syscall is " 491db0754dfSFam Zheng "unavailable, falling back to POSIX file locks"); 492db0754dfSFam Zheng error_printf("Due to the implementation, locks can be lost " 4932b218f5dSFam Zheng "unexpectedly.\n"); 4942b218f5dSFam Zheng } 495244a5668SFam Zheng break; 496244a5668SFam Zheng case ON_OFF_AUTO_OFF: 497244a5668SFam Zheng s->use_lock = false; 498244a5668SFam Zheng break; 499244a5668SFam Zheng case ON_OFF_AUTO_AUTO: 5002b218f5dSFam Zheng s->use_lock = qemu_has_ofd_lock(); 501244a5668SFam Zheng break; 502244a5668SFam Zheng default: 503244a5668SFam Zheng abort(); 504244a5668SFam Zheng } 505244a5668SFam Zheng 5067c9e5276SPaolo Bonzini str = qemu_opt_get(opts, "pr-manager"); 5077c9e5276SPaolo Bonzini if (str) { 5087c9e5276SPaolo Bonzini s->pr_mgr = pr_manager_lookup(str, &local_err); 5097c9e5276SPaolo Bonzini if (local_err) { 5107c9e5276SPaolo Bonzini error_propagate(errp, local_err); 5117c9e5276SPaolo Bonzini ret = -EINVAL; 5127c9e5276SPaolo Bonzini goto fail; 5137c9e5276SPaolo Bonzini } 5147c9e5276SPaolo Bonzini } 5157c9e5276SPaolo Bonzini 51631be8a2aSStefan Hajnoczi s->check_cache_dropped = qemu_opt_get_bool(opts, "x-check-cache-dropped", 51731be8a2aSStefan Hajnoczi false); 51831be8a2aSStefan Hajnoczi 519c1bb86cdSEric Blake s->open_flags = open_flags; 520c1bb86cdSEric Blake raw_parse_flags(bdrv_flags, &s->open_flags); 521c1bb86cdSEric Blake 522c1bb86cdSEric Blake s->fd = -1; 523c1bb86cdSEric Blake fd = qemu_open(filename, s->open_flags, 0644); 52464107dc0SKevin Wolf ret = fd < 0 ? -errno : 0; 52564107dc0SKevin Wolf 52664107dc0SKevin Wolf if (ret == -EACCES || ret == -EROFS) { 52764107dc0SKevin Wolf /* Try to degrade to read-only, but if it doesn't work, still use the 52864107dc0SKevin Wolf * normal error message. */ 52964107dc0SKevin Wolf if (bdrv_apply_auto_read_only(bs, NULL, NULL) == 0) { 53064107dc0SKevin Wolf bdrv_flags &= ~BDRV_O_RDWR; 53164107dc0SKevin Wolf raw_parse_flags(bdrv_flags, &s->open_flags); 53264107dc0SKevin Wolf assert(!(s->open_flags & O_CREAT)); 53364107dc0SKevin Wolf fd = qemu_open(filename, s->open_flags); 53464107dc0SKevin Wolf ret = fd < 0 ? -errno : 0; 53564107dc0SKevin Wolf } 53664107dc0SKevin Wolf } 53764107dc0SKevin Wolf 53864107dc0SKevin Wolf if (ret < 0) { 53964107dc0SKevin Wolf error_setg_errno(errp, -ret, "Could not open '%s'", filename); 540c1bb86cdSEric Blake if (ret == -EROFS) { 541c1bb86cdSEric Blake ret = -EACCES; 542c1bb86cdSEric Blake } 543c1bb86cdSEric Blake goto fail; 544c1bb86cdSEric Blake } 545c1bb86cdSEric Blake s->fd = fd; 546c1bb86cdSEric Blake 547244a5668SFam Zheng s->perm = 0; 548244a5668SFam Zheng s->shared_perm = BLK_PERM_ALL; 549244a5668SFam Zheng 550c1bb86cdSEric Blake #ifdef CONFIG_LINUX_AIO 551c1bb86cdSEric Blake /* Currently Linux does AIO only for files opened with O_DIRECT */ 552ed6e2161SNishanth Aravamudan if (s->use_linux_aio) { 553ed6e2161SNishanth Aravamudan if (!(s->open_flags & O_DIRECT)) { 554c1bb86cdSEric Blake error_setg(errp, "aio=native was specified, but it requires " 555c1bb86cdSEric Blake "cache.direct=on, which was not specified."); 556c1bb86cdSEric Blake ret = -EINVAL; 557c1bb86cdSEric Blake goto fail; 558c1bb86cdSEric Blake } 559ed6e2161SNishanth Aravamudan if (!aio_setup_linux_aio(bdrv_get_aio_context(bs), errp)) { 560ed6e2161SNishanth Aravamudan error_prepend(errp, "Unable to use native AIO: "); 561ed6e2161SNishanth Aravamudan goto fail; 562ed6e2161SNishanth Aravamudan } 563ed6e2161SNishanth Aravamudan } 564c1bb86cdSEric Blake #else 565c1bb86cdSEric Blake if (s->use_linux_aio) { 566c1bb86cdSEric Blake error_setg(errp, "aio=native was specified, but is not supported " 567c1bb86cdSEric Blake "in this build."); 568c1bb86cdSEric Blake ret = -EINVAL; 569c1bb86cdSEric Blake goto fail; 570c1bb86cdSEric Blake } 571c1bb86cdSEric Blake #endif /* !defined(CONFIG_LINUX_AIO) */ 572c1bb86cdSEric Blake 573c1bb86cdSEric Blake s->has_discard = true; 574c1bb86cdSEric Blake s->has_write_zeroes = true; 575c1bb86cdSEric Blake if ((bs->open_flags & BDRV_O_NOCACHE) != 0) { 576c1bb86cdSEric Blake s->needs_alignment = true; 577c1bb86cdSEric Blake } 578c1bb86cdSEric Blake 579c1bb86cdSEric Blake if (fstat(s->fd, &st) < 0) { 580c1bb86cdSEric Blake ret = -errno; 581c1bb86cdSEric Blake error_setg_errno(errp, errno, "Could not stat file"); 582c1bb86cdSEric Blake goto fail; 583c1bb86cdSEric Blake } 584230ff739SJohn Snow 585230ff739SJohn Snow if (!device) { 586230ff739SJohn Snow if (S_ISBLK(st.st_mode)) { 587230ff739SJohn Snow warn_report("Opening a block device as a file using the '%s' " 588230ff739SJohn Snow "driver is deprecated", bs->drv->format_name); 589230ff739SJohn Snow } else if (S_ISCHR(st.st_mode)) { 590230ff739SJohn Snow warn_report("Opening a character device as a file using the '%s' " 591230ff739SJohn Snow "driver is deprecated", bs->drv->format_name); 592230ff739SJohn Snow } else if (!S_ISREG(st.st_mode)) { 593230ff739SJohn Snow error_setg(errp, "A regular file was expected by the '%s' driver, " 594230ff739SJohn Snow "but something else was given", bs->drv->format_name); 595230ff739SJohn Snow ret = -EINVAL; 596230ff739SJohn Snow goto fail; 597230ff739SJohn Snow } else { 598c1bb86cdSEric Blake s->discard_zeroes = true; 599c1bb86cdSEric Blake s->has_fallocate = true; 600c1bb86cdSEric Blake } 601230ff739SJohn Snow } else { 602230ff739SJohn Snow if (!(S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) { 603230ff739SJohn Snow error_setg(errp, "'%s' driver expects either " 604230ff739SJohn Snow "a character or block device", bs->drv->format_name); 605230ff739SJohn Snow ret = -EINVAL; 606230ff739SJohn Snow goto fail; 607230ff739SJohn Snow } 608230ff739SJohn Snow } 609230ff739SJohn Snow 610c1bb86cdSEric Blake if (S_ISBLK(st.st_mode)) { 611c1bb86cdSEric Blake #ifdef BLKDISCARDZEROES 612c1bb86cdSEric Blake unsigned int arg; 613c1bb86cdSEric Blake if (ioctl(s->fd, BLKDISCARDZEROES, &arg) == 0 && arg) { 614c1bb86cdSEric Blake s->discard_zeroes = true; 615c1bb86cdSEric Blake } 616c1bb86cdSEric Blake #endif 617c1bb86cdSEric Blake #ifdef __linux__ 618c1bb86cdSEric Blake /* On Linux 3.10, BLKDISCARD leaves stale data in the page cache. Do 619c1bb86cdSEric Blake * not rely on the contents of discarded blocks unless using O_DIRECT. 620c1bb86cdSEric Blake * Same for BLKZEROOUT. 621c1bb86cdSEric Blake */ 622c1bb86cdSEric Blake if (!(bs->open_flags & BDRV_O_NOCACHE)) { 623c1bb86cdSEric Blake s->discard_zeroes = false; 624c1bb86cdSEric Blake s->has_write_zeroes = false; 625c1bb86cdSEric Blake } 626c1bb86cdSEric Blake #endif 627c1bb86cdSEric Blake } 628c1bb86cdSEric Blake #ifdef __FreeBSD__ 629c1bb86cdSEric Blake if (S_ISCHR(st.st_mode)) { 630c1bb86cdSEric Blake /* 631c1bb86cdSEric Blake * The file is a char device (disk), which on FreeBSD isn't behind 632c1bb86cdSEric Blake * a pager, so force all requests to be aligned. This is needed 633c1bb86cdSEric Blake * so QEMU makes sure all IO operations on the device are aligned 634c1bb86cdSEric Blake * to sector size, or else FreeBSD will reject them with EINVAL. 635c1bb86cdSEric Blake */ 636c1bb86cdSEric Blake s->needs_alignment = true; 637c1bb86cdSEric Blake } 638c1bb86cdSEric Blake #endif 639c1bb86cdSEric Blake 640c1bb86cdSEric Blake #ifdef CONFIG_XFS 641c1bb86cdSEric Blake if (platform_test_xfs_fd(s->fd)) { 642c1bb86cdSEric Blake s->is_xfs = true; 643c1bb86cdSEric Blake } 644c1bb86cdSEric Blake #endif 645c1bb86cdSEric Blake 64634fa110eSKevin Wolf bs->supported_zero_flags = BDRV_REQ_MAY_UNMAP; 647c1bb86cdSEric Blake ret = 0; 648c1bb86cdSEric Blake fail: 649c1bb86cdSEric Blake if (filename && (bdrv_flags & BDRV_O_TEMPORARY)) { 650c1bb86cdSEric Blake unlink(filename); 651c1bb86cdSEric Blake } 652c1bb86cdSEric Blake qemu_opts_del(opts); 653c1bb86cdSEric Blake return ret; 654c1bb86cdSEric Blake } 655c1bb86cdSEric Blake 656c1bb86cdSEric Blake static int raw_open(BlockDriverState *bs, QDict *options, int flags, 657c1bb86cdSEric Blake Error **errp) 658c1bb86cdSEric Blake { 659c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 660c1bb86cdSEric Blake 661c1bb86cdSEric Blake s->type = FTYPE_FILE; 662230ff739SJohn Snow return raw_open_common(bs, options, flags, 0, false, errp); 663c1bb86cdSEric Blake } 664c1bb86cdSEric Blake 665244a5668SFam Zheng typedef enum { 666244a5668SFam Zheng RAW_PL_PREPARE, 667244a5668SFam Zheng RAW_PL_COMMIT, 668244a5668SFam Zheng RAW_PL_ABORT, 669244a5668SFam Zheng } RawPermLockOp; 670244a5668SFam Zheng 671244a5668SFam Zheng #define PERM_FOREACH(i) \ 672244a5668SFam Zheng for ((i) = 0; (1ULL << (i)) <= BLK_PERM_ALL; i++) 673244a5668SFam Zheng 674244a5668SFam Zheng /* Lock bytes indicated by @perm_lock_bits and @shared_perm_lock_bits in the 675244a5668SFam Zheng * file; if @unlock == true, also unlock the unneeded bytes. 676244a5668SFam Zheng * @shared_perm_lock_bits is the mask of all permissions that are NOT shared. 677244a5668SFam Zheng */ 6782996ffadSFam Zheng static int raw_apply_lock_bytes(BDRVRawState *s, int fd, 679244a5668SFam Zheng uint64_t perm_lock_bits, 680244a5668SFam Zheng uint64_t shared_perm_lock_bits, 681244a5668SFam Zheng bool unlock, Error **errp) 682244a5668SFam Zheng { 683244a5668SFam Zheng int ret; 684244a5668SFam Zheng int i; 6852996ffadSFam Zheng uint64_t locked_perm, locked_shared_perm; 6862996ffadSFam Zheng 6872996ffadSFam Zheng if (s) { 6882996ffadSFam Zheng locked_perm = s->locked_perm; 6892996ffadSFam Zheng locked_shared_perm = s->locked_shared_perm; 6902996ffadSFam Zheng } else { 6912996ffadSFam Zheng /* 6922996ffadSFam Zheng * We don't have the previous bits, just lock/unlock for each of the 6932996ffadSFam Zheng * requested bits. 6942996ffadSFam Zheng */ 6952996ffadSFam Zheng if (unlock) { 6962996ffadSFam Zheng locked_perm = BLK_PERM_ALL; 6972996ffadSFam Zheng locked_shared_perm = BLK_PERM_ALL; 6982996ffadSFam Zheng } else { 6992996ffadSFam Zheng locked_perm = 0; 7002996ffadSFam Zheng locked_shared_perm = 0; 7012996ffadSFam Zheng } 7022996ffadSFam Zheng } 703244a5668SFam Zheng 704244a5668SFam Zheng PERM_FOREACH(i) { 705244a5668SFam Zheng int off = RAW_LOCK_PERM_BASE + i; 7062996ffadSFam Zheng uint64_t bit = (1ULL << i); 7072996ffadSFam Zheng if ((perm_lock_bits & bit) && !(locked_perm & bit)) { 708d0a96155SMax Reitz ret = qemu_lock_fd(fd, off, 1, false); 709244a5668SFam Zheng if (ret) { 710244a5668SFam Zheng error_setg(errp, "Failed to lock byte %d", off); 711244a5668SFam Zheng return ret; 7122996ffadSFam Zheng } else if (s) { 7132996ffadSFam Zheng s->locked_perm |= bit; 714244a5668SFam Zheng } 7152996ffadSFam Zheng } else if (unlock && (locked_perm & bit) && !(perm_lock_bits & bit)) { 716d0a96155SMax Reitz ret = qemu_unlock_fd(fd, off, 1); 717244a5668SFam Zheng if (ret) { 718244a5668SFam Zheng error_setg(errp, "Failed to unlock byte %d", off); 719244a5668SFam Zheng return ret; 7202996ffadSFam Zheng } else if (s) { 7212996ffadSFam Zheng s->locked_perm &= ~bit; 722244a5668SFam Zheng } 723244a5668SFam Zheng } 724244a5668SFam Zheng } 725244a5668SFam Zheng PERM_FOREACH(i) { 726244a5668SFam Zheng int off = RAW_LOCK_SHARED_BASE + i; 7272996ffadSFam Zheng uint64_t bit = (1ULL << i); 7282996ffadSFam Zheng if ((shared_perm_lock_bits & bit) && !(locked_shared_perm & bit)) { 729d0a96155SMax Reitz ret = qemu_lock_fd(fd, off, 1, false); 730244a5668SFam Zheng if (ret) { 731244a5668SFam Zheng error_setg(errp, "Failed to lock byte %d", off); 732244a5668SFam Zheng return ret; 7332996ffadSFam Zheng } else if (s) { 7342996ffadSFam Zheng s->locked_shared_perm |= bit; 735244a5668SFam Zheng } 7362996ffadSFam Zheng } else if (unlock && (locked_shared_perm & bit) && 7372996ffadSFam Zheng !(shared_perm_lock_bits & bit)) { 738d0a96155SMax Reitz ret = qemu_unlock_fd(fd, off, 1); 739244a5668SFam Zheng if (ret) { 740244a5668SFam Zheng error_setg(errp, "Failed to unlock byte %d", off); 741244a5668SFam Zheng return ret; 7422996ffadSFam Zheng } else if (s) { 7432996ffadSFam Zheng s->locked_shared_perm &= ~bit; 744244a5668SFam Zheng } 745244a5668SFam Zheng } 746244a5668SFam Zheng } 747244a5668SFam Zheng return 0; 748244a5668SFam Zheng } 749244a5668SFam Zheng 750244a5668SFam Zheng /* Check "unshared" bytes implied by @perm and ~@shared_perm in the file. */ 751d0a96155SMax Reitz static int raw_check_lock_bytes(int fd, uint64_t perm, uint64_t shared_perm, 752244a5668SFam Zheng Error **errp) 753244a5668SFam Zheng { 754244a5668SFam Zheng int ret; 755244a5668SFam Zheng int i; 756244a5668SFam Zheng 757244a5668SFam Zheng PERM_FOREACH(i) { 758244a5668SFam Zheng int off = RAW_LOCK_SHARED_BASE + i; 759244a5668SFam Zheng uint64_t p = 1ULL << i; 760244a5668SFam Zheng if (perm & p) { 761d0a96155SMax Reitz ret = qemu_lock_fd_test(fd, off, 1, true); 762244a5668SFam Zheng if (ret) { 763244a5668SFam Zheng char *perm_name = bdrv_perm_names(p); 764244a5668SFam Zheng error_setg(errp, 765244a5668SFam Zheng "Failed to get \"%s\" lock", 766244a5668SFam Zheng perm_name); 767244a5668SFam Zheng g_free(perm_name); 768244a5668SFam Zheng return ret; 769244a5668SFam Zheng } 770244a5668SFam Zheng } 771244a5668SFam Zheng } 772244a5668SFam Zheng PERM_FOREACH(i) { 773244a5668SFam Zheng int off = RAW_LOCK_PERM_BASE + i; 774244a5668SFam Zheng uint64_t p = 1ULL << i; 775244a5668SFam Zheng if (!(shared_perm & p)) { 776d0a96155SMax Reitz ret = qemu_lock_fd_test(fd, off, 1, true); 777244a5668SFam Zheng if (ret) { 778244a5668SFam Zheng char *perm_name = bdrv_perm_names(p); 779244a5668SFam Zheng error_setg(errp, 780244a5668SFam Zheng "Failed to get shared \"%s\" lock", 781244a5668SFam Zheng perm_name); 782244a5668SFam Zheng g_free(perm_name); 783244a5668SFam Zheng return ret; 784244a5668SFam Zheng } 785244a5668SFam Zheng } 786244a5668SFam Zheng } 787244a5668SFam Zheng return 0; 788244a5668SFam Zheng } 789244a5668SFam Zheng 790244a5668SFam Zheng static int raw_handle_perm_lock(BlockDriverState *bs, 791244a5668SFam Zheng RawPermLockOp op, 792244a5668SFam Zheng uint64_t new_perm, uint64_t new_shared, 793244a5668SFam Zheng Error **errp) 794244a5668SFam Zheng { 795244a5668SFam Zheng BDRVRawState *s = bs->opaque; 796244a5668SFam Zheng int ret = 0; 797244a5668SFam Zheng Error *local_err = NULL; 798244a5668SFam Zheng 799244a5668SFam Zheng if (!s->use_lock) { 800244a5668SFam Zheng return 0; 801244a5668SFam Zheng } 802244a5668SFam Zheng 803244a5668SFam Zheng if (bdrv_get_flags(bs) & BDRV_O_INACTIVE) { 804244a5668SFam Zheng return 0; 805244a5668SFam Zheng } 806244a5668SFam Zheng 807244a5668SFam Zheng switch (op) { 808244a5668SFam Zheng case RAW_PL_PREPARE: 809f2e3af29SFam Zheng ret = raw_apply_lock_bytes(s, s->fd, s->perm | new_perm, 810244a5668SFam Zheng ~s->shared_perm | ~new_shared, 811244a5668SFam Zheng false, errp); 812244a5668SFam Zheng if (!ret) { 813f2e3af29SFam Zheng ret = raw_check_lock_bytes(s->fd, new_perm, new_shared, errp); 814244a5668SFam Zheng if (!ret) { 815244a5668SFam Zheng return 0; 816244a5668SFam Zheng } 817b857431dSFam Zheng error_append_hint(errp, 818b857431dSFam Zheng "Is another process using the image [%s]?\n", 819b857431dSFam Zheng bs->filename); 820244a5668SFam Zheng } 821244a5668SFam Zheng op = RAW_PL_ABORT; 822244a5668SFam Zheng /* fall through to unlock bytes. */ 823244a5668SFam Zheng case RAW_PL_ABORT: 824f2e3af29SFam Zheng raw_apply_lock_bytes(s, s->fd, s->perm, ~s->shared_perm, 825d0a96155SMax Reitz true, &local_err); 826244a5668SFam Zheng if (local_err) { 827244a5668SFam Zheng /* Theoretically the above call only unlocks bytes and it cannot 828244a5668SFam Zheng * fail. Something weird happened, report it. 829244a5668SFam Zheng */ 830db0754dfSFam Zheng warn_report_err(local_err); 831244a5668SFam Zheng } 832244a5668SFam Zheng break; 833244a5668SFam Zheng case RAW_PL_COMMIT: 834f2e3af29SFam Zheng raw_apply_lock_bytes(s, s->fd, new_perm, ~new_shared, 835d0a96155SMax Reitz true, &local_err); 836244a5668SFam Zheng if (local_err) { 837244a5668SFam Zheng /* Theoretically the above call only unlocks bytes and it cannot 838244a5668SFam Zheng * fail. Something weird happened, report it. 839244a5668SFam Zheng */ 840db0754dfSFam Zheng warn_report_err(local_err); 841244a5668SFam Zheng } 842244a5668SFam Zheng break; 843244a5668SFam Zheng } 844244a5668SFam Zheng return ret; 845244a5668SFam Zheng } 846244a5668SFam Zheng 8475cec2870SKevin Wolf static int raw_reconfigure_getfd(BlockDriverState *bs, int flags, 8485cec2870SKevin Wolf int *open_flags, Error **errp) 8495cec2870SKevin Wolf { 8505cec2870SKevin Wolf BDRVRawState *s = bs->opaque; 8515cec2870SKevin Wolf int fd = -1; 8525cec2870SKevin Wolf int ret; 8535cec2870SKevin Wolf int fcntl_flags = O_APPEND | O_NONBLOCK; 8545cec2870SKevin Wolf #ifdef O_NOATIME 8555cec2870SKevin Wolf fcntl_flags |= O_NOATIME; 8565cec2870SKevin Wolf #endif 8575cec2870SKevin Wolf 8585cec2870SKevin Wolf *open_flags = 0; 8595cec2870SKevin Wolf if (s->type == FTYPE_CD) { 8605cec2870SKevin Wolf *open_flags |= O_NONBLOCK; 8615cec2870SKevin Wolf } 8625cec2870SKevin Wolf 8635cec2870SKevin Wolf raw_parse_flags(flags, open_flags); 8645cec2870SKevin Wolf 8655cec2870SKevin Wolf #ifdef O_ASYNC 8665cec2870SKevin Wolf /* Not all operating systems have O_ASYNC, and those that don't 8675cec2870SKevin Wolf * will not let us track the state into rs->open_flags (typically 8685cec2870SKevin Wolf * you achieve the same effect with an ioctl, for example I_SETSIG 8695cec2870SKevin Wolf * on Solaris). But we do not use O_ASYNC, so that's fine. 8705cec2870SKevin Wolf */ 8715cec2870SKevin Wolf assert((s->open_flags & O_ASYNC) == 0); 8725cec2870SKevin Wolf #endif 8735cec2870SKevin Wolf 8745cec2870SKevin Wolf if ((*open_flags & ~fcntl_flags) == (s->open_flags & ~fcntl_flags)) { 8755cec2870SKevin Wolf /* dup the original fd */ 8765cec2870SKevin Wolf fd = qemu_dup(s->fd); 8775cec2870SKevin Wolf if (fd >= 0) { 8785cec2870SKevin Wolf ret = fcntl_setfl(fd, *open_flags); 8795cec2870SKevin Wolf if (ret) { 8805cec2870SKevin Wolf qemu_close(fd); 8815cec2870SKevin Wolf fd = -1; 8825cec2870SKevin Wolf } 8835cec2870SKevin Wolf } 8845cec2870SKevin Wolf } 8855cec2870SKevin Wolf 8865cec2870SKevin Wolf /* If we cannot use fcntl, or fcntl failed, fall back to qemu_open() */ 8875cec2870SKevin Wolf if (fd == -1) { 8885cec2870SKevin Wolf const char *normalized_filename = bs->filename; 8895cec2870SKevin Wolf ret = raw_normalize_devicepath(&normalized_filename, errp); 8905cec2870SKevin Wolf if (ret >= 0) { 8915cec2870SKevin Wolf assert(!(*open_flags & O_CREAT)); 8925cec2870SKevin Wolf fd = qemu_open(normalized_filename, *open_flags); 8935cec2870SKevin Wolf if (fd == -1) { 8945cec2870SKevin Wolf error_setg_errno(errp, errno, "Could not reopen file"); 8955cec2870SKevin Wolf return -1; 8965cec2870SKevin Wolf } 8975cec2870SKevin Wolf } 8985cec2870SKevin Wolf } 8995cec2870SKevin Wolf 9005cec2870SKevin Wolf return fd; 9015cec2870SKevin Wolf } 9025cec2870SKevin Wolf 903c1bb86cdSEric Blake static int raw_reopen_prepare(BDRVReopenState *state, 904c1bb86cdSEric Blake BlockReopenQueue *queue, Error **errp) 905c1bb86cdSEric Blake { 906c1bb86cdSEric Blake BDRVRawState *s; 907c1bb86cdSEric Blake BDRVRawReopenState *rs; 90831be8a2aSStefan Hajnoczi QemuOpts *opts; 909c1bb86cdSEric Blake int ret = 0; 910c1bb86cdSEric Blake Error *local_err = NULL; 911c1bb86cdSEric Blake 912c1bb86cdSEric Blake assert(state != NULL); 913c1bb86cdSEric Blake assert(state->bs != NULL); 914c1bb86cdSEric Blake 915c1bb86cdSEric Blake s = state->bs->opaque; 916c1bb86cdSEric Blake 917c1bb86cdSEric Blake state->opaque = g_new0(BDRVRawReopenState, 1); 918c1bb86cdSEric Blake rs = state->opaque; 91931be8a2aSStefan Hajnoczi 92031be8a2aSStefan Hajnoczi /* Handle options changes */ 92131be8a2aSStefan Hajnoczi opts = qemu_opts_create(&raw_runtime_opts, NULL, 0, &error_abort); 92231be8a2aSStefan Hajnoczi qemu_opts_absorb_qdict(opts, state->options, &local_err); 92331be8a2aSStefan Hajnoczi if (local_err) { 92431be8a2aSStefan Hajnoczi error_propagate(errp, local_err); 92531be8a2aSStefan Hajnoczi ret = -EINVAL; 92631be8a2aSStefan Hajnoczi goto out; 92731be8a2aSStefan Hajnoczi } 92831be8a2aSStefan Hajnoczi 9298d324575SAlberto Garcia rs->check_cache_dropped = 9308d324575SAlberto Garcia qemu_opt_get_bool_del(opts, "x-check-cache-dropped", false); 9318d324575SAlberto Garcia 9328d324575SAlberto Garcia /* This driver's reopen function doesn't currently allow changing 9338d324575SAlberto Garcia * other options, so let's put them back in the original QDict and 9348d324575SAlberto Garcia * bdrv_reopen_prepare() will detect changes and complain. */ 9358d324575SAlberto Garcia qemu_opts_to_qdict(opts, state->options); 936c1bb86cdSEric Blake 9375cec2870SKevin Wolf rs->fd = raw_reconfigure_getfd(state->bs, state->flags, &rs->open_flags, 9385cec2870SKevin Wolf &local_err); 9395cec2870SKevin Wolf if (local_err) { 9405cec2870SKevin Wolf error_propagate(errp, local_err); 941c1bb86cdSEric Blake ret = -1; 9425cec2870SKevin Wolf goto out; 943c1bb86cdSEric Blake } 944c1bb86cdSEric Blake 945c1bb86cdSEric Blake /* Fail already reopen_prepare() if we can't get a working O_DIRECT 946c1bb86cdSEric Blake * alignment with the new fd. */ 947c1bb86cdSEric Blake if (rs->fd != -1) { 948c1bb86cdSEric Blake raw_probe_alignment(state->bs, rs->fd, &local_err); 949c1bb86cdSEric Blake if (local_err) { 950c1bb86cdSEric Blake qemu_close(rs->fd); 951c1bb86cdSEric Blake rs->fd = -1; 952c1bb86cdSEric Blake error_propagate(errp, local_err); 953c1bb86cdSEric Blake ret = -EINVAL; 954c1bb86cdSEric Blake } 955c1bb86cdSEric Blake } 956c1bb86cdSEric Blake 957*e0c9cf3aSKevin Wolf s->reopen_state = state; 95831be8a2aSStefan Hajnoczi out: 95931be8a2aSStefan Hajnoczi qemu_opts_del(opts); 960c1bb86cdSEric Blake return ret; 961c1bb86cdSEric Blake } 962c1bb86cdSEric Blake 963c1bb86cdSEric Blake static void raw_reopen_commit(BDRVReopenState *state) 964c1bb86cdSEric Blake { 965c1bb86cdSEric Blake BDRVRawReopenState *rs = state->opaque; 966c1bb86cdSEric Blake BDRVRawState *s = state->bs->opaque; 967f2e3af29SFam Zheng Error *local_err = NULL; 968c1bb86cdSEric Blake 96931be8a2aSStefan Hajnoczi s->check_cache_dropped = rs->check_cache_dropped; 970c1bb86cdSEric Blake s->open_flags = rs->open_flags; 971c1bb86cdSEric Blake 972f2e3af29SFam Zheng /* Copy locks to the new fd before closing the old one. */ 973f2e3af29SFam Zheng raw_apply_lock_bytes(NULL, rs->fd, s->locked_perm, 974577a1339SMax Reitz s->locked_shared_perm, false, &local_err); 975f2e3af29SFam Zheng if (local_err) { 976f2e3af29SFam Zheng /* shouldn't fail in a sane host, but report it just in case. */ 977f2e3af29SFam Zheng error_report_err(local_err); 978f2e3af29SFam Zheng } 979c1bb86cdSEric Blake qemu_close(s->fd); 980c1bb86cdSEric Blake s->fd = rs->fd; 981c1bb86cdSEric Blake 982c1bb86cdSEric Blake g_free(state->opaque); 983c1bb86cdSEric Blake state->opaque = NULL; 984*e0c9cf3aSKevin Wolf 985*e0c9cf3aSKevin Wolf assert(s->reopen_state == state); 986*e0c9cf3aSKevin Wolf s->reopen_state = NULL; 987c1bb86cdSEric Blake } 988c1bb86cdSEric Blake 989c1bb86cdSEric Blake 990c1bb86cdSEric Blake static void raw_reopen_abort(BDRVReopenState *state) 991c1bb86cdSEric Blake { 992c1bb86cdSEric Blake BDRVRawReopenState *rs = state->opaque; 993*e0c9cf3aSKevin Wolf BDRVRawState *s = state->bs->opaque; 994c1bb86cdSEric Blake 995c1bb86cdSEric Blake /* nothing to do if NULL, we didn't get far enough */ 996c1bb86cdSEric Blake if (rs == NULL) { 997c1bb86cdSEric Blake return; 998c1bb86cdSEric Blake } 999c1bb86cdSEric Blake 1000c1bb86cdSEric Blake if (rs->fd >= 0) { 1001c1bb86cdSEric Blake qemu_close(rs->fd); 1002c1bb86cdSEric Blake rs->fd = -1; 1003c1bb86cdSEric Blake } 1004c1bb86cdSEric Blake g_free(state->opaque); 1005c1bb86cdSEric Blake state->opaque = NULL; 1006*e0c9cf3aSKevin Wolf 1007*e0c9cf3aSKevin Wolf assert(s->reopen_state == state); 1008*e0c9cf3aSKevin Wolf s->reopen_state = NULL; 1009c1bb86cdSEric Blake } 1010c1bb86cdSEric Blake 101148265250SEric Farman static int hdev_get_max_transfer_length(BlockDriverState *bs, int fd) 1012c1bb86cdSEric Blake { 1013c1bb86cdSEric Blake #ifdef BLKSECTGET 101448265250SEric Farman int max_bytes = 0; 101548265250SEric Farman short max_sectors = 0; 101648265250SEric Farman if (bs->sg && ioctl(fd, BLKSECTGET, &max_bytes) == 0) { 101748265250SEric Farman return max_bytes; 101848265250SEric Farman } else if (!bs->sg && ioctl(fd, BLKSECTGET, &max_sectors) == 0) { 101948265250SEric Farman return max_sectors << BDRV_SECTOR_BITS; 1020c1bb86cdSEric Blake } else { 1021c1bb86cdSEric Blake return -errno; 1022c1bb86cdSEric Blake } 1023c1bb86cdSEric Blake #else 1024c1bb86cdSEric Blake return -ENOSYS; 1025c1bb86cdSEric Blake #endif 1026c1bb86cdSEric Blake } 1027c1bb86cdSEric Blake 10289103f1ceSFam Zheng static int hdev_get_max_segments(const struct stat *st) 10299103f1ceSFam Zheng { 10309103f1ceSFam Zheng #ifdef CONFIG_LINUX 10319103f1ceSFam Zheng char buf[32]; 10329103f1ceSFam Zheng const char *end; 10339103f1ceSFam Zheng char *sysfspath; 10349103f1ceSFam Zheng int ret; 10359103f1ceSFam Zheng int fd = -1; 10369103f1ceSFam Zheng long max_segments; 10379103f1ceSFam Zheng 10389103f1ceSFam Zheng sysfspath = g_strdup_printf("/sys/dev/block/%u:%u/queue/max_segments", 10399103f1ceSFam Zheng major(st->st_rdev), minor(st->st_rdev)); 10409103f1ceSFam Zheng fd = open(sysfspath, O_RDONLY); 10419103f1ceSFam Zheng if (fd == -1) { 10429103f1ceSFam Zheng ret = -errno; 10439103f1ceSFam Zheng goto out; 10449103f1ceSFam Zheng } 10459103f1ceSFam Zheng do { 104669583490SStefan Hajnoczi ret = read(fd, buf, sizeof(buf) - 1); 10479103f1ceSFam Zheng } while (ret == -1 && errno == EINTR); 10489103f1ceSFam Zheng if (ret < 0) { 10499103f1ceSFam Zheng ret = -errno; 10509103f1ceSFam Zheng goto out; 10519103f1ceSFam Zheng } else if (ret == 0) { 10529103f1ceSFam Zheng ret = -EIO; 10539103f1ceSFam Zheng goto out; 10549103f1ceSFam Zheng } 10559103f1ceSFam Zheng buf[ret] = 0; 10569103f1ceSFam Zheng /* The file is ended with '\n', pass 'end' to accept that. */ 10579103f1ceSFam Zheng ret = qemu_strtol(buf, &end, 10, &max_segments); 10589103f1ceSFam Zheng if (ret == 0 && end && *end == '\n') { 10599103f1ceSFam Zheng ret = max_segments; 10609103f1ceSFam Zheng } 10619103f1ceSFam Zheng 10629103f1ceSFam Zheng out: 1063fed414dfSFam Zheng if (fd != -1) { 1064fed414dfSFam Zheng close(fd); 1065fed414dfSFam Zheng } 10669103f1ceSFam Zheng g_free(sysfspath); 10679103f1ceSFam Zheng return ret; 10689103f1ceSFam Zheng #else 10699103f1ceSFam Zheng return -ENOTSUP; 10709103f1ceSFam Zheng #endif 10719103f1ceSFam Zheng } 10729103f1ceSFam Zheng 1073c1bb86cdSEric Blake static void raw_refresh_limits(BlockDriverState *bs, Error **errp) 1074c1bb86cdSEric Blake { 1075c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 1076c1bb86cdSEric Blake struct stat st; 1077c1bb86cdSEric Blake 1078c1bb86cdSEric Blake if (!fstat(s->fd, &st)) { 1079c4c41a0aSEric Farman if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode)) { 108048265250SEric Farman int ret = hdev_get_max_transfer_length(bs, s->fd); 108148265250SEric Farman if (ret > 0 && ret <= BDRV_REQUEST_MAX_BYTES) { 108248265250SEric Farman bs->bl.max_transfer = pow2floor(ret); 1083c1bb86cdSEric Blake } 10849103f1ceSFam Zheng ret = hdev_get_max_segments(&st); 10859103f1ceSFam Zheng if (ret > 0) { 10869103f1ceSFam Zheng bs->bl.max_transfer = MIN(bs->bl.max_transfer, 10879103f1ceSFam Zheng ret * getpagesize()); 10889103f1ceSFam Zheng } 1089c1bb86cdSEric Blake } 1090c1bb86cdSEric Blake } 1091c1bb86cdSEric Blake 1092c1bb86cdSEric Blake raw_probe_alignment(bs, s->fd, errp); 1093c1bb86cdSEric Blake bs->bl.min_mem_alignment = s->buf_align; 1094c1bb86cdSEric Blake bs->bl.opt_mem_alignment = MAX(s->buf_align, getpagesize()); 1095c1bb86cdSEric Blake } 1096c1bb86cdSEric Blake 1097c1bb86cdSEric Blake static int check_for_dasd(int fd) 1098c1bb86cdSEric Blake { 1099c1bb86cdSEric Blake #ifdef BIODASDINFO2 1100c1bb86cdSEric Blake struct dasd_information2_t info = {0}; 1101c1bb86cdSEric Blake 1102c1bb86cdSEric Blake return ioctl(fd, BIODASDINFO2, &info); 1103c1bb86cdSEric Blake #else 1104c1bb86cdSEric Blake return -1; 1105c1bb86cdSEric Blake #endif 1106c1bb86cdSEric Blake } 1107c1bb86cdSEric Blake 1108c1bb86cdSEric Blake /** 1109c1bb86cdSEric Blake * Try to get @bs's logical and physical block size. 1110c1bb86cdSEric Blake * On success, store them in @bsz and return zero. 1111c1bb86cdSEric Blake * On failure, return negative errno. 1112c1bb86cdSEric Blake */ 1113c1bb86cdSEric Blake static int hdev_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz) 1114c1bb86cdSEric Blake { 1115c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 1116c1bb86cdSEric Blake int ret; 1117c1bb86cdSEric Blake 1118c1bb86cdSEric Blake /* If DASD, get blocksizes */ 1119c1bb86cdSEric Blake if (check_for_dasd(s->fd) < 0) { 1120c1bb86cdSEric Blake return -ENOTSUP; 1121c1bb86cdSEric Blake } 1122c1bb86cdSEric Blake ret = probe_logical_blocksize(s->fd, &bsz->log); 1123c1bb86cdSEric Blake if (ret < 0) { 1124c1bb86cdSEric Blake return ret; 1125c1bb86cdSEric Blake } 1126c1bb86cdSEric Blake return probe_physical_blocksize(s->fd, &bsz->phys); 1127c1bb86cdSEric Blake } 1128c1bb86cdSEric Blake 1129c1bb86cdSEric Blake /** 1130c1bb86cdSEric Blake * Try to get @bs's geometry: cyls, heads, sectors. 1131c1bb86cdSEric Blake * On success, store them in @geo and return 0. 1132c1bb86cdSEric Blake * On failure return -errno. 1133c1bb86cdSEric Blake * (Allows block driver to assign default geometry values that guest sees) 1134c1bb86cdSEric Blake */ 1135c1bb86cdSEric Blake #ifdef __linux__ 1136c1bb86cdSEric Blake static int hdev_probe_geometry(BlockDriverState *bs, HDGeometry *geo) 1137c1bb86cdSEric Blake { 1138c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 1139c1bb86cdSEric Blake struct hd_geometry ioctl_geo = {0}; 1140c1bb86cdSEric Blake 1141c1bb86cdSEric Blake /* If DASD, get its geometry */ 1142c1bb86cdSEric Blake if (check_for_dasd(s->fd) < 0) { 1143c1bb86cdSEric Blake return -ENOTSUP; 1144c1bb86cdSEric Blake } 1145c1bb86cdSEric Blake if (ioctl(s->fd, HDIO_GETGEO, &ioctl_geo) < 0) { 1146c1bb86cdSEric Blake return -errno; 1147c1bb86cdSEric Blake } 1148c1bb86cdSEric Blake /* HDIO_GETGEO may return success even though geo contains zeros 1149c1bb86cdSEric Blake (e.g. certain multipath setups) */ 1150c1bb86cdSEric Blake if (!ioctl_geo.heads || !ioctl_geo.sectors || !ioctl_geo.cylinders) { 1151c1bb86cdSEric Blake return -ENOTSUP; 1152c1bb86cdSEric Blake } 1153c1bb86cdSEric Blake /* Do not return a geometry for partition */ 1154c1bb86cdSEric Blake if (ioctl_geo.start != 0) { 1155c1bb86cdSEric Blake return -ENOTSUP; 1156c1bb86cdSEric Blake } 1157c1bb86cdSEric Blake geo->heads = ioctl_geo.heads; 1158c1bb86cdSEric Blake geo->sectors = ioctl_geo.sectors; 1159c1bb86cdSEric Blake geo->cylinders = ioctl_geo.cylinders; 1160c1bb86cdSEric Blake 1161c1bb86cdSEric Blake return 0; 1162c1bb86cdSEric Blake } 1163c1bb86cdSEric Blake #else /* __linux__ */ 1164c1bb86cdSEric Blake static int hdev_probe_geometry(BlockDriverState *bs, HDGeometry *geo) 1165c1bb86cdSEric Blake { 1166c1bb86cdSEric Blake return -ENOTSUP; 1167c1bb86cdSEric Blake } 1168c1bb86cdSEric Blake #endif 1169c1bb86cdSEric Blake 117003425671SKevin Wolf #if defined(__linux__) 117103425671SKevin Wolf static int handle_aiocb_ioctl(void *opaque) 1172c1bb86cdSEric Blake { 117303425671SKevin Wolf RawPosixAIOData *aiocb = opaque; 1174c1bb86cdSEric Blake int ret; 1175c1bb86cdSEric Blake 1176d57c44d0SKevin Wolf ret = ioctl(aiocb->aio_fildes, aiocb->ioctl.cmd, aiocb->ioctl.buf); 1177c1bb86cdSEric Blake if (ret == -1) { 1178c1bb86cdSEric Blake return -errno; 1179c1bb86cdSEric Blake } 1180c1bb86cdSEric Blake 1181c1bb86cdSEric Blake return 0; 1182c1bb86cdSEric Blake } 118303425671SKevin Wolf #endif /* linux */ 1184c1bb86cdSEric Blake 118506dc9bd5SKevin Wolf static int handle_aiocb_flush(void *opaque) 1186c1bb86cdSEric Blake { 118706dc9bd5SKevin Wolf RawPosixAIOData *aiocb = opaque; 1188e5bcf967SKevin Wolf BDRVRawState *s = aiocb->bs->opaque; 1189c1bb86cdSEric Blake int ret; 1190c1bb86cdSEric Blake 1191e5bcf967SKevin Wolf if (s->page_cache_inconsistent) { 1192e5bcf967SKevin Wolf return -EIO; 1193e5bcf967SKevin Wolf } 1194e5bcf967SKevin Wolf 1195c1bb86cdSEric Blake ret = qemu_fdatasync(aiocb->aio_fildes); 1196c1bb86cdSEric Blake if (ret == -1) { 1197e5bcf967SKevin Wolf /* There is no clear definition of the semantics of a failing fsync(), 1198e5bcf967SKevin Wolf * so we may have to assume the worst. The sad truth is that this 1199e5bcf967SKevin Wolf * assumption is correct for Linux. Some pages are now probably marked 1200e5bcf967SKevin Wolf * clean in the page cache even though they are inconsistent with the 1201e5bcf967SKevin Wolf * on-disk contents. The next fdatasync() call would succeed, but no 1202e5bcf967SKevin Wolf * further writeback attempt will be made. We can't get back to a state 1203e5bcf967SKevin Wolf * in which we know what is on disk (we would have to rewrite 1204e5bcf967SKevin Wolf * everything that was touched since the last fdatasync() at least), so 1205e5bcf967SKevin Wolf * make bdrv_flush() fail permanently. Given that the behaviour isn't 1206e5bcf967SKevin Wolf * really defined, I have little hope that other OSes are doing better. 1207e5bcf967SKevin Wolf * 1208e5bcf967SKevin Wolf * Obviously, this doesn't affect O_DIRECT, which bypasses the page 1209e5bcf967SKevin Wolf * cache. */ 1210e5bcf967SKevin Wolf if ((s->open_flags & O_DIRECT) == 0) { 1211e5bcf967SKevin Wolf s->page_cache_inconsistent = true; 1212e5bcf967SKevin Wolf } 1213c1bb86cdSEric Blake return -errno; 1214c1bb86cdSEric Blake } 1215c1bb86cdSEric Blake return 0; 1216c1bb86cdSEric Blake } 1217c1bb86cdSEric Blake 1218c1bb86cdSEric Blake #ifdef CONFIG_PREADV 1219c1bb86cdSEric Blake 1220c1bb86cdSEric Blake static bool preadv_present = true; 1221c1bb86cdSEric Blake 1222c1bb86cdSEric Blake static ssize_t 1223c1bb86cdSEric Blake qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset) 1224c1bb86cdSEric Blake { 1225c1bb86cdSEric Blake return preadv(fd, iov, nr_iov, offset); 1226c1bb86cdSEric Blake } 1227c1bb86cdSEric Blake 1228c1bb86cdSEric Blake static ssize_t 1229c1bb86cdSEric Blake qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset) 1230c1bb86cdSEric Blake { 1231c1bb86cdSEric Blake return pwritev(fd, iov, nr_iov, offset); 1232c1bb86cdSEric Blake } 1233c1bb86cdSEric Blake 1234c1bb86cdSEric Blake #else 1235c1bb86cdSEric Blake 1236c1bb86cdSEric Blake static bool preadv_present = false; 1237c1bb86cdSEric Blake 1238c1bb86cdSEric Blake static ssize_t 1239c1bb86cdSEric Blake qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset) 1240c1bb86cdSEric Blake { 1241c1bb86cdSEric Blake return -ENOSYS; 1242c1bb86cdSEric Blake } 1243c1bb86cdSEric Blake 1244c1bb86cdSEric Blake static ssize_t 1245c1bb86cdSEric Blake qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset) 1246c1bb86cdSEric Blake { 1247c1bb86cdSEric Blake return -ENOSYS; 1248c1bb86cdSEric Blake } 1249c1bb86cdSEric Blake 1250c1bb86cdSEric Blake #endif 1251c1bb86cdSEric Blake 1252c1bb86cdSEric Blake static ssize_t handle_aiocb_rw_vector(RawPosixAIOData *aiocb) 1253c1bb86cdSEric Blake { 1254c1bb86cdSEric Blake ssize_t len; 1255c1bb86cdSEric Blake 1256c1bb86cdSEric Blake do { 1257c1bb86cdSEric Blake if (aiocb->aio_type & QEMU_AIO_WRITE) 1258c1bb86cdSEric Blake len = qemu_pwritev(aiocb->aio_fildes, 1259d57c44d0SKevin Wolf aiocb->io.iov, 1260d57c44d0SKevin Wolf aiocb->io.niov, 1261c1bb86cdSEric Blake aiocb->aio_offset); 1262c1bb86cdSEric Blake else 1263c1bb86cdSEric Blake len = qemu_preadv(aiocb->aio_fildes, 1264d57c44d0SKevin Wolf aiocb->io.iov, 1265d57c44d0SKevin Wolf aiocb->io.niov, 1266c1bb86cdSEric Blake aiocb->aio_offset); 1267c1bb86cdSEric Blake } while (len == -1 && errno == EINTR); 1268c1bb86cdSEric Blake 1269c1bb86cdSEric Blake if (len == -1) { 1270c1bb86cdSEric Blake return -errno; 1271c1bb86cdSEric Blake } 1272c1bb86cdSEric Blake return len; 1273c1bb86cdSEric Blake } 1274c1bb86cdSEric Blake 1275c1bb86cdSEric Blake /* 1276c1bb86cdSEric Blake * Read/writes the data to/from a given linear buffer. 1277c1bb86cdSEric Blake * 1278c1bb86cdSEric Blake * Returns the number of bytes handles or -errno in case of an error. Short 1279c1bb86cdSEric Blake * reads are only returned if the end of the file is reached. 1280c1bb86cdSEric Blake */ 1281c1bb86cdSEric Blake static ssize_t handle_aiocb_rw_linear(RawPosixAIOData *aiocb, char *buf) 1282c1bb86cdSEric Blake { 1283c1bb86cdSEric Blake ssize_t offset = 0; 1284c1bb86cdSEric Blake ssize_t len; 1285c1bb86cdSEric Blake 1286c1bb86cdSEric Blake while (offset < aiocb->aio_nbytes) { 1287c1bb86cdSEric Blake if (aiocb->aio_type & QEMU_AIO_WRITE) { 1288c1bb86cdSEric Blake len = pwrite(aiocb->aio_fildes, 1289c1bb86cdSEric Blake (const char *)buf + offset, 1290c1bb86cdSEric Blake aiocb->aio_nbytes - offset, 1291c1bb86cdSEric Blake aiocb->aio_offset + offset); 1292c1bb86cdSEric Blake } else { 1293c1bb86cdSEric Blake len = pread(aiocb->aio_fildes, 1294c1bb86cdSEric Blake buf + offset, 1295c1bb86cdSEric Blake aiocb->aio_nbytes - offset, 1296c1bb86cdSEric Blake aiocb->aio_offset + offset); 1297c1bb86cdSEric Blake } 1298c1bb86cdSEric Blake if (len == -1 && errno == EINTR) { 1299c1bb86cdSEric Blake continue; 1300c1bb86cdSEric Blake } else if (len == -1 && errno == EINVAL && 1301c1bb86cdSEric Blake (aiocb->bs->open_flags & BDRV_O_NOCACHE) && 1302c1bb86cdSEric Blake !(aiocb->aio_type & QEMU_AIO_WRITE) && 1303c1bb86cdSEric Blake offset > 0) { 1304c1bb86cdSEric Blake /* O_DIRECT pread() may fail with EINVAL when offset is unaligned 1305c1bb86cdSEric Blake * after a short read. Assume that O_DIRECT short reads only occur 1306c1bb86cdSEric Blake * at EOF. Therefore this is a short read, not an I/O error. 1307c1bb86cdSEric Blake */ 1308c1bb86cdSEric Blake break; 1309c1bb86cdSEric Blake } else if (len == -1) { 1310c1bb86cdSEric Blake offset = -errno; 1311c1bb86cdSEric Blake break; 1312c1bb86cdSEric Blake } else if (len == 0) { 1313c1bb86cdSEric Blake break; 1314c1bb86cdSEric Blake } 1315c1bb86cdSEric Blake offset += len; 1316c1bb86cdSEric Blake } 1317c1bb86cdSEric Blake 1318c1bb86cdSEric Blake return offset; 1319c1bb86cdSEric Blake } 1320c1bb86cdSEric Blake 1321999e6b69SKevin Wolf static int handle_aiocb_rw(void *opaque) 1322c1bb86cdSEric Blake { 1323999e6b69SKevin Wolf RawPosixAIOData *aiocb = opaque; 1324c1bb86cdSEric Blake ssize_t nbytes; 1325c1bb86cdSEric Blake char *buf; 1326c1bb86cdSEric Blake 1327c1bb86cdSEric Blake if (!(aiocb->aio_type & QEMU_AIO_MISALIGNED)) { 1328c1bb86cdSEric Blake /* 1329c1bb86cdSEric Blake * If there is just a single buffer, and it is properly aligned 1330c1bb86cdSEric Blake * we can just use plain pread/pwrite without any problems. 1331c1bb86cdSEric Blake */ 1332d57c44d0SKevin Wolf if (aiocb->io.niov == 1) { 133354c7ca1bSKevin Wolf nbytes = handle_aiocb_rw_linear(aiocb, aiocb->io.iov->iov_base); 133454c7ca1bSKevin Wolf goto out; 1335c1bb86cdSEric Blake } 1336c1bb86cdSEric Blake /* 1337c1bb86cdSEric Blake * We have more than one iovec, and all are properly aligned. 1338c1bb86cdSEric Blake * 1339c1bb86cdSEric Blake * Try preadv/pwritev first and fall back to linearizing the 1340c1bb86cdSEric Blake * buffer if it's not supported. 1341c1bb86cdSEric Blake */ 1342c1bb86cdSEric Blake if (preadv_present) { 1343c1bb86cdSEric Blake nbytes = handle_aiocb_rw_vector(aiocb); 1344c1bb86cdSEric Blake if (nbytes == aiocb->aio_nbytes || 1345c1bb86cdSEric Blake (nbytes < 0 && nbytes != -ENOSYS)) { 134654c7ca1bSKevin Wolf goto out; 1347c1bb86cdSEric Blake } 1348c1bb86cdSEric Blake preadv_present = false; 1349c1bb86cdSEric Blake } 1350c1bb86cdSEric Blake 1351c1bb86cdSEric Blake /* 1352c1bb86cdSEric Blake * XXX(hch): short read/write. no easy way to handle the reminder 1353c1bb86cdSEric Blake * using these interfaces. For now retry using plain 1354c1bb86cdSEric Blake * pread/pwrite? 1355c1bb86cdSEric Blake */ 1356c1bb86cdSEric Blake } 1357c1bb86cdSEric Blake 1358c1bb86cdSEric Blake /* 1359c1bb86cdSEric Blake * Ok, we have to do it the hard way, copy all segments into 1360c1bb86cdSEric Blake * a single aligned buffer. 1361c1bb86cdSEric Blake */ 1362c1bb86cdSEric Blake buf = qemu_try_blockalign(aiocb->bs, aiocb->aio_nbytes); 1363c1bb86cdSEric Blake if (buf == NULL) { 136454c7ca1bSKevin Wolf nbytes = -ENOMEM; 136554c7ca1bSKevin Wolf goto out; 1366c1bb86cdSEric Blake } 1367c1bb86cdSEric Blake 1368c1bb86cdSEric Blake if (aiocb->aio_type & QEMU_AIO_WRITE) { 1369c1bb86cdSEric Blake char *p = buf; 1370c1bb86cdSEric Blake int i; 1371c1bb86cdSEric Blake 1372d57c44d0SKevin Wolf for (i = 0; i < aiocb->io.niov; ++i) { 1373d57c44d0SKevin Wolf memcpy(p, aiocb->io.iov[i].iov_base, aiocb->io.iov[i].iov_len); 1374d57c44d0SKevin Wolf p += aiocb->io.iov[i].iov_len; 1375c1bb86cdSEric Blake } 1376c1bb86cdSEric Blake assert(p - buf == aiocb->aio_nbytes); 1377c1bb86cdSEric Blake } 1378c1bb86cdSEric Blake 1379c1bb86cdSEric Blake nbytes = handle_aiocb_rw_linear(aiocb, buf); 1380c1bb86cdSEric Blake if (!(aiocb->aio_type & QEMU_AIO_WRITE)) { 1381c1bb86cdSEric Blake char *p = buf; 1382c1bb86cdSEric Blake size_t count = aiocb->aio_nbytes, copy; 1383c1bb86cdSEric Blake int i; 1384c1bb86cdSEric Blake 1385d57c44d0SKevin Wolf for (i = 0; i < aiocb->io.niov && count; ++i) { 1386c1bb86cdSEric Blake copy = count; 1387d57c44d0SKevin Wolf if (copy > aiocb->io.iov[i].iov_len) { 1388d57c44d0SKevin Wolf copy = aiocb->io.iov[i].iov_len; 1389c1bb86cdSEric Blake } 1390d57c44d0SKevin Wolf memcpy(aiocb->io.iov[i].iov_base, p, copy); 1391c1bb86cdSEric Blake assert(count >= copy); 1392c1bb86cdSEric Blake p += copy; 1393c1bb86cdSEric Blake count -= copy; 1394c1bb86cdSEric Blake } 1395c1bb86cdSEric Blake assert(count == 0); 1396c1bb86cdSEric Blake } 1397c1bb86cdSEric Blake qemu_vfree(buf); 1398c1bb86cdSEric Blake 139954c7ca1bSKevin Wolf out: 140054c7ca1bSKevin Wolf if (nbytes == aiocb->aio_nbytes) { 140154c7ca1bSKevin Wolf return 0; 140254c7ca1bSKevin Wolf } else if (nbytes >= 0 && nbytes < aiocb->aio_nbytes) { 140354c7ca1bSKevin Wolf if (aiocb->aio_type & QEMU_AIO_WRITE) { 140454c7ca1bSKevin Wolf return -EINVAL; 140554c7ca1bSKevin Wolf } else { 140654c7ca1bSKevin Wolf iov_memset(aiocb->io.iov, aiocb->io.niov, nbytes, 140754c7ca1bSKevin Wolf 0, aiocb->aio_nbytes - nbytes); 140854c7ca1bSKevin Wolf return 0; 140954c7ca1bSKevin Wolf } 141054c7ca1bSKevin Wolf } else { 141154c7ca1bSKevin Wolf assert(nbytes < 0); 1412c1bb86cdSEric Blake return nbytes; 1413c1bb86cdSEric Blake } 141454c7ca1bSKevin Wolf } 1415c1bb86cdSEric Blake 1416c1bb86cdSEric Blake #ifdef CONFIG_XFS 1417c1bb86cdSEric Blake static int xfs_write_zeroes(BDRVRawState *s, int64_t offset, uint64_t bytes) 1418c1bb86cdSEric Blake { 1419c1bb86cdSEric Blake struct xfs_flock64 fl; 1420c1bb86cdSEric Blake int err; 1421c1bb86cdSEric Blake 1422c1bb86cdSEric Blake memset(&fl, 0, sizeof(fl)); 1423c1bb86cdSEric Blake fl.l_whence = SEEK_SET; 1424c1bb86cdSEric Blake fl.l_start = offset; 1425c1bb86cdSEric Blake fl.l_len = bytes; 1426c1bb86cdSEric Blake 1427c1bb86cdSEric Blake if (xfsctl(NULL, s->fd, XFS_IOC_ZERO_RANGE, &fl) < 0) { 1428c1bb86cdSEric Blake err = errno; 14294f7d28d7SLaurent Vivier trace_file_xfs_write_zeroes(strerror(errno)); 1430c1bb86cdSEric Blake return -err; 1431c1bb86cdSEric Blake } 1432c1bb86cdSEric Blake 1433c1bb86cdSEric Blake return 0; 1434c1bb86cdSEric Blake } 1435c1bb86cdSEric Blake 1436c1bb86cdSEric Blake static int xfs_discard(BDRVRawState *s, int64_t offset, uint64_t bytes) 1437c1bb86cdSEric Blake { 1438c1bb86cdSEric Blake struct xfs_flock64 fl; 1439c1bb86cdSEric Blake int err; 1440c1bb86cdSEric Blake 1441c1bb86cdSEric Blake memset(&fl, 0, sizeof(fl)); 1442c1bb86cdSEric Blake fl.l_whence = SEEK_SET; 1443c1bb86cdSEric Blake fl.l_start = offset; 1444c1bb86cdSEric Blake fl.l_len = bytes; 1445c1bb86cdSEric Blake 1446c1bb86cdSEric Blake if (xfsctl(NULL, s->fd, XFS_IOC_UNRESVSP64, &fl) < 0) { 1447c1bb86cdSEric Blake err = errno; 14484f7d28d7SLaurent Vivier trace_file_xfs_discard(strerror(errno)); 1449c1bb86cdSEric Blake return -err; 1450c1bb86cdSEric Blake } 1451c1bb86cdSEric Blake 1452c1bb86cdSEric Blake return 0; 1453c1bb86cdSEric Blake } 1454c1bb86cdSEric Blake #endif 1455c1bb86cdSEric Blake 1456c1bb86cdSEric Blake static int translate_err(int err) 1457c1bb86cdSEric Blake { 1458c1bb86cdSEric Blake if (err == -ENODEV || err == -ENOSYS || err == -EOPNOTSUPP || 1459c1bb86cdSEric Blake err == -ENOTTY) { 1460c1bb86cdSEric Blake err = -ENOTSUP; 1461c1bb86cdSEric Blake } 1462c1bb86cdSEric Blake return err; 1463c1bb86cdSEric Blake } 1464c1bb86cdSEric Blake 1465c1bb86cdSEric Blake #ifdef CONFIG_FALLOCATE 1466c1bb86cdSEric Blake static int do_fallocate(int fd, int mode, off_t offset, off_t len) 1467c1bb86cdSEric Blake { 1468c1bb86cdSEric Blake do { 1469c1bb86cdSEric Blake if (fallocate(fd, mode, offset, len) == 0) { 1470c1bb86cdSEric Blake return 0; 1471c1bb86cdSEric Blake } 1472c1bb86cdSEric Blake } while (errno == EINTR); 1473c1bb86cdSEric Blake return translate_err(-errno); 1474c1bb86cdSEric Blake } 1475c1bb86cdSEric Blake #endif 1476c1bb86cdSEric Blake 1477c1bb86cdSEric Blake static ssize_t handle_aiocb_write_zeroes_block(RawPosixAIOData *aiocb) 1478c1bb86cdSEric Blake { 1479c1bb86cdSEric Blake int ret = -ENOTSUP; 1480c1bb86cdSEric Blake BDRVRawState *s = aiocb->bs->opaque; 1481c1bb86cdSEric Blake 1482c1bb86cdSEric Blake if (!s->has_write_zeroes) { 1483c1bb86cdSEric Blake return -ENOTSUP; 1484c1bb86cdSEric Blake } 1485c1bb86cdSEric Blake 1486c1bb86cdSEric Blake #ifdef BLKZEROOUT 1487c1bb86cdSEric Blake do { 1488c1bb86cdSEric Blake uint64_t range[2] = { aiocb->aio_offset, aiocb->aio_nbytes }; 1489c1bb86cdSEric Blake if (ioctl(aiocb->aio_fildes, BLKZEROOUT, range) == 0) { 1490c1bb86cdSEric Blake return 0; 1491c1bb86cdSEric Blake } 1492c1bb86cdSEric Blake } while (errno == EINTR); 1493c1bb86cdSEric Blake 1494c1bb86cdSEric Blake ret = translate_err(-errno); 1495c1bb86cdSEric Blake #endif 1496c1bb86cdSEric Blake 1497c1bb86cdSEric Blake if (ret == -ENOTSUP) { 1498c1bb86cdSEric Blake s->has_write_zeroes = false; 1499c1bb86cdSEric Blake } 1500c1bb86cdSEric Blake return ret; 1501c1bb86cdSEric Blake } 1502c1bb86cdSEric Blake 15037154d8aeSKevin Wolf static int handle_aiocb_write_zeroes(void *opaque) 1504c1bb86cdSEric Blake { 15057154d8aeSKevin Wolf RawPosixAIOData *aiocb = opaque; 1506c1bb86cdSEric Blake #if defined(CONFIG_FALLOCATE) || defined(CONFIG_XFS) 1507c1bb86cdSEric Blake BDRVRawState *s = aiocb->bs->opaque; 1508c1bb86cdSEric Blake #endif 150970d9110bSDenis V. Lunev #ifdef CONFIG_FALLOCATE 151070d9110bSDenis V. Lunev int64_t len; 151170d9110bSDenis V. Lunev #endif 1512c1bb86cdSEric Blake 1513c1bb86cdSEric Blake if (aiocb->aio_type & QEMU_AIO_BLKDEV) { 1514c1bb86cdSEric Blake return handle_aiocb_write_zeroes_block(aiocb); 1515c1bb86cdSEric Blake } 1516c1bb86cdSEric Blake 1517c1bb86cdSEric Blake #ifdef CONFIG_XFS 1518c1bb86cdSEric Blake if (s->is_xfs) { 1519c1bb86cdSEric Blake return xfs_write_zeroes(s, aiocb->aio_offset, aiocb->aio_nbytes); 1520c1bb86cdSEric Blake } 1521c1bb86cdSEric Blake #endif 1522c1bb86cdSEric Blake 1523c1bb86cdSEric Blake #ifdef CONFIG_FALLOCATE_ZERO_RANGE 1524c1bb86cdSEric Blake if (s->has_write_zeroes) { 1525c1bb86cdSEric Blake int ret = do_fallocate(s->fd, FALLOC_FL_ZERO_RANGE, 1526c1bb86cdSEric Blake aiocb->aio_offset, aiocb->aio_nbytes); 1527c1bb86cdSEric Blake if (ret == 0 || ret != -ENOTSUP) { 1528c1bb86cdSEric Blake return ret; 1529c1bb86cdSEric Blake } 1530c1bb86cdSEric Blake s->has_write_zeroes = false; 1531c1bb86cdSEric Blake } 1532c1bb86cdSEric Blake #endif 1533c1bb86cdSEric Blake 1534c1bb86cdSEric Blake #ifdef CONFIG_FALLOCATE_PUNCH_HOLE 1535c1bb86cdSEric Blake if (s->has_discard && s->has_fallocate) { 1536c1bb86cdSEric Blake int ret = do_fallocate(s->fd, 1537c1bb86cdSEric Blake FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 1538c1bb86cdSEric Blake aiocb->aio_offset, aiocb->aio_nbytes); 1539c1bb86cdSEric Blake if (ret == 0) { 1540c1bb86cdSEric Blake ret = do_fallocate(s->fd, 0, aiocb->aio_offset, aiocb->aio_nbytes); 1541c1bb86cdSEric Blake if (ret == 0 || ret != -ENOTSUP) { 1542c1bb86cdSEric Blake return ret; 1543c1bb86cdSEric Blake } 1544c1bb86cdSEric Blake s->has_fallocate = false; 1545c1bb86cdSEric Blake } else if (ret != -ENOTSUP) { 1546c1bb86cdSEric Blake return ret; 1547c1bb86cdSEric Blake } else { 1548c1bb86cdSEric Blake s->has_discard = false; 1549c1bb86cdSEric Blake } 1550c1bb86cdSEric Blake } 1551c1bb86cdSEric Blake #endif 1552c1bb86cdSEric Blake 1553c1bb86cdSEric Blake #ifdef CONFIG_FALLOCATE 155470d9110bSDenis V. Lunev /* Last resort: we are trying to extend the file with zeroed data. This 155570d9110bSDenis V. Lunev * can be done via fallocate(fd, 0) */ 155670d9110bSDenis V. Lunev len = bdrv_getlength(aiocb->bs); 155770d9110bSDenis V. Lunev if (s->has_fallocate && len >= 0 && aiocb->aio_offset >= len) { 1558c1bb86cdSEric Blake int ret = do_fallocate(s->fd, 0, aiocb->aio_offset, aiocb->aio_nbytes); 1559c1bb86cdSEric Blake if (ret == 0 || ret != -ENOTSUP) { 1560c1bb86cdSEric Blake return ret; 1561c1bb86cdSEric Blake } 1562c1bb86cdSEric Blake s->has_fallocate = false; 1563c1bb86cdSEric Blake } 1564c1bb86cdSEric Blake #endif 1565c1bb86cdSEric Blake 1566c1bb86cdSEric Blake return -ENOTSUP; 1567c1bb86cdSEric Blake } 1568c1bb86cdSEric Blake 15697154d8aeSKevin Wolf static int handle_aiocb_write_zeroes_unmap(void *opaque) 157034fa110eSKevin Wolf { 15717154d8aeSKevin Wolf RawPosixAIOData *aiocb = opaque; 157234fa110eSKevin Wolf BDRVRawState *s G_GNUC_UNUSED = aiocb->bs->opaque; 157334fa110eSKevin Wolf int ret; 157434fa110eSKevin Wolf 157534fa110eSKevin Wolf /* First try to write zeros and unmap at the same time */ 157634fa110eSKevin Wolf 157734fa110eSKevin Wolf #ifdef CONFIG_FALLOCATE_PUNCH_HOLE 157834fa110eSKevin Wolf ret = do_fallocate(s->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 157934fa110eSKevin Wolf aiocb->aio_offset, aiocb->aio_nbytes); 158034fa110eSKevin Wolf if (ret != -ENOTSUP) { 158134fa110eSKevin Wolf return ret; 158234fa110eSKevin Wolf } 158334fa110eSKevin Wolf #endif 158434fa110eSKevin Wolf 158534fa110eSKevin Wolf #ifdef CONFIG_XFS 158634fa110eSKevin Wolf if (s->is_xfs) { 158734fa110eSKevin Wolf /* xfs_discard() guarantees that the discarded area reads as all-zero 158834fa110eSKevin Wolf * afterwards, so we can use it here. */ 158934fa110eSKevin Wolf return xfs_discard(s, aiocb->aio_offset, aiocb->aio_nbytes); 159034fa110eSKevin Wolf } 159134fa110eSKevin Wolf #endif 159234fa110eSKevin Wolf 159334fa110eSKevin Wolf /* If we couldn't manage to unmap while guaranteed that the area reads as 159434fa110eSKevin Wolf * all-zero afterwards, just write zeroes without unmapping */ 159534fa110eSKevin Wolf ret = handle_aiocb_write_zeroes(aiocb); 159634fa110eSKevin Wolf return ret; 159734fa110eSKevin Wolf } 159834fa110eSKevin Wolf 15991efad060SFam Zheng #ifndef HAVE_COPY_FILE_RANGE 16001efad060SFam Zheng static off_t copy_file_range(int in_fd, off_t *in_off, int out_fd, 16011efad060SFam Zheng off_t *out_off, size_t len, unsigned int flags) 16021efad060SFam Zheng { 16031efad060SFam Zheng #ifdef __NR_copy_file_range 16041efad060SFam Zheng return syscall(__NR_copy_file_range, in_fd, in_off, out_fd, 16051efad060SFam Zheng out_off, len, flags); 16061efad060SFam Zheng #else 16071efad060SFam Zheng errno = ENOSYS; 16081efad060SFam Zheng return -1; 16091efad060SFam Zheng #endif 16101efad060SFam Zheng } 16111efad060SFam Zheng #endif 16121efad060SFam Zheng 161358a209c4SKevin Wolf static int handle_aiocb_copy_range(void *opaque) 16141efad060SFam Zheng { 161558a209c4SKevin Wolf RawPosixAIOData *aiocb = opaque; 16161efad060SFam Zheng uint64_t bytes = aiocb->aio_nbytes; 16171efad060SFam Zheng off_t in_off = aiocb->aio_offset; 1618d57c44d0SKevin Wolf off_t out_off = aiocb->copy_range.aio_offset2; 16191efad060SFam Zheng 16201efad060SFam Zheng while (bytes) { 16211efad060SFam Zheng ssize_t ret = copy_file_range(aiocb->aio_fildes, &in_off, 1622d57c44d0SKevin Wolf aiocb->copy_range.aio_fd2, &out_off, 16231efad060SFam Zheng bytes, 0); 1624ecc983a5SFam Zheng trace_file_copy_file_range(aiocb->bs, aiocb->aio_fildes, in_off, 1625d57c44d0SKevin Wolf aiocb->copy_range.aio_fd2, out_off, bytes, 1626d57c44d0SKevin Wolf 0, ret); 1627c436e3d0SFam Zheng if (ret == 0) { 1628c436e3d0SFam Zheng /* No progress (e.g. when beyond EOF), let the caller fall back to 1629c436e3d0SFam Zheng * buffer I/O. */ 1630c436e3d0SFam Zheng return -ENOSPC; 16311efad060SFam Zheng } 16321efad060SFam Zheng if (ret < 0) { 1633c436e3d0SFam Zheng switch (errno) { 1634c436e3d0SFam Zheng case ENOSYS: 16351efad060SFam Zheng return -ENOTSUP; 1636c436e3d0SFam Zheng case EINTR: 1637c436e3d0SFam Zheng continue; 1638c436e3d0SFam Zheng default: 16391efad060SFam Zheng return -errno; 16401efad060SFam Zheng } 16411efad060SFam Zheng } 16421efad060SFam Zheng bytes -= ret; 16431efad060SFam Zheng } 16441efad060SFam Zheng return 0; 16451efad060SFam Zheng } 16461efad060SFam Zheng 164746ee0f46SKevin Wolf static int handle_aiocb_discard(void *opaque) 1648c1bb86cdSEric Blake { 164946ee0f46SKevin Wolf RawPosixAIOData *aiocb = opaque; 1650c1bb86cdSEric Blake int ret = -EOPNOTSUPP; 1651c1bb86cdSEric Blake BDRVRawState *s = aiocb->bs->opaque; 1652c1bb86cdSEric Blake 1653c1bb86cdSEric Blake if (!s->has_discard) { 1654c1bb86cdSEric Blake return -ENOTSUP; 1655c1bb86cdSEric Blake } 1656c1bb86cdSEric Blake 1657c1bb86cdSEric Blake if (aiocb->aio_type & QEMU_AIO_BLKDEV) { 1658c1bb86cdSEric Blake #ifdef BLKDISCARD 1659c1bb86cdSEric Blake do { 1660c1bb86cdSEric Blake uint64_t range[2] = { aiocb->aio_offset, aiocb->aio_nbytes }; 1661c1bb86cdSEric Blake if (ioctl(aiocb->aio_fildes, BLKDISCARD, range) == 0) { 1662c1bb86cdSEric Blake return 0; 1663c1bb86cdSEric Blake } 1664c1bb86cdSEric Blake } while (errno == EINTR); 1665c1bb86cdSEric Blake 1666c1bb86cdSEric Blake ret = -errno; 1667c1bb86cdSEric Blake #endif 1668c1bb86cdSEric Blake } else { 1669c1bb86cdSEric Blake #ifdef CONFIG_XFS 1670c1bb86cdSEric Blake if (s->is_xfs) { 1671c1bb86cdSEric Blake return xfs_discard(s, aiocb->aio_offset, aiocb->aio_nbytes); 1672c1bb86cdSEric Blake } 1673c1bb86cdSEric Blake #endif 1674c1bb86cdSEric Blake 1675c1bb86cdSEric Blake #ifdef CONFIG_FALLOCATE_PUNCH_HOLE 1676c1bb86cdSEric Blake ret = do_fallocate(s->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 1677c1bb86cdSEric Blake aiocb->aio_offset, aiocb->aio_nbytes); 1678c1bb86cdSEric Blake #endif 1679c1bb86cdSEric Blake } 1680c1bb86cdSEric Blake 1681c1bb86cdSEric Blake ret = translate_err(ret); 1682c1bb86cdSEric Blake if (ret == -ENOTSUP) { 1683c1bb86cdSEric Blake s->has_discard = false; 1684c1bb86cdSEric Blake } 1685c1bb86cdSEric Blake return ret; 1686c1bb86cdSEric Blake } 1687c1bb86cdSEric Blake 168829cb4c01SKevin Wolf static int handle_aiocb_truncate(void *opaque) 168993f4e2ffSKevin Wolf { 169029cb4c01SKevin Wolf RawPosixAIOData *aiocb = opaque; 169193f4e2ffSKevin Wolf int result = 0; 169293f4e2ffSKevin Wolf int64_t current_length = 0; 169393f4e2ffSKevin Wolf char *buf = NULL; 169493f4e2ffSKevin Wolf struct stat st; 169593f4e2ffSKevin Wolf int fd = aiocb->aio_fildes; 169693f4e2ffSKevin Wolf int64_t offset = aiocb->aio_offset; 1697d57c44d0SKevin Wolf PreallocMode prealloc = aiocb->truncate.prealloc; 1698d57c44d0SKevin Wolf Error **errp = aiocb->truncate.errp; 169993f4e2ffSKevin Wolf 170093f4e2ffSKevin Wolf if (fstat(fd, &st) < 0) { 170193f4e2ffSKevin Wolf result = -errno; 170293f4e2ffSKevin Wolf error_setg_errno(errp, -result, "Could not stat file"); 170393f4e2ffSKevin Wolf return result; 170493f4e2ffSKevin Wolf } 170593f4e2ffSKevin Wolf 170693f4e2ffSKevin Wolf current_length = st.st_size; 1707d57c44d0SKevin Wolf if (current_length > offset && prealloc != PREALLOC_MODE_OFF) { 170893f4e2ffSKevin Wolf error_setg(errp, "Cannot use preallocation for shrinking files"); 170993f4e2ffSKevin Wolf return -ENOTSUP; 171093f4e2ffSKevin Wolf } 171193f4e2ffSKevin Wolf 1712d57c44d0SKevin Wolf switch (prealloc) { 171393f4e2ffSKevin Wolf #ifdef CONFIG_POSIX_FALLOCATE 171493f4e2ffSKevin Wolf case PREALLOC_MODE_FALLOC: 171593f4e2ffSKevin Wolf /* 171693f4e2ffSKevin Wolf * Truncating before posix_fallocate() makes it about twice slower on 171793f4e2ffSKevin Wolf * file systems that do not support fallocate(), trying to check if a 171893f4e2ffSKevin Wolf * block is allocated before allocating it, so don't do that here. 171993f4e2ffSKevin Wolf */ 172093f4e2ffSKevin Wolf if (offset != current_length) { 172193f4e2ffSKevin Wolf result = -posix_fallocate(fd, current_length, 172293f4e2ffSKevin Wolf offset - current_length); 172393f4e2ffSKevin Wolf if (result != 0) { 172493f4e2ffSKevin Wolf /* posix_fallocate() doesn't set errno. */ 172593f4e2ffSKevin Wolf error_setg_errno(errp, -result, 172693f4e2ffSKevin Wolf "Could not preallocate new data"); 172793f4e2ffSKevin Wolf } 172893f4e2ffSKevin Wolf } else { 172993f4e2ffSKevin Wolf result = 0; 173093f4e2ffSKevin Wolf } 173193f4e2ffSKevin Wolf goto out; 173293f4e2ffSKevin Wolf #endif 173393f4e2ffSKevin Wolf case PREALLOC_MODE_FULL: 173493f4e2ffSKevin Wolf { 173593f4e2ffSKevin Wolf int64_t num = 0, left = offset - current_length; 173693f4e2ffSKevin Wolf off_t seek_result; 173793f4e2ffSKevin Wolf 173893f4e2ffSKevin Wolf /* 173993f4e2ffSKevin Wolf * Knowing the final size from the beginning could allow the file 174093f4e2ffSKevin Wolf * system driver to do less allocations and possibly avoid 174193f4e2ffSKevin Wolf * fragmentation of the file. 174293f4e2ffSKevin Wolf */ 174393f4e2ffSKevin Wolf if (ftruncate(fd, offset) != 0) { 174493f4e2ffSKevin Wolf result = -errno; 174593f4e2ffSKevin Wolf error_setg_errno(errp, -result, "Could not resize file"); 174693f4e2ffSKevin Wolf goto out; 174793f4e2ffSKevin Wolf } 174893f4e2ffSKevin Wolf 174993f4e2ffSKevin Wolf buf = g_malloc0(65536); 175093f4e2ffSKevin Wolf 175193f4e2ffSKevin Wolf seek_result = lseek(fd, current_length, SEEK_SET); 175293f4e2ffSKevin Wolf if (seek_result < 0) { 175393f4e2ffSKevin Wolf result = -errno; 175493f4e2ffSKevin Wolf error_setg_errno(errp, -result, 175593f4e2ffSKevin Wolf "Failed to seek to the old end of file"); 175693f4e2ffSKevin Wolf goto out; 175793f4e2ffSKevin Wolf } 175893f4e2ffSKevin Wolf 175993f4e2ffSKevin Wolf while (left > 0) { 176093f4e2ffSKevin Wolf num = MIN(left, 65536); 176193f4e2ffSKevin Wolf result = write(fd, buf, num); 176293f4e2ffSKevin Wolf if (result < 0) { 1763a1c81f4fSFam Zheng if (errno == EINTR) { 1764a1c81f4fSFam Zheng continue; 1765a1c81f4fSFam Zheng } 176693f4e2ffSKevin Wolf result = -errno; 176793f4e2ffSKevin Wolf error_setg_errno(errp, -result, 176893f4e2ffSKevin Wolf "Could not write zeros for preallocation"); 176993f4e2ffSKevin Wolf goto out; 177093f4e2ffSKevin Wolf } 177193f4e2ffSKevin Wolf left -= result; 177293f4e2ffSKevin Wolf } 177393f4e2ffSKevin Wolf if (result >= 0) { 177493f4e2ffSKevin Wolf result = fsync(fd); 177593f4e2ffSKevin Wolf if (result < 0) { 177693f4e2ffSKevin Wolf result = -errno; 177793f4e2ffSKevin Wolf error_setg_errno(errp, -result, 177893f4e2ffSKevin Wolf "Could not flush file to disk"); 177993f4e2ffSKevin Wolf goto out; 178093f4e2ffSKevin Wolf } 178193f4e2ffSKevin Wolf } 178293f4e2ffSKevin Wolf goto out; 178393f4e2ffSKevin Wolf } 178493f4e2ffSKevin Wolf case PREALLOC_MODE_OFF: 178593f4e2ffSKevin Wolf if (ftruncate(fd, offset) != 0) { 178693f4e2ffSKevin Wolf result = -errno; 178793f4e2ffSKevin Wolf error_setg_errno(errp, -result, "Could not resize file"); 178893f4e2ffSKevin Wolf } 178993f4e2ffSKevin Wolf return result; 179093f4e2ffSKevin Wolf default: 179193f4e2ffSKevin Wolf result = -ENOTSUP; 179293f4e2ffSKevin Wolf error_setg(errp, "Unsupported preallocation mode: %s", 1793d57c44d0SKevin Wolf PreallocMode_str(prealloc)); 179493f4e2ffSKevin Wolf return result; 179593f4e2ffSKevin Wolf } 179693f4e2ffSKevin Wolf 179793f4e2ffSKevin Wolf out: 179893f4e2ffSKevin Wolf if (result < 0) { 179993f4e2ffSKevin Wolf if (ftruncate(fd, current_length) < 0) { 180093f4e2ffSKevin Wolf error_report("Failed to restore old file length: %s", 180193f4e2ffSKevin Wolf strerror(errno)); 180293f4e2ffSKevin Wolf } 180393f4e2ffSKevin Wolf } 180493f4e2ffSKevin Wolf 180593f4e2ffSKevin Wolf g_free(buf); 180693f4e2ffSKevin Wolf return result; 180793f4e2ffSKevin Wolf } 180893f4e2ffSKevin Wolf 18095d5de250SKevin Wolf static int coroutine_fn raw_thread_pool_submit(BlockDriverState *bs, 18105d5de250SKevin Wolf ThreadPoolFunc func, void *arg) 18115d5de250SKevin Wolf { 18125d5de250SKevin Wolf /* @bs can be NULL, bdrv_get_aio_context() returns the main context then */ 18135d5de250SKevin Wolf ThreadPool *pool = aio_get_thread_pool(bdrv_get_aio_context(bs)); 18145d5de250SKevin Wolf return thread_pool_submit_co(pool, func, arg); 18155d5de250SKevin Wolf } 18165d5de250SKevin Wolf 1817c1bb86cdSEric Blake static int coroutine_fn raw_co_prw(BlockDriverState *bs, uint64_t offset, 1818c1bb86cdSEric Blake uint64_t bytes, QEMUIOVector *qiov, int type) 1819c1bb86cdSEric Blake { 1820c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 1821999e6b69SKevin Wolf RawPosixAIOData acb; 1822c1bb86cdSEric Blake 1823c1bb86cdSEric Blake if (fd_open(bs) < 0) 1824c1bb86cdSEric Blake return -EIO; 1825c1bb86cdSEric Blake 1826c1bb86cdSEric Blake /* 1827c1bb86cdSEric Blake * Check if the underlying device requires requests to be aligned, 1828c1bb86cdSEric Blake * and if the request we are trying to submit is aligned or not. 1829c1bb86cdSEric Blake * If this is the case tell the low-level driver that it needs 1830c1bb86cdSEric Blake * to copy the buffer. 1831c1bb86cdSEric Blake */ 1832c1bb86cdSEric Blake if (s->needs_alignment) { 1833c1bb86cdSEric Blake if (!bdrv_qiov_is_aligned(bs, qiov)) { 1834c1bb86cdSEric Blake type |= QEMU_AIO_MISALIGNED; 1835c1bb86cdSEric Blake #ifdef CONFIG_LINUX_AIO 1836c1bb86cdSEric Blake } else if (s->use_linux_aio) { 1837c1bb86cdSEric Blake LinuxAioState *aio = aio_get_linux_aio(bdrv_get_aio_context(bs)); 1838c1bb86cdSEric Blake assert(qiov->size == bytes); 1839c1bb86cdSEric Blake return laio_co_submit(bs, aio, s->fd, offset, qiov, type); 1840c1bb86cdSEric Blake #endif 1841c1bb86cdSEric Blake } 1842c1bb86cdSEric Blake } 1843c1bb86cdSEric Blake 1844999e6b69SKevin Wolf acb = (RawPosixAIOData) { 1845999e6b69SKevin Wolf .bs = bs, 1846999e6b69SKevin Wolf .aio_fildes = s->fd, 1847999e6b69SKevin Wolf .aio_type = type, 1848999e6b69SKevin Wolf .aio_offset = offset, 1849999e6b69SKevin Wolf .aio_nbytes = bytes, 1850999e6b69SKevin Wolf .io = { 1851999e6b69SKevin Wolf .iov = qiov->iov, 1852999e6b69SKevin Wolf .niov = qiov->niov, 1853999e6b69SKevin Wolf }, 1854999e6b69SKevin Wolf }; 1855999e6b69SKevin Wolf 1856999e6b69SKevin Wolf assert(qiov->size == bytes); 1857999e6b69SKevin Wolf return raw_thread_pool_submit(bs, handle_aiocb_rw, &acb); 1858c1bb86cdSEric Blake } 1859c1bb86cdSEric Blake 1860c1bb86cdSEric Blake static int coroutine_fn raw_co_preadv(BlockDriverState *bs, uint64_t offset, 1861c1bb86cdSEric Blake uint64_t bytes, QEMUIOVector *qiov, 1862c1bb86cdSEric Blake int flags) 1863c1bb86cdSEric Blake { 1864c1bb86cdSEric Blake return raw_co_prw(bs, offset, bytes, qiov, QEMU_AIO_READ); 1865c1bb86cdSEric Blake } 1866c1bb86cdSEric Blake 1867c1bb86cdSEric Blake static int coroutine_fn raw_co_pwritev(BlockDriverState *bs, uint64_t offset, 1868c1bb86cdSEric Blake uint64_t bytes, QEMUIOVector *qiov, 1869c1bb86cdSEric Blake int flags) 1870c1bb86cdSEric Blake { 1871c1bb86cdSEric Blake assert(flags == 0); 1872c1bb86cdSEric Blake return raw_co_prw(bs, offset, bytes, qiov, QEMU_AIO_WRITE); 1873c1bb86cdSEric Blake } 1874c1bb86cdSEric Blake 1875c1bb86cdSEric Blake static void raw_aio_plug(BlockDriverState *bs) 1876c1bb86cdSEric Blake { 1877c1bb86cdSEric Blake #ifdef CONFIG_LINUX_AIO 1878c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 1879c1bb86cdSEric Blake if (s->use_linux_aio) { 1880c1bb86cdSEric Blake LinuxAioState *aio = aio_get_linux_aio(bdrv_get_aio_context(bs)); 1881c1bb86cdSEric Blake laio_io_plug(bs, aio); 1882c1bb86cdSEric Blake } 1883c1bb86cdSEric Blake #endif 1884c1bb86cdSEric Blake } 1885c1bb86cdSEric Blake 1886c1bb86cdSEric Blake static void raw_aio_unplug(BlockDriverState *bs) 1887c1bb86cdSEric Blake { 1888c1bb86cdSEric Blake #ifdef CONFIG_LINUX_AIO 1889c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 1890c1bb86cdSEric Blake if (s->use_linux_aio) { 1891c1bb86cdSEric Blake LinuxAioState *aio = aio_get_linux_aio(bdrv_get_aio_context(bs)); 1892c1bb86cdSEric Blake laio_io_unplug(bs, aio); 1893c1bb86cdSEric Blake } 1894c1bb86cdSEric Blake #endif 1895c1bb86cdSEric Blake } 1896c1bb86cdSEric Blake 189733d70fb6SKevin Wolf static int raw_co_flush_to_disk(BlockDriverState *bs) 1898c1bb86cdSEric Blake { 1899c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 190006dc9bd5SKevin Wolf RawPosixAIOData acb; 190133d70fb6SKevin Wolf int ret; 1902c1bb86cdSEric Blake 190333d70fb6SKevin Wolf ret = fd_open(bs); 190433d70fb6SKevin Wolf if (ret < 0) { 190533d70fb6SKevin Wolf return ret; 190633d70fb6SKevin Wolf } 1907c1bb86cdSEric Blake 190806dc9bd5SKevin Wolf acb = (RawPosixAIOData) { 190906dc9bd5SKevin Wolf .bs = bs, 191006dc9bd5SKevin Wolf .aio_fildes = s->fd, 191106dc9bd5SKevin Wolf .aio_type = QEMU_AIO_FLUSH, 191206dc9bd5SKevin Wolf }; 191306dc9bd5SKevin Wolf 191406dc9bd5SKevin Wolf return raw_thread_pool_submit(bs, handle_aiocb_flush, &acb); 1915c1bb86cdSEric Blake } 1916c1bb86cdSEric Blake 1917ed6e2161SNishanth Aravamudan static void raw_aio_attach_aio_context(BlockDriverState *bs, 1918ed6e2161SNishanth Aravamudan AioContext *new_context) 1919ed6e2161SNishanth Aravamudan { 1920ed6e2161SNishanth Aravamudan #ifdef CONFIG_LINUX_AIO 1921ed6e2161SNishanth Aravamudan BDRVRawState *s = bs->opaque; 1922ed6e2161SNishanth Aravamudan if (s->use_linux_aio) { 1923ed6e2161SNishanth Aravamudan Error *local_err; 1924ed6e2161SNishanth Aravamudan if (!aio_setup_linux_aio(new_context, &local_err)) { 1925ed6e2161SNishanth Aravamudan error_reportf_err(local_err, "Unable to use native AIO, " 1926ed6e2161SNishanth Aravamudan "falling back to thread pool: "); 1927ed6e2161SNishanth Aravamudan s->use_linux_aio = false; 1928ed6e2161SNishanth Aravamudan } 1929ed6e2161SNishanth Aravamudan } 1930ed6e2161SNishanth Aravamudan #endif 1931ed6e2161SNishanth Aravamudan } 1932ed6e2161SNishanth Aravamudan 1933c1bb86cdSEric Blake static void raw_close(BlockDriverState *bs) 1934c1bb86cdSEric Blake { 1935c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 1936c1bb86cdSEric Blake 1937c1bb86cdSEric Blake if (s->fd >= 0) { 1938c1bb86cdSEric Blake qemu_close(s->fd); 1939c1bb86cdSEric Blake s->fd = -1; 1940c1bb86cdSEric Blake } 1941c1bb86cdSEric Blake } 1942c1bb86cdSEric Blake 1943d0bc9e5dSMax Reitz /** 1944d0bc9e5dSMax Reitz * Truncates the given regular file @fd to @offset and, when growing, fills the 1945d0bc9e5dSMax Reitz * new space according to @prealloc. 1946d0bc9e5dSMax Reitz * 1947d0bc9e5dSMax Reitz * Returns: 0 on success, -errno on failure. 1948d0bc9e5dSMax Reitz */ 194993f4e2ffSKevin Wolf static int coroutine_fn 195093f4e2ffSKevin Wolf raw_regular_truncate(BlockDriverState *bs, int fd, int64_t offset, 195193f4e2ffSKevin Wolf PreallocMode prealloc, Error **errp) 19529f63b07eSMax Reitz { 195329cb4c01SKevin Wolf RawPosixAIOData acb; 1954d0bc9e5dSMax Reitz 195529cb4c01SKevin Wolf acb = (RawPosixAIOData) { 195693f4e2ffSKevin Wolf .bs = bs, 195793f4e2ffSKevin Wolf .aio_fildes = fd, 195893f4e2ffSKevin Wolf .aio_type = QEMU_AIO_TRUNCATE, 195993f4e2ffSKevin Wolf .aio_offset = offset, 1960d57c44d0SKevin Wolf .truncate = { 196193f4e2ffSKevin Wolf .prealloc = prealloc, 196293f4e2ffSKevin Wolf .errp = errp, 1963d57c44d0SKevin Wolf }, 196493f4e2ffSKevin Wolf }; 1965d0bc9e5dSMax Reitz 196629cb4c01SKevin Wolf return raw_thread_pool_submit(bs, handle_aiocb_truncate, &acb); 19679f63b07eSMax Reitz } 19689f63b07eSMax Reitz 1969061ca8a3SKevin Wolf static int coroutine_fn raw_co_truncate(BlockDriverState *bs, int64_t offset, 19708243ccb7SMax Reitz PreallocMode prealloc, Error **errp) 1971c1bb86cdSEric Blake { 1972c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 1973c1bb86cdSEric Blake struct stat st; 1974f59adb32SMax Reitz int ret; 1975c1bb86cdSEric Blake 1976c1bb86cdSEric Blake if (fstat(s->fd, &st)) { 1977f59adb32SMax Reitz ret = -errno; 1978f59adb32SMax Reitz error_setg_errno(errp, -ret, "Failed to fstat() the file"); 1979f59adb32SMax Reitz return ret; 1980c1bb86cdSEric Blake } 1981c1bb86cdSEric Blake 1982c1bb86cdSEric Blake if (S_ISREG(st.st_mode)) { 198393f4e2ffSKevin Wolf return raw_regular_truncate(bs, s->fd, offset, prealloc, errp); 1984c1bb86cdSEric Blake } 198535d72602SMax Reitz 198635d72602SMax Reitz if (prealloc != PREALLOC_MODE_OFF) { 198735d72602SMax Reitz error_setg(errp, "Preallocation mode '%s' unsupported for this " 1988977c736fSMarkus Armbruster "non-regular file", PreallocMode_str(prealloc)); 198935d72602SMax Reitz return -ENOTSUP; 199035d72602SMax Reitz } 199135d72602SMax Reitz 199235d72602SMax Reitz if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) { 1993c1bb86cdSEric Blake if (offset > raw_getlength(bs)) { 1994f59adb32SMax Reitz error_setg(errp, "Cannot grow device files"); 1995c1bb86cdSEric Blake return -EINVAL; 1996c1bb86cdSEric Blake } 1997c1bb86cdSEric Blake } else { 1998f59adb32SMax Reitz error_setg(errp, "Resizing this file is not supported"); 1999c1bb86cdSEric Blake return -ENOTSUP; 2000c1bb86cdSEric Blake } 2001c1bb86cdSEric Blake 2002c1bb86cdSEric Blake return 0; 2003c1bb86cdSEric Blake } 2004c1bb86cdSEric Blake 2005c1bb86cdSEric Blake #ifdef __OpenBSD__ 2006c1bb86cdSEric Blake static int64_t raw_getlength(BlockDriverState *bs) 2007c1bb86cdSEric Blake { 2008c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2009c1bb86cdSEric Blake int fd = s->fd; 2010c1bb86cdSEric Blake struct stat st; 2011c1bb86cdSEric Blake 2012c1bb86cdSEric Blake if (fstat(fd, &st)) 2013c1bb86cdSEric Blake return -errno; 2014c1bb86cdSEric Blake if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) { 2015c1bb86cdSEric Blake struct disklabel dl; 2016c1bb86cdSEric Blake 2017c1bb86cdSEric Blake if (ioctl(fd, DIOCGDINFO, &dl)) 2018c1bb86cdSEric Blake return -errno; 2019c1bb86cdSEric Blake return (uint64_t)dl.d_secsize * 2020c1bb86cdSEric Blake dl.d_partitions[DISKPART(st.st_rdev)].p_size; 2021c1bb86cdSEric Blake } else 2022c1bb86cdSEric Blake return st.st_size; 2023c1bb86cdSEric Blake } 2024c1bb86cdSEric Blake #elif defined(__NetBSD__) 2025c1bb86cdSEric Blake static int64_t raw_getlength(BlockDriverState *bs) 2026c1bb86cdSEric Blake { 2027c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2028c1bb86cdSEric Blake int fd = s->fd; 2029c1bb86cdSEric Blake struct stat st; 2030c1bb86cdSEric Blake 2031c1bb86cdSEric Blake if (fstat(fd, &st)) 2032c1bb86cdSEric Blake return -errno; 2033c1bb86cdSEric Blake if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) { 2034c1bb86cdSEric Blake struct dkwedge_info dkw; 2035c1bb86cdSEric Blake 2036c1bb86cdSEric Blake if (ioctl(fd, DIOCGWEDGEINFO, &dkw) != -1) { 2037c1bb86cdSEric Blake return dkw.dkw_size * 512; 2038c1bb86cdSEric Blake } else { 2039c1bb86cdSEric Blake struct disklabel dl; 2040c1bb86cdSEric Blake 2041c1bb86cdSEric Blake if (ioctl(fd, DIOCGDINFO, &dl)) 2042c1bb86cdSEric Blake return -errno; 2043c1bb86cdSEric Blake return (uint64_t)dl.d_secsize * 2044c1bb86cdSEric Blake dl.d_partitions[DISKPART(st.st_rdev)].p_size; 2045c1bb86cdSEric Blake } 2046c1bb86cdSEric Blake } else 2047c1bb86cdSEric Blake return st.st_size; 2048c1bb86cdSEric Blake } 2049c1bb86cdSEric Blake #elif defined(__sun__) 2050c1bb86cdSEric Blake static int64_t raw_getlength(BlockDriverState *bs) 2051c1bb86cdSEric Blake { 2052c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2053c1bb86cdSEric Blake struct dk_minfo minfo; 2054c1bb86cdSEric Blake int ret; 2055c1bb86cdSEric Blake int64_t size; 2056c1bb86cdSEric Blake 2057c1bb86cdSEric Blake ret = fd_open(bs); 2058c1bb86cdSEric Blake if (ret < 0) { 2059c1bb86cdSEric Blake return ret; 2060c1bb86cdSEric Blake } 2061c1bb86cdSEric Blake 2062c1bb86cdSEric Blake /* 2063c1bb86cdSEric Blake * Use the DKIOCGMEDIAINFO ioctl to read the size. 2064c1bb86cdSEric Blake */ 2065c1bb86cdSEric Blake ret = ioctl(s->fd, DKIOCGMEDIAINFO, &minfo); 2066c1bb86cdSEric Blake if (ret != -1) { 2067c1bb86cdSEric Blake return minfo.dki_lbsize * minfo.dki_capacity; 2068c1bb86cdSEric Blake } 2069c1bb86cdSEric Blake 2070c1bb86cdSEric Blake /* 2071c1bb86cdSEric Blake * There are reports that lseek on some devices fails, but 2072c1bb86cdSEric Blake * irc discussion said that contingency on contingency was overkill. 2073c1bb86cdSEric Blake */ 2074c1bb86cdSEric Blake size = lseek(s->fd, 0, SEEK_END); 2075c1bb86cdSEric Blake if (size < 0) { 2076c1bb86cdSEric Blake return -errno; 2077c1bb86cdSEric Blake } 2078c1bb86cdSEric Blake return size; 2079c1bb86cdSEric Blake } 2080c1bb86cdSEric Blake #elif defined(CONFIG_BSD) 2081c1bb86cdSEric Blake static int64_t raw_getlength(BlockDriverState *bs) 2082c1bb86cdSEric Blake { 2083c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2084c1bb86cdSEric Blake int fd = s->fd; 2085c1bb86cdSEric Blake int64_t size; 2086c1bb86cdSEric Blake struct stat sb; 2087c1bb86cdSEric Blake #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__) 2088c1bb86cdSEric Blake int reopened = 0; 2089c1bb86cdSEric Blake #endif 2090c1bb86cdSEric Blake int ret; 2091c1bb86cdSEric Blake 2092c1bb86cdSEric Blake ret = fd_open(bs); 2093c1bb86cdSEric Blake if (ret < 0) 2094c1bb86cdSEric Blake return ret; 2095c1bb86cdSEric Blake 2096c1bb86cdSEric Blake #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__) 2097c1bb86cdSEric Blake again: 2098c1bb86cdSEric Blake #endif 2099c1bb86cdSEric Blake if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) { 2100c1bb86cdSEric Blake #ifdef DIOCGMEDIASIZE 2101c1bb86cdSEric Blake if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size)) 2102c1bb86cdSEric Blake #elif defined(DIOCGPART) 2103c1bb86cdSEric Blake { 2104c1bb86cdSEric Blake struct partinfo pi; 2105c1bb86cdSEric Blake if (ioctl(fd, DIOCGPART, &pi) == 0) 2106c1bb86cdSEric Blake size = pi.media_size; 2107c1bb86cdSEric Blake else 2108c1bb86cdSEric Blake size = 0; 2109c1bb86cdSEric Blake } 2110c1bb86cdSEric Blake if (size == 0) 2111c1bb86cdSEric Blake #endif 2112c1bb86cdSEric Blake #if defined(__APPLE__) && defined(__MACH__) 2113c1bb86cdSEric Blake { 2114c1bb86cdSEric Blake uint64_t sectors = 0; 2115c1bb86cdSEric Blake uint32_t sector_size = 0; 2116c1bb86cdSEric Blake 2117c1bb86cdSEric Blake if (ioctl(fd, DKIOCGETBLOCKCOUNT, §ors) == 0 2118c1bb86cdSEric Blake && ioctl(fd, DKIOCGETBLOCKSIZE, §or_size) == 0) { 2119c1bb86cdSEric Blake size = sectors * sector_size; 2120c1bb86cdSEric Blake } else { 2121c1bb86cdSEric Blake size = lseek(fd, 0LL, SEEK_END); 2122c1bb86cdSEric Blake if (size < 0) { 2123c1bb86cdSEric Blake return -errno; 2124c1bb86cdSEric Blake } 2125c1bb86cdSEric Blake } 2126c1bb86cdSEric Blake } 2127c1bb86cdSEric Blake #else 2128c1bb86cdSEric Blake size = lseek(fd, 0LL, SEEK_END); 2129c1bb86cdSEric Blake if (size < 0) { 2130c1bb86cdSEric Blake return -errno; 2131c1bb86cdSEric Blake } 2132c1bb86cdSEric Blake #endif 2133c1bb86cdSEric Blake #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) 2134c1bb86cdSEric Blake switch(s->type) { 2135c1bb86cdSEric Blake case FTYPE_CD: 2136c1bb86cdSEric Blake /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */ 2137c1bb86cdSEric Blake if (size == 2048LL * (unsigned)-1) 2138c1bb86cdSEric Blake size = 0; 2139c1bb86cdSEric Blake /* XXX no disc? maybe we need to reopen... */ 2140c1bb86cdSEric Blake if (size <= 0 && !reopened && cdrom_reopen(bs) >= 0) { 2141c1bb86cdSEric Blake reopened = 1; 2142c1bb86cdSEric Blake goto again; 2143c1bb86cdSEric Blake } 2144c1bb86cdSEric Blake } 2145c1bb86cdSEric Blake #endif 2146c1bb86cdSEric Blake } else { 2147c1bb86cdSEric Blake size = lseek(fd, 0, SEEK_END); 2148c1bb86cdSEric Blake if (size < 0) { 2149c1bb86cdSEric Blake return -errno; 2150c1bb86cdSEric Blake } 2151c1bb86cdSEric Blake } 2152c1bb86cdSEric Blake return size; 2153c1bb86cdSEric Blake } 2154c1bb86cdSEric Blake #else 2155c1bb86cdSEric Blake static int64_t raw_getlength(BlockDriverState *bs) 2156c1bb86cdSEric Blake { 2157c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2158c1bb86cdSEric Blake int ret; 2159c1bb86cdSEric Blake int64_t size; 2160c1bb86cdSEric Blake 2161c1bb86cdSEric Blake ret = fd_open(bs); 2162c1bb86cdSEric Blake if (ret < 0) { 2163c1bb86cdSEric Blake return ret; 2164c1bb86cdSEric Blake } 2165c1bb86cdSEric Blake 2166c1bb86cdSEric Blake size = lseek(s->fd, 0, SEEK_END); 2167c1bb86cdSEric Blake if (size < 0) { 2168c1bb86cdSEric Blake return -errno; 2169c1bb86cdSEric Blake } 2170c1bb86cdSEric Blake return size; 2171c1bb86cdSEric Blake } 2172c1bb86cdSEric Blake #endif 2173c1bb86cdSEric Blake 2174c1bb86cdSEric Blake static int64_t raw_get_allocated_file_size(BlockDriverState *bs) 2175c1bb86cdSEric Blake { 2176c1bb86cdSEric Blake struct stat st; 2177c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2178c1bb86cdSEric Blake 2179c1bb86cdSEric Blake if (fstat(s->fd, &st) < 0) { 2180c1bb86cdSEric Blake return -errno; 2181c1bb86cdSEric Blake } 2182c1bb86cdSEric Blake return (int64_t)st.st_blocks * 512; 2183c1bb86cdSEric Blake } 2184c1bb86cdSEric Blake 218593f4e2ffSKevin Wolf static int coroutine_fn 218693f4e2ffSKevin Wolf raw_co_create(BlockdevCreateOptions *options, Error **errp) 2187c1bb86cdSEric Blake { 2188927f11e1SKevin Wolf BlockdevCreateOptionsFile *file_opts; 21897c20c808SMax Reitz Error *local_err = NULL; 2190c1bb86cdSEric Blake int fd; 2191d815efcaSMax Reitz uint64_t perm, shared; 2192c1bb86cdSEric Blake int result = 0; 2193c1bb86cdSEric Blake 2194927f11e1SKevin Wolf /* Validate options and set default values */ 2195927f11e1SKevin Wolf assert(options->driver == BLOCKDEV_DRIVER_FILE); 2196927f11e1SKevin Wolf file_opts = &options->u.file; 2197c1bb86cdSEric Blake 2198927f11e1SKevin Wolf if (!file_opts->has_nocow) { 2199927f11e1SKevin Wolf file_opts->nocow = false; 2200927f11e1SKevin Wolf } 2201927f11e1SKevin Wolf if (!file_opts->has_preallocation) { 2202927f11e1SKevin Wolf file_opts->preallocation = PREALLOC_MODE_OFF; 2203c1bb86cdSEric Blake } 2204c1bb86cdSEric Blake 2205927f11e1SKevin Wolf /* Create file */ 2206b8cf1913SMax Reitz fd = qemu_open(file_opts->filename, O_RDWR | O_CREAT | O_BINARY, 0644); 2207c1bb86cdSEric Blake if (fd < 0) { 2208c1bb86cdSEric Blake result = -errno; 2209c1bb86cdSEric Blake error_setg_errno(errp, -result, "Could not create file"); 2210c1bb86cdSEric Blake goto out; 2211c1bb86cdSEric Blake } 2212c1bb86cdSEric Blake 2213b8cf1913SMax Reitz /* Take permissions: We want to discard everything, so we need 2214b8cf1913SMax Reitz * BLK_PERM_WRITE; and truncation to the desired size requires 2215b8cf1913SMax Reitz * BLK_PERM_RESIZE. 2216b8cf1913SMax Reitz * On the other hand, we cannot share the RESIZE permission 2217b8cf1913SMax Reitz * because we promise that after this function, the file has the 2218b8cf1913SMax Reitz * size given in the options. If someone else were to resize it 2219b8cf1913SMax Reitz * concurrently, we could not guarantee that. 2220b8cf1913SMax Reitz * Note that after this function, we can no longer guarantee that 2221b8cf1913SMax Reitz * the file is not touched by a third party, so it may be resized 2222b8cf1913SMax Reitz * then. */ 2223b8cf1913SMax Reitz perm = BLK_PERM_WRITE | BLK_PERM_RESIZE; 2224b8cf1913SMax Reitz shared = BLK_PERM_ALL & ~BLK_PERM_RESIZE; 2225b8cf1913SMax Reitz 2226b8cf1913SMax Reitz /* Step one: Take locks */ 22272996ffadSFam Zheng result = raw_apply_lock_bytes(NULL, fd, perm, ~shared, false, errp); 2228b8cf1913SMax Reitz if (result < 0) { 2229b8cf1913SMax Reitz goto out_close; 2230b8cf1913SMax Reitz } 2231b8cf1913SMax Reitz 2232b8cf1913SMax Reitz /* Step two: Check that nobody else has taken conflicting locks */ 2233b8cf1913SMax Reitz result = raw_check_lock_bytes(fd, perm, shared, errp); 2234b8cf1913SMax Reitz if (result < 0) { 2235b857431dSFam Zheng error_append_hint(errp, 2236b857431dSFam Zheng "Is another process using the image [%s]?\n", 2237b857431dSFam Zheng file_opts->filename); 22387c20c808SMax Reitz goto out_unlock; 2239b8cf1913SMax Reitz } 2240b8cf1913SMax Reitz 2241b8cf1913SMax Reitz /* Clear the file by truncating it to 0 */ 224293f4e2ffSKevin Wolf result = raw_regular_truncate(NULL, fd, 0, PREALLOC_MODE_OFF, errp); 2243b8cf1913SMax Reitz if (result < 0) { 22447c20c808SMax Reitz goto out_unlock; 2245b8cf1913SMax Reitz } 2246b8cf1913SMax Reitz 2247927f11e1SKevin Wolf if (file_opts->nocow) { 2248c1bb86cdSEric Blake #ifdef __linux__ 2249c1bb86cdSEric Blake /* Set NOCOW flag to solve performance issue on fs like btrfs. 2250c1bb86cdSEric Blake * This is an optimisation. The FS_IOC_SETFLAGS ioctl return value 2251c1bb86cdSEric Blake * will be ignored since any failure of this operation should not 2252c1bb86cdSEric Blake * block the left work. 2253c1bb86cdSEric Blake */ 2254c1bb86cdSEric Blake int attr; 2255c1bb86cdSEric Blake if (ioctl(fd, FS_IOC_GETFLAGS, &attr) == 0) { 2256c1bb86cdSEric Blake attr |= FS_NOCOW_FL; 2257c1bb86cdSEric Blake ioctl(fd, FS_IOC_SETFLAGS, &attr); 2258c1bb86cdSEric Blake } 2259c1bb86cdSEric Blake #endif 2260c1bb86cdSEric Blake } 2261c1bb86cdSEric Blake 2262b8cf1913SMax Reitz /* Resize and potentially preallocate the file to the desired 2263b8cf1913SMax Reitz * final size */ 226493f4e2ffSKevin Wolf result = raw_regular_truncate(NULL, fd, file_opts->size, 226593f4e2ffSKevin Wolf file_opts->preallocation, errp); 22669f63b07eSMax Reitz if (result < 0) { 22677c20c808SMax Reitz goto out_unlock; 22687c20c808SMax Reitz } 22697c20c808SMax Reitz 22707c20c808SMax Reitz out_unlock: 22712996ffadSFam Zheng raw_apply_lock_bytes(NULL, fd, 0, 0, true, &local_err); 22727c20c808SMax Reitz if (local_err) { 22737c20c808SMax Reitz /* The above call should not fail, and if it does, that does 22747c20c808SMax Reitz * not mean the whole creation operation has failed. So 22757c20c808SMax Reitz * report it the user for their convenience, but do not report 22767c20c808SMax Reitz * it to the caller. */ 2277db0754dfSFam Zheng warn_report_err(local_err); 22785a1dad9dSNir Soffer } 22795a1dad9dSNir Soffer 22805a1dad9dSNir Soffer out_close: 2281c1bb86cdSEric Blake if (qemu_close(fd) != 0 && result == 0) { 2282c1bb86cdSEric Blake result = -errno; 2283c1bb86cdSEric Blake error_setg_errno(errp, -result, "Could not close the new file"); 2284c1bb86cdSEric Blake } 2285c1bb86cdSEric Blake out: 2286c1bb86cdSEric Blake return result; 2287c1bb86cdSEric Blake } 2288c1bb86cdSEric Blake 2289927f11e1SKevin Wolf static int coroutine_fn raw_co_create_opts(const char *filename, QemuOpts *opts, 2290927f11e1SKevin Wolf Error **errp) 2291927f11e1SKevin Wolf { 2292927f11e1SKevin Wolf BlockdevCreateOptions options; 2293927f11e1SKevin Wolf int64_t total_size = 0; 2294927f11e1SKevin Wolf bool nocow = false; 2295927f11e1SKevin Wolf PreallocMode prealloc; 2296927f11e1SKevin Wolf char *buf = NULL; 2297927f11e1SKevin Wolf Error *local_err = NULL; 2298927f11e1SKevin Wolf 2299927f11e1SKevin Wolf /* Skip file: protocol prefix */ 2300927f11e1SKevin Wolf strstart(filename, "file:", &filename); 2301927f11e1SKevin Wolf 2302927f11e1SKevin Wolf /* Read out options */ 2303927f11e1SKevin Wolf total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), 2304927f11e1SKevin Wolf BDRV_SECTOR_SIZE); 2305927f11e1SKevin Wolf nocow = qemu_opt_get_bool(opts, BLOCK_OPT_NOCOW, false); 2306927f11e1SKevin Wolf buf = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC); 2307927f11e1SKevin Wolf prealloc = qapi_enum_parse(&PreallocMode_lookup, buf, 2308927f11e1SKevin Wolf PREALLOC_MODE_OFF, &local_err); 2309927f11e1SKevin Wolf g_free(buf); 2310927f11e1SKevin Wolf if (local_err) { 2311927f11e1SKevin Wolf error_propagate(errp, local_err); 2312927f11e1SKevin Wolf return -EINVAL; 2313927f11e1SKevin Wolf } 2314927f11e1SKevin Wolf 2315927f11e1SKevin Wolf options = (BlockdevCreateOptions) { 2316927f11e1SKevin Wolf .driver = BLOCKDEV_DRIVER_FILE, 2317927f11e1SKevin Wolf .u.file = { 2318927f11e1SKevin Wolf .filename = (char *) filename, 2319927f11e1SKevin Wolf .size = total_size, 2320927f11e1SKevin Wolf .has_preallocation = true, 2321927f11e1SKevin Wolf .preallocation = prealloc, 2322927f11e1SKevin Wolf .has_nocow = true, 2323927f11e1SKevin Wolf .nocow = nocow, 2324927f11e1SKevin Wolf }, 2325927f11e1SKevin Wolf }; 2326927f11e1SKevin Wolf return raw_co_create(&options, errp); 2327927f11e1SKevin Wolf } 2328927f11e1SKevin Wolf 2329c1bb86cdSEric Blake /* 2330c1bb86cdSEric Blake * Find allocation range in @bs around offset @start. 2331c1bb86cdSEric Blake * May change underlying file descriptor's file offset. 2332c1bb86cdSEric Blake * If @start is not in a hole, store @start in @data, and the 2333c1bb86cdSEric Blake * beginning of the next hole in @hole, and return 0. 2334c1bb86cdSEric Blake * If @start is in a non-trailing hole, store @start in @hole and the 2335c1bb86cdSEric Blake * beginning of the next non-hole in @data, and return 0. 2336c1bb86cdSEric Blake * If @start is in a trailing hole or beyond EOF, return -ENXIO. 2337c1bb86cdSEric Blake * If we can't find out, return a negative errno other than -ENXIO. 2338c1bb86cdSEric Blake */ 2339c1bb86cdSEric Blake static int find_allocation(BlockDriverState *bs, off_t start, 2340c1bb86cdSEric Blake off_t *data, off_t *hole) 2341c1bb86cdSEric Blake { 2342c1bb86cdSEric Blake #if defined SEEK_HOLE && defined SEEK_DATA 2343c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2344c1bb86cdSEric Blake off_t offs; 2345c1bb86cdSEric Blake 2346c1bb86cdSEric Blake /* 2347c1bb86cdSEric Blake * SEEK_DATA cases: 2348c1bb86cdSEric Blake * D1. offs == start: start is in data 2349c1bb86cdSEric Blake * D2. offs > start: start is in a hole, next data at offs 2350c1bb86cdSEric Blake * D3. offs < 0, errno = ENXIO: either start is in a trailing hole 2351c1bb86cdSEric Blake * or start is beyond EOF 2352c1bb86cdSEric Blake * If the latter happens, the file has been truncated behind 2353c1bb86cdSEric Blake * our back since we opened it. All bets are off then. 2354c1bb86cdSEric Blake * Treating like a trailing hole is simplest. 2355c1bb86cdSEric Blake * D4. offs < 0, errno != ENXIO: we learned nothing 2356c1bb86cdSEric Blake */ 2357c1bb86cdSEric Blake offs = lseek(s->fd, start, SEEK_DATA); 2358c1bb86cdSEric Blake if (offs < 0) { 2359c1bb86cdSEric Blake return -errno; /* D3 or D4 */ 2360c1bb86cdSEric Blake } 2361a03083a0SJeff Cody 2362a03083a0SJeff Cody if (offs < start) { 2363a03083a0SJeff Cody /* This is not a valid return by lseek(). We are safe to just return 2364a03083a0SJeff Cody * -EIO in this case, and we'll treat it like D4. */ 2365a03083a0SJeff Cody return -EIO; 2366a03083a0SJeff Cody } 2367c1bb86cdSEric Blake 2368c1bb86cdSEric Blake if (offs > start) { 2369c1bb86cdSEric Blake /* D2: in hole, next data at offs */ 2370c1bb86cdSEric Blake *hole = start; 2371c1bb86cdSEric Blake *data = offs; 2372c1bb86cdSEric Blake return 0; 2373c1bb86cdSEric Blake } 2374c1bb86cdSEric Blake 2375c1bb86cdSEric Blake /* D1: in data, end not yet known */ 2376c1bb86cdSEric Blake 2377c1bb86cdSEric Blake /* 2378c1bb86cdSEric Blake * SEEK_HOLE cases: 2379c1bb86cdSEric Blake * H1. offs == start: start is in a hole 2380c1bb86cdSEric Blake * If this happens here, a hole has been dug behind our back 2381c1bb86cdSEric Blake * since the previous lseek(). 2382c1bb86cdSEric Blake * H2. offs > start: either start is in data, next hole at offs, 2383c1bb86cdSEric Blake * or start is in trailing hole, EOF at offs 2384c1bb86cdSEric Blake * Linux treats trailing holes like any other hole: offs == 2385c1bb86cdSEric Blake * start. Solaris seeks to EOF instead: offs > start (blech). 2386c1bb86cdSEric Blake * If that happens here, a hole has been dug behind our back 2387c1bb86cdSEric Blake * since the previous lseek(). 2388c1bb86cdSEric Blake * H3. offs < 0, errno = ENXIO: start is beyond EOF 2389c1bb86cdSEric Blake * If this happens, the file has been truncated behind our 2390c1bb86cdSEric Blake * back since we opened it. Treat it like a trailing hole. 2391c1bb86cdSEric Blake * H4. offs < 0, errno != ENXIO: we learned nothing 2392c1bb86cdSEric Blake * Pretend we know nothing at all, i.e. "forget" about D1. 2393c1bb86cdSEric Blake */ 2394c1bb86cdSEric Blake offs = lseek(s->fd, start, SEEK_HOLE); 2395c1bb86cdSEric Blake if (offs < 0) { 2396c1bb86cdSEric Blake return -errno; /* D1 and (H3 or H4) */ 2397c1bb86cdSEric Blake } 2398a03083a0SJeff Cody 2399a03083a0SJeff Cody if (offs < start) { 2400a03083a0SJeff Cody /* This is not a valid return by lseek(). We are safe to just return 2401a03083a0SJeff Cody * -EIO in this case, and we'll treat it like H4. */ 2402a03083a0SJeff Cody return -EIO; 2403a03083a0SJeff Cody } 2404c1bb86cdSEric Blake 2405c1bb86cdSEric Blake if (offs > start) { 2406c1bb86cdSEric Blake /* 2407c1bb86cdSEric Blake * D1 and H2: either in data, next hole at offs, or it was in 2408c1bb86cdSEric Blake * data but is now in a trailing hole. In the latter case, 2409c1bb86cdSEric Blake * all bets are off. Treating it as if it there was data all 2410c1bb86cdSEric Blake * the way to EOF is safe, so simply do that. 2411c1bb86cdSEric Blake */ 2412c1bb86cdSEric Blake *data = start; 2413c1bb86cdSEric Blake *hole = offs; 2414c1bb86cdSEric Blake return 0; 2415c1bb86cdSEric Blake } 2416c1bb86cdSEric Blake 2417c1bb86cdSEric Blake /* D1 and H1 */ 2418c1bb86cdSEric Blake return -EBUSY; 2419c1bb86cdSEric Blake #else 2420c1bb86cdSEric Blake return -ENOTSUP; 2421c1bb86cdSEric Blake #endif 2422c1bb86cdSEric Blake } 2423c1bb86cdSEric Blake 2424c1bb86cdSEric Blake /* 2425a290f085SEric Blake * Returns the allocation status of the specified offset. 2426c1bb86cdSEric Blake * 2427a290f085SEric Blake * The block layer guarantees 'offset' and 'bytes' are within bounds. 2428c1bb86cdSEric Blake * 2429a290f085SEric Blake * 'pnum' is set to the number of bytes (including and immediately following 2430a290f085SEric Blake * the specified offset) that are known to be in the same 2431c1bb86cdSEric Blake * allocated/unallocated state. 2432c1bb86cdSEric Blake * 2433a290f085SEric Blake * 'bytes' is the max value 'pnum' should be set to. 2434c1bb86cdSEric Blake */ 2435a290f085SEric Blake static int coroutine_fn raw_co_block_status(BlockDriverState *bs, 2436a290f085SEric Blake bool want_zero, 2437a290f085SEric Blake int64_t offset, 2438a290f085SEric Blake int64_t bytes, int64_t *pnum, 2439a290f085SEric Blake int64_t *map, 2440c1bb86cdSEric Blake BlockDriverState **file) 2441c1bb86cdSEric Blake { 2442a290f085SEric Blake off_t data = 0, hole = 0; 2443c1bb86cdSEric Blake int ret; 2444c1bb86cdSEric Blake 2445c1bb86cdSEric Blake ret = fd_open(bs); 2446c1bb86cdSEric Blake if (ret < 0) { 2447c1bb86cdSEric Blake return ret; 2448c1bb86cdSEric Blake } 2449c1bb86cdSEric Blake 2450a290f085SEric Blake if (!want_zero) { 2451a290f085SEric Blake *pnum = bytes; 2452a290f085SEric Blake *map = offset; 2453a290f085SEric Blake *file = bs; 2454a290f085SEric Blake return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID; 2455c1bb86cdSEric Blake } 2456c1bb86cdSEric Blake 2457a290f085SEric Blake ret = find_allocation(bs, offset, &data, &hole); 2458c1bb86cdSEric Blake if (ret == -ENXIO) { 2459c1bb86cdSEric Blake /* Trailing hole */ 2460a290f085SEric Blake *pnum = bytes; 2461c1bb86cdSEric Blake ret = BDRV_BLOCK_ZERO; 2462c1bb86cdSEric Blake } else if (ret < 0) { 2463c1bb86cdSEric Blake /* No info available, so pretend there are no holes */ 2464a290f085SEric Blake *pnum = bytes; 2465c1bb86cdSEric Blake ret = BDRV_BLOCK_DATA; 2466a290f085SEric Blake } else if (data == offset) { 2467a290f085SEric Blake /* On a data extent, compute bytes to the end of the extent, 2468c1bb86cdSEric Blake * possibly including a partial sector at EOF. */ 2469a290f085SEric Blake *pnum = MIN(bytes, hole - offset); 2470c1bb86cdSEric Blake ret = BDRV_BLOCK_DATA; 2471c1bb86cdSEric Blake } else { 2472a290f085SEric Blake /* On a hole, compute bytes to the beginning of the next extent. */ 2473a290f085SEric Blake assert(hole == offset); 2474a290f085SEric Blake *pnum = MIN(bytes, data - offset); 2475c1bb86cdSEric Blake ret = BDRV_BLOCK_ZERO; 2476c1bb86cdSEric Blake } 2477a290f085SEric Blake *map = offset; 2478c1bb86cdSEric Blake *file = bs; 2479a290f085SEric Blake return ret | BDRV_BLOCK_OFFSET_VALID; 2480c1bb86cdSEric Blake } 2481c1bb86cdSEric Blake 248231be8a2aSStefan Hajnoczi #if defined(__linux__) 248331be8a2aSStefan Hajnoczi /* Verify that the file is not in the page cache */ 248431be8a2aSStefan Hajnoczi static void check_cache_dropped(BlockDriverState *bs, Error **errp) 248531be8a2aSStefan Hajnoczi { 248631be8a2aSStefan Hajnoczi const size_t window_size = 128 * 1024 * 1024; 248731be8a2aSStefan Hajnoczi BDRVRawState *s = bs->opaque; 248831be8a2aSStefan Hajnoczi void *window = NULL; 248931be8a2aSStefan Hajnoczi size_t length = 0; 249031be8a2aSStefan Hajnoczi unsigned char *vec; 249131be8a2aSStefan Hajnoczi size_t page_size; 249231be8a2aSStefan Hajnoczi off_t offset; 249331be8a2aSStefan Hajnoczi off_t end; 249431be8a2aSStefan Hajnoczi 249531be8a2aSStefan Hajnoczi /* mincore(2) page status information requires 1 byte per page */ 249631be8a2aSStefan Hajnoczi page_size = sysconf(_SC_PAGESIZE); 249731be8a2aSStefan Hajnoczi vec = g_malloc(DIV_ROUND_UP(window_size, page_size)); 249831be8a2aSStefan Hajnoczi 249931be8a2aSStefan Hajnoczi end = raw_getlength(bs); 250031be8a2aSStefan Hajnoczi 250131be8a2aSStefan Hajnoczi for (offset = 0; offset < end; offset += window_size) { 250231be8a2aSStefan Hajnoczi void *new_window; 250331be8a2aSStefan Hajnoczi size_t new_length; 250431be8a2aSStefan Hajnoczi size_t vec_end; 250531be8a2aSStefan Hajnoczi size_t i; 250631be8a2aSStefan Hajnoczi int ret; 250731be8a2aSStefan Hajnoczi 250831be8a2aSStefan Hajnoczi /* Unmap previous window if size has changed */ 250931be8a2aSStefan Hajnoczi new_length = MIN(end - offset, window_size); 251031be8a2aSStefan Hajnoczi if (new_length != length) { 251131be8a2aSStefan Hajnoczi munmap(window, length); 251231be8a2aSStefan Hajnoczi window = NULL; 251331be8a2aSStefan Hajnoczi length = 0; 251431be8a2aSStefan Hajnoczi } 251531be8a2aSStefan Hajnoczi 251631be8a2aSStefan Hajnoczi new_window = mmap(window, new_length, PROT_NONE, MAP_PRIVATE, 251731be8a2aSStefan Hajnoczi s->fd, offset); 251831be8a2aSStefan Hajnoczi if (new_window == MAP_FAILED) { 251931be8a2aSStefan Hajnoczi error_setg_errno(errp, errno, "mmap failed"); 252031be8a2aSStefan Hajnoczi break; 252131be8a2aSStefan Hajnoczi } 252231be8a2aSStefan Hajnoczi 252331be8a2aSStefan Hajnoczi window = new_window; 252431be8a2aSStefan Hajnoczi length = new_length; 252531be8a2aSStefan Hajnoczi 252631be8a2aSStefan Hajnoczi ret = mincore(window, length, vec); 252731be8a2aSStefan Hajnoczi if (ret < 0) { 252831be8a2aSStefan Hajnoczi error_setg_errno(errp, errno, "mincore failed"); 252931be8a2aSStefan Hajnoczi break; 253031be8a2aSStefan Hajnoczi } 253131be8a2aSStefan Hajnoczi 253231be8a2aSStefan Hajnoczi vec_end = DIV_ROUND_UP(length, page_size); 253331be8a2aSStefan Hajnoczi for (i = 0; i < vec_end; i++) { 253431be8a2aSStefan Hajnoczi if (vec[i] & 0x1) { 253531be8a2aSStefan Hajnoczi error_setg(errp, "page cache still in use!"); 253631be8a2aSStefan Hajnoczi break; 253731be8a2aSStefan Hajnoczi } 253831be8a2aSStefan Hajnoczi } 253931be8a2aSStefan Hajnoczi } 254031be8a2aSStefan Hajnoczi 254131be8a2aSStefan Hajnoczi if (window) { 254231be8a2aSStefan Hajnoczi munmap(window, length); 254331be8a2aSStefan Hajnoczi } 254431be8a2aSStefan Hajnoczi 254531be8a2aSStefan Hajnoczi g_free(vec); 254631be8a2aSStefan Hajnoczi } 254731be8a2aSStefan Hajnoczi #endif /* __linux__ */ 254831be8a2aSStefan Hajnoczi 2549dd577a26SStefan Hajnoczi static void coroutine_fn raw_co_invalidate_cache(BlockDriverState *bs, 2550dd577a26SStefan Hajnoczi Error **errp) 2551dd577a26SStefan Hajnoczi { 2552dd577a26SStefan Hajnoczi BDRVRawState *s = bs->opaque; 2553dd577a26SStefan Hajnoczi int ret; 2554dd577a26SStefan Hajnoczi 2555dd577a26SStefan Hajnoczi ret = fd_open(bs); 2556dd577a26SStefan Hajnoczi if (ret < 0) { 2557dd577a26SStefan Hajnoczi error_setg_errno(errp, -ret, "The file descriptor is not open"); 2558dd577a26SStefan Hajnoczi return; 2559dd577a26SStefan Hajnoczi } 2560dd577a26SStefan Hajnoczi 2561dd577a26SStefan Hajnoczi if (s->open_flags & O_DIRECT) { 2562dd577a26SStefan Hajnoczi return; /* No host kernel page cache */ 2563dd577a26SStefan Hajnoczi } 2564dd577a26SStefan Hajnoczi 2565dd577a26SStefan Hajnoczi #if defined(__linux__) 2566dd577a26SStefan Hajnoczi /* This sets the scene for the next syscall... */ 2567dd577a26SStefan Hajnoczi ret = bdrv_co_flush(bs); 2568dd577a26SStefan Hajnoczi if (ret < 0) { 2569dd577a26SStefan Hajnoczi error_setg_errno(errp, -ret, "flush failed"); 2570dd577a26SStefan Hajnoczi return; 2571dd577a26SStefan Hajnoczi } 2572dd577a26SStefan Hajnoczi 2573dd577a26SStefan Hajnoczi /* Linux does not invalidate pages that are dirty, locked, or mmapped by a 2574dd577a26SStefan Hajnoczi * process. These limitations are okay because we just fsynced the file, 2575dd577a26SStefan Hajnoczi * we don't use mmap, and the file should not be in use by other processes. 2576dd577a26SStefan Hajnoczi */ 2577dd577a26SStefan Hajnoczi ret = posix_fadvise(s->fd, 0, 0, POSIX_FADV_DONTNEED); 2578dd577a26SStefan Hajnoczi if (ret != 0) { /* the return value is a positive errno */ 2579dd577a26SStefan Hajnoczi error_setg_errno(errp, ret, "fadvise failed"); 2580dd577a26SStefan Hajnoczi return; 2581dd577a26SStefan Hajnoczi } 258231be8a2aSStefan Hajnoczi 258331be8a2aSStefan Hajnoczi if (s->check_cache_dropped) { 258431be8a2aSStefan Hajnoczi check_cache_dropped(bs, errp); 258531be8a2aSStefan Hajnoczi } 2586dd577a26SStefan Hajnoczi #else /* __linux__ */ 2587dd577a26SStefan Hajnoczi /* Do nothing. Live migration to a remote host with cache.direct=off is 2588dd577a26SStefan Hajnoczi * unsupported on other host operating systems. Cache consistency issues 2589dd577a26SStefan Hajnoczi * may occur but no error is reported here, partly because that's the 2590dd577a26SStefan Hajnoczi * historical behavior and partly because it's hard to differentiate valid 2591dd577a26SStefan Hajnoczi * configurations that should not cause errors. 2592dd577a26SStefan Hajnoczi */ 2593dd577a26SStefan Hajnoczi #endif /* !__linux__ */ 2594dd577a26SStefan Hajnoczi } 2595dd577a26SStefan Hajnoczi 259633d70fb6SKevin Wolf static coroutine_fn int 259746ee0f46SKevin Wolf raw_do_pdiscard(BlockDriverState *bs, int64_t offset, int bytes, bool blkdev) 2598c1bb86cdSEric Blake { 2599c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 260046ee0f46SKevin Wolf RawPosixAIOData acb; 2601c1bb86cdSEric Blake 260246ee0f46SKevin Wolf acb = (RawPosixAIOData) { 260346ee0f46SKevin Wolf .bs = bs, 260446ee0f46SKevin Wolf .aio_fildes = s->fd, 260546ee0f46SKevin Wolf .aio_type = QEMU_AIO_DISCARD, 260646ee0f46SKevin Wolf .aio_offset = offset, 260746ee0f46SKevin Wolf .aio_nbytes = bytes, 260846ee0f46SKevin Wolf }; 260946ee0f46SKevin Wolf 261046ee0f46SKevin Wolf if (blkdev) { 261146ee0f46SKevin Wolf acb.aio_type |= QEMU_AIO_BLKDEV; 261246ee0f46SKevin Wolf } 261346ee0f46SKevin Wolf 261446ee0f46SKevin Wolf return raw_thread_pool_submit(bs, handle_aiocb_discard, &acb); 261546ee0f46SKevin Wolf } 261646ee0f46SKevin Wolf 261746ee0f46SKevin Wolf static coroutine_fn int 261846ee0f46SKevin Wolf raw_co_pdiscard(BlockDriverState *bs, int64_t offset, int bytes) 261946ee0f46SKevin Wolf { 262046ee0f46SKevin Wolf return raw_do_pdiscard(bs, offset, bytes, false); 2621c1bb86cdSEric Blake } 2622c1bb86cdSEric Blake 26237154d8aeSKevin Wolf static int coroutine_fn 26247154d8aeSKevin Wolf raw_do_pwrite_zeroes(BlockDriverState *bs, int64_t offset, int bytes, 26257154d8aeSKevin Wolf BdrvRequestFlags flags, bool blkdev) 26267154d8aeSKevin Wolf { 26277154d8aeSKevin Wolf BDRVRawState *s = bs->opaque; 26287154d8aeSKevin Wolf RawPosixAIOData acb; 26297154d8aeSKevin Wolf ThreadPoolFunc *handler; 26307154d8aeSKevin Wolf 26317154d8aeSKevin Wolf acb = (RawPosixAIOData) { 26327154d8aeSKevin Wolf .bs = bs, 26337154d8aeSKevin Wolf .aio_fildes = s->fd, 26347154d8aeSKevin Wolf .aio_type = QEMU_AIO_WRITE_ZEROES, 26357154d8aeSKevin Wolf .aio_offset = offset, 26367154d8aeSKevin Wolf .aio_nbytes = bytes, 26377154d8aeSKevin Wolf }; 26387154d8aeSKevin Wolf 26397154d8aeSKevin Wolf if (blkdev) { 26407154d8aeSKevin Wolf acb.aio_type |= QEMU_AIO_BLKDEV; 26417154d8aeSKevin Wolf } 26427154d8aeSKevin Wolf 26437154d8aeSKevin Wolf if (flags & BDRV_REQ_MAY_UNMAP) { 26447154d8aeSKevin Wolf acb.aio_type |= QEMU_AIO_DISCARD; 26457154d8aeSKevin Wolf handler = handle_aiocb_write_zeroes_unmap; 26467154d8aeSKevin Wolf } else { 26477154d8aeSKevin Wolf handler = handle_aiocb_write_zeroes; 26487154d8aeSKevin Wolf } 26497154d8aeSKevin Wolf 26507154d8aeSKevin Wolf return raw_thread_pool_submit(bs, handler, &acb); 26517154d8aeSKevin Wolf } 26527154d8aeSKevin Wolf 2653c1bb86cdSEric Blake static int coroutine_fn raw_co_pwrite_zeroes( 2654c1bb86cdSEric Blake BlockDriverState *bs, int64_t offset, 2655f5a5ca79SManos Pitsidianakis int bytes, BdrvRequestFlags flags) 2656c1bb86cdSEric Blake { 26577154d8aeSKevin Wolf return raw_do_pwrite_zeroes(bs, offset, bytes, flags, false); 2658c1bb86cdSEric Blake } 2659c1bb86cdSEric Blake 2660c1bb86cdSEric Blake static int raw_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 2661c1bb86cdSEric Blake { 2662c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2663c1bb86cdSEric Blake 2664c1bb86cdSEric Blake bdi->unallocated_blocks_are_zero = s->discard_zeroes; 2665c1bb86cdSEric Blake return 0; 2666c1bb86cdSEric Blake } 2667c1bb86cdSEric Blake 2668c1bb86cdSEric Blake static QemuOptsList raw_create_opts = { 2669c1bb86cdSEric Blake .name = "raw-create-opts", 2670c1bb86cdSEric Blake .head = QTAILQ_HEAD_INITIALIZER(raw_create_opts.head), 2671c1bb86cdSEric Blake .desc = { 2672c1bb86cdSEric Blake { 2673c1bb86cdSEric Blake .name = BLOCK_OPT_SIZE, 2674c1bb86cdSEric Blake .type = QEMU_OPT_SIZE, 2675c1bb86cdSEric Blake .help = "Virtual disk size" 2676c1bb86cdSEric Blake }, 2677c1bb86cdSEric Blake { 2678c1bb86cdSEric Blake .name = BLOCK_OPT_NOCOW, 2679c1bb86cdSEric Blake .type = QEMU_OPT_BOOL, 2680c1bb86cdSEric Blake .help = "Turn off copy-on-write (valid only on btrfs)" 2681c1bb86cdSEric Blake }, 2682c1bb86cdSEric Blake { 2683c1bb86cdSEric Blake .name = BLOCK_OPT_PREALLOC, 2684c1bb86cdSEric Blake .type = QEMU_OPT_STRING, 2685c1bb86cdSEric Blake .help = "Preallocation mode (allowed values: off, falloc, full)" 2686c1bb86cdSEric Blake }, 2687c1bb86cdSEric Blake { /* end of list */ } 2688c1bb86cdSEric Blake } 2689c1bb86cdSEric Blake }; 2690c1bb86cdSEric Blake 2691244a5668SFam Zheng static int raw_check_perm(BlockDriverState *bs, uint64_t perm, uint64_t shared, 2692244a5668SFam Zheng Error **errp) 2693244a5668SFam Zheng { 2694244a5668SFam Zheng return raw_handle_perm_lock(bs, RAW_PL_PREPARE, perm, shared, errp); 2695244a5668SFam Zheng } 2696244a5668SFam Zheng 2697244a5668SFam Zheng static void raw_set_perm(BlockDriverState *bs, uint64_t perm, uint64_t shared) 2698244a5668SFam Zheng { 2699244a5668SFam Zheng BDRVRawState *s = bs->opaque; 2700244a5668SFam Zheng raw_handle_perm_lock(bs, RAW_PL_COMMIT, perm, shared, NULL); 2701244a5668SFam Zheng s->perm = perm; 2702244a5668SFam Zheng s->shared_perm = shared; 2703244a5668SFam Zheng } 2704244a5668SFam Zheng 2705244a5668SFam Zheng static void raw_abort_perm_update(BlockDriverState *bs) 2706244a5668SFam Zheng { 2707244a5668SFam Zheng raw_handle_perm_lock(bs, RAW_PL_ABORT, 0, 0, NULL); 2708244a5668SFam Zheng } 2709244a5668SFam Zheng 271067b51fb9SVladimir Sementsov-Ogievskiy static int coroutine_fn raw_co_copy_range_from( 271167b51fb9SVladimir Sementsov-Ogievskiy BlockDriverState *bs, BdrvChild *src, uint64_t src_offset, 271267b51fb9SVladimir Sementsov-Ogievskiy BdrvChild *dst, uint64_t dst_offset, uint64_t bytes, 271367b51fb9SVladimir Sementsov-Ogievskiy BdrvRequestFlags read_flags, BdrvRequestFlags write_flags) 27141efad060SFam Zheng { 271567b51fb9SVladimir Sementsov-Ogievskiy return bdrv_co_copy_range_to(src, src_offset, dst, dst_offset, bytes, 271667b51fb9SVladimir Sementsov-Ogievskiy read_flags, write_flags); 27171efad060SFam Zheng } 27181efad060SFam Zheng 27191efad060SFam Zheng static int coroutine_fn raw_co_copy_range_to(BlockDriverState *bs, 272067b51fb9SVladimir Sementsov-Ogievskiy BdrvChild *src, 272167b51fb9SVladimir Sementsov-Ogievskiy uint64_t src_offset, 272267b51fb9SVladimir Sementsov-Ogievskiy BdrvChild *dst, 272367b51fb9SVladimir Sementsov-Ogievskiy uint64_t dst_offset, 272467b51fb9SVladimir Sementsov-Ogievskiy uint64_t bytes, 272567b51fb9SVladimir Sementsov-Ogievskiy BdrvRequestFlags read_flags, 272667b51fb9SVladimir Sementsov-Ogievskiy BdrvRequestFlags write_flags) 27271efad060SFam Zheng { 272858a209c4SKevin Wolf RawPosixAIOData acb; 27291efad060SFam Zheng BDRVRawState *s = bs->opaque; 27301efad060SFam Zheng BDRVRawState *src_s; 27311efad060SFam Zheng 27321efad060SFam Zheng assert(dst->bs == bs); 27331efad060SFam Zheng if (src->bs->drv->bdrv_co_copy_range_to != raw_co_copy_range_to) { 27341efad060SFam Zheng return -ENOTSUP; 27351efad060SFam Zheng } 27361efad060SFam Zheng 27371efad060SFam Zheng src_s = src->bs->opaque; 27389f850f67SFam Zheng if (fd_open(src->bs) < 0 || fd_open(dst->bs) < 0) { 27391efad060SFam Zheng return -EIO; 27401efad060SFam Zheng } 274158a209c4SKevin Wolf 274258a209c4SKevin Wolf acb = (RawPosixAIOData) { 274358a209c4SKevin Wolf .bs = bs, 274458a209c4SKevin Wolf .aio_type = QEMU_AIO_COPY_RANGE, 274558a209c4SKevin Wolf .aio_fildes = src_s->fd, 274658a209c4SKevin Wolf .aio_offset = src_offset, 274758a209c4SKevin Wolf .aio_nbytes = bytes, 274858a209c4SKevin Wolf .copy_range = { 274958a209c4SKevin Wolf .aio_fd2 = s->fd, 275058a209c4SKevin Wolf .aio_offset2 = dst_offset, 275158a209c4SKevin Wolf }, 275258a209c4SKevin Wolf }; 275358a209c4SKevin Wolf 275458a209c4SKevin Wolf return raw_thread_pool_submit(bs, handle_aiocb_copy_range, &acb); 27551efad060SFam Zheng } 27561efad060SFam Zheng 2757c1bb86cdSEric Blake BlockDriver bdrv_file = { 2758c1bb86cdSEric Blake .format_name = "file", 2759c1bb86cdSEric Blake .protocol_name = "file", 2760c1bb86cdSEric Blake .instance_size = sizeof(BDRVRawState), 2761c1bb86cdSEric Blake .bdrv_needs_filename = true, 2762c1bb86cdSEric Blake .bdrv_probe = NULL, /* no probe for protocols */ 2763c1bb86cdSEric Blake .bdrv_parse_filename = raw_parse_filename, 2764c1bb86cdSEric Blake .bdrv_file_open = raw_open, 2765c1bb86cdSEric Blake .bdrv_reopen_prepare = raw_reopen_prepare, 2766c1bb86cdSEric Blake .bdrv_reopen_commit = raw_reopen_commit, 2767c1bb86cdSEric Blake .bdrv_reopen_abort = raw_reopen_abort, 2768c1bb86cdSEric Blake .bdrv_close = raw_close, 2769927f11e1SKevin Wolf .bdrv_co_create = raw_co_create, 2770efc75e2aSStefan Hajnoczi .bdrv_co_create_opts = raw_co_create_opts, 2771c1bb86cdSEric Blake .bdrv_has_zero_init = bdrv_has_zero_init_1, 2772a290f085SEric Blake .bdrv_co_block_status = raw_co_block_status, 2773dd577a26SStefan Hajnoczi .bdrv_co_invalidate_cache = raw_co_invalidate_cache, 2774c1bb86cdSEric Blake .bdrv_co_pwrite_zeroes = raw_co_pwrite_zeroes, 2775c1bb86cdSEric Blake 2776c1bb86cdSEric Blake .bdrv_co_preadv = raw_co_preadv, 2777c1bb86cdSEric Blake .bdrv_co_pwritev = raw_co_pwritev, 277833d70fb6SKevin Wolf .bdrv_co_flush_to_disk = raw_co_flush_to_disk, 277933d70fb6SKevin Wolf .bdrv_co_pdiscard = raw_co_pdiscard, 27801efad060SFam Zheng .bdrv_co_copy_range_from = raw_co_copy_range_from, 27811efad060SFam Zheng .bdrv_co_copy_range_to = raw_co_copy_range_to, 2782c1bb86cdSEric Blake .bdrv_refresh_limits = raw_refresh_limits, 2783c1bb86cdSEric Blake .bdrv_io_plug = raw_aio_plug, 2784c1bb86cdSEric Blake .bdrv_io_unplug = raw_aio_unplug, 2785ed6e2161SNishanth Aravamudan .bdrv_attach_aio_context = raw_aio_attach_aio_context, 2786c1bb86cdSEric Blake 2787061ca8a3SKevin Wolf .bdrv_co_truncate = raw_co_truncate, 2788c1bb86cdSEric Blake .bdrv_getlength = raw_getlength, 2789c1bb86cdSEric Blake .bdrv_get_info = raw_get_info, 2790c1bb86cdSEric Blake .bdrv_get_allocated_file_size 2791c1bb86cdSEric Blake = raw_get_allocated_file_size, 2792244a5668SFam Zheng .bdrv_check_perm = raw_check_perm, 2793244a5668SFam Zheng .bdrv_set_perm = raw_set_perm, 2794244a5668SFam Zheng .bdrv_abort_perm_update = raw_abort_perm_update, 2795c1bb86cdSEric Blake .create_opts = &raw_create_opts, 2796c1bb86cdSEric Blake }; 2797c1bb86cdSEric Blake 2798c1bb86cdSEric Blake /***********************************************/ 2799c1bb86cdSEric Blake /* host device */ 2800c1bb86cdSEric Blake 2801c1bb86cdSEric Blake #if defined(__APPLE__) && defined(__MACH__) 2802c1bb86cdSEric Blake static kern_return_t GetBSDPath(io_iterator_t mediaIterator, char *bsdPath, 2803c1bb86cdSEric Blake CFIndex maxPathSize, int flags); 2804c1bb86cdSEric Blake static char *FindEjectableOpticalMedia(io_iterator_t *mediaIterator) 2805c1bb86cdSEric Blake { 2806c1bb86cdSEric Blake kern_return_t kernResult = KERN_FAILURE; 2807c1bb86cdSEric Blake mach_port_t masterPort; 2808c1bb86cdSEric Blake CFMutableDictionaryRef classesToMatch; 2809c1bb86cdSEric Blake const char *matching_array[] = {kIODVDMediaClass, kIOCDMediaClass}; 2810c1bb86cdSEric Blake char *mediaType = NULL; 2811c1bb86cdSEric Blake 2812c1bb86cdSEric Blake kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort ); 2813c1bb86cdSEric Blake if ( KERN_SUCCESS != kernResult ) { 2814c1bb86cdSEric Blake printf( "IOMasterPort returned %d\n", kernResult ); 2815c1bb86cdSEric Blake } 2816c1bb86cdSEric Blake 2817c1bb86cdSEric Blake int index; 2818c1bb86cdSEric Blake for (index = 0; index < ARRAY_SIZE(matching_array); index++) { 2819c1bb86cdSEric Blake classesToMatch = IOServiceMatching(matching_array[index]); 2820c1bb86cdSEric Blake if (classesToMatch == NULL) { 2821c1bb86cdSEric Blake error_report("IOServiceMatching returned NULL for %s", 2822c1bb86cdSEric Blake matching_array[index]); 2823c1bb86cdSEric Blake continue; 2824c1bb86cdSEric Blake } 2825c1bb86cdSEric Blake CFDictionarySetValue(classesToMatch, CFSTR(kIOMediaEjectableKey), 2826c1bb86cdSEric Blake kCFBooleanTrue); 2827c1bb86cdSEric Blake kernResult = IOServiceGetMatchingServices(masterPort, classesToMatch, 2828c1bb86cdSEric Blake mediaIterator); 2829c1bb86cdSEric Blake if (kernResult != KERN_SUCCESS) { 2830c1bb86cdSEric Blake error_report("Note: IOServiceGetMatchingServices returned %d", 2831c1bb86cdSEric Blake kernResult); 2832c1bb86cdSEric Blake continue; 2833c1bb86cdSEric Blake } 2834c1bb86cdSEric Blake 2835c1bb86cdSEric Blake /* If a match was found, leave the loop */ 2836c1bb86cdSEric Blake if (*mediaIterator != 0) { 28374f7d28d7SLaurent Vivier trace_file_FindEjectableOpticalMedia(matching_array[index]); 2838c1bb86cdSEric Blake mediaType = g_strdup(matching_array[index]); 2839c1bb86cdSEric Blake break; 2840c1bb86cdSEric Blake } 2841c1bb86cdSEric Blake } 2842c1bb86cdSEric Blake return mediaType; 2843c1bb86cdSEric Blake } 2844c1bb86cdSEric Blake 2845c1bb86cdSEric Blake kern_return_t GetBSDPath(io_iterator_t mediaIterator, char *bsdPath, 2846c1bb86cdSEric Blake CFIndex maxPathSize, int flags) 2847c1bb86cdSEric Blake { 2848c1bb86cdSEric Blake io_object_t nextMedia; 2849c1bb86cdSEric Blake kern_return_t kernResult = KERN_FAILURE; 2850c1bb86cdSEric Blake *bsdPath = '\0'; 2851c1bb86cdSEric Blake nextMedia = IOIteratorNext( mediaIterator ); 2852c1bb86cdSEric Blake if ( nextMedia ) 2853c1bb86cdSEric Blake { 2854c1bb86cdSEric Blake CFTypeRef bsdPathAsCFString; 2855c1bb86cdSEric Blake bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 ); 2856c1bb86cdSEric Blake if ( bsdPathAsCFString ) { 2857c1bb86cdSEric Blake size_t devPathLength; 2858c1bb86cdSEric Blake strcpy( bsdPath, _PATH_DEV ); 2859c1bb86cdSEric Blake if (flags & BDRV_O_NOCACHE) { 2860c1bb86cdSEric Blake strcat(bsdPath, "r"); 2861c1bb86cdSEric Blake } 2862c1bb86cdSEric Blake devPathLength = strlen( bsdPath ); 2863c1bb86cdSEric Blake if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) { 2864c1bb86cdSEric Blake kernResult = KERN_SUCCESS; 2865c1bb86cdSEric Blake } 2866c1bb86cdSEric Blake CFRelease( bsdPathAsCFString ); 2867c1bb86cdSEric Blake } 2868c1bb86cdSEric Blake IOObjectRelease( nextMedia ); 2869c1bb86cdSEric Blake } 2870c1bb86cdSEric Blake 2871c1bb86cdSEric Blake return kernResult; 2872c1bb86cdSEric Blake } 2873c1bb86cdSEric Blake 2874c1bb86cdSEric Blake /* Sets up a real cdrom for use in QEMU */ 2875c1bb86cdSEric Blake static bool setup_cdrom(char *bsd_path, Error **errp) 2876c1bb86cdSEric Blake { 2877c1bb86cdSEric Blake int index, num_of_test_partitions = 2, fd; 2878c1bb86cdSEric Blake char test_partition[MAXPATHLEN]; 2879c1bb86cdSEric Blake bool partition_found = false; 2880c1bb86cdSEric Blake 2881c1bb86cdSEric Blake /* look for a working partition */ 2882c1bb86cdSEric Blake for (index = 0; index < num_of_test_partitions; index++) { 2883c1bb86cdSEric Blake snprintf(test_partition, sizeof(test_partition), "%ss%d", bsd_path, 2884c1bb86cdSEric Blake index); 2885c1bb86cdSEric Blake fd = qemu_open(test_partition, O_RDONLY | O_BINARY | O_LARGEFILE); 2886c1bb86cdSEric Blake if (fd >= 0) { 2887c1bb86cdSEric Blake partition_found = true; 2888c1bb86cdSEric Blake qemu_close(fd); 2889c1bb86cdSEric Blake break; 2890c1bb86cdSEric Blake } 2891c1bb86cdSEric Blake } 2892c1bb86cdSEric Blake 2893c1bb86cdSEric Blake /* if a working partition on the device was not found */ 2894c1bb86cdSEric Blake if (partition_found == false) { 2895c1bb86cdSEric Blake error_setg(errp, "Failed to find a working partition on disc"); 2896c1bb86cdSEric Blake } else { 28974f7d28d7SLaurent Vivier trace_file_setup_cdrom(test_partition); 2898c1bb86cdSEric Blake pstrcpy(bsd_path, MAXPATHLEN, test_partition); 2899c1bb86cdSEric Blake } 2900c1bb86cdSEric Blake return partition_found; 2901c1bb86cdSEric Blake } 2902c1bb86cdSEric Blake 2903c1bb86cdSEric Blake /* Prints directions on mounting and unmounting a device */ 2904c1bb86cdSEric Blake static void print_unmounting_directions(const char *file_name) 2905c1bb86cdSEric Blake { 2906c1bb86cdSEric Blake error_report("If device %s is mounted on the desktop, unmount" 2907c1bb86cdSEric Blake " it first before using it in QEMU", file_name); 2908c1bb86cdSEric Blake error_report("Command to unmount device: diskutil unmountDisk %s", 2909c1bb86cdSEric Blake file_name); 2910c1bb86cdSEric Blake error_report("Command to mount device: diskutil mountDisk %s", file_name); 2911c1bb86cdSEric Blake } 2912c1bb86cdSEric Blake 2913c1bb86cdSEric Blake #endif /* defined(__APPLE__) && defined(__MACH__) */ 2914c1bb86cdSEric Blake 2915c1bb86cdSEric Blake static int hdev_probe_device(const char *filename) 2916c1bb86cdSEric Blake { 2917c1bb86cdSEric Blake struct stat st; 2918c1bb86cdSEric Blake 2919c1bb86cdSEric Blake /* allow a dedicated CD-ROM driver to match with a higher priority */ 2920c1bb86cdSEric Blake if (strstart(filename, "/dev/cdrom", NULL)) 2921c1bb86cdSEric Blake return 50; 2922c1bb86cdSEric Blake 2923c1bb86cdSEric Blake if (stat(filename, &st) >= 0 && 2924c1bb86cdSEric Blake (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) { 2925c1bb86cdSEric Blake return 100; 2926c1bb86cdSEric Blake } 2927c1bb86cdSEric Blake 2928c1bb86cdSEric Blake return 0; 2929c1bb86cdSEric Blake } 2930c1bb86cdSEric Blake 2931c1bb86cdSEric Blake static int check_hdev_writable(BDRVRawState *s) 2932c1bb86cdSEric Blake { 2933c1bb86cdSEric Blake #if defined(BLKROGET) 2934c1bb86cdSEric Blake /* Linux block devices can be configured "read-only" using blockdev(8). 2935c1bb86cdSEric Blake * This is independent of device node permissions and therefore open(2) 2936c1bb86cdSEric Blake * with O_RDWR succeeds. Actual writes fail with EPERM. 2937c1bb86cdSEric Blake * 2938c1bb86cdSEric Blake * bdrv_open() is supposed to fail if the disk is read-only. Explicitly 2939c1bb86cdSEric Blake * check for read-only block devices so that Linux block devices behave 2940c1bb86cdSEric Blake * properly. 2941c1bb86cdSEric Blake */ 2942c1bb86cdSEric Blake struct stat st; 2943c1bb86cdSEric Blake int readonly = 0; 2944c1bb86cdSEric Blake 2945c1bb86cdSEric Blake if (fstat(s->fd, &st)) { 2946c1bb86cdSEric Blake return -errno; 2947c1bb86cdSEric Blake } 2948c1bb86cdSEric Blake 2949c1bb86cdSEric Blake if (!S_ISBLK(st.st_mode)) { 2950c1bb86cdSEric Blake return 0; 2951c1bb86cdSEric Blake } 2952c1bb86cdSEric Blake 2953c1bb86cdSEric Blake if (ioctl(s->fd, BLKROGET, &readonly) < 0) { 2954c1bb86cdSEric Blake return -errno; 2955c1bb86cdSEric Blake } 2956c1bb86cdSEric Blake 2957c1bb86cdSEric Blake if (readonly) { 2958c1bb86cdSEric Blake return -EACCES; 2959c1bb86cdSEric Blake } 2960c1bb86cdSEric Blake #endif /* defined(BLKROGET) */ 2961c1bb86cdSEric Blake return 0; 2962c1bb86cdSEric Blake } 2963c1bb86cdSEric Blake 2964c1bb86cdSEric Blake static void hdev_parse_filename(const char *filename, QDict *options, 2965c1bb86cdSEric Blake Error **errp) 2966c1bb86cdSEric Blake { 296703c320d8SMax Reitz bdrv_parse_filename_strip_prefix(filename, "host_device:", options); 2968c1bb86cdSEric Blake } 2969c1bb86cdSEric Blake 2970c1bb86cdSEric Blake static bool hdev_is_sg(BlockDriverState *bs) 2971c1bb86cdSEric Blake { 2972c1bb86cdSEric Blake 2973c1bb86cdSEric Blake #if defined(__linux__) 2974c1bb86cdSEric Blake 2975c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2976c1bb86cdSEric Blake struct stat st; 2977c1bb86cdSEric Blake struct sg_scsi_id scsiid; 2978c1bb86cdSEric Blake int sg_version; 2979c1bb86cdSEric Blake int ret; 2980c1bb86cdSEric Blake 2981c1bb86cdSEric Blake if (stat(bs->filename, &st) < 0 || !S_ISCHR(st.st_mode)) { 2982c1bb86cdSEric Blake return false; 2983c1bb86cdSEric Blake } 2984c1bb86cdSEric Blake 2985c1bb86cdSEric Blake ret = ioctl(s->fd, SG_GET_VERSION_NUM, &sg_version); 2986c1bb86cdSEric Blake if (ret < 0) { 2987c1bb86cdSEric Blake return false; 2988c1bb86cdSEric Blake } 2989c1bb86cdSEric Blake 2990c1bb86cdSEric Blake ret = ioctl(s->fd, SG_GET_SCSI_ID, &scsiid); 2991c1bb86cdSEric Blake if (ret >= 0) { 29924f7d28d7SLaurent Vivier trace_file_hdev_is_sg(scsiid.scsi_type, sg_version); 2993c1bb86cdSEric Blake return true; 2994c1bb86cdSEric Blake } 2995c1bb86cdSEric Blake 2996c1bb86cdSEric Blake #endif 2997c1bb86cdSEric Blake 2998c1bb86cdSEric Blake return false; 2999c1bb86cdSEric Blake } 3000c1bb86cdSEric Blake 3001c1bb86cdSEric Blake static int hdev_open(BlockDriverState *bs, QDict *options, int flags, 3002c1bb86cdSEric Blake Error **errp) 3003c1bb86cdSEric Blake { 3004c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 3005c1bb86cdSEric Blake Error *local_err = NULL; 3006c1bb86cdSEric Blake int ret; 3007c1bb86cdSEric Blake 3008c1bb86cdSEric Blake #if defined(__APPLE__) && defined(__MACH__) 3009129c7d1cSMarkus Armbruster /* 3010129c7d1cSMarkus Armbruster * Caution: while qdict_get_str() is fine, getting non-string types 3011129c7d1cSMarkus Armbruster * would require more care. When @options come from -blockdev or 3012129c7d1cSMarkus Armbruster * blockdev_add, its members are typed according to the QAPI 3013129c7d1cSMarkus Armbruster * schema, but when they come from -drive, they're all QString. 3014129c7d1cSMarkus Armbruster */ 3015c1bb86cdSEric Blake const char *filename = qdict_get_str(options, "filename"); 3016c1bb86cdSEric Blake char bsd_path[MAXPATHLEN] = ""; 3017c1bb86cdSEric Blake bool error_occurred = false; 3018c1bb86cdSEric Blake 3019c1bb86cdSEric Blake /* If using a real cdrom */ 3020c1bb86cdSEric Blake if (strcmp(filename, "/dev/cdrom") == 0) { 3021c1bb86cdSEric Blake char *mediaType = NULL; 3022c1bb86cdSEric Blake kern_return_t ret_val; 3023c1bb86cdSEric Blake io_iterator_t mediaIterator = 0; 3024c1bb86cdSEric Blake 3025c1bb86cdSEric Blake mediaType = FindEjectableOpticalMedia(&mediaIterator); 3026c1bb86cdSEric Blake if (mediaType == NULL) { 3027c1bb86cdSEric Blake error_setg(errp, "Please make sure your CD/DVD is in the optical" 3028c1bb86cdSEric Blake " drive"); 3029c1bb86cdSEric Blake error_occurred = true; 3030c1bb86cdSEric Blake goto hdev_open_Mac_error; 3031c1bb86cdSEric Blake } 3032c1bb86cdSEric Blake 3033c1bb86cdSEric Blake ret_val = GetBSDPath(mediaIterator, bsd_path, sizeof(bsd_path), flags); 3034c1bb86cdSEric Blake if (ret_val != KERN_SUCCESS) { 3035c1bb86cdSEric Blake error_setg(errp, "Could not get BSD path for optical drive"); 3036c1bb86cdSEric Blake error_occurred = true; 3037c1bb86cdSEric Blake goto hdev_open_Mac_error; 3038c1bb86cdSEric Blake } 3039c1bb86cdSEric Blake 3040c1bb86cdSEric Blake /* If a real optical drive was not found */ 3041c1bb86cdSEric Blake if (bsd_path[0] == '\0') { 3042c1bb86cdSEric Blake error_setg(errp, "Failed to obtain bsd path for optical drive"); 3043c1bb86cdSEric Blake error_occurred = true; 3044c1bb86cdSEric Blake goto hdev_open_Mac_error; 3045c1bb86cdSEric Blake } 3046c1bb86cdSEric Blake 3047c1bb86cdSEric Blake /* If using a cdrom disc and finding a partition on the disc failed */ 3048c1bb86cdSEric Blake if (strncmp(mediaType, kIOCDMediaClass, 9) == 0 && 3049c1bb86cdSEric Blake setup_cdrom(bsd_path, errp) == false) { 3050c1bb86cdSEric Blake print_unmounting_directions(bsd_path); 3051c1bb86cdSEric Blake error_occurred = true; 3052c1bb86cdSEric Blake goto hdev_open_Mac_error; 3053c1bb86cdSEric Blake } 3054c1bb86cdSEric Blake 305546f5ac20SEric Blake qdict_put_str(options, "filename", bsd_path); 3056c1bb86cdSEric Blake 3057c1bb86cdSEric Blake hdev_open_Mac_error: 3058c1bb86cdSEric Blake g_free(mediaType); 3059c1bb86cdSEric Blake if (mediaIterator) { 3060c1bb86cdSEric Blake IOObjectRelease(mediaIterator); 3061c1bb86cdSEric Blake } 3062c1bb86cdSEric Blake if (error_occurred) { 3063c1bb86cdSEric Blake return -ENOENT; 3064c1bb86cdSEric Blake } 3065c1bb86cdSEric Blake } 3066c1bb86cdSEric Blake #endif /* defined(__APPLE__) && defined(__MACH__) */ 3067c1bb86cdSEric Blake 3068c1bb86cdSEric Blake s->type = FTYPE_FILE; 3069c1bb86cdSEric Blake 3070230ff739SJohn Snow ret = raw_open_common(bs, options, flags, 0, true, &local_err); 3071c1bb86cdSEric Blake if (ret < 0) { 3072c1bb86cdSEric Blake error_propagate(errp, local_err); 3073c1bb86cdSEric Blake #if defined(__APPLE__) && defined(__MACH__) 3074c1bb86cdSEric Blake if (*bsd_path) { 3075c1bb86cdSEric Blake filename = bsd_path; 3076c1bb86cdSEric Blake } 3077c1bb86cdSEric Blake /* if a physical device experienced an error while being opened */ 3078c1bb86cdSEric Blake if (strncmp(filename, "/dev/", 5) == 0) { 3079c1bb86cdSEric Blake print_unmounting_directions(filename); 3080c1bb86cdSEric Blake } 3081c1bb86cdSEric Blake #endif /* defined(__APPLE__) && defined(__MACH__) */ 3082c1bb86cdSEric Blake return ret; 3083c1bb86cdSEric Blake } 3084c1bb86cdSEric Blake 3085c1bb86cdSEric Blake /* Since this does ioctl the device must be already opened */ 3086c1bb86cdSEric Blake bs->sg = hdev_is_sg(bs); 3087c1bb86cdSEric Blake 3088c1bb86cdSEric Blake if (flags & BDRV_O_RDWR) { 3089c1bb86cdSEric Blake ret = check_hdev_writable(s); 3090c1bb86cdSEric Blake if (ret < 0) { 3091c1bb86cdSEric Blake raw_close(bs); 3092c1bb86cdSEric Blake error_setg_errno(errp, -ret, "The device is not writable"); 3093c1bb86cdSEric Blake return ret; 3094c1bb86cdSEric Blake } 3095c1bb86cdSEric Blake } 3096c1bb86cdSEric Blake 3097c1bb86cdSEric Blake return ret; 3098c1bb86cdSEric Blake } 3099c1bb86cdSEric Blake 3100c1bb86cdSEric Blake #if defined(__linux__) 31012f3a7ab3SKevin Wolf static int coroutine_fn 31022f3a7ab3SKevin Wolf hdev_co_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) 3103c1bb86cdSEric Blake { 3104c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 310503425671SKevin Wolf RawPosixAIOData acb; 31062f3a7ab3SKevin Wolf int ret; 3107c1bb86cdSEric Blake 31082f3a7ab3SKevin Wolf ret = fd_open(bs); 31092f3a7ab3SKevin Wolf if (ret < 0) { 31102f3a7ab3SKevin Wolf return ret; 31112f3a7ab3SKevin Wolf } 3112c1bb86cdSEric Blake 31137c9e5276SPaolo Bonzini if (req == SG_IO && s->pr_mgr) { 31147c9e5276SPaolo Bonzini struct sg_io_hdr *io_hdr = buf; 31157c9e5276SPaolo Bonzini if (io_hdr->cmdp[0] == PERSISTENT_RESERVE_OUT || 31167c9e5276SPaolo Bonzini io_hdr->cmdp[0] == PERSISTENT_RESERVE_IN) { 31177c9e5276SPaolo Bonzini return pr_manager_execute(s->pr_mgr, bdrv_get_aio_context(bs), 31182f3a7ab3SKevin Wolf s->fd, io_hdr); 31197c9e5276SPaolo Bonzini } 31207c9e5276SPaolo Bonzini } 31217c9e5276SPaolo Bonzini 312203425671SKevin Wolf acb = (RawPosixAIOData) { 312303425671SKevin Wolf .bs = bs, 312403425671SKevin Wolf .aio_type = QEMU_AIO_IOCTL, 312503425671SKevin Wolf .aio_fildes = s->fd, 312603425671SKevin Wolf .aio_offset = 0, 312703425671SKevin Wolf .ioctl = { 312803425671SKevin Wolf .buf = buf, 312903425671SKevin Wolf .cmd = req, 313003425671SKevin Wolf }, 313103425671SKevin Wolf }; 313203425671SKevin Wolf 313303425671SKevin Wolf return raw_thread_pool_submit(bs, handle_aiocb_ioctl, &acb); 3134c1bb86cdSEric Blake } 3135c1bb86cdSEric Blake #endif /* linux */ 3136c1bb86cdSEric Blake 3137c1bb86cdSEric Blake static int fd_open(BlockDriverState *bs) 3138c1bb86cdSEric Blake { 3139c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 3140c1bb86cdSEric Blake 3141c1bb86cdSEric Blake /* this is just to ensure s->fd is sane (its called by io ops) */ 3142c1bb86cdSEric Blake if (s->fd >= 0) 3143c1bb86cdSEric Blake return 0; 3144c1bb86cdSEric Blake return -EIO; 3145c1bb86cdSEric Blake } 3146c1bb86cdSEric Blake 314733d70fb6SKevin Wolf static coroutine_fn int 314833d70fb6SKevin Wolf hdev_co_pdiscard(BlockDriverState *bs, int64_t offset, int bytes) 3149c1bb86cdSEric Blake { 315033d70fb6SKevin Wolf int ret; 3151c1bb86cdSEric Blake 315233d70fb6SKevin Wolf ret = fd_open(bs); 315333d70fb6SKevin Wolf if (ret < 0) { 315433d70fb6SKevin Wolf return ret; 3155c1bb86cdSEric Blake } 315646ee0f46SKevin Wolf return raw_do_pdiscard(bs, offset, bytes, true); 3157c1bb86cdSEric Blake } 3158c1bb86cdSEric Blake 3159c1bb86cdSEric Blake static coroutine_fn int hdev_co_pwrite_zeroes(BlockDriverState *bs, 3160f5a5ca79SManos Pitsidianakis int64_t offset, int bytes, BdrvRequestFlags flags) 3161c1bb86cdSEric Blake { 3162c1bb86cdSEric Blake int rc; 3163c1bb86cdSEric Blake 3164c1bb86cdSEric Blake rc = fd_open(bs); 3165c1bb86cdSEric Blake if (rc < 0) { 3166c1bb86cdSEric Blake return rc; 3167c1bb86cdSEric Blake } 316834fa110eSKevin Wolf 31697154d8aeSKevin Wolf return raw_do_pwrite_zeroes(bs, offset, bytes, flags, true); 3170c1bb86cdSEric Blake } 3171c1bb86cdSEric Blake 3172efc75e2aSStefan Hajnoczi static int coroutine_fn hdev_co_create_opts(const char *filename, QemuOpts *opts, 3173c1bb86cdSEric Blake Error **errp) 3174c1bb86cdSEric Blake { 3175c1bb86cdSEric Blake int fd; 3176c1bb86cdSEric Blake int ret = 0; 3177c1bb86cdSEric Blake struct stat stat_buf; 3178c1bb86cdSEric Blake int64_t total_size = 0; 3179c1bb86cdSEric Blake bool has_prefix; 3180c1bb86cdSEric Blake 3181c1bb86cdSEric Blake /* This function is used by both protocol block drivers and therefore either 3182c1bb86cdSEric Blake * of these prefixes may be given. 3183c1bb86cdSEric Blake * The return value has to be stored somewhere, otherwise this is an error 3184c1bb86cdSEric Blake * due to -Werror=unused-value. */ 3185c1bb86cdSEric Blake has_prefix = 3186c1bb86cdSEric Blake strstart(filename, "host_device:", &filename) || 3187c1bb86cdSEric Blake strstart(filename, "host_cdrom:" , &filename); 3188c1bb86cdSEric Blake 3189c1bb86cdSEric Blake (void)has_prefix; 3190c1bb86cdSEric Blake 3191db0754dfSFam Zheng ret = raw_normalize_devicepath(&filename, errp); 3192c1bb86cdSEric Blake if (ret < 0) { 3193c1bb86cdSEric Blake return ret; 3194c1bb86cdSEric Blake } 3195c1bb86cdSEric Blake 3196c1bb86cdSEric Blake /* Read out options */ 3197c1bb86cdSEric Blake total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), 3198c1bb86cdSEric Blake BDRV_SECTOR_SIZE); 3199c1bb86cdSEric Blake 3200c1bb86cdSEric Blake fd = qemu_open(filename, O_WRONLY | O_BINARY); 3201c1bb86cdSEric Blake if (fd < 0) { 3202c1bb86cdSEric Blake ret = -errno; 3203c1bb86cdSEric Blake error_setg_errno(errp, -ret, "Could not open device"); 3204c1bb86cdSEric Blake return ret; 3205c1bb86cdSEric Blake } 3206c1bb86cdSEric Blake 3207c1bb86cdSEric Blake if (fstat(fd, &stat_buf) < 0) { 3208c1bb86cdSEric Blake ret = -errno; 3209c1bb86cdSEric Blake error_setg_errno(errp, -ret, "Could not stat device"); 3210c1bb86cdSEric Blake } else if (!S_ISBLK(stat_buf.st_mode) && !S_ISCHR(stat_buf.st_mode)) { 3211c1bb86cdSEric Blake error_setg(errp, 3212c1bb86cdSEric Blake "The given file is neither a block nor a character device"); 3213c1bb86cdSEric Blake ret = -ENODEV; 3214c1bb86cdSEric Blake } else if (lseek(fd, 0, SEEK_END) < total_size) { 3215c1bb86cdSEric Blake error_setg(errp, "Device is too small"); 3216c1bb86cdSEric Blake ret = -ENOSPC; 3217c1bb86cdSEric Blake } 3218c1bb86cdSEric Blake 321997ec9117SFam Zheng if (!ret && total_size) { 322097ec9117SFam Zheng uint8_t buf[BDRV_SECTOR_SIZE] = { 0 }; 322197ec9117SFam Zheng int64_t zero_size = MIN(BDRV_SECTOR_SIZE, total_size); 322297ec9117SFam Zheng if (lseek(fd, 0, SEEK_SET) == -1) { 322397ec9117SFam Zheng ret = -errno; 322497ec9117SFam Zheng } else { 322597ec9117SFam Zheng ret = qemu_write_full(fd, buf, zero_size); 322697ec9117SFam Zheng ret = ret == zero_size ? 0 : -errno; 322797ec9117SFam Zheng } 322897ec9117SFam Zheng } 3229c1bb86cdSEric Blake qemu_close(fd); 3230c1bb86cdSEric Blake return ret; 3231c1bb86cdSEric Blake } 3232c1bb86cdSEric Blake 3233c1bb86cdSEric Blake static BlockDriver bdrv_host_device = { 3234c1bb86cdSEric Blake .format_name = "host_device", 3235c1bb86cdSEric Blake .protocol_name = "host_device", 3236c1bb86cdSEric Blake .instance_size = sizeof(BDRVRawState), 3237c1bb86cdSEric Blake .bdrv_needs_filename = true, 3238c1bb86cdSEric Blake .bdrv_probe_device = hdev_probe_device, 3239c1bb86cdSEric Blake .bdrv_parse_filename = hdev_parse_filename, 3240c1bb86cdSEric Blake .bdrv_file_open = hdev_open, 3241c1bb86cdSEric Blake .bdrv_close = raw_close, 3242c1bb86cdSEric Blake .bdrv_reopen_prepare = raw_reopen_prepare, 3243c1bb86cdSEric Blake .bdrv_reopen_commit = raw_reopen_commit, 3244c1bb86cdSEric Blake .bdrv_reopen_abort = raw_reopen_abort, 3245efc75e2aSStefan Hajnoczi .bdrv_co_create_opts = hdev_co_create_opts, 3246c1bb86cdSEric Blake .create_opts = &raw_create_opts, 3247dd577a26SStefan Hajnoczi .bdrv_co_invalidate_cache = raw_co_invalidate_cache, 3248c1bb86cdSEric Blake .bdrv_co_pwrite_zeroes = hdev_co_pwrite_zeroes, 3249c1bb86cdSEric Blake 3250c1bb86cdSEric Blake .bdrv_co_preadv = raw_co_preadv, 3251c1bb86cdSEric Blake .bdrv_co_pwritev = raw_co_pwritev, 325233d70fb6SKevin Wolf .bdrv_co_flush_to_disk = raw_co_flush_to_disk, 325333d70fb6SKevin Wolf .bdrv_co_pdiscard = hdev_co_pdiscard, 32541efad060SFam Zheng .bdrv_co_copy_range_from = raw_co_copy_range_from, 32551efad060SFam Zheng .bdrv_co_copy_range_to = raw_co_copy_range_to, 3256c1bb86cdSEric Blake .bdrv_refresh_limits = raw_refresh_limits, 3257c1bb86cdSEric Blake .bdrv_io_plug = raw_aio_plug, 3258c1bb86cdSEric Blake .bdrv_io_unplug = raw_aio_unplug, 3259042b757cSNishanth Aravamudan .bdrv_attach_aio_context = raw_aio_attach_aio_context, 3260c1bb86cdSEric Blake 3261061ca8a3SKevin Wolf .bdrv_co_truncate = raw_co_truncate, 3262c1bb86cdSEric Blake .bdrv_getlength = raw_getlength, 3263c1bb86cdSEric Blake .bdrv_get_info = raw_get_info, 3264c1bb86cdSEric Blake .bdrv_get_allocated_file_size 3265c1bb86cdSEric Blake = raw_get_allocated_file_size, 3266244a5668SFam Zheng .bdrv_check_perm = raw_check_perm, 3267244a5668SFam Zheng .bdrv_set_perm = raw_set_perm, 3268244a5668SFam Zheng .bdrv_abort_perm_update = raw_abort_perm_update, 3269c1bb86cdSEric Blake .bdrv_probe_blocksizes = hdev_probe_blocksizes, 3270c1bb86cdSEric Blake .bdrv_probe_geometry = hdev_probe_geometry, 3271c1bb86cdSEric Blake 3272c1bb86cdSEric Blake /* generic scsi device */ 3273c1bb86cdSEric Blake #ifdef __linux__ 32742f3a7ab3SKevin Wolf .bdrv_co_ioctl = hdev_co_ioctl, 3275c1bb86cdSEric Blake #endif 3276c1bb86cdSEric Blake }; 3277c1bb86cdSEric Blake 3278c1bb86cdSEric Blake #if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) 3279c1bb86cdSEric Blake static void cdrom_parse_filename(const char *filename, QDict *options, 3280c1bb86cdSEric Blake Error **errp) 3281c1bb86cdSEric Blake { 328203c320d8SMax Reitz bdrv_parse_filename_strip_prefix(filename, "host_cdrom:", options); 3283c1bb86cdSEric Blake } 3284c1bb86cdSEric Blake #endif 3285c1bb86cdSEric Blake 3286c1bb86cdSEric Blake #ifdef __linux__ 3287c1bb86cdSEric Blake static int cdrom_open(BlockDriverState *bs, QDict *options, int flags, 3288c1bb86cdSEric Blake Error **errp) 3289c1bb86cdSEric Blake { 3290c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 3291c1bb86cdSEric Blake 3292c1bb86cdSEric Blake s->type = FTYPE_CD; 3293c1bb86cdSEric Blake 3294c1bb86cdSEric Blake /* open will not fail even if no CD is inserted, so add O_NONBLOCK */ 3295230ff739SJohn Snow return raw_open_common(bs, options, flags, O_NONBLOCK, true, errp); 3296c1bb86cdSEric Blake } 3297c1bb86cdSEric Blake 3298c1bb86cdSEric Blake static int cdrom_probe_device(const char *filename) 3299c1bb86cdSEric Blake { 3300c1bb86cdSEric Blake int fd, ret; 3301c1bb86cdSEric Blake int prio = 0; 3302c1bb86cdSEric Blake struct stat st; 3303c1bb86cdSEric Blake 3304c1bb86cdSEric Blake fd = qemu_open(filename, O_RDONLY | O_NONBLOCK); 3305c1bb86cdSEric Blake if (fd < 0) { 3306c1bb86cdSEric Blake goto out; 3307c1bb86cdSEric Blake } 3308c1bb86cdSEric Blake ret = fstat(fd, &st); 3309c1bb86cdSEric Blake if (ret == -1 || !S_ISBLK(st.st_mode)) { 3310c1bb86cdSEric Blake goto outc; 3311c1bb86cdSEric Blake } 3312c1bb86cdSEric Blake 3313c1bb86cdSEric Blake /* Attempt to detect via a CDROM specific ioctl */ 3314c1bb86cdSEric Blake ret = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT); 3315c1bb86cdSEric Blake if (ret >= 0) 3316c1bb86cdSEric Blake prio = 100; 3317c1bb86cdSEric Blake 3318c1bb86cdSEric Blake outc: 3319c1bb86cdSEric Blake qemu_close(fd); 3320c1bb86cdSEric Blake out: 3321c1bb86cdSEric Blake return prio; 3322c1bb86cdSEric Blake } 3323c1bb86cdSEric Blake 3324c1bb86cdSEric Blake static bool cdrom_is_inserted(BlockDriverState *bs) 3325c1bb86cdSEric Blake { 3326c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 3327c1bb86cdSEric Blake int ret; 3328c1bb86cdSEric Blake 3329c1bb86cdSEric Blake ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT); 3330c1bb86cdSEric Blake return ret == CDS_DISC_OK; 3331c1bb86cdSEric Blake } 3332c1bb86cdSEric Blake 3333c1bb86cdSEric Blake static void cdrom_eject(BlockDriverState *bs, bool eject_flag) 3334c1bb86cdSEric Blake { 3335c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 3336c1bb86cdSEric Blake 3337c1bb86cdSEric Blake if (eject_flag) { 3338c1bb86cdSEric Blake if (ioctl(s->fd, CDROMEJECT, NULL) < 0) 3339c1bb86cdSEric Blake perror("CDROMEJECT"); 3340c1bb86cdSEric Blake } else { 3341c1bb86cdSEric Blake if (ioctl(s->fd, CDROMCLOSETRAY, NULL) < 0) 3342c1bb86cdSEric Blake perror("CDROMEJECT"); 3343c1bb86cdSEric Blake } 3344c1bb86cdSEric Blake } 3345c1bb86cdSEric Blake 3346c1bb86cdSEric Blake static void cdrom_lock_medium(BlockDriverState *bs, bool locked) 3347c1bb86cdSEric Blake { 3348c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 3349c1bb86cdSEric Blake 3350c1bb86cdSEric Blake if (ioctl(s->fd, CDROM_LOCKDOOR, locked) < 0) { 3351c1bb86cdSEric Blake /* 3352c1bb86cdSEric Blake * Note: an error can happen if the distribution automatically 3353c1bb86cdSEric Blake * mounts the CD-ROM 3354c1bb86cdSEric Blake */ 3355c1bb86cdSEric Blake /* perror("CDROM_LOCKDOOR"); */ 3356c1bb86cdSEric Blake } 3357c1bb86cdSEric Blake } 3358c1bb86cdSEric Blake 3359c1bb86cdSEric Blake static BlockDriver bdrv_host_cdrom = { 3360c1bb86cdSEric Blake .format_name = "host_cdrom", 3361c1bb86cdSEric Blake .protocol_name = "host_cdrom", 3362c1bb86cdSEric Blake .instance_size = sizeof(BDRVRawState), 3363c1bb86cdSEric Blake .bdrv_needs_filename = true, 3364c1bb86cdSEric Blake .bdrv_probe_device = cdrom_probe_device, 3365c1bb86cdSEric Blake .bdrv_parse_filename = cdrom_parse_filename, 3366c1bb86cdSEric Blake .bdrv_file_open = cdrom_open, 3367c1bb86cdSEric Blake .bdrv_close = raw_close, 3368c1bb86cdSEric Blake .bdrv_reopen_prepare = raw_reopen_prepare, 3369c1bb86cdSEric Blake .bdrv_reopen_commit = raw_reopen_commit, 3370c1bb86cdSEric Blake .bdrv_reopen_abort = raw_reopen_abort, 3371efc75e2aSStefan Hajnoczi .bdrv_co_create_opts = hdev_co_create_opts, 3372c1bb86cdSEric Blake .create_opts = &raw_create_opts, 3373dd577a26SStefan Hajnoczi .bdrv_co_invalidate_cache = raw_co_invalidate_cache, 3374c1bb86cdSEric Blake 3375c1bb86cdSEric Blake 3376c1bb86cdSEric Blake .bdrv_co_preadv = raw_co_preadv, 3377c1bb86cdSEric Blake .bdrv_co_pwritev = raw_co_pwritev, 337833d70fb6SKevin Wolf .bdrv_co_flush_to_disk = raw_co_flush_to_disk, 3379c1bb86cdSEric Blake .bdrv_refresh_limits = raw_refresh_limits, 3380c1bb86cdSEric Blake .bdrv_io_plug = raw_aio_plug, 3381c1bb86cdSEric Blake .bdrv_io_unplug = raw_aio_unplug, 3382042b757cSNishanth Aravamudan .bdrv_attach_aio_context = raw_aio_attach_aio_context, 3383c1bb86cdSEric Blake 3384061ca8a3SKevin Wolf .bdrv_co_truncate = raw_co_truncate, 3385c1bb86cdSEric Blake .bdrv_getlength = raw_getlength, 3386c1bb86cdSEric Blake .has_variable_length = true, 3387c1bb86cdSEric Blake .bdrv_get_allocated_file_size 3388c1bb86cdSEric Blake = raw_get_allocated_file_size, 3389c1bb86cdSEric Blake 3390c1bb86cdSEric Blake /* removable device support */ 3391c1bb86cdSEric Blake .bdrv_is_inserted = cdrom_is_inserted, 3392c1bb86cdSEric Blake .bdrv_eject = cdrom_eject, 3393c1bb86cdSEric Blake .bdrv_lock_medium = cdrom_lock_medium, 3394c1bb86cdSEric Blake 3395c1bb86cdSEric Blake /* generic scsi device */ 33962f3a7ab3SKevin Wolf .bdrv_co_ioctl = hdev_co_ioctl, 3397c1bb86cdSEric Blake }; 3398c1bb86cdSEric Blake #endif /* __linux__ */ 3399c1bb86cdSEric Blake 3400c1bb86cdSEric Blake #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__) 3401c1bb86cdSEric Blake static int cdrom_open(BlockDriverState *bs, QDict *options, int flags, 3402c1bb86cdSEric Blake Error **errp) 3403c1bb86cdSEric Blake { 3404c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 3405c1bb86cdSEric Blake Error *local_err = NULL; 3406c1bb86cdSEric Blake int ret; 3407c1bb86cdSEric Blake 3408c1bb86cdSEric Blake s->type = FTYPE_CD; 3409c1bb86cdSEric Blake 3410230ff739SJohn Snow ret = raw_open_common(bs, options, flags, 0, true, &local_err); 3411c1bb86cdSEric Blake if (ret) { 3412c1bb86cdSEric Blake error_propagate(errp, local_err); 3413c1bb86cdSEric Blake return ret; 3414c1bb86cdSEric Blake } 3415c1bb86cdSEric Blake 3416c1bb86cdSEric Blake /* make sure the door isn't locked at this time */ 3417c1bb86cdSEric Blake ioctl(s->fd, CDIOCALLOW); 3418c1bb86cdSEric Blake return 0; 3419c1bb86cdSEric Blake } 3420c1bb86cdSEric Blake 3421c1bb86cdSEric Blake static int cdrom_probe_device(const char *filename) 3422c1bb86cdSEric Blake { 3423c1bb86cdSEric Blake if (strstart(filename, "/dev/cd", NULL) || 3424c1bb86cdSEric Blake strstart(filename, "/dev/acd", NULL)) 3425c1bb86cdSEric Blake return 100; 3426c1bb86cdSEric Blake return 0; 3427c1bb86cdSEric Blake } 3428c1bb86cdSEric Blake 3429c1bb86cdSEric Blake static int cdrom_reopen(BlockDriverState *bs) 3430c1bb86cdSEric Blake { 3431c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 3432c1bb86cdSEric Blake int fd; 3433c1bb86cdSEric Blake 3434c1bb86cdSEric Blake /* 3435c1bb86cdSEric Blake * Force reread of possibly changed/newly loaded disc, 3436c1bb86cdSEric Blake * FreeBSD seems to not notice sometimes... 3437c1bb86cdSEric Blake */ 3438c1bb86cdSEric Blake if (s->fd >= 0) 3439c1bb86cdSEric Blake qemu_close(s->fd); 3440c1bb86cdSEric Blake fd = qemu_open(bs->filename, s->open_flags, 0644); 3441c1bb86cdSEric Blake if (fd < 0) { 3442c1bb86cdSEric Blake s->fd = -1; 3443c1bb86cdSEric Blake return -EIO; 3444c1bb86cdSEric Blake } 3445c1bb86cdSEric Blake s->fd = fd; 3446c1bb86cdSEric Blake 3447c1bb86cdSEric Blake /* make sure the door isn't locked at this time */ 3448c1bb86cdSEric Blake ioctl(s->fd, CDIOCALLOW); 3449c1bb86cdSEric Blake return 0; 3450c1bb86cdSEric Blake } 3451c1bb86cdSEric Blake 3452c1bb86cdSEric Blake static bool cdrom_is_inserted(BlockDriverState *bs) 3453c1bb86cdSEric Blake { 3454c1bb86cdSEric Blake return raw_getlength(bs) > 0; 3455c1bb86cdSEric Blake } 3456c1bb86cdSEric Blake 3457c1bb86cdSEric Blake static void cdrom_eject(BlockDriverState *bs, bool eject_flag) 3458c1bb86cdSEric Blake { 3459c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 3460c1bb86cdSEric Blake 3461c1bb86cdSEric Blake if (s->fd < 0) 3462c1bb86cdSEric Blake return; 3463c1bb86cdSEric Blake 3464c1bb86cdSEric Blake (void) ioctl(s->fd, CDIOCALLOW); 3465c1bb86cdSEric Blake 3466c1bb86cdSEric Blake if (eject_flag) { 3467c1bb86cdSEric Blake if (ioctl(s->fd, CDIOCEJECT) < 0) 3468c1bb86cdSEric Blake perror("CDIOCEJECT"); 3469c1bb86cdSEric Blake } else { 3470c1bb86cdSEric Blake if (ioctl(s->fd, CDIOCCLOSE) < 0) 3471c1bb86cdSEric Blake perror("CDIOCCLOSE"); 3472c1bb86cdSEric Blake } 3473c1bb86cdSEric Blake 3474c1bb86cdSEric Blake cdrom_reopen(bs); 3475c1bb86cdSEric Blake } 3476c1bb86cdSEric Blake 3477c1bb86cdSEric Blake static void cdrom_lock_medium(BlockDriverState *bs, bool locked) 3478c1bb86cdSEric Blake { 3479c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 3480c1bb86cdSEric Blake 3481c1bb86cdSEric Blake if (s->fd < 0) 3482c1bb86cdSEric Blake return; 3483c1bb86cdSEric Blake if (ioctl(s->fd, (locked ? CDIOCPREVENT : CDIOCALLOW)) < 0) { 3484c1bb86cdSEric Blake /* 3485c1bb86cdSEric Blake * Note: an error can happen if the distribution automatically 3486c1bb86cdSEric Blake * mounts the CD-ROM 3487c1bb86cdSEric Blake */ 3488c1bb86cdSEric Blake /* perror("CDROM_LOCKDOOR"); */ 3489c1bb86cdSEric Blake } 3490c1bb86cdSEric Blake } 3491c1bb86cdSEric Blake 3492c1bb86cdSEric Blake static BlockDriver bdrv_host_cdrom = { 3493c1bb86cdSEric Blake .format_name = "host_cdrom", 3494c1bb86cdSEric Blake .protocol_name = "host_cdrom", 3495c1bb86cdSEric Blake .instance_size = sizeof(BDRVRawState), 3496c1bb86cdSEric Blake .bdrv_needs_filename = true, 3497c1bb86cdSEric Blake .bdrv_probe_device = cdrom_probe_device, 3498c1bb86cdSEric Blake .bdrv_parse_filename = cdrom_parse_filename, 3499c1bb86cdSEric Blake .bdrv_file_open = cdrom_open, 3500c1bb86cdSEric Blake .bdrv_close = raw_close, 3501c1bb86cdSEric Blake .bdrv_reopen_prepare = raw_reopen_prepare, 3502c1bb86cdSEric Blake .bdrv_reopen_commit = raw_reopen_commit, 3503c1bb86cdSEric Blake .bdrv_reopen_abort = raw_reopen_abort, 3504efc75e2aSStefan Hajnoczi .bdrv_co_create_opts = hdev_co_create_opts, 3505c1bb86cdSEric Blake .create_opts = &raw_create_opts, 3506c1bb86cdSEric Blake 3507c1bb86cdSEric Blake .bdrv_co_preadv = raw_co_preadv, 3508c1bb86cdSEric Blake .bdrv_co_pwritev = raw_co_pwritev, 350933d70fb6SKevin Wolf .bdrv_co_flush_to_disk = raw_co_flush_to_disk, 3510c1bb86cdSEric Blake .bdrv_refresh_limits = raw_refresh_limits, 3511c1bb86cdSEric Blake .bdrv_io_plug = raw_aio_plug, 3512c1bb86cdSEric Blake .bdrv_io_unplug = raw_aio_unplug, 3513042b757cSNishanth Aravamudan .bdrv_attach_aio_context = raw_aio_attach_aio_context, 3514c1bb86cdSEric Blake 3515061ca8a3SKevin Wolf .bdrv_co_truncate = raw_co_truncate, 3516c1bb86cdSEric Blake .bdrv_getlength = raw_getlength, 3517c1bb86cdSEric Blake .has_variable_length = true, 3518c1bb86cdSEric Blake .bdrv_get_allocated_file_size 3519c1bb86cdSEric Blake = raw_get_allocated_file_size, 3520c1bb86cdSEric Blake 3521c1bb86cdSEric Blake /* removable device support */ 3522c1bb86cdSEric Blake .bdrv_is_inserted = cdrom_is_inserted, 3523c1bb86cdSEric Blake .bdrv_eject = cdrom_eject, 3524c1bb86cdSEric Blake .bdrv_lock_medium = cdrom_lock_medium, 3525c1bb86cdSEric Blake }; 3526c1bb86cdSEric Blake #endif /* __FreeBSD__ */ 3527c1bb86cdSEric Blake 3528c1bb86cdSEric Blake static void bdrv_file_init(void) 3529c1bb86cdSEric Blake { 3530c1bb86cdSEric Blake /* 3531c1bb86cdSEric Blake * Register all the drivers. Note that order is important, the driver 3532c1bb86cdSEric Blake * registered last will get probed first. 3533c1bb86cdSEric Blake */ 3534c1bb86cdSEric Blake bdrv_register(&bdrv_file); 3535c1bb86cdSEric Blake bdrv_register(&bdrv_host_device); 3536c1bb86cdSEric Blake #ifdef __linux__ 3537c1bb86cdSEric Blake bdrv_register(&bdrv_host_cdrom); 3538c1bb86cdSEric Blake #endif 3539c1bb86cdSEric Blake #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) 3540c1bb86cdSEric Blake bdrv_register(&bdrv_host_cdrom); 3541c1bb86cdSEric Blake #endif 3542c1bb86cdSEric Blake } 3543c1bb86cdSEric Blake 3544c1bb86cdSEric Blake block_init(bdrv_file_init); 3545