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 */ 24c1bb86cdSEric Blake #include "qemu/osdep.h" 25c1bb86cdSEric Blake #include "qapi/error.h" 26c1bb86cdSEric Blake #include "qemu/cutils.h" 27c1bb86cdSEric Blake #include "qemu/error-report.h" 28c1bb86cdSEric Blake #include "block/block_int.h" 29c1bb86cdSEric Blake #include "qemu/module.h" 30c1bb86cdSEric Blake #include "trace.h" 31c1bb86cdSEric Blake #include "block/thread-pool.h" 32c1bb86cdSEric Blake #include "qemu/iov.h" 33c1bb86cdSEric Blake #include "block/raw-aio.h" 34c1bb86cdSEric Blake #include "qapi/util.h" 35c1bb86cdSEric Blake #include "qapi/qmp/qstring.h" 36c1bb86cdSEric Blake 37c1bb86cdSEric Blake #if defined(__APPLE__) && (__MACH__) 38c1bb86cdSEric Blake #include <paths.h> 39c1bb86cdSEric Blake #include <sys/param.h> 40c1bb86cdSEric Blake #include <IOKit/IOKitLib.h> 41c1bb86cdSEric Blake #include <IOKit/IOBSD.h> 42c1bb86cdSEric Blake #include <IOKit/storage/IOMediaBSDClient.h> 43c1bb86cdSEric Blake #include <IOKit/storage/IOMedia.h> 44c1bb86cdSEric Blake #include <IOKit/storage/IOCDMedia.h> 45c1bb86cdSEric Blake //#include <IOKit/storage/IOCDTypes.h> 46c1bb86cdSEric Blake #include <IOKit/storage/IODVDMedia.h> 47c1bb86cdSEric Blake #include <CoreFoundation/CoreFoundation.h> 48c1bb86cdSEric Blake #endif 49c1bb86cdSEric Blake 50c1bb86cdSEric Blake #ifdef __sun__ 51c1bb86cdSEric Blake #define _POSIX_PTHREAD_SEMANTICS 1 52c1bb86cdSEric Blake #include <sys/dkio.h> 53c1bb86cdSEric Blake #endif 54c1bb86cdSEric Blake #ifdef __linux__ 55c1bb86cdSEric Blake #include <sys/ioctl.h> 56c1bb86cdSEric Blake #include <sys/param.h> 57c1bb86cdSEric Blake #include <linux/cdrom.h> 58c1bb86cdSEric Blake #include <linux/fd.h> 59c1bb86cdSEric Blake #include <linux/fs.h> 60c1bb86cdSEric Blake #include <linux/hdreg.h> 61c1bb86cdSEric Blake #include <scsi/sg.h> 62c1bb86cdSEric Blake #ifdef __s390__ 63c1bb86cdSEric Blake #include <asm/dasd.h> 64c1bb86cdSEric Blake #endif 65c1bb86cdSEric Blake #ifndef FS_NOCOW_FL 66c1bb86cdSEric Blake #define FS_NOCOW_FL 0x00800000 /* Do not cow file */ 67c1bb86cdSEric Blake #endif 68c1bb86cdSEric Blake #endif 69c1bb86cdSEric Blake #if defined(CONFIG_FALLOCATE_PUNCH_HOLE) || defined(CONFIG_FALLOCATE_ZERO_RANGE) 70c1bb86cdSEric Blake #include <linux/falloc.h> 71c1bb86cdSEric Blake #endif 72c1bb86cdSEric Blake #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__) 73c1bb86cdSEric Blake #include <sys/disk.h> 74c1bb86cdSEric Blake #include <sys/cdio.h> 75c1bb86cdSEric Blake #endif 76c1bb86cdSEric Blake 77c1bb86cdSEric Blake #ifdef __OpenBSD__ 78c1bb86cdSEric Blake #include <sys/ioctl.h> 79c1bb86cdSEric Blake #include <sys/disklabel.h> 80c1bb86cdSEric Blake #include <sys/dkio.h> 81c1bb86cdSEric Blake #endif 82c1bb86cdSEric Blake 83c1bb86cdSEric Blake #ifdef __NetBSD__ 84c1bb86cdSEric Blake #include <sys/ioctl.h> 85c1bb86cdSEric Blake #include <sys/disklabel.h> 86c1bb86cdSEric Blake #include <sys/dkio.h> 87c1bb86cdSEric Blake #include <sys/disk.h> 88c1bb86cdSEric Blake #endif 89c1bb86cdSEric Blake 90c1bb86cdSEric Blake #ifdef __DragonFly__ 91c1bb86cdSEric Blake #include <sys/ioctl.h> 92c1bb86cdSEric Blake #include <sys/diskslice.h> 93c1bb86cdSEric Blake #endif 94c1bb86cdSEric Blake 95c1bb86cdSEric Blake #ifdef CONFIG_XFS 96c1bb86cdSEric Blake #include <xfs/xfs.h> 97c1bb86cdSEric Blake #endif 98c1bb86cdSEric Blake 99c1bb86cdSEric Blake //#define DEBUG_BLOCK 100c1bb86cdSEric Blake 101c1bb86cdSEric Blake #ifdef DEBUG_BLOCK 102c1bb86cdSEric Blake # define DEBUG_BLOCK_PRINT 1 103c1bb86cdSEric Blake #else 104c1bb86cdSEric Blake # define DEBUG_BLOCK_PRINT 0 105c1bb86cdSEric Blake #endif 106c1bb86cdSEric Blake #define DPRINTF(fmt, ...) \ 107c1bb86cdSEric Blake do { \ 108c1bb86cdSEric Blake if (DEBUG_BLOCK_PRINT) { \ 109c1bb86cdSEric Blake printf(fmt, ## __VA_ARGS__); \ 110c1bb86cdSEric Blake } \ 111c1bb86cdSEric Blake } while (0) 112c1bb86cdSEric Blake 113c1bb86cdSEric Blake /* OS X does not have O_DSYNC */ 114c1bb86cdSEric Blake #ifndef O_DSYNC 115c1bb86cdSEric Blake #ifdef O_SYNC 116c1bb86cdSEric Blake #define O_DSYNC O_SYNC 117c1bb86cdSEric Blake #elif defined(O_FSYNC) 118c1bb86cdSEric Blake #define O_DSYNC O_FSYNC 119c1bb86cdSEric Blake #endif 120c1bb86cdSEric Blake #endif 121c1bb86cdSEric Blake 122c1bb86cdSEric Blake /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */ 123c1bb86cdSEric Blake #ifndef O_DIRECT 124c1bb86cdSEric Blake #define O_DIRECT O_DSYNC 125c1bb86cdSEric Blake #endif 126c1bb86cdSEric Blake 127c1bb86cdSEric Blake #define FTYPE_FILE 0 128c1bb86cdSEric Blake #define FTYPE_CD 1 129c1bb86cdSEric Blake 130c1bb86cdSEric Blake #define MAX_BLOCKSIZE 4096 131c1bb86cdSEric Blake 132244a5668SFam Zheng /* Posix file locking bytes. Libvirt takes byte 0, we start from higher bytes, 133244a5668SFam Zheng * leaving a few more bytes for its future use. */ 134244a5668SFam Zheng #define RAW_LOCK_PERM_BASE 100 135244a5668SFam Zheng #define RAW_LOCK_SHARED_BASE 200 136244a5668SFam Zheng 137c1bb86cdSEric Blake typedef struct BDRVRawState { 138c1bb86cdSEric Blake int fd; 139244a5668SFam Zheng int lock_fd; 140244a5668SFam Zheng bool use_lock; 141c1bb86cdSEric Blake int type; 142c1bb86cdSEric Blake int open_flags; 143c1bb86cdSEric Blake size_t buf_align; 144c1bb86cdSEric Blake 145244a5668SFam Zheng /* The current permissions. */ 146244a5668SFam Zheng uint64_t perm; 147244a5668SFam Zheng uint64_t shared_perm; 148244a5668SFam Zheng 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; 159c1bb86cdSEric Blake } BDRVRawState; 160c1bb86cdSEric Blake 161c1bb86cdSEric Blake typedef struct BDRVRawReopenState { 162c1bb86cdSEric Blake int fd; 163c1bb86cdSEric Blake int open_flags; 164c1bb86cdSEric Blake } BDRVRawReopenState; 165c1bb86cdSEric Blake 166c1bb86cdSEric Blake static int fd_open(BlockDriverState *bs); 167c1bb86cdSEric Blake static int64_t raw_getlength(BlockDriverState *bs); 168c1bb86cdSEric Blake 169c1bb86cdSEric Blake typedef struct RawPosixAIOData { 170c1bb86cdSEric Blake BlockDriverState *bs; 171c1bb86cdSEric Blake int aio_fildes; 172c1bb86cdSEric Blake union { 173c1bb86cdSEric Blake struct iovec *aio_iov; 174c1bb86cdSEric Blake void *aio_ioctl_buf; 175c1bb86cdSEric Blake }; 176c1bb86cdSEric Blake int aio_niov; 177c1bb86cdSEric Blake uint64_t aio_nbytes; 178c1bb86cdSEric Blake #define aio_ioctl_cmd aio_nbytes /* for QEMU_AIO_IOCTL */ 179c1bb86cdSEric Blake off_t aio_offset; 180c1bb86cdSEric Blake int aio_type; 181c1bb86cdSEric Blake } RawPosixAIOData; 182c1bb86cdSEric Blake 183c1bb86cdSEric Blake #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) 184c1bb86cdSEric Blake static int cdrom_reopen(BlockDriverState *bs); 185c1bb86cdSEric Blake #endif 186c1bb86cdSEric Blake 187c1bb86cdSEric Blake #if defined(__NetBSD__) 188c1bb86cdSEric Blake static int raw_normalize_devicepath(const char **filename) 189c1bb86cdSEric Blake { 190c1bb86cdSEric Blake static char namebuf[PATH_MAX]; 191c1bb86cdSEric Blake const char *dp, *fname; 192c1bb86cdSEric Blake struct stat sb; 193c1bb86cdSEric Blake 194c1bb86cdSEric Blake fname = *filename; 195c1bb86cdSEric Blake dp = strrchr(fname, '/'); 196c1bb86cdSEric Blake if (lstat(fname, &sb) < 0) { 197c1bb86cdSEric Blake fprintf(stderr, "%s: stat failed: %s\n", 198c1bb86cdSEric Blake fname, strerror(errno)); 199c1bb86cdSEric Blake return -errno; 200c1bb86cdSEric Blake } 201c1bb86cdSEric Blake 202c1bb86cdSEric Blake if (!S_ISBLK(sb.st_mode)) { 203c1bb86cdSEric Blake return 0; 204c1bb86cdSEric Blake } 205c1bb86cdSEric Blake 206c1bb86cdSEric Blake if (dp == NULL) { 207c1bb86cdSEric Blake snprintf(namebuf, PATH_MAX, "r%s", fname); 208c1bb86cdSEric Blake } else { 209c1bb86cdSEric Blake snprintf(namebuf, PATH_MAX, "%.*s/r%s", 210c1bb86cdSEric Blake (int)(dp - fname), fname, dp + 1); 211c1bb86cdSEric Blake } 212c1bb86cdSEric Blake fprintf(stderr, "%s is a block device", fname); 213c1bb86cdSEric Blake *filename = namebuf; 214c1bb86cdSEric Blake fprintf(stderr, ", using %s\n", *filename); 215c1bb86cdSEric Blake 216c1bb86cdSEric Blake return 0; 217c1bb86cdSEric Blake } 218c1bb86cdSEric Blake #else 219c1bb86cdSEric Blake static int raw_normalize_devicepath(const char **filename) 220c1bb86cdSEric Blake { 221c1bb86cdSEric Blake return 0; 222c1bb86cdSEric Blake } 223c1bb86cdSEric Blake #endif 224c1bb86cdSEric Blake 225c1bb86cdSEric Blake /* 226c1bb86cdSEric Blake * Get logical block size via ioctl. On success store it in @sector_size_p. 227c1bb86cdSEric Blake */ 228c1bb86cdSEric Blake static int probe_logical_blocksize(int fd, unsigned int *sector_size_p) 229c1bb86cdSEric Blake { 230c1bb86cdSEric Blake unsigned int sector_size; 231c1bb86cdSEric Blake bool success = false; 232700f9ce0SPeter Maydell int i; 233c1bb86cdSEric Blake 234c1bb86cdSEric Blake errno = ENOTSUP; 235700f9ce0SPeter Maydell static const unsigned long ioctl_list[] = { 236c1bb86cdSEric Blake #ifdef BLKSSZGET 237700f9ce0SPeter Maydell BLKSSZGET, 238c1bb86cdSEric Blake #endif 239c1bb86cdSEric Blake #ifdef DKIOCGETBLOCKSIZE 240700f9ce0SPeter Maydell DKIOCGETBLOCKSIZE, 241c1bb86cdSEric Blake #endif 242c1bb86cdSEric Blake #ifdef DIOCGSECTORSIZE 243700f9ce0SPeter Maydell DIOCGSECTORSIZE, 244700f9ce0SPeter Maydell #endif 245700f9ce0SPeter Maydell }; 246700f9ce0SPeter Maydell 247700f9ce0SPeter Maydell /* Try a few ioctls to get the right size */ 248700f9ce0SPeter Maydell for (i = 0; i < (int)ARRAY_SIZE(ioctl_list); i++) { 249700f9ce0SPeter Maydell if (ioctl(fd, ioctl_list[i], §or_size) >= 0) { 250c1bb86cdSEric Blake *sector_size_p = sector_size; 251c1bb86cdSEric Blake success = true; 252c1bb86cdSEric Blake } 253700f9ce0SPeter Maydell } 254c1bb86cdSEric Blake 255c1bb86cdSEric Blake return success ? 0 : -errno; 256c1bb86cdSEric Blake } 257c1bb86cdSEric Blake 258c1bb86cdSEric Blake /** 259c1bb86cdSEric Blake * Get physical block size of @fd. 260c1bb86cdSEric Blake * On success, store it in @blk_size and return 0. 261c1bb86cdSEric Blake * On failure, return -errno. 262c1bb86cdSEric Blake */ 263c1bb86cdSEric Blake static int probe_physical_blocksize(int fd, unsigned int *blk_size) 264c1bb86cdSEric Blake { 265c1bb86cdSEric Blake #ifdef BLKPBSZGET 266c1bb86cdSEric Blake if (ioctl(fd, BLKPBSZGET, blk_size) < 0) { 267c1bb86cdSEric Blake return -errno; 268c1bb86cdSEric Blake } 269c1bb86cdSEric Blake return 0; 270c1bb86cdSEric Blake #else 271c1bb86cdSEric Blake return -ENOTSUP; 272c1bb86cdSEric Blake #endif 273c1bb86cdSEric Blake } 274c1bb86cdSEric Blake 275c1bb86cdSEric Blake /* Check if read is allowed with given memory buffer and length. 276c1bb86cdSEric Blake * 277c1bb86cdSEric Blake * This function is used to check O_DIRECT memory buffer and request alignment. 278c1bb86cdSEric Blake */ 279c1bb86cdSEric Blake static bool raw_is_io_aligned(int fd, void *buf, size_t len) 280c1bb86cdSEric Blake { 281c1bb86cdSEric Blake ssize_t ret = pread(fd, buf, len, 0); 282c1bb86cdSEric Blake 283c1bb86cdSEric Blake if (ret >= 0) { 284c1bb86cdSEric Blake return true; 285c1bb86cdSEric Blake } 286c1bb86cdSEric Blake 287c1bb86cdSEric Blake #ifdef __linux__ 288c1bb86cdSEric Blake /* The Linux kernel returns EINVAL for misaligned O_DIRECT reads. Ignore 289c1bb86cdSEric Blake * other errors (e.g. real I/O error), which could happen on a failed 290c1bb86cdSEric Blake * drive, since we only care about probing alignment. 291c1bb86cdSEric Blake */ 292c1bb86cdSEric Blake if (errno != EINVAL) { 293c1bb86cdSEric Blake return true; 294c1bb86cdSEric Blake } 295c1bb86cdSEric Blake #endif 296c1bb86cdSEric Blake 297c1bb86cdSEric Blake return false; 298c1bb86cdSEric Blake } 299c1bb86cdSEric Blake 300c1bb86cdSEric Blake static void raw_probe_alignment(BlockDriverState *bs, int fd, Error **errp) 301c1bb86cdSEric Blake { 302c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 303c1bb86cdSEric Blake char *buf; 304c1bb86cdSEric Blake size_t max_align = MAX(MAX_BLOCKSIZE, getpagesize()); 305c1bb86cdSEric Blake 306c1bb86cdSEric Blake /* For SCSI generic devices the alignment is not really used. 307c1bb86cdSEric Blake With buffered I/O, we don't have any restrictions. */ 308c1bb86cdSEric Blake if (bdrv_is_sg(bs) || !s->needs_alignment) { 309c1bb86cdSEric Blake bs->bl.request_alignment = 1; 310c1bb86cdSEric Blake s->buf_align = 1; 311c1bb86cdSEric Blake return; 312c1bb86cdSEric Blake } 313c1bb86cdSEric Blake 314c1bb86cdSEric Blake bs->bl.request_alignment = 0; 315c1bb86cdSEric Blake s->buf_align = 0; 316c1bb86cdSEric Blake /* Let's try to use the logical blocksize for the alignment. */ 317c1bb86cdSEric Blake if (probe_logical_blocksize(fd, &bs->bl.request_alignment) < 0) { 318c1bb86cdSEric Blake bs->bl.request_alignment = 0; 319c1bb86cdSEric Blake } 320c1bb86cdSEric Blake #ifdef CONFIG_XFS 321c1bb86cdSEric Blake if (s->is_xfs) { 322c1bb86cdSEric Blake struct dioattr da; 323c1bb86cdSEric Blake if (xfsctl(NULL, fd, XFS_IOC_DIOINFO, &da) >= 0) { 324c1bb86cdSEric Blake bs->bl.request_alignment = da.d_miniosz; 325c1bb86cdSEric Blake /* The kernel returns wrong information for d_mem */ 326c1bb86cdSEric Blake /* s->buf_align = da.d_mem; */ 327c1bb86cdSEric Blake } 328c1bb86cdSEric Blake } 329c1bb86cdSEric Blake #endif 330c1bb86cdSEric Blake 331c1bb86cdSEric Blake /* If we could not get the sizes so far, we can only guess them */ 332c1bb86cdSEric Blake if (!s->buf_align) { 333c1bb86cdSEric Blake size_t align; 334c1bb86cdSEric Blake buf = qemu_memalign(max_align, 2 * max_align); 335c1bb86cdSEric Blake for (align = 512; align <= max_align; align <<= 1) { 336c1bb86cdSEric Blake if (raw_is_io_aligned(fd, buf + align, max_align)) { 337c1bb86cdSEric Blake s->buf_align = align; 338c1bb86cdSEric Blake break; 339c1bb86cdSEric Blake } 340c1bb86cdSEric Blake } 341c1bb86cdSEric Blake qemu_vfree(buf); 342c1bb86cdSEric Blake } 343c1bb86cdSEric Blake 344c1bb86cdSEric Blake if (!bs->bl.request_alignment) { 345c1bb86cdSEric Blake size_t align; 346c1bb86cdSEric Blake buf = qemu_memalign(s->buf_align, max_align); 347c1bb86cdSEric Blake for (align = 512; align <= max_align; align <<= 1) { 348c1bb86cdSEric Blake if (raw_is_io_aligned(fd, buf, align)) { 349c1bb86cdSEric Blake bs->bl.request_alignment = align; 350c1bb86cdSEric Blake break; 351c1bb86cdSEric Blake } 352c1bb86cdSEric Blake } 353c1bb86cdSEric Blake qemu_vfree(buf); 354c1bb86cdSEric Blake } 355c1bb86cdSEric Blake 356c1bb86cdSEric Blake if (!s->buf_align || !bs->bl.request_alignment) { 357c1bb86cdSEric Blake error_setg(errp, "Could not find working O_DIRECT alignment"); 358c1bb86cdSEric Blake error_append_hint(errp, "Try cache.direct=off\n"); 359c1bb86cdSEric Blake } 360c1bb86cdSEric Blake } 361c1bb86cdSEric Blake 362c1bb86cdSEric Blake static void raw_parse_flags(int bdrv_flags, int *open_flags) 363c1bb86cdSEric Blake { 364c1bb86cdSEric Blake assert(open_flags != NULL); 365c1bb86cdSEric Blake 366c1bb86cdSEric Blake *open_flags |= O_BINARY; 367c1bb86cdSEric Blake *open_flags &= ~O_ACCMODE; 368c1bb86cdSEric Blake if (bdrv_flags & BDRV_O_RDWR) { 369c1bb86cdSEric Blake *open_flags |= O_RDWR; 370c1bb86cdSEric Blake } else { 371c1bb86cdSEric Blake *open_flags |= O_RDONLY; 372c1bb86cdSEric Blake } 373c1bb86cdSEric Blake 374c1bb86cdSEric Blake /* Use O_DSYNC for write-through caching, no flags for write-back caching, 375c1bb86cdSEric Blake * and O_DIRECT for no caching. */ 376c1bb86cdSEric Blake if ((bdrv_flags & BDRV_O_NOCACHE)) { 377c1bb86cdSEric Blake *open_flags |= O_DIRECT; 378c1bb86cdSEric Blake } 379c1bb86cdSEric Blake } 380c1bb86cdSEric Blake 381c1bb86cdSEric Blake static void raw_parse_filename(const char *filename, QDict *options, 382c1bb86cdSEric Blake Error **errp) 383c1bb86cdSEric Blake { 38403c320d8SMax Reitz bdrv_parse_filename_strip_prefix(filename, "file:", options); 385c1bb86cdSEric Blake } 386c1bb86cdSEric Blake 387c1bb86cdSEric Blake static QemuOptsList raw_runtime_opts = { 388c1bb86cdSEric Blake .name = "raw", 389c1bb86cdSEric Blake .head = QTAILQ_HEAD_INITIALIZER(raw_runtime_opts.head), 390c1bb86cdSEric Blake .desc = { 391c1bb86cdSEric Blake { 392c1bb86cdSEric Blake .name = "filename", 393c1bb86cdSEric Blake .type = QEMU_OPT_STRING, 394c1bb86cdSEric Blake .help = "File name of the image", 395c1bb86cdSEric Blake }, 396c1bb86cdSEric Blake { 397c1bb86cdSEric Blake .name = "aio", 398c1bb86cdSEric Blake .type = QEMU_OPT_STRING, 399c1bb86cdSEric Blake .help = "host AIO implementation (threads, native)", 400c1bb86cdSEric Blake }, 40116b48d5dSFam Zheng { 40216b48d5dSFam Zheng .name = "locking", 40316b48d5dSFam Zheng .type = QEMU_OPT_STRING, 40416b48d5dSFam Zheng .help = "file locking mode (on/off/auto, default: auto)", 40516b48d5dSFam Zheng }, 406c1bb86cdSEric Blake { /* end of list */ } 407c1bb86cdSEric Blake }, 408c1bb86cdSEric Blake }; 409c1bb86cdSEric Blake 410c1bb86cdSEric Blake static int raw_open_common(BlockDriverState *bs, QDict *options, 411c1bb86cdSEric Blake int bdrv_flags, int open_flags, Error **errp) 412c1bb86cdSEric Blake { 413c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 414c1bb86cdSEric Blake QemuOpts *opts; 415c1bb86cdSEric Blake Error *local_err = NULL; 416c1bb86cdSEric Blake const char *filename = NULL; 417c1bb86cdSEric Blake BlockdevAioOptions aio, aio_default; 418c1bb86cdSEric Blake int fd, ret; 419c1bb86cdSEric Blake struct stat st; 420244a5668SFam Zheng OnOffAuto locking; 421c1bb86cdSEric Blake 422c1bb86cdSEric Blake opts = qemu_opts_create(&raw_runtime_opts, NULL, 0, &error_abort); 423c1bb86cdSEric Blake qemu_opts_absorb_qdict(opts, options, &local_err); 424c1bb86cdSEric Blake if (local_err) { 425c1bb86cdSEric Blake error_propagate(errp, local_err); 426c1bb86cdSEric Blake ret = -EINVAL; 427c1bb86cdSEric Blake goto fail; 428c1bb86cdSEric Blake } 429c1bb86cdSEric Blake 430c1bb86cdSEric Blake filename = qemu_opt_get(opts, "filename"); 431c1bb86cdSEric Blake 432c1bb86cdSEric Blake ret = raw_normalize_devicepath(&filename); 433c1bb86cdSEric Blake if (ret != 0) { 434c1bb86cdSEric Blake error_setg_errno(errp, -ret, "Could not normalize device path"); 435c1bb86cdSEric Blake goto fail; 436c1bb86cdSEric Blake } 437c1bb86cdSEric Blake 438c1bb86cdSEric Blake aio_default = (bdrv_flags & BDRV_O_NATIVE_AIO) 439c1bb86cdSEric Blake ? BLOCKDEV_AIO_OPTIONS_NATIVE 440c1bb86cdSEric Blake : BLOCKDEV_AIO_OPTIONS_THREADS; 441c1bb86cdSEric Blake aio = qapi_enum_parse(BlockdevAioOptions_lookup, qemu_opt_get(opts, "aio"), 442*06c60b6cSMarkus Armbruster aio_default, &local_err); 443c1bb86cdSEric Blake if (local_err) { 444c1bb86cdSEric Blake error_propagate(errp, local_err); 445c1bb86cdSEric Blake ret = -EINVAL; 446c1bb86cdSEric Blake goto fail; 447c1bb86cdSEric Blake } 448c1bb86cdSEric Blake s->use_linux_aio = (aio == BLOCKDEV_AIO_OPTIONS_NATIVE); 449c1bb86cdSEric Blake 450244a5668SFam Zheng locking = qapi_enum_parse(OnOffAuto_lookup, qemu_opt_get(opts, "locking"), 451*06c60b6cSMarkus Armbruster ON_OFF_AUTO_AUTO, &local_err); 452244a5668SFam Zheng if (local_err) { 453244a5668SFam Zheng error_propagate(errp, local_err); 454244a5668SFam Zheng ret = -EINVAL; 455244a5668SFam Zheng goto fail; 456244a5668SFam Zheng } 457244a5668SFam Zheng switch (locking) { 458244a5668SFam Zheng case ON_OFF_AUTO_ON: 459244a5668SFam Zheng s->use_lock = true; 4602b218f5dSFam Zheng if (!qemu_has_ofd_lock()) { 461244a5668SFam Zheng fprintf(stderr, 4622b218f5dSFam Zheng "File lock requested but OFD locking syscall is " 4632b218f5dSFam Zheng "unavailable, falling back to POSIX file locks.\n" 4642b218f5dSFam Zheng "Due to the implementation, locks can be lost " 4652b218f5dSFam Zheng "unexpectedly.\n"); 4662b218f5dSFam Zheng } 467244a5668SFam Zheng break; 468244a5668SFam Zheng case ON_OFF_AUTO_OFF: 469244a5668SFam Zheng s->use_lock = false; 470244a5668SFam Zheng break; 471244a5668SFam Zheng case ON_OFF_AUTO_AUTO: 4722b218f5dSFam Zheng s->use_lock = qemu_has_ofd_lock(); 473244a5668SFam Zheng break; 474244a5668SFam Zheng default: 475244a5668SFam Zheng abort(); 476244a5668SFam Zheng } 477244a5668SFam Zheng 478c1bb86cdSEric Blake s->open_flags = open_flags; 479c1bb86cdSEric Blake raw_parse_flags(bdrv_flags, &s->open_flags); 480c1bb86cdSEric Blake 481c1bb86cdSEric Blake s->fd = -1; 482c1bb86cdSEric Blake fd = qemu_open(filename, s->open_flags, 0644); 483c1bb86cdSEric Blake if (fd < 0) { 484c1bb86cdSEric Blake ret = -errno; 485c1bb86cdSEric Blake error_setg_errno(errp, errno, "Could not open '%s'", filename); 486c1bb86cdSEric Blake if (ret == -EROFS) { 487c1bb86cdSEric Blake ret = -EACCES; 488c1bb86cdSEric Blake } 489c1bb86cdSEric Blake goto fail; 490c1bb86cdSEric Blake } 491c1bb86cdSEric Blake s->fd = fd; 492c1bb86cdSEric Blake 493244a5668SFam Zheng s->lock_fd = -1; 494244a5668SFam Zheng if (s->use_lock) { 495244a5668SFam Zheng fd = qemu_open(filename, s->open_flags); 496244a5668SFam Zheng if (fd < 0) { 497244a5668SFam Zheng ret = -errno; 498244a5668SFam Zheng error_setg_errno(errp, errno, "Could not open '%s' for locking", 499244a5668SFam Zheng filename); 500244a5668SFam Zheng qemu_close(s->fd); 501244a5668SFam Zheng goto fail; 502244a5668SFam Zheng } 503244a5668SFam Zheng s->lock_fd = fd; 504244a5668SFam Zheng } 505244a5668SFam Zheng s->perm = 0; 506244a5668SFam Zheng s->shared_perm = BLK_PERM_ALL; 507244a5668SFam Zheng 508c1bb86cdSEric Blake #ifdef CONFIG_LINUX_AIO 509c1bb86cdSEric Blake /* Currently Linux does AIO only for files opened with O_DIRECT */ 510c1bb86cdSEric Blake if (s->use_linux_aio && !(s->open_flags & O_DIRECT)) { 511c1bb86cdSEric Blake error_setg(errp, "aio=native was specified, but it requires " 512c1bb86cdSEric Blake "cache.direct=on, which was not specified."); 513c1bb86cdSEric Blake ret = -EINVAL; 514c1bb86cdSEric Blake goto fail; 515c1bb86cdSEric Blake } 516c1bb86cdSEric Blake #else 517c1bb86cdSEric Blake if (s->use_linux_aio) { 518c1bb86cdSEric Blake error_setg(errp, "aio=native was specified, but is not supported " 519c1bb86cdSEric Blake "in this build."); 520c1bb86cdSEric Blake ret = -EINVAL; 521c1bb86cdSEric Blake goto fail; 522c1bb86cdSEric Blake } 523c1bb86cdSEric Blake #endif /* !defined(CONFIG_LINUX_AIO) */ 524c1bb86cdSEric Blake 525c1bb86cdSEric Blake s->has_discard = true; 526c1bb86cdSEric Blake s->has_write_zeroes = true; 527c1bb86cdSEric Blake bs->supported_zero_flags = BDRV_REQ_MAY_UNMAP; 528c1bb86cdSEric Blake if ((bs->open_flags & BDRV_O_NOCACHE) != 0) { 529c1bb86cdSEric Blake s->needs_alignment = true; 530c1bb86cdSEric Blake } 531c1bb86cdSEric Blake 532c1bb86cdSEric Blake if (fstat(s->fd, &st) < 0) { 533c1bb86cdSEric Blake ret = -errno; 534c1bb86cdSEric Blake error_setg_errno(errp, errno, "Could not stat file"); 535c1bb86cdSEric Blake goto fail; 536c1bb86cdSEric Blake } 537c1bb86cdSEric Blake if (S_ISREG(st.st_mode)) { 538c1bb86cdSEric Blake s->discard_zeroes = true; 539c1bb86cdSEric Blake s->has_fallocate = true; 540c1bb86cdSEric Blake } 541c1bb86cdSEric Blake if (S_ISBLK(st.st_mode)) { 542c1bb86cdSEric Blake #ifdef BLKDISCARDZEROES 543c1bb86cdSEric Blake unsigned int arg; 544c1bb86cdSEric Blake if (ioctl(s->fd, BLKDISCARDZEROES, &arg) == 0 && arg) { 545c1bb86cdSEric Blake s->discard_zeroes = true; 546c1bb86cdSEric Blake } 547c1bb86cdSEric Blake #endif 548c1bb86cdSEric Blake #ifdef __linux__ 549c1bb86cdSEric Blake /* On Linux 3.10, BLKDISCARD leaves stale data in the page cache. Do 550c1bb86cdSEric Blake * not rely on the contents of discarded blocks unless using O_DIRECT. 551c1bb86cdSEric Blake * Same for BLKZEROOUT. 552c1bb86cdSEric Blake */ 553c1bb86cdSEric Blake if (!(bs->open_flags & BDRV_O_NOCACHE)) { 554c1bb86cdSEric Blake s->discard_zeroes = false; 555c1bb86cdSEric Blake s->has_write_zeroes = false; 556c1bb86cdSEric Blake } 557c1bb86cdSEric Blake #endif 558c1bb86cdSEric Blake } 559c1bb86cdSEric Blake #ifdef __FreeBSD__ 560c1bb86cdSEric Blake if (S_ISCHR(st.st_mode)) { 561c1bb86cdSEric Blake /* 562c1bb86cdSEric Blake * The file is a char device (disk), which on FreeBSD isn't behind 563c1bb86cdSEric Blake * a pager, so force all requests to be aligned. This is needed 564c1bb86cdSEric Blake * so QEMU makes sure all IO operations on the device are aligned 565c1bb86cdSEric Blake * to sector size, or else FreeBSD will reject them with EINVAL. 566c1bb86cdSEric Blake */ 567c1bb86cdSEric Blake s->needs_alignment = true; 568c1bb86cdSEric Blake } 569c1bb86cdSEric Blake #endif 570c1bb86cdSEric Blake 571c1bb86cdSEric Blake #ifdef CONFIG_XFS 572c1bb86cdSEric Blake if (platform_test_xfs_fd(s->fd)) { 573c1bb86cdSEric Blake s->is_xfs = true; 574c1bb86cdSEric Blake } 575c1bb86cdSEric Blake #endif 576c1bb86cdSEric Blake 577c1bb86cdSEric Blake ret = 0; 578c1bb86cdSEric Blake fail: 579c1bb86cdSEric Blake if (filename && (bdrv_flags & BDRV_O_TEMPORARY)) { 580c1bb86cdSEric Blake unlink(filename); 581c1bb86cdSEric Blake } 582c1bb86cdSEric Blake qemu_opts_del(opts); 583c1bb86cdSEric Blake return ret; 584c1bb86cdSEric Blake } 585c1bb86cdSEric Blake 586c1bb86cdSEric Blake static int raw_open(BlockDriverState *bs, QDict *options, int flags, 587c1bb86cdSEric Blake Error **errp) 588c1bb86cdSEric Blake { 589c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 590c1bb86cdSEric Blake 591c1bb86cdSEric Blake s->type = FTYPE_FILE; 592c1bb86cdSEric Blake return raw_open_common(bs, options, flags, 0, errp); 593c1bb86cdSEric Blake } 594c1bb86cdSEric Blake 595244a5668SFam Zheng typedef enum { 596244a5668SFam Zheng RAW_PL_PREPARE, 597244a5668SFam Zheng RAW_PL_COMMIT, 598244a5668SFam Zheng RAW_PL_ABORT, 599244a5668SFam Zheng } RawPermLockOp; 600244a5668SFam Zheng 601244a5668SFam Zheng #define PERM_FOREACH(i) \ 602244a5668SFam Zheng for ((i) = 0; (1ULL << (i)) <= BLK_PERM_ALL; i++) 603244a5668SFam Zheng 604244a5668SFam Zheng /* Lock bytes indicated by @perm_lock_bits and @shared_perm_lock_bits in the 605244a5668SFam Zheng * file; if @unlock == true, also unlock the unneeded bytes. 606244a5668SFam Zheng * @shared_perm_lock_bits is the mask of all permissions that are NOT shared. 607244a5668SFam Zheng */ 608244a5668SFam Zheng static int raw_apply_lock_bytes(BDRVRawState *s, 609244a5668SFam Zheng uint64_t perm_lock_bits, 610244a5668SFam Zheng uint64_t shared_perm_lock_bits, 611244a5668SFam Zheng bool unlock, Error **errp) 612244a5668SFam Zheng { 613244a5668SFam Zheng int ret; 614244a5668SFam Zheng int i; 615244a5668SFam Zheng 616244a5668SFam Zheng PERM_FOREACH(i) { 617244a5668SFam Zheng int off = RAW_LOCK_PERM_BASE + i; 618244a5668SFam Zheng if (perm_lock_bits & (1ULL << i)) { 619244a5668SFam Zheng ret = qemu_lock_fd(s->lock_fd, off, 1, false); 620244a5668SFam Zheng if (ret) { 621244a5668SFam Zheng error_setg(errp, "Failed to lock byte %d", off); 622244a5668SFam Zheng return ret; 623244a5668SFam Zheng } 624244a5668SFam Zheng } else if (unlock) { 625244a5668SFam Zheng ret = qemu_unlock_fd(s->lock_fd, off, 1); 626244a5668SFam Zheng if (ret) { 627244a5668SFam Zheng error_setg(errp, "Failed to unlock byte %d", off); 628244a5668SFam Zheng return ret; 629244a5668SFam Zheng } 630244a5668SFam Zheng } 631244a5668SFam Zheng } 632244a5668SFam Zheng PERM_FOREACH(i) { 633244a5668SFam Zheng int off = RAW_LOCK_SHARED_BASE + i; 634244a5668SFam Zheng if (shared_perm_lock_bits & (1ULL << i)) { 635244a5668SFam Zheng ret = qemu_lock_fd(s->lock_fd, off, 1, false); 636244a5668SFam Zheng if (ret) { 637244a5668SFam Zheng error_setg(errp, "Failed to lock byte %d", off); 638244a5668SFam Zheng return ret; 639244a5668SFam Zheng } 640244a5668SFam Zheng } else if (unlock) { 641244a5668SFam Zheng ret = qemu_unlock_fd(s->lock_fd, off, 1); 642244a5668SFam Zheng if (ret) { 643244a5668SFam Zheng error_setg(errp, "Failed to unlock byte %d", off); 644244a5668SFam Zheng return ret; 645244a5668SFam Zheng } 646244a5668SFam Zheng } 647244a5668SFam Zheng } 648244a5668SFam Zheng return 0; 649244a5668SFam Zheng } 650244a5668SFam Zheng 651244a5668SFam Zheng /* Check "unshared" bytes implied by @perm and ~@shared_perm in the file. */ 652244a5668SFam Zheng static int raw_check_lock_bytes(BDRVRawState *s, 653244a5668SFam Zheng uint64_t perm, uint64_t shared_perm, 654244a5668SFam Zheng Error **errp) 655244a5668SFam Zheng { 656244a5668SFam Zheng int ret; 657244a5668SFam Zheng int i; 658244a5668SFam Zheng 659244a5668SFam Zheng PERM_FOREACH(i) { 660244a5668SFam Zheng int off = RAW_LOCK_SHARED_BASE + i; 661244a5668SFam Zheng uint64_t p = 1ULL << i; 662244a5668SFam Zheng if (perm & p) { 663244a5668SFam Zheng ret = qemu_lock_fd_test(s->lock_fd, off, 1, true); 664244a5668SFam Zheng if (ret) { 665244a5668SFam Zheng char *perm_name = bdrv_perm_names(p); 666244a5668SFam Zheng error_setg(errp, 667244a5668SFam Zheng "Failed to get \"%s\" lock", 668244a5668SFam Zheng perm_name); 669244a5668SFam Zheng g_free(perm_name); 670244a5668SFam Zheng error_append_hint(errp, 671244a5668SFam Zheng "Is another process using the image?\n"); 672244a5668SFam Zheng return ret; 673244a5668SFam Zheng } 674244a5668SFam Zheng } 675244a5668SFam Zheng } 676244a5668SFam Zheng PERM_FOREACH(i) { 677244a5668SFam Zheng int off = RAW_LOCK_PERM_BASE + i; 678244a5668SFam Zheng uint64_t p = 1ULL << i; 679244a5668SFam Zheng if (!(shared_perm & p)) { 680244a5668SFam Zheng ret = qemu_lock_fd_test(s->lock_fd, off, 1, true); 681244a5668SFam Zheng if (ret) { 682244a5668SFam Zheng char *perm_name = bdrv_perm_names(p); 683244a5668SFam Zheng error_setg(errp, 684244a5668SFam Zheng "Failed to get shared \"%s\" lock", 685244a5668SFam Zheng perm_name); 686244a5668SFam Zheng g_free(perm_name); 687244a5668SFam Zheng error_append_hint(errp, 688244a5668SFam Zheng "Is another process using the image?\n"); 689244a5668SFam Zheng return ret; 690244a5668SFam Zheng } 691244a5668SFam Zheng } 692244a5668SFam Zheng } 693244a5668SFam Zheng return 0; 694244a5668SFam Zheng } 695244a5668SFam Zheng 696244a5668SFam Zheng static int raw_handle_perm_lock(BlockDriverState *bs, 697244a5668SFam Zheng RawPermLockOp op, 698244a5668SFam Zheng uint64_t new_perm, uint64_t new_shared, 699244a5668SFam Zheng Error **errp) 700244a5668SFam Zheng { 701244a5668SFam Zheng BDRVRawState *s = bs->opaque; 702244a5668SFam Zheng int ret = 0; 703244a5668SFam Zheng Error *local_err = NULL; 704244a5668SFam Zheng 705244a5668SFam Zheng if (!s->use_lock) { 706244a5668SFam Zheng return 0; 707244a5668SFam Zheng } 708244a5668SFam Zheng 709244a5668SFam Zheng if (bdrv_get_flags(bs) & BDRV_O_INACTIVE) { 710244a5668SFam Zheng return 0; 711244a5668SFam Zheng } 712244a5668SFam Zheng 713244a5668SFam Zheng assert(s->lock_fd > 0); 714244a5668SFam Zheng 715244a5668SFam Zheng switch (op) { 716244a5668SFam Zheng case RAW_PL_PREPARE: 717244a5668SFam Zheng ret = raw_apply_lock_bytes(s, s->perm | new_perm, 718244a5668SFam Zheng ~s->shared_perm | ~new_shared, 719244a5668SFam Zheng false, errp); 720244a5668SFam Zheng if (!ret) { 721244a5668SFam Zheng ret = raw_check_lock_bytes(s, new_perm, new_shared, errp); 722244a5668SFam Zheng if (!ret) { 723244a5668SFam Zheng return 0; 724244a5668SFam Zheng } 725244a5668SFam Zheng } 726244a5668SFam Zheng op = RAW_PL_ABORT; 727244a5668SFam Zheng /* fall through to unlock bytes. */ 728244a5668SFam Zheng case RAW_PL_ABORT: 729244a5668SFam Zheng raw_apply_lock_bytes(s, s->perm, ~s->shared_perm, true, &local_err); 730244a5668SFam Zheng if (local_err) { 731244a5668SFam Zheng /* Theoretically the above call only unlocks bytes and it cannot 732244a5668SFam Zheng * fail. Something weird happened, report it. 733244a5668SFam Zheng */ 734244a5668SFam Zheng error_report_err(local_err); 735244a5668SFam Zheng } 736244a5668SFam Zheng break; 737244a5668SFam Zheng case RAW_PL_COMMIT: 738244a5668SFam Zheng raw_apply_lock_bytes(s, new_perm, ~new_shared, true, &local_err); 739244a5668SFam Zheng if (local_err) { 740244a5668SFam Zheng /* Theoretically the above call only unlocks bytes and it cannot 741244a5668SFam Zheng * fail. Something weird happened, report it. 742244a5668SFam Zheng */ 743244a5668SFam Zheng error_report_err(local_err); 744244a5668SFam Zheng } 745244a5668SFam Zheng break; 746244a5668SFam Zheng } 747244a5668SFam Zheng return ret; 748244a5668SFam Zheng } 749244a5668SFam Zheng 750c1bb86cdSEric Blake static int raw_reopen_prepare(BDRVReopenState *state, 751c1bb86cdSEric Blake BlockReopenQueue *queue, Error **errp) 752c1bb86cdSEric Blake { 753c1bb86cdSEric Blake BDRVRawState *s; 754c1bb86cdSEric Blake BDRVRawReopenState *rs; 755c1bb86cdSEric Blake int ret = 0; 756c1bb86cdSEric Blake Error *local_err = NULL; 757c1bb86cdSEric Blake 758c1bb86cdSEric Blake assert(state != NULL); 759c1bb86cdSEric Blake assert(state->bs != NULL); 760c1bb86cdSEric Blake 761c1bb86cdSEric Blake s = state->bs->opaque; 762c1bb86cdSEric Blake 763c1bb86cdSEric Blake state->opaque = g_new0(BDRVRawReopenState, 1); 764c1bb86cdSEric Blake rs = state->opaque; 765c1bb86cdSEric Blake 766c1bb86cdSEric Blake if (s->type == FTYPE_CD) { 767c1bb86cdSEric Blake rs->open_flags |= O_NONBLOCK; 768c1bb86cdSEric Blake } 769c1bb86cdSEric Blake 770c1bb86cdSEric Blake raw_parse_flags(state->flags, &rs->open_flags); 771c1bb86cdSEric Blake 772c1bb86cdSEric Blake rs->fd = -1; 773c1bb86cdSEric Blake 774c1bb86cdSEric Blake int fcntl_flags = O_APPEND | O_NONBLOCK; 775c1bb86cdSEric Blake #ifdef O_NOATIME 776c1bb86cdSEric Blake fcntl_flags |= O_NOATIME; 777c1bb86cdSEric Blake #endif 778c1bb86cdSEric Blake 779c1bb86cdSEric Blake #ifdef O_ASYNC 780c1bb86cdSEric Blake /* Not all operating systems have O_ASYNC, and those that don't 781c1bb86cdSEric Blake * will not let us track the state into rs->open_flags (typically 782c1bb86cdSEric Blake * you achieve the same effect with an ioctl, for example I_SETSIG 783c1bb86cdSEric Blake * on Solaris). But we do not use O_ASYNC, so that's fine. 784c1bb86cdSEric Blake */ 785c1bb86cdSEric Blake assert((s->open_flags & O_ASYNC) == 0); 786c1bb86cdSEric Blake #endif 787c1bb86cdSEric Blake 788c1bb86cdSEric Blake if ((rs->open_flags & ~fcntl_flags) == (s->open_flags & ~fcntl_flags)) { 789c1bb86cdSEric Blake /* dup the original fd */ 790c1bb86cdSEric Blake rs->fd = qemu_dup(s->fd); 791c1bb86cdSEric Blake if (rs->fd >= 0) { 792c1bb86cdSEric Blake ret = fcntl_setfl(rs->fd, rs->open_flags); 793c1bb86cdSEric Blake if (ret) { 794c1bb86cdSEric Blake qemu_close(rs->fd); 795c1bb86cdSEric Blake rs->fd = -1; 796c1bb86cdSEric Blake } 797c1bb86cdSEric Blake } 798c1bb86cdSEric Blake } 799c1bb86cdSEric Blake 800c1bb86cdSEric Blake /* If we cannot use fcntl, or fcntl failed, fall back to qemu_open() */ 801c1bb86cdSEric Blake if (rs->fd == -1) { 802c1bb86cdSEric Blake const char *normalized_filename = state->bs->filename; 803c1bb86cdSEric Blake ret = raw_normalize_devicepath(&normalized_filename); 804c1bb86cdSEric Blake if (ret < 0) { 805c1bb86cdSEric Blake error_setg_errno(errp, -ret, "Could not normalize device path"); 806c1bb86cdSEric Blake } else { 807c1bb86cdSEric Blake assert(!(rs->open_flags & O_CREAT)); 808c1bb86cdSEric Blake rs->fd = qemu_open(normalized_filename, rs->open_flags); 809c1bb86cdSEric Blake if (rs->fd == -1) { 810c1bb86cdSEric Blake error_setg_errno(errp, errno, "Could not reopen file"); 811c1bb86cdSEric Blake ret = -1; 812c1bb86cdSEric Blake } 813c1bb86cdSEric Blake } 814c1bb86cdSEric Blake } 815c1bb86cdSEric Blake 816c1bb86cdSEric Blake /* Fail already reopen_prepare() if we can't get a working O_DIRECT 817c1bb86cdSEric Blake * alignment with the new fd. */ 818c1bb86cdSEric Blake if (rs->fd != -1) { 819c1bb86cdSEric Blake raw_probe_alignment(state->bs, rs->fd, &local_err); 820c1bb86cdSEric Blake if (local_err) { 821c1bb86cdSEric Blake qemu_close(rs->fd); 822c1bb86cdSEric Blake rs->fd = -1; 823c1bb86cdSEric Blake error_propagate(errp, local_err); 824c1bb86cdSEric Blake ret = -EINVAL; 825c1bb86cdSEric Blake } 826c1bb86cdSEric Blake } 827c1bb86cdSEric Blake 828c1bb86cdSEric Blake return ret; 829c1bb86cdSEric Blake } 830c1bb86cdSEric Blake 831c1bb86cdSEric Blake static void raw_reopen_commit(BDRVReopenState *state) 832c1bb86cdSEric Blake { 833c1bb86cdSEric Blake BDRVRawReopenState *rs = state->opaque; 834c1bb86cdSEric Blake BDRVRawState *s = state->bs->opaque; 835c1bb86cdSEric Blake 836c1bb86cdSEric Blake s->open_flags = rs->open_flags; 837c1bb86cdSEric Blake 838c1bb86cdSEric Blake qemu_close(s->fd); 839c1bb86cdSEric Blake s->fd = rs->fd; 840c1bb86cdSEric Blake 841c1bb86cdSEric Blake g_free(state->opaque); 842c1bb86cdSEric Blake state->opaque = NULL; 843c1bb86cdSEric Blake } 844c1bb86cdSEric Blake 845c1bb86cdSEric Blake 846c1bb86cdSEric Blake static void raw_reopen_abort(BDRVReopenState *state) 847c1bb86cdSEric Blake { 848c1bb86cdSEric Blake BDRVRawReopenState *rs = state->opaque; 849c1bb86cdSEric Blake 850c1bb86cdSEric Blake /* nothing to do if NULL, we didn't get far enough */ 851c1bb86cdSEric Blake if (rs == NULL) { 852c1bb86cdSEric Blake return; 853c1bb86cdSEric Blake } 854c1bb86cdSEric Blake 855c1bb86cdSEric Blake if (rs->fd >= 0) { 856c1bb86cdSEric Blake qemu_close(rs->fd); 857c1bb86cdSEric Blake rs->fd = -1; 858c1bb86cdSEric Blake } 859c1bb86cdSEric Blake g_free(state->opaque); 860c1bb86cdSEric Blake state->opaque = NULL; 861c1bb86cdSEric Blake } 862c1bb86cdSEric Blake 86348265250SEric Farman static int hdev_get_max_transfer_length(BlockDriverState *bs, int fd) 864c1bb86cdSEric Blake { 865c1bb86cdSEric Blake #ifdef BLKSECTGET 86648265250SEric Farman int max_bytes = 0; 86748265250SEric Farman short max_sectors = 0; 86848265250SEric Farman if (bs->sg && ioctl(fd, BLKSECTGET, &max_bytes) == 0) { 86948265250SEric Farman return max_bytes; 87048265250SEric Farman } else if (!bs->sg && ioctl(fd, BLKSECTGET, &max_sectors) == 0) { 87148265250SEric Farman return max_sectors << BDRV_SECTOR_BITS; 872c1bb86cdSEric Blake } else { 873c1bb86cdSEric Blake return -errno; 874c1bb86cdSEric Blake } 875c1bb86cdSEric Blake #else 876c1bb86cdSEric Blake return -ENOSYS; 877c1bb86cdSEric Blake #endif 878c1bb86cdSEric Blake } 879c1bb86cdSEric Blake 8809103f1ceSFam Zheng static int hdev_get_max_segments(const struct stat *st) 8819103f1ceSFam Zheng { 8829103f1ceSFam Zheng #ifdef CONFIG_LINUX 8839103f1ceSFam Zheng char buf[32]; 8849103f1ceSFam Zheng const char *end; 8859103f1ceSFam Zheng char *sysfspath; 8869103f1ceSFam Zheng int ret; 8879103f1ceSFam Zheng int fd = -1; 8889103f1ceSFam Zheng long max_segments; 8899103f1ceSFam Zheng 8909103f1ceSFam Zheng sysfspath = g_strdup_printf("/sys/dev/block/%u:%u/queue/max_segments", 8919103f1ceSFam Zheng major(st->st_rdev), minor(st->st_rdev)); 8929103f1ceSFam Zheng fd = open(sysfspath, O_RDONLY); 8939103f1ceSFam Zheng if (fd == -1) { 8949103f1ceSFam Zheng ret = -errno; 8959103f1ceSFam Zheng goto out; 8969103f1ceSFam Zheng } 8979103f1ceSFam Zheng do { 89869583490SStefan Hajnoczi ret = read(fd, buf, sizeof(buf) - 1); 8999103f1ceSFam Zheng } while (ret == -1 && errno == EINTR); 9009103f1ceSFam Zheng if (ret < 0) { 9019103f1ceSFam Zheng ret = -errno; 9029103f1ceSFam Zheng goto out; 9039103f1ceSFam Zheng } else if (ret == 0) { 9049103f1ceSFam Zheng ret = -EIO; 9059103f1ceSFam Zheng goto out; 9069103f1ceSFam Zheng } 9079103f1ceSFam Zheng buf[ret] = 0; 9089103f1ceSFam Zheng /* The file is ended with '\n', pass 'end' to accept that. */ 9099103f1ceSFam Zheng ret = qemu_strtol(buf, &end, 10, &max_segments); 9109103f1ceSFam Zheng if (ret == 0 && end && *end == '\n') { 9119103f1ceSFam Zheng ret = max_segments; 9129103f1ceSFam Zheng } 9139103f1ceSFam Zheng 9149103f1ceSFam Zheng out: 915fed414dfSFam Zheng if (fd != -1) { 916fed414dfSFam Zheng close(fd); 917fed414dfSFam Zheng } 9189103f1ceSFam Zheng g_free(sysfspath); 9199103f1ceSFam Zheng return ret; 9209103f1ceSFam Zheng #else 9219103f1ceSFam Zheng return -ENOTSUP; 9229103f1ceSFam Zheng #endif 9239103f1ceSFam Zheng } 9249103f1ceSFam Zheng 925c1bb86cdSEric Blake static void raw_refresh_limits(BlockDriverState *bs, Error **errp) 926c1bb86cdSEric Blake { 927c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 928c1bb86cdSEric Blake struct stat st; 929c1bb86cdSEric Blake 930c1bb86cdSEric Blake if (!fstat(s->fd, &st)) { 931c4c41a0aSEric Farman if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode)) { 93248265250SEric Farman int ret = hdev_get_max_transfer_length(bs, s->fd); 93348265250SEric Farman if (ret > 0 && ret <= BDRV_REQUEST_MAX_BYTES) { 93448265250SEric Farman bs->bl.max_transfer = pow2floor(ret); 935c1bb86cdSEric Blake } 9369103f1ceSFam Zheng ret = hdev_get_max_segments(&st); 9379103f1ceSFam Zheng if (ret > 0) { 9389103f1ceSFam Zheng bs->bl.max_transfer = MIN(bs->bl.max_transfer, 9399103f1ceSFam Zheng ret * getpagesize()); 9409103f1ceSFam Zheng } 941c1bb86cdSEric Blake } 942c1bb86cdSEric Blake } 943c1bb86cdSEric Blake 944c1bb86cdSEric Blake raw_probe_alignment(bs, s->fd, errp); 945c1bb86cdSEric Blake bs->bl.min_mem_alignment = s->buf_align; 946c1bb86cdSEric Blake bs->bl.opt_mem_alignment = MAX(s->buf_align, getpagesize()); 947c1bb86cdSEric Blake } 948c1bb86cdSEric Blake 949c1bb86cdSEric Blake static int check_for_dasd(int fd) 950c1bb86cdSEric Blake { 951c1bb86cdSEric Blake #ifdef BIODASDINFO2 952c1bb86cdSEric Blake struct dasd_information2_t info = {0}; 953c1bb86cdSEric Blake 954c1bb86cdSEric Blake return ioctl(fd, BIODASDINFO2, &info); 955c1bb86cdSEric Blake #else 956c1bb86cdSEric Blake return -1; 957c1bb86cdSEric Blake #endif 958c1bb86cdSEric Blake } 959c1bb86cdSEric Blake 960c1bb86cdSEric Blake /** 961c1bb86cdSEric Blake * Try to get @bs's logical and physical block size. 962c1bb86cdSEric Blake * On success, store them in @bsz and return zero. 963c1bb86cdSEric Blake * On failure, return negative errno. 964c1bb86cdSEric Blake */ 965c1bb86cdSEric Blake static int hdev_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz) 966c1bb86cdSEric Blake { 967c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 968c1bb86cdSEric Blake int ret; 969c1bb86cdSEric Blake 970c1bb86cdSEric Blake /* If DASD, get blocksizes */ 971c1bb86cdSEric Blake if (check_for_dasd(s->fd) < 0) { 972c1bb86cdSEric Blake return -ENOTSUP; 973c1bb86cdSEric Blake } 974c1bb86cdSEric Blake ret = probe_logical_blocksize(s->fd, &bsz->log); 975c1bb86cdSEric Blake if (ret < 0) { 976c1bb86cdSEric Blake return ret; 977c1bb86cdSEric Blake } 978c1bb86cdSEric Blake return probe_physical_blocksize(s->fd, &bsz->phys); 979c1bb86cdSEric Blake } 980c1bb86cdSEric Blake 981c1bb86cdSEric Blake /** 982c1bb86cdSEric Blake * Try to get @bs's geometry: cyls, heads, sectors. 983c1bb86cdSEric Blake * On success, store them in @geo and return 0. 984c1bb86cdSEric Blake * On failure return -errno. 985c1bb86cdSEric Blake * (Allows block driver to assign default geometry values that guest sees) 986c1bb86cdSEric Blake */ 987c1bb86cdSEric Blake #ifdef __linux__ 988c1bb86cdSEric Blake static int hdev_probe_geometry(BlockDriverState *bs, HDGeometry *geo) 989c1bb86cdSEric Blake { 990c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 991c1bb86cdSEric Blake struct hd_geometry ioctl_geo = {0}; 992c1bb86cdSEric Blake 993c1bb86cdSEric Blake /* If DASD, get its geometry */ 994c1bb86cdSEric Blake if (check_for_dasd(s->fd) < 0) { 995c1bb86cdSEric Blake return -ENOTSUP; 996c1bb86cdSEric Blake } 997c1bb86cdSEric Blake if (ioctl(s->fd, HDIO_GETGEO, &ioctl_geo) < 0) { 998c1bb86cdSEric Blake return -errno; 999c1bb86cdSEric Blake } 1000c1bb86cdSEric Blake /* HDIO_GETGEO may return success even though geo contains zeros 1001c1bb86cdSEric Blake (e.g. certain multipath setups) */ 1002c1bb86cdSEric Blake if (!ioctl_geo.heads || !ioctl_geo.sectors || !ioctl_geo.cylinders) { 1003c1bb86cdSEric Blake return -ENOTSUP; 1004c1bb86cdSEric Blake } 1005c1bb86cdSEric Blake /* Do not return a geometry for partition */ 1006c1bb86cdSEric Blake if (ioctl_geo.start != 0) { 1007c1bb86cdSEric Blake return -ENOTSUP; 1008c1bb86cdSEric Blake } 1009c1bb86cdSEric Blake geo->heads = ioctl_geo.heads; 1010c1bb86cdSEric Blake geo->sectors = ioctl_geo.sectors; 1011c1bb86cdSEric Blake geo->cylinders = ioctl_geo.cylinders; 1012c1bb86cdSEric Blake 1013c1bb86cdSEric Blake return 0; 1014c1bb86cdSEric Blake } 1015c1bb86cdSEric Blake #else /* __linux__ */ 1016c1bb86cdSEric Blake static int hdev_probe_geometry(BlockDriverState *bs, HDGeometry *geo) 1017c1bb86cdSEric Blake { 1018c1bb86cdSEric Blake return -ENOTSUP; 1019c1bb86cdSEric Blake } 1020c1bb86cdSEric Blake #endif 1021c1bb86cdSEric Blake 1022c1bb86cdSEric Blake static ssize_t handle_aiocb_ioctl(RawPosixAIOData *aiocb) 1023c1bb86cdSEric Blake { 1024c1bb86cdSEric Blake int ret; 1025c1bb86cdSEric Blake 1026c1bb86cdSEric Blake ret = ioctl(aiocb->aio_fildes, aiocb->aio_ioctl_cmd, aiocb->aio_ioctl_buf); 1027c1bb86cdSEric Blake if (ret == -1) { 1028c1bb86cdSEric Blake return -errno; 1029c1bb86cdSEric Blake } 1030c1bb86cdSEric Blake 1031c1bb86cdSEric Blake return 0; 1032c1bb86cdSEric Blake } 1033c1bb86cdSEric Blake 1034c1bb86cdSEric Blake static ssize_t handle_aiocb_flush(RawPosixAIOData *aiocb) 1035c1bb86cdSEric Blake { 1036e5bcf967SKevin Wolf BDRVRawState *s = aiocb->bs->opaque; 1037c1bb86cdSEric Blake int ret; 1038c1bb86cdSEric Blake 1039e5bcf967SKevin Wolf if (s->page_cache_inconsistent) { 1040e5bcf967SKevin Wolf return -EIO; 1041e5bcf967SKevin Wolf } 1042e5bcf967SKevin Wolf 1043c1bb86cdSEric Blake ret = qemu_fdatasync(aiocb->aio_fildes); 1044c1bb86cdSEric Blake if (ret == -1) { 1045e5bcf967SKevin Wolf /* There is no clear definition of the semantics of a failing fsync(), 1046e5bcf967SKevin Wolf * so we may have to assume the worst. The sad truth is that this 1047e5bcf967SKevin Wolf * assumption is correct for Linux. Some pages are now probably marked 1048e5bcf967SKevin Wolf * clean in the page cache even though they are inconsistent with the 1049e5bcf967SKevin Wolf * on-disk contents. The next fdatasync() call would succeed, but no 1050e5bcf967SKevin Wolf * further writeback attempt will be made. We can't get back to a state 1051e5bcf967SKevin Wolf * in which we know what is on disk (we would have to rewrite 1052e5bcf967SKevin Wolf * everything that was touched since the last fdatasync() at least), so 1053e5bcf967SKevin Wolf * make bdrv_flush() fail permanently. Given that the behaviour isn't 1054e5bcf967SKevin Wolf * really defined, I have little hope that other OSes are doing better. 1055e5bcf967SKevin Wolf * 1056e5bcf967SKevin Wolf * Obviously, this doesn't affect O_DIRECT, which bypasses the page 1057e5bcf967SKevin Wolf * cache. */ 1058e5bcf967SKevin Wolf if ((s->open_flags & O_DIRECT) == 0) { 1059e5bcf967SKevin Wolf s->page_cache_inconsistent = true; 1060e5bcf967SKevin Wolf } 1061c1bb86cdSEric Blake return -errno; 1062c1bb86cdSEric Blake } 1063c1bb86cdSEric Blake return 0; 1064c1bb86cdSEric Blake } 1065c1bb86cdSEric Blake 1066c1bb86cdSEric Blake #ifdef CONFIG_PREADV 1067c1bb86cdSEric Blake 1068c1bb86cdSEric Blake static bool preadv_present = true; 1069c1bb86cdSEric Blake 1070c1bb86cdSEric Blake static ssize_t 1071c1bb86cdSEric Blake qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset) 1072c1bb86cdSEric Blake { 1073c1bb86cdSEric Blake return preadv(fd, iov, nr_iov, offset); 1074c1bb86cdSEric Blake } 1075c1bb86cdSEric Blake 1076c1bb86cdSEric Blake static ssize_t 1077c1bb86cdSEric Blake qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset) 1078c1bb86cdSEric Blake { 1079c1bb86cdSEric Blake return pwritev(fd, iov, nr_iov, offset); 1080c1bb86cdSEric Blake } 1081c1bb86cdSEric Blake 1082c1bb86cdSEric Blake #else 1083c1bb86cdSEric Blake 1084c1bb86cdSEric Blake static bool preadv_present = false; 1085c1bb86cdSEric Blake 1086c1bb86cdSEric Blake static ssize_t 1087c1bb86cdSEric Blake qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset) 1088c1bb86cdSEric Blake { 1089c1bb86cdSEric Blake return -ENOSYS; 1090c1bb86cdSEric Blake } 1091c1bb86cdSEric Blake 1092c1bb86cdSEric Blake static ssize_t 1093c1bb86cdSEric Blake qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset) 1094c1bb86cdSEric Blake { 1095c1bb86cdSEric Blake return -ENOSYS; 1096c1bb86cdSEric Blake } 1097c1bb86cdSEric Blake 1098c1bb86cdSEric Blake #endif 1099c1bb86cdSEric Blake 1100c1bb86cdSEric Blake static ssize_t handle_aiocb_rw_vector(RawPosixAIOData *aiocb) 1101c1bb86cdSEric Blake { 1102c1bb86cdSEric Blake ssize_t len; 1103c1bb86cdSEric Blake 1104c1bb86cdSEric Blake do { 1105c1bb86cdSEric Blake if (aiocb->aio_type & QEMU_AIO_WRITE) 1106c1bb86cdSEric Blake len = qemu_pwritev(aiocb->aio_fildes, 1107c1bb86cdSEric Blake aiocb->aio_iov, 1108c1bb86cdSEric Blake aiocb->aio_niov, 1109c1bb86cdSEric Blake aiocb->aio_offset); 1110c1bb86cdSEric Blake else 1111c1bb86cdSEric Blake len = qemu_preadv(aiocb->aio_fildes, 1112c1bb86cdSEric Blake aiocb->aio_iov, 1113c1bb86cdSEric Blake aiocb->aio_niov, 1114c1bb86cdSEric Blake aiocb->aio_offset); 1115c1bb86cdSEric Blake } while (len == -1 && errno == EINTR); 1116c1bb86cdSEric Blake 1117c1bb86cdSEric Blake if (len == -1) { 1118c1bb86cdSEric Blake return -errno; 1119c1bb86cdSEric Blake } 1120c1bb86cdSEric Blake return len; 1121c1bb86cdSEric Blake } 1122c1bb86cdSEric Blake 1123c1bb86cdSEric Blake /* 1124c1bb86cdSEric Blake * Read/writes the data to/from a given linear buffer. 1125c1bb86cdSEric Blake * 1126c1bb86cdSEric Blake * Returns the number of bytes handles or -errno in case of an error. Short 1127c1bb86cdSEric Blake * reads are only returned if the end of the file is reached. 1128c1bb86cdSEric Blake */ 1129c1bb86cdSEric Blake static ssize_t handle_aiocb_rw_linear(RawPosixAIOData *aiocb, char *buf) 1130c1bb86cdSEric Blake { 1131c1bb86cdSEric Blake ssize_t offset = 0; 1132c1bb86cdSEric Blake ssize_t len; 1133c1bb86cdSEric Blake 1134c1bb86cdSEric Blake while (offset < aiocb->aio_nbytes) { 1135c1bb86cdSEric Blake if (aiocb->aio_type & QEMU_AIO_WRITE) { 1136c1bb86cdSEric Blake len = pwrite(aiocb->aio_fildes, 1137c1bb86cdSEric Blake (const char *)buf + offset, 1138c1bb86cdSEric Blake aiocb->aio_nbytes - offset, 1139c1bb86cdSEric Blake aiocb->aio_offset + offset); 1140c1bb86cdSEric Blake } else { 1141c1bb86cdSEric Blake len = pread(aiocb->aio_fildes, 1142c1bb86cdSEric Blake buf + offset, 1143c1bb86cdSEric Blake aiocb->aio_nbytes - offset, 1144c1bb86cdSEric Blake aiocb->aio_offset + offset); 1145c1bb86cdSEric Blake } 1146c1bb86cdSEric Blake if (len == -1 && errno == EINTR) { 1147c1bb86cdSEric Blake continue; 1148c1bb86cdSEric Blake } else if (len == -1 && errno == EINVAL && 1149c1bb86cdSEric Blake (aiocb->bs->open_flags & BDRV_O_NOCACHE) && 1150c1bb86cdSEric Blake !(aiocb->aio_type & QEMU_AIO_WRITE) && 1151c1bb86cdSEric Blake offset > 0) { 1152c1bb86cdSEric Blake /* O_DIRECT pread() may fail with EINVAL when offset is unaligned 1153c1bb86cdSEric Blake * after a short read. Assume that O_DIRECT short reads only occur 1154c1bb86cdSEric Blake * at EOF. Therefore this is a short read, not an I/O error. 1155c1bb86cdSEric Blake */ 1156c1bb86cdSEric Blake break; 1157c1bb86cdSEric Blake } else if (len == -1) { 1158c1bb86cdSEric Blake offset = -errno; 1159c1bb86cdSEric Blake break; 1160c1bb86cdSEric Blake } else if (len == 0) { 1161c1bb86cdSEric Blake break; 1162c1bb86cdSEric Blake } 1163c1bb86cdSEric Blake offset += len; 1164c1bb86cdSEric Blake } 1165c1bb86cdSEric Blake 1166c1bb86cdSEric Blake return offset; 1167c1bb86cdSEric Blake } 1168c1bb86cdSEric Blake 1169c1bb86cdSEric Blake static ssize_t handle_aiocb_rw(RawPosixAIOData *aiocb) 1170c1bb86cdSEric Blake { 1171c1bb86cdSEric Blake ssize_t nbytes; 1172c1bb86cdSEric Blake char *buf; 1173c1bb86cdSEric Blake 1174c1bb86cdSEric Blake if (!(aiocb->aio_type & QEMU_AIO_MISALIGNED)) { 1175c1bb86cdSEric Blake /* 1176c1bb86cdSEric Blake * If there is just a single buffer, and it is properly aligned 1177c1bb86cdSEric Blake * we can just use plain pread/pwrite without any problems. 1178c1bb86cdSEric Blake */ 1179c1bb86cdSEric Blake if (aiocb->aio_niov == 1) { 1180c1bb86cdSEric Blake return handle_aiocb_rw_linear(aiocb, aiocb->aio_iov->iov_base); 1181c1bb86cdSEric Blake } 1182c1bb86cdSEric Blake /* 1183c1bb86cdSEric Blake * We have more than one iovec, and all are properly aligned. 1184c1bb86cdSEric Blake * 1185c1bb86cdSEric Blake * Try preadv/pwritev first and fall back to linearizing the 1186c1bb86cdSEric Blake * buffer if it's not supported. 1187c1bb86cdSEric Blake */ 1188c1bb86cdSEric Blake if (preadv_present) { 1189c1bb86cdSEric Blake nbytes = handle_aiocb_rw_vector(aiocb); 1190c1bb86cdSEric Blake if (nbytes == aiocb->aio_nbytes || 1191c1bb86cdSEric Blake (nbytes < 0 && nbytes != -ENOSYS)) { 1192c1bb86cdSEric Blake return nbytes; 1193c1bb86cdSEric Blake } 1194c1bb86cdSEric Blake preadv_present = false; 1195c1bb86cdSEric Blake } 1196c1bb86cdSEric Blake 1197c1bb86cdSEric Blake /* 1198c1bb86cdSEric Blake * XXX(hch): short read/write. no easy way to handle the reminder 1199c1bb86cdSEric Blake * using these interfaces. For now retry using plain 1200c1bb86cdSEric Blake * pread/pwrite? 1201c1bb86cdSEric Blake */ 1202c1bb86cdSEric Blake } 1203c1bb86cdSEric Blake 1204c1bb86cdSEric Blake /* 1205c1bb86cdSEric Blake * Ok, we have to do it the hard way, copy all segments into 1206c1bb86cdSEric Blake * a single aligned buffer. 1207c1bb86cdSEric Blake */ 1208c1bb86cdSEric Blake buf = qemu_try_blockalign(aiocb->bs, aiocb->aio_nbytes); 1209c1bb86cdSEric Blake if (buf == NULL) { 1210c1bb86cdSEric Blake return -ENOMEM; 1211c1bb86cdSEric Blake } 1212c1bb86cdSEric Blake 1213c1bb86cdSEric Blake if (aiocb->aio_type & QEMU_AIO_WRITE) { 1214c1bb86cdSEric Blake char *p = buf; 1215c1bb86cdSEric Blake int i; 1216c1bb86cdSEric Blake 1217c1bb86cdSEric Blake for (i = 0; i < aiocb->aio_niov; ++i) { 1218c1bb86cdSEric Blake memcpy(p, aiocb->aio_iov[i].iov_base, aiocb->aio_iov[i].iov_len); 1219c1bb86cdSEric Blake p += aiocb->aio_iov[i].iov_len; 1220c1bb86cdSEric Blake } 1221c1bb86cdSEric Blake assert(p - buf == aiocb->aio_nbytes); 1222c1bb86cdSEric Blake } 1223c1bb86cdSEric Blake 1224c1bb86cdSEric Blake nbytes = handle_aiocb_rw_linear(aiocb, buf); 1225c1bb86cdSEric Blake if (!(aiocb->aio_type & QEMU_AIO_WRITE)) { 1226c1bb86cdSEric Blake char *p = buf; 1227c1bb86cdSEric Blake size_t count = aiocb->aio_nbytes, copy; 1228c1bb86cdSEric Blake int i; 1229c1bb86cdSEric Blake 1230c1bb86cdSEric Blake for (i = 0; i < aiocb->aio_niov && count; ++i) { 1231c1bb86cdSEric Blake copy = count; 1232c1bb86cdSEric Blake if (copy > aiocb->aio_iov[i].iov_len) { 1233c1bb86cdSEric Blake copy = aiocb->aio_iov[i].iov_len; 1234c1bb86cdSEric Blake } 1235c1bb86cdSEric Blake memcpy(aiocb->aio_iov[i].iov_base, p, copy); 1236c1bb86cdSEric Blake assert(count >= copy); 1237c1bb86cdSEric Blake p += copy; 1238c1bb86cdSEric Blake count -= copy; 1239c1bb86cdSEric Blake } 1240c1bb86cdSEric Blake assert(count == 0); 1241c1bb86cdSEric Blake } 1242c1bb86cdSEric Blake qemu_vfree(buf); 1243c1bb86cdSEric Blake 1244c1bb86cdSEric Blake return nbytes; 1245c1bb86cdSEric Blake } 1246c1bb86cdSEric Blake 1247c1bb86cdSEric Blake #ifdef CONFIG_XFS 1248c1bb86cdSEric Blake static int xfs_write_zeroes(BDRVRawState *s, int64_t offset, uint64_t bytes) 1249c1bb86cdSEric Blake { 1250c1bb86cdSEric Blake struct xfs_flock64 fl; 1251c1bb86cdSEric Blake int err; 1252c1bb86cdSEric Blake 1253c1bb86cdSEric Blake memset(&fl, 0, sizeof(fl)); 1254c1bb86cdSEric Blake fl.l_whence = SEEK_SET; 1255c1bb86cdSEric Blake fl.l_start = offset; 1256c1bb86cdSEric Blake fl.l_len = bytes; 1257c1bb86cdSEric Blake 1258c1bb86cdSEric Blake if (xfsctl(NULL, s->fd, XFS_IOC_ZERO_RANGE, &fl) < 0) { 1259c1bb86cdSEric Blake err = errno; 1260c1bb86cdSEric Blake DPRINTF("cannot write zero range (%s)\n", strerror(errno)); 1261c1bb86cdSEric Blake return -err; 1262c1bb86cdSEric Blake } 1263c1bb86cdSEric Blake 1264c1bb86cdSEric Blake return 0; 1265c1bb86cdSEric Blake } 1266c1bb86cdSEric Blake 1267c1bb86cdSEric Blake static int xfs_discard(BDRVRawState *s, int64_t offset, uint64_t bytes) 1268c1bb86cdSEric Blake { 1269c1bb86cdSEric Blake struct xfs_flock64 fl; 1270c1bb86cdSEric Blake int err; 1271c1bb86cdSEric Blake 1272c1bb86cdSEric Blake memset(&fl, 0, sizeof(fl)); 1273c1bb86cdSEric Blake fl.l_whence = SEEK_SET; 1274c1bb86cdSEric Blake fl.l_start = offset; 1275c1bb86cdSEric Blake fl.l_len = bytes; 1276c1bb86cdSEric Blake 1277c1bb86cdSEric Blake if (xfsctl(NULL, s->fd, XFS_IOC_UNRESVSP64, &fl) < 0) { 1278c1bb86cdSEric Blake err = errno; 1279c1bb86cdSEric Blake DPRINTF("cannot punch hole (%s)\n", strerror(errno)); 1280c1bb86cdSEric Blake return -err; 1281c1bb86cdSEric Blake } 1282c1bb86cdSEric Blake 1283c1bb86cdSEric Blake return 0; 1284c1bb86cdSEric Blake } 1285c1bb86cdSEric Blake #endif 1286c1bb86cdSEric Blake 1287c1bb86cdSEric Blake static int translate_err(int err) 1288c1bb86cdSEric Blake { 1289c1bb86cdSEric Blake if (err == -ENODEV || err == -ENOSYS || err == -EOPNOTSUPP || 1290c1bb86cdSEric Blake err == -ENOTTY) { 1291c1bb86cdSEric Blake err = -ENOTSUP; 1292c1bb86cdSEric Blake } 1293c1bb86cdSEric Blake return err; 1294c1bb86cdSEric Blake } 1295c1bb86cdSEric Blake 1296c1bb86cdSEric Blake #ifdef CONFIG_FALLOCATE 1297c1bb86cdSEric Blake static int do_fallocate(int fd, int mode, off_t offset, off_t len) 1298c1bb86cdSEric Blake { 1299c1bb86cdSEric Blake do { 1300c1bb86cdSEric Blake if (fallocate(fd, mode, offset, len) == 0) { 1301c1bb86cdSEric Blake return 0; 1302c1bb86cdSEric Blake } 1303c1bb86cdSEric Blake } while (errno == EINTR); 1304c1bb86cdSEric Blake return translate_err(-errno); 1305c1bb86cdSEric Blake } 1306c1bb86cdSEric Blake #endif 1307c1bb86cdSEric Blake 1308c1bb86cdSEric Blake static ssize_t handle_aiocb_write_zeroes_block(RawPosixAIOData *aiocb) 1309c1bb86cdSEric Blake { 1310c1bb86cdSEric Blake int ret = -ENOTSUP; 1311c1bb86cdSEric Blake BDRVRawState *s = aiocb->bs->opaque; 1312c1bb86cdSEric Blake 1313c1bb86cdSEric Blake if (!s->has_write_zeroes) { 1314c1bb86cdSEric Blake return -ENOTSUP; 1315c1bb86cdSEric Blake } 1316c1bb86cdSEric Blake 1317c1bb86cdSEric Blake #ifdef BLKZEROOUT 1318c1bb86cdSEric Blake do { 1319c1bb86cdSEric Blake uint64_t range[2] = { aiocb->aio_offset, aiocb->aio_nbytes }; 1320c1bb86cdSEric Blake if (ioctl(aiocb->aio_fildes, BLKZEROOUT, range) == 0) { 1321c1bb86cdSEric Blake return 0; 1322c1bb86cdSEric Blake } 1323c1bb86cdSEric Blake } while (errno == EINTR); 1324c1bb86cdSEric Blake 1325c1bb86cdSEric Blake ret = translate_err(-errno); 1326c1bb86cdSEric Blake #endif 1327c1bb86cdSEric Blake 1328c1bb86cdSEric Blake if (ret == -ENOTSUP) { 1329c1bb86cdSEric Blake s->has_write_zeroes = false; 1330c1bb86cdSEric Blake } 1331c1bb86cdSEric Blake return ret; 1332c1bb86cdSEric Blake } 1333c1bb86cdSEric Blake 1334c1bb86cdSEric Blake static ssize_t handle_aiocb_write_zeroes(RawPosixAIOData *aiocb) 1335c1bb86cdSEric Blake { 1336c1bb86cdSEric Blake #if defined(CONFIG_FALLOCATE) || defined(CONFIG_XFS) 1337c1bb86cdSEric Blake BDRVRawState *s = aiocb->bs->opaque; 1338c1bb86cdSEric Blake #endif 133970d9110bSDenis V. Lunev #ifdef CONFIG_FALLOCATE 134070d9110bSDenis V. Lunev int64_t len; 134170d9110bSDenis V. Lunev #endif 1342c1bb86cdSEric Blake 1343c1bb86cdSEric Blake if (aiocb->aio_type & QEMU_AIO_BLKDEV) { 1344c1bb86cdSEric Blake return handle_aiocb_write_zeroes_block(aiocb); 1345c1bb86cdSEric Blake } 1346c1bb86cdSEric Blake 1347c1bb86cdSEric Blake #ifdef CONFIG_XFS 1348c1bb86cdSEric Blake if (s->is_xfs) { 1349c1bb86cdSEric Blake return xfs_write_zeroes(s, aiocb->aio_offset, aiocb->aio_nbytes); 1350c1bb86cdSEric Blake } 1351c1bb86cdSEric Blake #endif 1352c1bb86cdSEric Blake 1353c1bb86cdSEric Blake #ifdef CONFIG_FALLOCATE_ZERO_RANGE 1354c1bb86cdSEric Blake if (s->has_write_zeroes) { 1355c1bb86cdSEric Blake int ret = do_fallocate(s->fd, FALLOC_FL_ZERO_RANGE, 1356c1bb86cdSEric Blake aiocb->aio_offset, aiocb->aio_nbytes); 1357c1bb86cdSEric Blake if (ret == 0 || ret != -ENOTSUP) { 1358c1bb86cdSEric Blake return ret; 1359c1bb86cdSEric Blake } 1360c1bb86cdSEric Blake s->has_write_zeroes = false; 1361c1bb86cdSEric Blake } 1362c1bb86cdSEric Blake #endif 1363c1bb86cdSEric Blake 1364c1bb86cdSEric Blake #ifdef CONFIG_FALLOCATE_PUNCH_HOLE 1365c1bb86cdSEric Blake if (s->has_discard && s->has_fallocate) { 1366c1bb86cdSEric Blake int ret = do_fallocate(s->fd, 1367c1bb86cdSEric Blake FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 1368c1bb86cdSEric Blake aiocb->aio_offset, aiocb->aio_nbytes); 1369c1bb86cdSEric Blake if (ret == 0) { 1370c1bb86cdSEric Blake ret = do_fallocate(s->fd, 0, aiocb->aio_offset, aiocb->aio_nbytes); 1371c1bb86cdSEric Blake if (ret == 0 || ret != -ENOTSUP) { 1372c1bb86cdSEric Blake return ret; 1373c1bb86cdSEric Blake } 1374c1bb86cdSEric Blake s->has_fallocate = false; 1375c1bb86cdSEric Blake } else if (ret != -ENOTSUP) { 1376c1bb86cdSEric Blake return ret; 1377c1bb86cdSEric Blake } else { 1378c1bb86cdSEric Blake s->has_discard = false; 1379c1bb86cdSEric Blake } 1380c1bb86cdSEric Blake } 1381c1bb86cdSEric Blake #endif 1382c1bb86cdSEric Blake 1383c1bb86cdSEric Blake #ifdef CONFIG_FALLOCATE 138470d9110bSDenis V. Lunev /* Last resort: we are trying to extend the file with zeroed data. This 138570d9110bSDenis V. Lunev * can be done via fallocate(fd, 0) */ 138670d9110bSDenis V. Lunev len = bdrv_getlength(aiocb->bs); 138770d9110bSDenis V. Lunev if (s->has_fallocate && len >= 0 && aiocb->aio_offset >= len) { 1388c1bb86cdSEric Blake int ret = do_fallocate(s->fd, 0, aiocb->aio_offset, aiocb->aio_nbytes); 1389c1bb86cdSEric Blake if (ret == 0 || ret != -ENOTSUP) { 1390c1bb86cdSEric Blake return ret; 1391c1bb86cdSEric Blake } 1392c1bb86cdSEric Blake s->has_fallocate = false; 1393c1bb86cdSEric Blake } 1394c1bb86cdSEric Blake #endif 1395c1bb86cdSEric Blake 1396c1bb86cdSEric Blake return -ENOTSUP; 1397c1bb86cdSEric Blake } 1398c1bb86cdSEric Blake 1399c1bb86cdSEric Blake static ssize_t handle_aiocb_discard(RawPosixAIOData *aiocb) 1400c1bb86cdSEric Blake { 1401c1bb86cdSEric Blake int ret = -EOPNOTSUPP; 1402c1bb86cdSEric Blake BDRVRawState *s = aiocb->bs->opaque; 1403c1bb86cdSEric Blake 1404c1bb86cdSEric Blake if (!s->has_discard) { 1405c1bb86cdSEric Blake return -ENOTSUP; 1406c1bb86cdSEric Blake } 1407c1bb86cdSEric Blake 1408c1bb86cdSEric Blake if (aiocb->aio_type & QEMU_AIO_BLKDEV) { 1409c1bb86cdSEric Blake #ifdef BLKDISCARD 1410c1bb86cdSEric Blake do { 1411c1bb86cdSEric Blake uint64_t range[2] = { aiocb->aio_offset, aiocb->aio_nbytes }; 1412c1bb86cdSEric Blake if (ioctl(aiocb->aio_fildes, BLKDISCARD, range) == 0) { 1413c1bb86cdSEric Blake return 0; 1414c1bb86cdSEric Blake } 1415c1bb86cdSEric Blake } while (errno == EINTR); 1416c1bb86cdSEric Blake 1417c1bb86cdSEric Blake ret = -errno; 1418c1bb86cdSEric Blake #endif 1419c1bb86cdSEric Blake } else { 1420c1bb86cdSEric Blake #ifdef CONFIG_XFS 1421c1bb86cdSEric Blake if (s->is_xfs) { 1422c1bb86cdSEric Blake return xfs_discard(s, aiocb->aio_offset, aiocb->aio_nbytes); 1423c1bb86cdSEric Blake } 1424c1bb86cdSEric Blake #endif 1425c1bb86cdSEric Blake 1426c1bb86cdSEric Blake #ifdef CONFIG_FALLOCATE_PUNCH_HOLE 1427c1bb86cdSEric Blake ret = do_fallocate(s->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 1428c1bb86cdSEric Blake aiocb->aio_offset, aiocb->aio_nbytes); 1429c1bb86cdSEric Blake #endif 1430c1bb86cdSEric Blake } 1431c1bb86cdSEric Blake 1432c1bb86cdSEric Blake ret = translate_err(ret); 1433c1bb86cdSEric Blake if (ret == -ENOTSUP) { 1434c1bb86cdSEric Blake s->has_discard = false; 1435c1bb86cdSEric Blake } 1436c1bb86cdSEric Blake return ret; 1437c1bb86cdSEric Blake } 1438c1bb86cdSEric Blake 1439c1bb86cdSEric Blake static int aio_worker(void *arg) 1440c1bb86cdSEric Blake { 1441c1bb86cdSEric Blake RawPosixAIOData *aiocb = arg; 1442c1bb86cdSEric Blake ssize_t ret = 0; 1443c1bb86cdSEric Blake 1444c1bb86cdSEric Blake switch (aiocb->aio_type & QEMU_AIO_TYPE_MASK) { 1445c1bb86cdSEric Blake case QEMU_AIO_READ: 1446c1bb86cdSEric Blake ret = handle_aiocb_rw(aiocb); 1447c1bb86cdSEric Blake if (ret >= 0 && ret < aiocb->aio_nbytes) { 1448c1bb86cdSEric Blake iov_memset(aiocb->aio_iov, aiocb->aio_niov, ret, 1449c1bb86cdSEric Blake 0, aiocb->aio_nbytes - ret); 1450c1bb86cdSEric Blake 1451c1bb86cdSEric Blake ret = aiocb->aio_nbytes; 1452c1bb86cdSEric Blake } 1453c1bb86cdSEric Blake if (ret == aiocb->aio_nbytes) { 1454c1bb86cdSEric Blake ret = 0; 1455c1bb86cdSEric Blake } else if (ret >= 0 && ret < aiocb->aio_nbytes) { 1456c1bb86cdSEric Blake ret = -EINVAL; 1457c1bb86cdSEric Blake } 1458c1bb86cdSEric Blake break; 1459c1bb86cdSEric Blake case QEMU_AIO_WRITE: 1460c1bb86cdSEric Blake ret = handle_aiocb_rw(aiocb); 1461c1bb86cdSEric Blake if (ret == aiocb->aio_nbytes) { 1462c1bb86cdSEric Blake ret = 0; 1463c1bb86cdSEric Blake } else if (ret >= 0 && ret < aiocb->aio_nbytes) { 1464c1bb86cdSEric Blake ret = -EINVAL; 1465c1bb86cdSEric Blake } 1466c1bb86cdSEric Blake break; 1467c1bb86cdSEric Blake case QEMU_AIO_FLUSH: 1468c1bb86cdSEric Blake ret = handle_aiocb_flush(aiocb); 1469c1bb86cdSEric Blake break; 1470c1bb86cdSEric Blake case QEMU_AIO_IOCTL: 1471c1bb86cdSEric Blake ret = handle_aiocb_ioctl(aiocb); 1472c1bb86cdSEric Blake break; 1473c1bb86cdSEric Blake case QEMU_AIO_DISCARD: 1474c1bb86cdSEric Blake ret = handle_aiocb_discard(aiocb); 1475c1bb86cdSEric Blake break; 1476c1bb86cdSEric Blake case QEMU_AIO_WRITE_ZEROES: 1477c1bb86cdSEric Blake ret = handle_aiocb_write_zeroes(aiocb); 1478c1bb86cdSEric Blake break; 1479c1bb86cdSEric Blake default: 1480c1bb86cdSEric Blake fprintf(stderr, "invalid aio request (0x%x)\n", aiocb->aio_type); 1481c1bb86cdSEric Blake ret = -EINVAL; 1482c1bb86cdSEric Blake break; 1483c1bb86cdSEric Blake } 1484c1bb86cdSEric Blake 1485c1bb86cdSEric Blake g_free(aiocb); 1486c1bb86cdSEric Blake return ret; 1487c1bb86cdSEric Blake } 1488c1bb86cdSEric Blake 1489c1bb86cdSEric Blake static int paio_submit_co(BlockDriverState *bs, int fd, 1490c1bb86cdSEric Blake int64_t offset, QEMUIOVector *qiov, 1491f5a5ca79SManos Pitsidianakis int bytes, int type) 1492c1bb86cdSEric Blake { 1493c1bb86cdSEric Blake RawPosixAIOData *acb = g_new(RawPosixAIOData, 1); 1494c1bb86cdSEric Blake ThreadPool *pool; 1495c1bb86cdSEric Blake 1496c1bb86cdSEric Blake acb->bs = bs; 1497c1bb86cdSEric Blake acb->aio_type = type; 1498c1bb86cdSEric Blake acb->aio_fildes = fd; 1499c1bb86cdSEric Blake 1500f5a5ca79SManos Pitsidianakis acb->aio_nbytes = bytes; 1501c1bb86cdSEric Blake acb->aio_offset = offset; 1502c1bb86cdSEric Blake 1503c1bb86cdSEric Blake if (qiov) { 1504c1bb86cdSEric Blake acb->aio_iov = qiov->iov; 1505c1bb86cdSEric Blake acb->aio_niov = qiov->niov; 1506f5a5ca79SManos Pitsidianakis assert(qiov->size == bytes); 1507c1bb86cdSEric Blake } 1508c1bb86cdSEric Blake 1509f5a5ca79SManos Pitsidianakis trace_paio_submit_co(offset, bytes, type); 1510c1bb86cdSEric Blake pool = aio_get_thread_pool(bdrv_get_aio_context(bs)); 1511c1bb86cdSEric Blake return thread_pool_submit_co(pool, aio_worker, acb); 1512c1bb86cdSEric Blake } 1513c1bb86cdSEric Blake 1514c1bb86cdSEric Blake static BlockAIOCB *paio_submit(BlockDriverState *bs, int fd, 1515f5a5ca79SManos Pitsidianakis int64_t offset, QEMUIOVector *qiov, int bytes, 1516c1bb86cdSEric Blake BlockCompletionFunc *cb, void *opaque, int type) 1517c1bb86cdSEric Blake { 1518c1bb86cdSEric Blake RawPosixAIOData *acb = g_new(RawPosixAIOData, 1); 1519c1bb86cdSEric Blake ThreadPool *pool; 1520c1bb86cdSEric Blake 1521c1bb86cdSEric Blake acb->bs = bs; 1522c1bb86cdSEric Blake acb->aio_type = type; 1523c1bb86cdSEric Blake acb->aio_fildes = fd; 1524c1bb86cdSEric Blake 1525f5a5ca79SManos Pitsidianakis acb->aio_nbytes = bytes; 1526c1bb86cdSEric Blake acb->aio_offset = offset; 1527c1bb86cdSEric Blake 1528c1bb86cdSEric Blake if (qiov) { 1529c1bb86cdSEric Blake acb->aio_iov = qiov->iov; 1530c1bb86cdSEric Blake acb->aio_niov = qiov->niov; 1531c1bb86cdSEric Blake assert(qiov->size == acb->aio_nbytes); 1532c1bb86cdSEric Blake } 1533c1bb86cdSEric Blake 1534f5a5ca79SManos Pitsidianakis trace_paio_submit(acb, opaque, offset, bytes, type); 1535c1bb86cdSEric Blake pool = aio_get_thread_pool(bdrv_get_aio_context(bs)); 1536c1bb86cdSEric Blake return thread_pool_submit_aio(pool, aio_worker, acb, cb, opaque); 1537c1bb86cdSEric Blake } 1538c1bb86cdSEric Blake 1539c1bb86cdSEric Blake static int coroutine_fn raw_co_prw(BlockDriverState *bs, uint64_t offset, 1540c1bb86cdSEric Blake uint64_t bytes, QEMUIOVector *qiov, int type) 1541c1bb86cdSEric Blake { 1542c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 1543c1bb86cdSEric Blake 1544c1bb86cdSEric Blake if (fd_open(bs) < 0) 1545c1bb86cdSEric Blake return -EIO; 1546c1bb86cdSEric Blake 1547c1bb86cdSEric Blake /* 1548c1bb86cdSEric Blake * Check if the underlying device requires requests to be aligned, 1549c1bb86cdSEric Blake * and if the request we are trying to submit is aligned or not. 1550c1bb86cdSEric Blake * If this is the case tell the low-level driver that it needs 1551c1bb86cdSEric Blake * to copy the buffer. 1552c1bb86cdSEric Blake */ 1553c1bb86cdSEric Blake if (s->needs_alignment) { 1554c1bb86cdSEric Blake if (!bdrv_qiov_is_aligned(bs, qiov)) { 1555c1bb86cdSEric Blake type |= QEMU_AIO_MISALIGNED; 1556c1bb86cdSEric Blake #ifdef CONFIG_LINUX_AIO 1557c1bb86cdSEric Blake } else if (s->use_linux_aio) { 1558c1bb86cdSEric Blake LinuxAioState *aio = aio_get_linux_aio(bdrv_get_aio_context(bs)); 1559c1bb86cdSEric Blake assert(qiov->size == bytes); 1560c1bb86cdSEric Blake return laio_co_submit(bs, aio, s->fd, offset, qiov, type); 1561c1bb86cdSEric Blake #endif 1562c1bb86cdSEric Blake } 1563c1bb86cdSEric Blake } 1564c1bb86cdSEric Blake 1565c1bb86cdSEric Blake return paio_submit_co(bs, s->fd, offset, qiov, bytes, type); 1566c1bb86cdSEric Blake } 1567c1bb86cdSEric Blake 1568c1bb86cdSEric Blake static int coroutine_fn raw_co_preadv(BlockDriverState *bs, uint64_t offset, 1569c1bb86cdSEric Blake uint64_t bytes, QEMUIOVector *qiov, 1570c1bb86cdSEric Blake int flags) 1571c1bb86cdSEric Blake { 1572c1bb86cdSEric Blake return raw_co_prw(bs, offset, bytes, qiov, QEMU_AIO_READ); 1573c1bb86cdSEric Blake } 1574c1bb86cdSEric Blake 1575c1bb86cdSEric Blake static int coroutine_fn raw_co_pwritev(BlockDriverState *bs, uint64_t offset, 1576c1bb86cdSEric Blake uint64_t bytes, QEMUIOVector *qiov, 1577c1bb86cdSEric Blake int flags) 1578c1bb86cdSEric Blake { 1579c1bb86cdSEric Blake assert(flags == 0); 1580c1bb86cdSEric Blake return raw_co_prw(bs, offset, bytes, qiov, QEMU_AIO_WRITE); 1581c1bb86cdSEric Blake } 1582c1bb86cdSEric Blake 1583c1bb86cdSEric Blake static void raw_aio_plug(BlockDriverState *bs) 1584c1bb86cdSEric Blake { 1585c1bb86cdSEric Blake #ifdef CONFIG_LINUX_AIO 1586c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 1587c1bb86cdSEric Blake if (s->use_linux_aio) { 1588c1bb86cdSEric Blake LinuxAioState *aio = aio_get_linux_aio(bdrv_get_aio_context(bs)); 1589c1bb86cdSEric Blake laio_io_plug(bs, aio); 1590c1bb86cdSEric Blake } 1591c1bb86cdSEric Blake #endif 1592c1bb86cdSEric Blake } 1593c1bb86cdSEric Blake 1594c1bb86cdSEric Blake static void raw_aio_unplug(BlockDriverState *bs) 1595c1bb86cdSEric Blake { 1596c1bb86cdSEric Blake #ifdef CONFIG_LINUX_AIO 1597c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 1598c1bb86cdSEric Blake if (s->use_linux_aio) { 1599c1bb86cdSEric Blake LinuxAioState *aio = aio_get_linux_aio(bdrv_get_aio_context(bs)); 1600c1bb86cdSEric Blake laio_io_unplug(bs, aio); 1601c1bb86cdSEric Blake } 1602c1bb86cdSEric Blake #endif 1603c1bb86cdSEric Blake } 1604c1bb86cdSEric Blake 1605c1bb86cdSEric Blake static BlockAIOCB *raw_aio_flush(BlockDriverState *bs, 1606c1bb86cdSEric Blake BlockCompletionFunc *cb, void *opaque) 1607c1bb86cdSEric Blake { 1608c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 1609c1bb86cdSEric Blake 1610c1bb86cdSEric Blake if (fd_open(bs) < 0) 1611c1bb86cdSEric Blake return NULL; 1612c1bb86cdSEric Blake 1613c1bb86cdSEric Blake return paio_submit(bs, s->fd, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH); 1614c1bb86cdSEric Blake } 1615c1bb86cdSEric Blake 1616c1bb86cdSEric Blake static void raw_close(BlockDriverState *bs) 1617c1bb86cdSEric Blake { 1618c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 1619c1bb86cdSEric Blake 1620c1bb86cdSEric Blake if (s->fd >= 0) { 1621c1bb86cdSEric Blake qemu_close(s->fd); 1622c1bb86cdSEric Blake s->fd = -1; 1623c1bb86cdSEric Blake } 1624244a5668SFam Zheng if (s->lock_fd >= 0) { 1625244a5668SFam Zheng qemu_close(s->lock_fd); 1626244a5668SFam Zheng s->lock_fd = -1; 1627244a5668SFam Zheng } 1628c1bb86cdSEric Blake } 1629c1bb86cdSEric Blake 1630d0bc9e5dSMax Reitz /** 1631d0bc9e5dSMax Reitz * Truncates the given regular file @fd to @offset and, when growing, fills the 1632d0bc9e5dSMax Reitz * new space according to @prealloc. 1633d0bc9e5dSMax Reitz * 1634d0bc9e5dSMax Reitz * Returns: 0 on success, -errno on failure. 1635d0bc9e5dSMax Reitz */ 16369f63b07eSMax Reitz static int raw_regular_truncate(int fd, int64_t offset, PreallocMode prealloc, 16379f63b07eSMax Reitz Error **errp) 16389f63b07eSMax Reitz { 16399f63b07eSMax Reitz int result = 0; 1640d0bc9e5dSMax Reitz int64_t current_length = 0; 1641d0bc9e5dSMax Reitz char *buf = NULL; 1642d0bc9e5dSMax Reitz struct stat st; 1643d0bc9e5dSMax Reitz 1644d0bc9e5dSMax Reitz if (fstat(fd, &st) < 0) { 1645d0bc9e5dSMax Reitz result = -errno; 1646d0bc9e5dSMax Reitz error_setg_errno(errp, -result, "Could not stat file"); 1647d0bc9e5dSMax Reitz return result; 1648d0bc9e5dSMax Reitz } 1649d0bc9e5dSMax Reitz 1650d0bc9e5dSMax Reitz current_length = st.st_size; 1651d0bc9e5dSMax Reitz if (current_length > offset && prealloc != PREALLOC_MODE_OFF) { 1652d0bc9e5dSMax Reitz error_setg(errp, "Cannot use preallocation for shrinking files"); 1653d0bc9e5dSMax Reitz return -ENOTSUP; 1654d0bc9e5dSMax Reitz } 16559f63b07eSMax Reitz 16569f63b07eSMax Reitz switch (prealloc) { 16579f63b07eSMax Reitz #ifdef CONFIG_POSIX_FALLOCATE 16589f63b07eSMax Reitz case PREALLOC_MODE_FALLOC: 16599f63b07eSMax Reitz /* 16609f63b07eSMax Reitz * Truncating before posix_fallocate() makes it about twice slower on 16619f63b07eSMax Reitz * file systems that do not support fallocate(), trying to check if a 16629f63b07eSMax Reitz * block is allocated before allocating it, so don't do that here. 16639f63b07eSMax Reitz */ 1664d0bc9e5dSMax Reitz result = -posix_fallocate(fd, current_length, offset - current_length); 16659f63b07eSMax Reitz if (result != 0) { 16669f63b07eSMax Reitz /* posix_fallocate() doesn't set errno. */ 16679f63b07eSMax Reitz error_setg_errno(errp, -result, 1668d0bc9e5dSMax Reitz "Could not preallocate new data"); 16699f63b07eSMax Reitz } 1670d0bc9e5dSMax Reitz goto out; 16719f63b07eSMax Reitz #endif 16729f63b07eSMax Reitz case PREALLOC_MODE_FULL: 16739f63b07eSMax Reitz { 1674d0bc9e5dSMax Reitz int64_t num = 0, left = offset - current_length; 16759f63b07eSMax Reitz 16769f63b07eSMax Reitz /* 16779f63b07eSMax Reitz * Knowing the final size from the beginning could allow the file 16789f63b07eSMax Reitz * system driver to do less allocations and possibly avoid 16799f63b07eSMax Reitz * fragmentation of the file. 16809f63b07eSMax Reitz */ 16819f63b07eSMax Reitz if (ftruncate(fd, offset) != 0) { 16829f63b07eSMax Reitz result = -errno; 16839f63b07eSMax Reitz error_setg_errno(errp, -result, "Could not resize file"); 1684d0bc9e5dSMax Reitz goto out; 16859f63b07eSMax Reitz } 16869f63b07eSMax Reitz 16879f63b07eSMax Reitz buf = g_malloc0(65536); 16889f63b07eSMax Reitz 1689d0bc9e5dSMax Reitz result = lseek(fd, current_length, SEEK_SET); 1690d0bc9e5dSMax Reitz if (result < 0) { 1691d0bc9e5dSMax Reitz result = -errno; 1692d0bc9e5dSMax Reitz error_setg_errno(errp, -result, 1693d0bc9e5dSMax Reitz "Failed to seek to the old end of file"); 1694d0bc9e5dSMax Reitz goto out; 1695d0bc9e5dSMax Reitz } 1696d0bc9e5dSMax Reitz 16979f63b07eSMax Reitz while (left > 0) { 16989f63b07eSMax Reitz num = MIN(left, 65536); 16999f63b07eSMax Reitz result = write(fd, buf, num); 17009f63b07eSMax Reitz if (result < 0) { 17019f63b07eSMax Reitz result = -errno; 17029f63b07eSMax Reitz error_setg_errno(errp, -result, 1703d0bc9e5dSMax Reitz "Could not write zeros for preallocation"); 1704d0bc9e5dSMax Reitz goto out; 17059f63b07eSMax Reitz } 17069f63b07eSMax Reitz left -= result; 17079f63b07eSMax Reitz } 17089f63b07eSMax Reitz if (result >= 0) { 17099f63b07eSMax Reitz result = fsync(fd); 17109f63b07eSMax Reitz if (result < 0) { 17119f63b07eSMax Reitz result = -errno; 17129f63b07eSMax Reitz error_setg_errno(errp, -result, 1713d0bc9e5dSMax Reitz "Could not flush file to disk"); 1714d0bc9e5dSMax Reitz goto out; 17159f63b07eSMax Reitz } 17169f63b07eSMax Reitz } 1717d0bc9e5dSMax Reitz goto out; 17189f63b07eSMax Reitz } 17199f63b07eSMax Reitz case PREALLOC_MODE_OFF: 17209f63b07eSMax Reitz if (ftruncate(fd, offset) != 0) { 17219f63b07eSMax Reitz result = -errno; 17229f63b07eSMax Reitz error_setg_errno(errp, -result, "Could not resize file"); 17239f63b07eSMax Reitz } 17249f63b07eSMax Reitz return result; 17259f63b07eSMax Reitz default: 17269f63b07eSMax Reitz result = -ENOTSUP; 17279f63b07eSMax Reitz error_setg(errp, "Unsupported preallocation mode: %s", 17289f63b07eSMax Reitz PreallocMode_lookup[prealloc]); 17299f63b07eSMax Reitz return result; 17309f63b07eSMax Reitz } 1731d0bc9e5dSMax Reitz 1732d0bc9e5dSMax Reitz out: 1733d0bc9e5dSMax Reitz if (result < 0) { 1734d0bc9e5dSMax Reitz if (ftruncate(fd, current_length) < 0) { 1735d0bc9e5dSMax Reitz error_report("Failed to restore old file length: %s", 1736d0bc9e5dSMax Reitz strerror(errno)); 1737d0bc9e5dSMax Reitz } 1738d0bc9e5dSMax Reitz } 1739d0bc9e5dSMax Reitz 1740d0bc9e5dSMax Reitz g_free(buf); 1741d0bc9e5dSMax Reitz return result; 17429f63b07eSMax Reitz } 17439f63b07eSMax Reitz 17448243ccb7SMax Reitz static int raw_truncate(BlockDriverState *bs, int64_t offset, 17458243ccb7SMax Reitz PreallocMode prealloc, Error **errp) 1746c1bb86cdSEric Blake { 1747c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 1748c1bb86cdSEric Blake struct stat st; 1749f59adb32SMax Reitz int ret; 1750c1bb86cdSEric Blake 1751c1bb86cdSEric Blake if (fstat(s->fd, &st)) { 1752f59adb32SMax Reitz ret = -errno; 1753f59adb32SMax Reitz error_setg_errno(errp, -ret, "Failed to fstat() the file"); 1754f59adb32SMax Reitz return ret; 1755c1bb86cdSEric Blake } 1756c1bb86cdSEric Blake 1757c1bb86cdSEric Blake if (S_ISREG(st.st_mode)) { 175835d72602SMax Reitz return raw_regular_truncate(s->fd, offset, prealloc, errp); 1759c1bb86cdSEric Blake } 176035d72602SMax Reitz 176135d72602SMax Reitz if (prealloc != PREALLOC_MODE_OFF) { 176235d72602SMax Reitz error_setg(errp, "Preallocation mode '%s' unsupported for this " 176335d72602SMax Reitz "non-regular file", PreallocMode_lookup[prealloc]); 176435d72602SMax Reitz return -ENOTSUP; 176535d72602SMax Reitz } 176635d72602SMax Reitz 176735d72602SMax Reitz if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) { 1768c1bb86cdSEric Blake if (offset > raw_getlength(bs)) { 1769f59adb32SMax Reitz error_setg(errp, "Cannot grow device files"); 1770c1bb86cdSEric Blake return -EINVAL; 1771c1bb86cdSEric Blake } 1772c1bb86cdSEric Blake } else { 1773f59adb32SMax Reitz error_setg(errp, "Resizing this file is not supported"); 1774c1bb86cdSEric Blake return -ENOTSUP; 1775c1bb86cdSEric Blake } 1776c1bb86cdSEric Blake 1777c1bb86cdSEric Blake return 0; 1778c1bb86cdSEric Blake } 1779c1bb86cdSEric Blake 1780c1bb86cdSEric Blake #ifdef __OpenBSD__ 1781c1bb86cdSEric Blake static int64_t raw_getlength(BlockDriverState *bs) 1782c1bb86cdSEric Blake { 1783c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 1784c1bb86cdSEric Blake int fd = s->fd; 1785c1bb86cdSEric Blake struct stat st; 1786c1bb86cdSEric Blake 1787c1bb86cdSEric Blake if (fstat(fd, &st)) 1788c1bb86cdSEric Blake return -errno; 1789c1bb86cdSEric Blake if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) { 1790c1bb86cdSEric Blake struct disklabel dl; 1791c1bb86cdSEric Blake 1792c1bb86cdSEric Blake if (ioctl(fd, DIOCGDINFO, &dl)) 1793c1bb86cdSEric Blake return -errno; 1794c1bb86cdSEric Blake return (uint64_t)dl.d_secsize * 1795c1bb86cdSEric Blake dl.d_partitions[DISKPART(st.st_rdev)].p_size; 1796c1bb86cdSEric Blake } else 1797c1bb86cdSEric Blake return st.st_size; 1798c1bb86cdSEric Blake } 1799c1bb86cdSEric Blake #elif defined(__NetBSD__) 1800c1bb86cdSEric Blake static int64_t raw_getlength(BlockDriverState *bs) 1801c1bb86cdSEric Blake { 1802c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 1803c1bb86cdSEric Blake int fd = s->fd; 1804c1bb86cdSEric Blake struct stat st; 1805c1bb86cdSEric Blake 1806c1bb86cdSEric Blake if (fstat(fd, &st)) 1807c1bb86cdSEric Blake return -errno; 1808c1bb86cdSEric Blake if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) { 1809c1bb86cdSEric Blake struct dkwedge_info dkw; 1810c1bb86cdSEric Blake 1811c1bb86cdSEric Blake if (ioctl(fd, DIOCGWEDGEINFO, &dkw) != -1) { 1812c1bb86cdSEric Blake return dkw.dkw_size * 512; 1813c1bb86cdSEric Blake } else { 1814c1bb86cdSEric Blake struct disklabel dl; 1815c1bb86cdSEric Blake 1816c1bb86cdSEric Blake if (ioctl(fd, DIOCGDINFO, &dl)) 1817c1bb86cdSEric Blake return -errno; 1818c1bb86cdSEric Blake return (uint64_t)dl.d_secsize * 1819c1bb86cdSEric Blake dl.d_partitions[DISKPART(st.st_rdev)].p_size; 1820c1bb86cdSEric Blake } 1821c1bb86cdSEric Blake } else 1822c1bb86cdSEric Blake return st.st_size; 1823c1bb86cdSEric Blake } 1824c1bb86cdSEric Blake #elif defined(__sun__) 1825c1bb86cdSEric Blake static int64_t raw_getlength(BlockDriverState *bs) 1826c1bb86cdSEric Blake { 1827c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 1828c1bb86cdSEric Blake struct dk_minfo minfo; 1829c1bb86cdSEric Blake int ret; 1830c1bb86cdSEric Blake int64_t size; 1831c1bb86cdSEric Blake 1832c1bb86cdSEric Blake ret = fd_open(bs); 1833c1bb86cdSEric Blake if (ret < 0) { 1834c1bb86cdSEric Blake return ret; 1835c1bb86cdSEric Blake } 1836c1bb86cdSEric Blake 1837c1bb86cdSEric Blake /* 1838c1bb86cdSEric Blake * Use the DKIOCGMEDIAINFO ioctl to read the size. 1839c1bb86cdSEric Blake */ 1840c1bb86cdSEric Blake ret = ioctl(s->fd, DKIOCGMEDIAINFO, &minfo); 1841c1bb86cdSEric Blake if (ret != -1) { 1842c1bb86cdSEric Blake return minfo.dki_lbsize * minfo.dki_capacity; 1843c1bb86cdSEric Blake } 1844c1bb86cdSEric Blake 1845c1bb86cdSEric Blake /* 1846c1bb86cdSEric Blake * There are reports that lseek on some devices fails, but 1847c1bb86cdSEric Blake * irc discussion said that contingency on contingency was overkill. 1848c1bb86cdSEric Blake */ 1849c1bb86cdSEric Blake size = lseek(s->fd, 0, SEEK_END); 1850c1bb86cdSEric Blake if (size < 0) { 1851c1bb86cdSEric Blake return -errno; 1852c1bb86cdSEric Blake } 1853c1bb86cdSEric Blake return size; 1854c1bb86cdSEric Blake } 1855c1bb86cdSEric Blake #elif defined(CONFIG_BSD) 1856c1bb86cdSEric Blake static int64_t raw_getlength(BlockDriverState *bs) 1857c1bb86cdSEric Blake { 1858c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 1859c1bb86cdSEric Blake int fd = s->fd; 1860c1bb86cdSEric Blake int64_t size; 1861c1bb86cdSEric Blake struct stat sb; 1862c1bb86cdSEric Blake #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__) 1863c1bb86cdSEric Blake int reopened = 0; 1864c1bb86cdSEric Blake #endif 1865c1bb86cdSEric Blake int ret; 1866c1bb86cdSEric Blake 1867c1bb86cdSEric Blake ret = fd_open(bs); 1868c1bb86cdSEric Blake if (ret < 0) 1869c1bb86cdSEric Blake return ret; 1870c1bb86cdSEric Blake 1871c1bb86cdSEric Blake #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__) 1872c1bb86cdSEric Blake again: 1873c1bb86cdSEric Blake #endif 1874c1bb86cdSEric Blake if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) { 1875c1bb86cdSEric Blake #ifdef DIOCGMEDIASIZE 1876c1bb86cdSEric Blake if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size)) 1877c1bb86cdSEric Blake #elif defined(DIOCGPART) 1878c1bb86cdSEric Blake { 1879c1bb86cdSEric Blake struct partinfo pi; 1880c1bb86cdSEric Blake if (ioctl(fd, DIOCGPART, &pi) == 0) 1881c1bb86cdSEric Blake size = pi.media_size; 1882c1bb86cdSEric Blake else 1883c1bb86cdSEric Blake size = 0; 1884c1bb86cdSEric Blake } 1885c1bb86cdSEric Blake if (size == 0) 1886c1bb86cdSEric Blake #endif 1887c1bb86cdSEric Blake #if defined(__APPLE__) && defined(__MACH__) 1888c1bb86cdSEric Blake { 1889c1bb86cdSEric Blake uint64_t sectors = 0; 1890c1bb86cdSEric Blake uint32_t sector_size = 0; 1891c1bb86cdSEric Blake 1892c1bb86cdSEric Blake if (ioctl(fd, DKIOCGETBLOCKCOUNT, §ors) == 0 1893c1bb86cdSEric Blake && ioctl(fd, DKIOCGETBLOCKSIZE, §or_size) == 0) { 1894c1bb86cdSEric Blake size = sectors * sector_size; 1895c1bb86cdSEric Blake } else { 1896c1bb86cdSEric Blake size = lseek(fd, 0LL, SEEK_END); 1897c1bb86cdSEric Blake if (size < 0) { 1898c1bb86cdSEric Blake return -errno; 1899c1bb86cdSEric Blake } 1900c1bb86cdSEric Blake } 1901c1bb86cdSEric Blake } 1902c1bb86cdSEric Blake #else 1903c1bb86cdSEric Blake size = lseek(fd, 0LL, SEEK_END); 1904c1bb86cdSEric Blake if (size < 0) { 1905c1bb86cdSEric Blake return -errno; 1906c1bb86cdSEric Blake } 1907c1bb86cdSEric Blake #endif 1908c1bb86cdSEric Blake #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) 1909c1bb86cdSEric Blake switch(s->type) { 1910c1bb86cdSEric Blake case FTYPE_CD: 1911c1bb86cdSEric Blake /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */ 1912c1bb86cdSEric Blake if (size == 2048LL * (unsigned)-1) 1913c1bb86cdSEric Blake size = 0; 1914c1bb86cdSEric Blake /* XXX no disc? maybe we need to reopen... */ 1915c1bb86cdSEric Blake if (size <= 0 && !reopened && cdrom_reopen(bs) >= 0) { 1916c1bb86cdSEric Blake reopened = 1; 1917c1bb86cdSEric Blake goto again; 1918c1bb86cdSEric Blake } 1919c1bb86cdSEric Blake } 1920c1bb86cdSEric Blake #endif 1921c1bb86cdSEric Blake } else { 1922c1bb86cdSEric Blake size = lseek(fd, 0, SEEK_END); 1923c1bb86cdSEric Blake if (size < 0) { 1924c1bb86cdSEric Blake return -errno; 1925c1bb86cdSEric Blake } 1926c1bb86cdSEric Blake } 1927c1bb86cdSEric Blake return size; 1928c1bb86cdSEric Blake } 1929c1bb86cdSEric Blake #else 1930c1bb86cdSEric Blake static int64_t raw_getlength(BlockDriverState *bs) 1931c1bb86cdSEric Blake { 1932c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 1933c1bb86cdSEric Blake int ret; 1934c1bb86cdSEric Blake int64_t size; 1935c1bb86cdSEric Blake 1936c1bb86cdSEric Blake ret = fd_open(bs); 1937c1bb86cdSEric Blake if (ret < 0) { 1938c1bb86cdSEric Blake return ret; 1939c1bb86cdSEric Blake } 1940c1bb86cdSEric Blake 1941c1bb86cdSEric Blake size = lseek(s->fd, 0, SEEK_END); 1942c1bb86cdSEric Blake if (size < 0) { 1943c1bb86cdSEric Blake return -errno; 1944c1bb86cdSEric Blake } 1945c1bb86cdSEric Blake return size; 1946c1bb86cdSEric Blake } 1947c1bb86cdSEric Blake #endif 1948c1bb86cdSEric Blake 1949c1bb86cdSEric Blake static int64_t raw_get_allocated_file_size(BlockDriverState *bs) 1950c1bb86cdSEric Blake { 1951c1bb86cdSEric Blake struct stat st; 1952c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 1953c1bb86cdSEric Blake 1954c1bb86cdSEric Blake if (fstat(s->fd, &st) < 0) { 1955c1bb86cdSEric Blake return -errno; 1956c1bb86cdSEric Blake } 1957c1bb86cdSEric Blake return (int64_t)st.st_blocks * 512; 1958c1bb86cdSEric Blake } 1959c1bb86cdSEric Blake 1960c1bb86cdSEric Blake static int raw_create(const char *filename, QemuOpts *opts, Error **errp) 1961c1bb86cdSEric Blake { 1962c1bb86cdSEric Blake int fd; 1963c1bb86cdSEric Blake int result = 0; 1964c1bb86cdSEric Blake int64_t total_size = 0; 1965c1bb86cdSEric Blake bool nocow = false; 1966c1bb86cdSEric Blake PreallocMode prealloc; 1967c1bb86cdSEric Blake char *buf = NULL; 1968c1bb86cdSEric Blake Error *local_err = NULL; 1969c1bb86cdSEric Blake 1970c1bb86cdSEric Blake strstart(filename, "file:", &filename); 1971c1bb86cdSEric Blake 1972c1bb86cdSEric Blake /* Read out options */ 1973c1bb86cdSEric Blake total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), 1974c1bb86cdSEric Blake BDRV_SECTOR_SIZE); 1975c1bb86cdSEric Blake nocow = qemu_opt_get_bool(opts, BLOCK_OPT_NOCOW, false); 1976c1bb86cdSEric Blake buf = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC); 1977c1bb86cdSEric Blake prealloc = qapi_enum_parse(PreallocMode_lookup, buf, 1978*06c60b6cSMarkus Armbruster PREALLOC_MODE_OFF, &local_err); 1979c1bb86cdSEric Blake g_free(buf); 1980c1bb86cdSEric Blake if (local_err) { 1981c1bb86cdSEric Blake error_propagate(errp, local_err); 1982c1bb86cdSEric Blake result = -EINVAL; 1983c1bb86cdSEric Blake goto out; 1984c1bb86cdSEric Blake } 1985c1bb86cdSEric Blake 1986c1bb86cdSEric Blake fd = qemu_open(filename, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 1987c1bb86cdSEric Blake 0644); 1988c1bb86cdSEric Blake if (fd < 0) { 1989c1bb86cdSEric Blake result = -errno; 1990c1bb86cdSEric Blake error_setg_errno(errp, -result, "Could not create file"); 1991c1bb86cdSEric Blake goto out; 1992c1bb86cdSEric Blake } 1993c1bb86cdSEric Blake 1994c1bb86cdSEric Blake if (nocow) { 1995c1bb86cdSEric Blake #ifdef __linux__ 1996c1bb86cdSEric Blake /* Set NOCOW flag to solve performance issue on fs like btrfs. 1997c1bb86cdSEric Blake * This is an optimisation. The FS_IOC_SETFLAGS ioctl return value 1998c1bb86cdSEric Blake * will be ignored since any failure of this operation should not 1999c1bb86cdSEric Blake * block the left work. 2000c1bb86cdSEric Blake */ 2001c1bb86cdSEric Blake int attr; 2002c1bb86cdSEric Blake if (ioctl(fd, FS_IOC_GETFLAGS, &attr) == 0) { 2003c1bb86cdSEric Blake attr |= FS_NOCOW_FL; 2004c1bb86cdSEric Blake ioctl(fd, FS_IOC_SETFLAGS, &attr); 2005c1bb86cdSEric Blake } 2006c1bb86cdSEric Blake #endif 2007c1bb86cdSEric Blake } 2008c1bb86cdSEric Blake 20099f63b07eSMax Reitz result = raw_regular_truncate(fd, total_size, prealloc, errp); 20109f63b07eSMax Reitz if (result < 0) { 20115a1dad9dSNir Soffer goto out_close; 20125a1dad9dSNir Soffer } 20135a1dad9dSNir Soffer 20145a1dad9dSNir Soffer out_close: 2015c1bb86cdSEric Blake if (qemu_close(fd) != 0 && result == 0) { 2016c1bb86cdSEric Blake result = -errno; 2017c1bb86cdSEric Blake error_setg_errno(errp, -result, "Could not close the new file"); 2018c1bb86cdSEric Blake } 2019c1bb86cdSEric Blake out: 2020c1bb86cdSEric Blake return result; 2021c1bb86cdSEric Blake } 2022c1bb86cdSEric Blake 2023c1bb86cdSEric Blake /* 2024c1bb86cdSEric Blake * Find allocation range in @bs around offset @start. 2025c1bb86cdSEric Blake * May change underlying file descriptor's file offset. 2026c1bb86cdSEric Blake * If @start is not in a hole, store @start in @data, and the 2027c1bb86cdSEric Blake * beginning of the next hole in @hole, and return 0. 2028c1bb86cdSEric Blake * If @start is in a non-trailing hole, store @start in @hole and the 2029c1bb86cdSEric Blake * beginning of the next non-hole in @data, and return 0. 2030c1bb86cdSEric Blake * If @start is in a trailing hole or beyond EOF, return -ENXIO. 2031c1bb86cdSEric Blake * If we can't find out, return a negative errno other than -ENXIO. 2032c1bb86cdSEric Blake */ 2033c1bb86cdSEric Blake static int find_allocation(BlockDriverState *bs, off_t start, 2034c1bb86cdSEric Blake off_t *data, off_t *hole) 2035c1bb86cdSEric Blake { 2036c1bb86cdSEric Blake #if defined SEEK_HOLE && defined SEEK_DATA 2037c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2038c1bb86cdSEric Blake off_t offs; 2039c1bb86cdSEric Blake 2040c1bb86cdSEric Blake /* 2041c1bb86cdSEric Blake * SEEK_DATA cases: 2042c1bb86cdSEric Blake * D1. offs == start: start is in data 2043c1bb86cdSEric Blake * D2. offs > start: start is in a hole, next data at offs 2044c1bb86cdSEric Blake * D3. offs < 0, errno = ENXIO: either start is in a trailing hole 2045c1bb86cdSEric Blake * or start is beyond EOF 2046c1bb86cdSEric Blake * If the latter happens, the file has been truncated behind 2047c1bb86cdSEric Blake * our back since we opened it. All bets are off then. 2048c1bb86cdSEric Blake * Treating like a trailing hole is simplest. 2049c1bb86cdSEric Blake * D4. offs < 0, errno != ENXIO: we learned nothing 2050c1bb86cdSEric Blake */ 2051c1bb86cdSEric Blake offs = lseek(s->fd, start, SEEK_DATA); 2052c1bb86cdSEric Blake if (offs < 0) { 2053c1bb86cdSEric Blake return -errno; /* D3 or D4 */ 2054c1bb86cdSEric Blake } 2055c1bb86cdSEric Blake assert(offs >= start); 2056c1bb86cdSEric Blake 2057c1bb86cdSEric Blake if (offs > start) { 2058c1bb86cdSEric Blake /* D2: in hole, next data at offs */ 2059c1bb86cdSEric Blake *hole = start; 2060c1bb86cdSEric Blake *data = offs; 2061c1bb86cdSEric Blake return 0; 2062c1bb86cdSEric Blake } 2063c1bb86cdSEric Blake 2064c1bb86cdSEric Blake /* D1: in data, end not yet known */ 2065c1bb86cdSEric Blake 2066c1bb86cdSEric Blake /* 2067c1bb86cdSEric Blake * SEEK_HOLE cases: 2068c1bb86cdSEric Blake * H1. offs == start: start is in a hole 2069c1bb86cdSEric Blake * If this happens here, a hole has been dug behind our back 2070c1bb86cdSEric Blake * since the previous lseek(). 2071c1bb86cdSEric Blake * H2. offs > start: either start is in data, next hole at offs, 2072c1bb86cdSEric Blake * or start is in trailing hole, EOF at offs 2073c1bb86cdSEric Blake * Linux treats trailing holes like any other hole: offs == 2074c1bb86cdSEric Blake * start. Solaris seeks to EOF instead: offs > start (blech). 2075c1bb86cdSEric Blake * If that happens here, a hole has been dug behind our back 2076c1bb86cdSEric Blake * since the previous lseek(). 2077c1bb86cdSEric Blake * H3. offs < 0, errno = ENXIO: start is beyond EOF 2078c1bb86cdSEric Blake * If this happens, the file has been truncated behind our 2079c1bb86cdSEric Blake * back since we opened it. Treat it like a trailing hole. 2080c1bb86cdSEric Blake * H4. offs < 0, errno != ENXIO: we learned nothing 2081c1bb86cdSEric Blake * Pretend we know nothing at all, i.e. "forget" about D1. 2082c1bb86cdSEric Blake */ 2083c1bb86cdSEric Blake offs = lseek(s->fd, start, SEEK_HOLE); 2084c1bb86cdSEric Blake if (offs < 0) { 2085c1bb86cdSEric Blake return -errno; /* D1 and (H3 or H4) */ 2086c1bb86cdSEric Blake } 2087c1bb86cdSEric Blake assert(offs >= start); 2088c1bb86cdSEric Blake 2089c1bb86cdSEric Blake if (offs > start) { 2090c1bb86cdSEric Blake /* 2091c1bb86cdSEric Blake * D1 and H2: either in data, next hole at offs, or it was in 2092c1bb86cdSEric Blake * data but is now in a trailing hole. In the latter case, 2093c1bb86cdSEric Blake * all bets are off. Treating it as if it there was data all 2094c1bb86cdSEric Blake * the way to EOF is safe, so simply do that. 2095c1bb86cdSEric Blake */ 2096c1bb86cdSEric Blake *data = start; 2097c1bb86cdSEric Blake *hole = offs; 2098c1bb86cdSEric Blake return 0; 2099c1bb86cdSEric Blake } 2100c1bb86cdSEric Blake 2101c1bb86cdSEric Blake /* D1 and H1 */ 2102c1bb86cdSEric Blake return -EBUSY; 2103c1bb86cdSEric Blake #else 2104c1bb86cdSEric Blake return -ENOTSUP; 2105c1bb86cdSEric Blake #endif 2106c1bb86cdSEric Blake } 2107c1bb86cdSEric Blake 2108c1bb86cdSEric Blake /* 2109c1bb86cdSEric Blake * Returns the allocation status of the specified sectors. 2110c1bb86cdSEric Blake * 2111c1bb86cdSEric Blake * If 'sector_num' is beyond the end of the disk image the return value is 0 2112c1bb86cdSEric Blake * and 'pnum' is set to 0. 2113c1bb86cdSEric Blake * 2114c1bb86cdSEric Blake * 'pnum' is set to the number of sectors (including and immediately following 2115c1bb86cdSEric Blake * the specified sector) that are known to be in the same 2116c1bb86cdSEric Blake * allocated/unallocated state. 2117c1bb86cdSEric Blake * 2118c1bb86cdSEric Blake * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes 2119c1bb86cdSEric Blake * beyond the end of the disk image it will be clamped. 2120c1bb86cdSEric Blake */ 2121c1bb86cdSEric Blake static int64_t coroutine_fn raw_co_get_block_status(BlockDriverState *bs, 2122c1bb86cdSEric Blake int64_t sector_num, 2123c1bb86cdSEric Blake int nb_sectors, int *pnum, 2124c1bb86cdSEric Blake BlockDriverState **file) 2125c1bb86cdSEric Blake { 2126c1bb86cdSEric Blake off_t start, data = 0, hole = 0; 2127c1bb86cdSEric Blake int64_t total_size; 2128c1bb86cdSEric Blake int ret; 2129c1bb86cdSEric Blake 2130c1bb86cdSEric Blake ret = fd_open(bs); 2131c1bb86cdSEric Blake if (ret < 0) { 2132c1bb86cdSEric Blake return ret; 2133c1bb86cdSEric Blake } 2134c1bb86cdSEric Blake 2135c1bb86cdSEric Blake start = sector_num * BDRV_SECTOR_SIZE; 2136c1bb86cdSEric Blake total_size = bdrv_getlength(bs); 2137c1bb86cdSEric Blake if (total_size < 0) { 2138c1bb86cdSEric Blake return total_size; 2139c1bb86cdSEric Blake } else if (start >= total_size) { 2140c1bb86cdSEric Blake *pnum = 0; 2141c1bb86cdSEric Blake return 0; 2142c1bb86cdSEric Blake } else if (start + nb_sectors * BDRV_SECTOR_SIZE > total_size) { 2143c1bb86cdSEric Blake nb_sectors = DIV_ROUND_UP(total_size - start, BDRV_SECTOR_SIZE); 2144c1bb86cdSEric Blake } 2145c1bb86cdSEric Blake 2146c1bb86cdSEric Blake ret = find_allocation(bs, start, &data, &hole); 2147c1bb86cdSEric Blake if (ret == -ENXIO) { 2148c1bb86cdSEric Blake /* Trailing hole */ 2149c1bb86cdSEric Blake *pnum = nb_sectors; 2150c1bb86cdSEric Blake ret = BDRV_BLOCK_ZERO; 2151c1bb86cdSEric Blake } else if (ret < 0) { 2152c1bb86cdSEric Blake /* No info available, so pretend there are no holes */ 2153c1bb86cdSEric Blake *pnum = nb_sectors; 2154c1bb86cdSEric Blake ret = BDRV_BLOCK_DATA; 2155c1bb86cdSEric Blake } else if (data == start) { 2156c1bb86cdSEric Blake /* On a data extent, compute sectors to the end of the extent, 2157c1bb86cdSEric Blake * possibly including a partial sector at EOF. */ 2158c1bb86cdSEric Blake *pnum = MIN(nb_sectors, DIV_ROUND_UP(hole - start, BDRV_SECTOR_SIZE)); 2159c1bb86cdSEric Blake ret = BDRV_BLOCK_DATA; 2160c1bb86cdSEric Blake } else { 2161c1bb86cdSEric Blake /* On a hole, compute sectors to the beginning of the next extent. */ 2162c1bb86cdSEric Blake assert(hole == start); 2163c1bb86cdSEric Blake *pnum = MIN(nb_sectors, (data - start) / BDRV_SECTOR_SIZE); 2164c1bb86cdSEric Blake ret = BDRV_BLOCK_ZERO; 2165c1bb86cdSEric Blake } 2166c1bb86cdSEric Blake *file = bs; 2167c1bb86cdSEric Blake return ret | BDRV_BLOCK_OFFSET_VALID | start; 2168c1bb86cdSEric Blake } 2169c1bb86cdSEric Blake 2170c1bb86cdSEric Blake static coroutine_fn BlockAIOCB *raw_aio_pdiscard(BlockDriverState *bs, 2171f5a5ca79SManos Pitsidianakis int64_t offset, int bytes, 2172c1bb86cdSEric Blake BlockCompletionFunc *cb, void *opaque) 2173c1bb86cdSEric Blake { 2174c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2175c1bb86cdSEric Blake 2176f5a5ca79SManos Pitsidianakis return paio_submit(bs, s->fd, offset, NULL, bytes, 2177c1bb86cdSEric Blake cb, opaque, QEMU_AIO_DISCARD); 2178c1bb86cdSEric Blake } 2179c1bb86cdSEric Blake 2180c1bb86cdSEric Blake static int coroutine_fn raw_co_pwrite_zeroes( 2181c1bb86cdSEric Blake BlockDriverState *bs, int64_t offset, 2182f5a5ca79SManos Pitsidianakis int bytes, BdrvRequestFlags flags) 2183c1bb86cdSEric Blake { 2184c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2185c1bb86cdSEric Blake 2186c1bb86cdSEric Blake if (!(flags & BDRV_REQ_MAY_UNMAP)) { 2187f5a5ca79SManos Pitsidianakis return paio_submit_co(bs, s->fd, offset, NULL, bytes, 2188c1bb86cdSEric Blake QEMU_AIO_WRITE_ZEROES); 2189c1bb86cdSEric Blake } else if (s->discard_zeroes) { 2190f5a5ca79SManos Pitsidianakis return paio_submit_co(bs, s->fd, offset, NULL, bytes, 2191c1bb86cdSEric Blake QEMU_AIO_DISCARD); 2192c1bb86cdSEric Blake } 2193c1bb86cdSEric Blake return -ENOTSUP; 2194c1bb86cdSEric Blake } 2195c1bb86cdSEric Blake 2196c1bb86cdSEric Blake static int raw_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 2197c1bb86cdSEric Blake { 2198c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2199c1bb86cdSEric Blake 2200c1bb86cdSEric Blake bdi->unallocated_blocks_are_zero = s->discard_zeroes; 2201c1bb86cdSEric Blake bdi->can_write_zeroes_with_unmap = s->discard_zeroes; 2202c1bb86cdSEric Blake return 0; 2203c1bb86cdSEric Blake } 2204c1bb86cdSEric Blake 2205c1bb86cdSEric Blake static QemuOptsList raw_create_opts = { 2206c1bb86cdSEric Blake .name = "raw-create-opts", 2207c1bb86cdSEric Blake .head = QTAILQ_HEAD_INITIALIZER(raw_create_opts.head), 2208c1bb86cdSEric Blake .desc = { 2209c1bb86cdSEric Blake { 2210c1bb86cdSEric Blake .name = BLOCK_OPT_SIZE, 2211c1bb86cdSEric Blake .type = QEMU_OPT_SIZE, 2212c1bb86cdSEric Blake .help = "Virtual disk size" 2213c1bb86cdSEric Blake }, 2214c1bb86cdSEric Blake { 2215c1bb86cdSEric Blake .name = BLOCK_OPT_NOCOW, 2216c1bb86cdSEric Blake .type = QEMU_OPT_BOOL, 2217c1bb86cdSEric Blake .help = "Turn off copy-on-write (valid only on btrfs)" 2218c1bb86cdSEric Blake }, 2219c1bb86cdSEric Blake { 2220c1bb86cdSEric Blake .name = BLOCK_OPT_PREALLOC, 2221c1bb86cdSEric Blake .type = QEMU_OPT_STRING, 2222c1bb86cdSEric Blake .help = "Preallocation mode (allowed values: off, falloc, full)" 2223c1bb86cdSEric Blake }, 2224c1bb86cdSEric Blake { /* end of list */ } 2225c1bb86cdSEric Blake } 2226c1bb86cdSEric Blake }; 2227c1bb86cdSEric Blake 2228244a5668SFam Zheng static int raw_check_perm(BlockDriverState *bs, uint64_t perm, uint64_t shared, 2229244a5668SFam Zheng Error **errp) 2230244a5668SFam Zheng { 2231244a5668SFam Zheng return raw_handle_perm_lock(bs, RAW_PL_PREPARE, perm, shared, errp); 2232244a5668SFam Zheng } 2233244a5668SFam Zheng 2234244a5668SFam Zheng static void raw_set_perm(BlockDriverState *bs, uint64_t perm, uint64_t shared) 2235244a5668SFam Zheng { 2236244a5668SFam Zheng BDRVRawState *s = bs->opaque; 2237244a5668SFam Zheng raw_handle_perm_lock(bs, RAW_PL_COMMIT, perm, shared, NULL); 2238244a5668SFam Zheng s->perm = perm; 2239244a5668SFam Zheng s->shared_perm = shared; 2240244a5668SFam Zheng } 2241244a5668SFam Zheng 2242244a5668SFam Zheng static void raw_abort_perm_update(BlockDriverState *bs) 2243244a5668SFam Zheng { 2244244a5668SFam Zheng raw_handle_perm_lock(bs, RAW_PL_ABORT, 0, 0, NULL); 2245244a5668SFam Zheng } 2246244a5668SFam Zheng 2247c1bb86cdSEric Blake BlockDriver bdrv_file = { 2248c1bb86cdSEric Blake .format_name = "file", 2249c1bb86cdSEric Blake .protocol_name = "file", 2250c1bb86cdSEric Blake .instance_size = sizeof(BDRVRawState), 2251c1bb86cdSEric Blake .bdrv_needs_filename = true, 2252c1bb86cdSEric Blake .bdrv_probe = NULL, /* no probe for protocols */ 2253c1bb86cdSEric Blake .bdrv_parse_filename = raw_parse_filename, 2254c1bb86cdSEric Blake .bdrv_file_open = raw_open, 2255c1bb86cdSEric Blake .bdrv_reopen_prepare = raw_reopen_prepare, 2256c1bb86cdSEric Blake .bdrv_reopen_commit = raw_reopen_commit, 2257c1bb86cdSEric Blake .bdrv_reopen_abort = raw_reopen_abort, 2258c1bb86cdSEric Blake .bdrv_close = raw_close, 2259c1bb86cdSEric Blake .bdrv_create = raw_create, 2260c1bb86cdSEric Blake .bdrv_has_zero_init = bdrv_has_zero_init_1, 2261c1bb86cdSEric Blake .bdrv_co_get_block_status = raw_co_get_block_status, 2262c1bb86cdSEric Blake .bdrv_co_pwrite_zeroes = raw_co_pwrite_zeroes, 2263c1bb86cdSEric Blake 2264c1bb86cdSEric Blake .bdrv_co_preadv = raw_co_preadv, 2265c1bb86cdSEric Blake .bdrv_co_pwritev = raw_co_pwritev, 2266c1bb86cdSEric Blake .bdrv_aio_flush = raw_aio_flush, 2267c1bb86cdSEric Blake .bdrv_aio_pdiscard = raw_aio_pdiscard, 2268c1bb86cdSEric Blake .bdrv_refresh_limits = raw_refresh_limits, 2269c1bb86cdSEric Blake .bdrv_io_plug = raw_aio_plug, 2270c1bb86cdSEric Blake .bdrv_io_unplug = raw_aio_unplug, 2271c1bb86cdSEric Blake 2272c1bb86cdSEric Blake .bdrv_truncate = raw_truncate, 2273c1bb86cdSEric Blake .bdrv_getlength = raw_getlength, 2274c1bb86cdSEric Blake .bdrv_get_info = raw_get_info, 2275c1bb86cdSEric Blake .bdrv_get_allocated_file_size 2276c1bb86cdSEric Blake = raw_get_allocated_file_size, 2277244a5668SFam Zheng .bdrv_check_perm = raw_check_perm, 2278244a5668SFam Zheng .bdrv_set_perm = raw_set_perm, 2279244a5668SFam Zheng .bdrv_abort_perm_update = raw_abort_perm_update, 2280c1bb86cdSEric Blake .create_opts = &raw_create_opts, 2281c1bb86cdSEric Blake }; 2282c1bb86cdSEric Blake 2283c1bb86cdSEric Blake /***********************************************/ 2284c1bb86cdSEric Blake /* host device */ 2285c1bb86cdSEric Blake 2286c1bb86cdSEric Blake #if defined(__APPLE__) && defined(__MACH__) 2287c1bb86cdSEric Blake static kern_return_t GetBSDPath(io_iterator_t mediaIterator, char *bsdPath, 2288c1bb86cdSEric Blake CFIndex maxPathSize, int flags); 2289c1bb86cdSEric Blake static char *FindEjectableOpticalMedia(io_iterator_t *mediaIterator) 2290c1bb86cdSEric Blake { 2291c1bb86cdSEric Blake kern_return_t kernResult = KERN_FAILURE; 2292c1bb86cdSEric Blake mach_port_t masterPort; 2293c1bb86cdSEric Blake CFMutableDictionaryRef classesToMatch; 2294c1bb86cdSEric Blake const char *matching_array[] = {kIODVDMediaClass, kIOCDMediaClass}; 2295c1bb86cdSEric Blake char *mediaType = NULL; 2296c1bb86cdSEric Blake 2297c1bb86cdSEric Blake kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort ); 2298c1bb86cdSEric Blake if ( KERN_SUCCESS != kernResult ) { 2299c1bb86cdSEric Blake printf( "IOMasterPort returned %d\n", kernResult ); 2300c1bb86cdSEric Blake } 2301c1bb86cdSEric Blake 2302c1bb86cdSEric Blake int index; 2303c1bb86cdSEric Blake for (index = 0; index < ARRAY_SIZE(matching_array); index++) { 2304c1bb86cdSEric Blake classesToMatch = IOServiceMatching(matching_array[index]); 2305c1bb86cdSEric Blake if (classesToMatch == NULL) { 2306c1bb86cdSEric Blake error_report("IOServiceMatching returned NULL for %s", 2307c1bb86cdSEric Blake matching_array[index]); 2308c1bb86cdSEric Blake continue; 2309c1bb86cdSEric Blake } 2310c1bb86cdSEric Blake CFDictionarySetValue(classesToMatch, CFSTR(kIOMediaEjectableKey), 2311c1bb86cdSEric Blake kCFBooleanTrue); 2312c1bb86cdSEric Blake kernResult = IOServiceGetMatchingServices(masterPort, classesToMatch, 2313c1bb86cdSEric Blake mediaIterator); 2314c1bb86cdSEric Blake if (kernResult != KERN_SUCCESS) { 2315c1bb86cdSEric Blake error_report("Note: IOServiceGetMatchingServices returned %d", 2316c1bb86cdSEric Blake kernResult); 2317c1bb86cdSEric Blake continue; 2318c1bb86cdSEric Blake } 2319c1bb86cdSEric Blake 2320c1bb86cdSEric Blake /* If a match was found, leave the loop */ 2321c1bb86cdSEric Blake if (*mediaIterator != 0) { 2322c1bb86cdSEric Blake DPRINTF("Matching using %s\n", matching_array[index]); 2323c1bb86cdSEric Blake mediaType = g_strdup(matching_array[index]); 2324c1bb86cdSEric Blake break; 2325c1bb86cdSEric Blake } 2326c1bb86cdSEric Blake } 2327c1bb86cdSEric Blake return mediaType; 2328c1bb86cdSEric Blake } 2329c1bb86cdSEric Blake 2330c1bb86cdSEric Blake kern_return_t GetBSDPath(io_iterator_t mediaIterator, char *bsdPath, 2331c1bb86cdSEric Blake CFIndex maxPathSize, int flags) 2332c1bb86cdSEric Blake { 2333c1bb86cdSEric Blake io_object_t nextMedia; 2334c1bb86cdSEric Blake kern_return_t kernResult = KERN_FAILURE; 2335c1bb86cdSEric Blake *bsdPath = '\0'; 2336c1bb86cdSEric Blake nextMedia = IOIteratorNext( mediaIterator ); 2337c1bb86cdSEric Blake if ( nextMedia ) 2338c1bb86cdSEric Blake { 2339c1bb86cdSEric Blake CFTypeRef bsdPathAsCFString; 2340c1bb86cdSEric Blake bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 ); 2341c1bb86cdSEric Blake if ( bsdPathAsCFString ) { 2342c1bb86cdSEric Blake size_t devPathLength; 2343c1bb86cdSEric Blake strcpy( bsdPath, _PATH_DEV ); 2344c1bb86cdSEric Blake if (flags & BDRV_O_NOCACHE) { 2345c1bb86cdSEric Blake strcat(bsdPath, "r"); 2346c1bb86cdSEric Blake } 2347c1bb86cdSEric Blake devPathLength = strlen( bsdPath ); 2348c1bb86cdSEric Blake if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) { 2349c1bb86cdSEric Blake kernResult = KERN_SUCCESS; 2350c1bb86cdSEric Blake } 2351c1bb86cdSEric Blake CFRelease( bsdPathAsCFString ); 2352c1bb86cdSEric Blake } 2353c1bb86cdSEric Blake IOObjectRelease( nextMedia ); 2354c1bb86cdSEric Blake } 2355c1bb86cdSEric Blake 2356c1bb86cdSEric Blake return kernResult; 2357c1bb86cdSEric Blake } 2358c1bb86cdSEric Blake 2359c1bb86cdSEric Blake /* Sets up a real cdrom for use in QEMU */ 2360c1bb86cdSEric Blake static bool setup_cdrom(char *bsd_path, Error **errp) 2361c1bb86cdSEric Blake { 2362c1bb86cdSEric Blake int index, num_of_test_partitions = 2, fd; 2363c1bb86cdSEric Blake char test_partition[MAXPATHLEN]; 2364c1bb86cdSEric Blake bool partition_found = false; 2365c1bb86cdSEric Blake 2366c1bb86cdSEric Blake /* look for a working partition */ 2367c1bb86cdSEric Blake for (index = 0; index < num_of_test_partitions; index++) { 2368c1bb86cdSEric Blake snprintf(test_partition, sizeof(test_partition), "%ss%d", bsd_path, 2369c1bb86cdSEric Blake index); 2370c1bb86cdSEric Blake fd = qemu_open(test_partition, O_RDONLY | O_BINARY | O_LARGEFILE); 2371c1bb86cdSEric Blake if (fd >= 0) { 2372c1bb86cdSEric Blake partition_found = true; 2373c1bb86cdSEric Blake qemu_close(fd); 2374c1bb86cdSEric Blake break; 2375c1bb86cdSEric Blake } 2376c1bb86cdSEric Blake } 2377c1bb86cdSEric Blake 2378c1bb86cdSEric Blake /* if a working partition on the device was not found */ 2379c1bb86cdSEric Blake if (partition_found == false) { 2380c1bb86cdSEric Blake error_setg(errp, "Failed to find a working partition on disc"); 2381c1bb86cdSEric Blake } else { 2382c1bb86cdSEric Blake DPRINTF("Using %s as optical disc\n", test_partition); 2383c1bb86cdSEric Blake pstrcpy(bsd_path, MAXPATHLEN, test_partition); 2384c1bb86cdSEric Blake } 2385c1bb86cdSEric Blake return partition_found; 2386c1bb86cdSEric Blake } 2387c1bb86cdSEric Blake 2388c1bb86cdSEric Blake /* Prints directions on mounting and unmounting a device */ 2389c1bb86cdSEric Blake static void print_unmounting_directions(const char *file_name) 2390c1bb86cdSEric Blake { 2391c1bb86cdSEric Blake error_report("If device %s is mounted on the desktop, unmount" 2392c1bb86cdSEric Blake " it first before using it in QEMU", file_name); 2393c1bb86cdSEric Blake error_report("Command to unmount device: diskutil unmountDisk %s", 2394c1bb86cdSEric Blake file_name); 2395c1bb86cdSEric Blake error_report("Command to mount device: diskutil mountDisk %s", file_name); 2396c1bb86cdSEric Blake } 2397c1bb86cdSEric Blake 2398c1bb86cdSEric Blake #endif /* defined(__APPLE__) && defined(__MACH__) */ 2399c1bb86cdSEric Blake 2400c1bb86cdSEric Blake static int hdev_probe_device(const char *filename) 2401c1bb86cdSEric Blake { 2402c1bb86cdSEric Blake struct stat st; 2403c1bb86cdSEric Blake 2404c1bb86cdSEric Blake /* allow a dedicated CD-ROM driver to match with a higher priority */ 2405c1bb86cdSEric Blake if (strstart(filename, "/dev/cdrom", NULL)) 2406c1bb86cdSEric Blake return 50; 2407c1bb86cdSEric Blake 2408c1bb86cdSEric Blake if (stat(filename, &st) >= 0 && 2409c1bb86cdSEric Blake (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) { 2410c1bb86cdSEric Blake return 100; 2411c1bb86cdSEric Blake } 2412c1bb86cdSEric Blake 2413c1bb86cdSEric Blake return 0; 2414c1bb86cdSEric Blake } 2415c1bb86cdSEric Blake 2416c1bb86cdSEric Blake static int check_hdev_writable(BDRVRawState *s) 2417c1bb86cdSEric Blake { 2418c1bb86cdSEric Blake #if defined(BLKROGET) 2419c1bb86cdSEric Blake /* Linux block devices can be configured "read-only" using blockdev(8). 2420c1bb86cdSEric Blake * This is independent of device node permissions and therefore open(2) 2421c1bb86cdSEric Blake * with O_RDWR succeeds. Actual writes fail with EPERM. 2422c1bb86cdSEric Blake * 2423c1bb86cdSEric Blake * bdrv_open() is supposed to fail if the disk is read-only. Explicitly 2424c1bb86cdSEric Blake * check for read-only block devices so that Linux block devices behave 2425c1bb86cdSEric Blake * properly. 2426c1bb86cdSEric Blake */ 2427c1bb86cdSEric Blake struct stat st; 2428c1bb86cdSEric Blake int readonly = 0; 2429c1bb86cdSEric Blake 2430c1bb86cdSEric Blake if (fstat(s->fd, &st)) { 2431c1bb86cdSEric Blake return -errno; 2432c1bb86cdSEric Blake } 2433c1bb86cdSEric Blake 2434c1bb86cdSEric Blake if (!S_ISBLK(st.st_mode)) { 2435c1bb86cdSEric Blake return 0; 2436c1bb86cdSEric Blake } 2437c1bb86cdSEric Blake 2438c1bb86cdSEric Blake if (ioctl(s->fd, BLKROGET, &readonly) < 0) { 2439c1bb86cdSEric Blake return -errno; 2440c1bb86cdSEric Blake } 2441c1bb86cdSEric Blake 2442c1bb86cdSEric Blake if (readonly) { 2443c1bb86cdSEric Blake return -EACCES; 2444c1bb86cdSEric Blake } 2445c1bb86cdSEric Blake #endif /* defined(BLKROGET) */ 2446c1bb86cdSEric Blake return 0; 2447c1bb86cdSEric Blake } 2448c1bb86cdSEric Blake 2449c1bb86cdSEric Blake static void hdev_parse_filename(const char *filename, QDict *options, 2450c1bb86cdSEric Blake Error **errp) 2451c1bb86cdSEric Blake { 245203c320d8SMax Reitz bdrv_parse_filename_strip_prefix(filename, "host_device:", options); 2453c1bb86cdSEric Blake } 2454c1bb86cdSEric Blake 2455c1bb86cdSEric Blake static bool hdev_is_sg(BlockDriverState *bs) 2456c1bb86cdSEric Blake { 2457c1bb86cdSEric Blake 2458c1bb86cdSEric Blake #if defined(__linux__) 2459c1bb86cdSEric Blake 2460c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2461c1bb86cdSEric Blake struct stat st; 2462c1bb86cdSEric Blake struct sg_scsi_id scsiid; 2463c1bb86cdSEric Blake int sg_version; 2464c1bb86cdSEric Blake int ret; 2465c1bb86cdSEric Blake 2466c1bb86cdSEric Blake if (stat(bs->filename, &st) < 0 || !S_ISCHR(st.st_mode)) { 2467c1bb86cdSEric Blake return false; 2468c1bb86cdSEric Blake } 2469c1bb86cdSEric Blake 2470c1bb86cdSEric Blake ret = ioctl(s->fd, SG_GET_VERSION_NUM, &sg_version); 2471c1bb86cdSEric Blake if (ret < 0) { 2472c1bb86cdSEric Blake return false; 2473c1bb86cdSEric Blake } 2474c1bb86cdSEric Blake 2475c1bb86cdSEric Blake ret = ioctl(s->fd, SG_GET_SCSI_ID, &scsiid); 2476c1bb86cdSEric Blake if (ret >= 0) { 2477c1bb86cdSEric Blake DPRINTF("SG device found: type=%d, version=%d\n", 2478c1bb86cdSEric Blake scsiid.scsi_type, sg_version); 2479c1bb86cdSEric Blake return true; 2480c1bb86cdSEric Blake } 2481c1bb86cdSEric Blake 2482c1bb86cdSEric Blake #endif 2483c1bb86cdSEric Blake 2484c1bb86cdSEric Blake return false; 2485c1bb86cdSEric Blake } 2486c1bb86cdSEric Blake 2487c1bb86cdSEric Blake static int hdev_open(BlockDriverState *bs, QDict *options, int flags, 2488c1bb86cdSEric Blake Error **errp) 2489c1bb86cdSEric Blake { 2490c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2491c1bb86cdSEric Blake Error *local_err = NULL; 2492c1bb86cdSEric Blake int ret; 2493c1bb86cdSEric Blake 2494c1bb86cdSEric Blake #if defined(__APPLE__) && defined(__MACH__) 2495129c7d1cSMarkus Armbruster /* 2496129c7d1cSMarkus Armbruster * Caution: while qdict_get_str() is fine, getting non-string types 2497129c7d1cSMarkus Armbruster * would require more care. When @options come from -blockdev or 2498129c7d1cSMarkus Armbruster * blockdev_add, its members are typed according to the QAPI 2499129c7d1cSMarkus Armbruster * schema, but when they come from -drive, they're all QString. 2500129c7d1cSMarkus Armbruster */ 2501c1bb86cdSEric Blake const char *filename = qdict_get_str(options, "filename"); 2502c1bb86cdSEric Blake char bsd_path[MAXPATHLEN] = ""; 2503c1bb86cdSEric Blake bool error_occurred = false; 2504c1bb86cdSEric Blake 2505c1bb86cdSEric Blake /* If using a real cdrom */ 2506c1bb86cdSEric Blake if (strcmp(filename, "/dev/cdrom") == 0) { 2507c1bb86cdSEric Blake char *mediaType = NULL; 2508c1bb86cdSEric Blake kern_return_t ret_val; 2509c1bb86cdSEric Blake io_iterator_t mediaIterator = 0; 2510c1bb86cdSEric Blake 2511c1bb86cdSEric Blake mediaType = FindEjectableOpticalMedia(&mediaIterator); 2512c1bb86cdSEric Blake if (mediaType == NULL) { 2513c1bb86cdSEric Blake error_setg(errp, "Please make sure your CD/DVD is in the optical" 2514c1bb86cdSEric Blake " drive"); 2515c1bb86cdSEric Blake error_occurred = true; 2516c1bb86cdSEric Blake goto hdev_open_Mac_error; 2517c1bb86cdSEric Blake } 2518c1bb86cdSEric Blake 2519c1bb86cdSEric Blake ret_val = GetBSDPath(mediaIterator, bsd_path, sizeof(bsd_path), flags); 2520c1bb86cdSEric Blake if (ret_val != KERN_SUCCESS) { 2521c1bb86cdSEric Blake error_setg(errp, "Could not get BSD path for optical drive"); 2522c1bb86cdSEric Blake error_occurred = true; 2523c1bb86cdSEric Blake goto hdev_open_Mac_error; 2524c1bb86cdSEric Blake } 2525c1bb86cdSEric Blake 2526c1bb86cdSEric Blake /* If a real optical drive was not found */ 2527c1bb86cdSEric Blake if (bsd_path[0] == '\0') { 2528c1bb86cdSEric Blake error_setg(errp, "Failed to obtain bsd path for optical drive"); 2529c1bb86cdSEric Blake error_occurred = true; 2530c1bb86cdSEric Blake goto hdev_open_Mac_error; 2531c1bb86cdSEric Blake } 2532c1bb86cdSEric Blake 2533c1bb86cdSEric Blake /* If using a cdrom disc and finding a partition on the disc failed */ 2534c1bb86cdSEric Blake if (strncmp(mediaType, kIOCDMediaClass, 9) == 0 && 2535c1bb86cdSEric Blake setup_cdrom(bsd_path, errp) == false) { 2536c1bb86cdSEric Blake print_unmounting_directions(bsd_path); 2537c1bb86cdSEric Blake error_occurred = true; 2538c1bb86cdSEric Blake goto hdev_open_Mac_error; 2539c1bb86cdSEric Blake } 2540c1bb86cdSEric Blake 254146f5ac20SEric Blake qdict_put_str(options, "filename", bsd_path); 2542c1bb86cdSEric Blake 2543c1bb86cdSEric Blake hdev_open_Mac_error: 2544c1bb86cdSEric Blake g_free(mediaType); 2545c1bb86cdSEric Blake if (mediaIterator) { 2546c1bb86cdSEric Blake IOObjectRelease(mediaIterator); 2547c1bb86cdSEric Blake } 2548c1bb86cdSEric Blake if (error_occurred) { 2549c1bb86cdSEric Blake return -ENOENT; 2550c1bb86cdSEric Blake } 2551c1bb86cdSEric Blake } 2552c1bb86cdSEric Blake #endif /* defined(__APPLE__) && defined(__MACH__) */ 2553c1bb86cdSEric Blake 2554c1bb86cdSEric Blake s->type = FTYPE_FILE; 2555c1bb86cdSEric Blake 2556c1bb86cdSEric Blake ret = raw_open_common(bs, options, flags, 0, &local_err); 2557c1bb86cdSEric Blake if (ret < 0) { 2558c1bb86cdSEric Blake error_propagate(errp, local_err); 2559c1bb86cdSEric Blake #if defined(__APPLE__) && defined(__MACH__) 2560c1bb86cdSEric Blake if (*bsd_path) { 2561c1bb86cdSEric Blake filename = bsd_path; 2562c1bb86cdSEric Blake } 2563c1bb86cdSEric Blake /* if a physical device experienced an error while being opened */ 2564c1bb86cdSEric Blake if (strncmp(filename, "/dev/", 5) == 0) { 2565c1bb86cdSEric Blake print_unmounting_directions(filename); 2566c1bb86cdSEric Blake } 2567c1bb86cdSEric Blake #endif /* defined(__APPLE__) && defined(__MACH__) */ 2568c1bb86cdSEric Blake return ret; 2569c1bb86cdSEric Blake } 2570c1bb86cdSEric Blake 2571c1bb86cdSEric Blake /* Since this does ioctl the device must be already opened */ 2572c1bb86cdSEric Blake bs->sg = hdev_is_sg(bs); 2573c1bb86cdSEric Blake 2574c1bb86cdSEric Blake if (flags & BDRV_O_RDWR) { 2575c1bb86cdSEric Blake ret = check_hdev_writable(s); 2576c1bb86cdSEric Blake if (ret < 0) { 2577c1bb86cdSEric Blake raw_close(bs); 2578c1bb86cdSEric Blake error_setg_errno(errp, -ret, "The device is not writable"); 2579c1bb86cdSEric Blake return ret; 2580c1bb86cdSEric Blake } 2581c1bb86cdSEric Blake } 2582c1bb86cdSEric Blake 2583c1bb86cdSEric Blake return ret; 2584c1bb86cdSEric Blake } 2585c1bb86cdSEric Blake 2586c1bb86cdSEric Blake #if defined(__linux__) 2587c1bb86cdSEric Blake 2588c1bb86cdSEric Blake static BlockAIOCB *hdev_aio_ioctl(BlockDriverState *bs, 2589c1bb86cdSEric Blake unsigned long int req, void *buf, 2590c1bb86cdSEric Blake BlockCompletionFunc *cb, void *opaque) 2591c1bb86cdSEric Blake { 2592c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2593c1bb86cdSEric Blake RawPosixAIOData *acb; 2594c1bb86cdSEric Blake ThreadPool *pool; 2595c1bb86cdSEric Blake 2596c1bb86cdSEric Blake if (fd_open(bs) < 0) 2597c1bb86cdSEric Blake return NULL; 2598c1bb86cdSEric Blake 2599c1bb86cdSEric Blake acb = g_new(RawPosixAIOData, 1); 2600c1bb86cdSEric Blake acb->bs = bs; 2601c1bb86cdSEric Blake acb->aio_type = QEMU_AIO_IOCTL; 2602c1bb86cdSEric Blake acb->aio_fildes = s->fd; 2603c1bb86cdSEric Blake acb->aio_offset = 0; 2604c1bb86cdSEric Blake acb->aio_ioctl_buf = buf; 2605c1bb86cdSEric Blake acb->aio_ioctl_cmd = req; 2606c1bb86cdSEric Blake pool = aio_get_thread_pool(bdrv_get_aio_context(bs)); 2607c1bb86cdSEric Blake return thread_pool_submit_aio(pool, aio_worker, acb, cb, opaque); 2608c1bb86cdSEric Blake } 2609c1bb86cdSEric Blake #endif /* linux */ 2610c1bb86cdSEric Blake 2611c1bb86cdSEric Blake static int fd_open(BlockDriverState *bs) 2612c1bb86cdSEric Blake { 2613c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2614c1bb86cdSEric Blake 2615c1bb86cdSEric Blake /* this is just to ensure s->fd is sane (its called by io ops) */ 2616c1bb86cdSEric Blake if (s->fd >= 0) 2617c1bb86cdSEric Blake return 0; 2618c1bb86cdSEric Blake return -EIO; 2619c1bb86cdSEric Blake } 2620c1bb86cdSEric Blake 2621c1bb86cdSEric Blake static coroutine_fn BlockAIOCB *hdev_aio_pdiscard(BlockDriverState *bs, 2622f5a5ca79SManos Pitsidianakis int64_t offset, int bytes, 2623c1bb86cdSEric Blake BlockCompletionFunc *cb, void *opaque) 2624c1bb86cdSEric Blake { 2625c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2626c1bb86cdSEric Blake 2627c1bb86cdSEric Blake if (fd_open(bs) < 0) { 2628c1bb86cdSEric Blake return NULL; 2629c1bb86cdSEric Blake } 2630f5a5ca79SManos Pitsidianakis return paio_submit(bs, s->fd, offset, NULL, bytes, 2631c1bb86cdSEric Blake cb, opaque, QEMU_AIO_DISCARD|QEMU_AIO_BLKDEV); 2632c1bb86cdSEric Blake } 2633c1bb86cdSEric Blake 2634c1bb86cdSEric Blake static coroutine_fn int hdev_co_pwrite_zeroes(BlockDriverState *bs, 2635f5a5ca79SManos Pitsidianakis int64_t offset, int bytes, BdrvRequestFlags flags) 2636c1bb86cdSEric Blake { 2637c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2638c1bb86cdSEric Blake int rc; 2639c1bb86cdSEric Blake 2640c1bb86cdSEric Blake rc = fd_open(bs); 2641c1bb86cdSEric Blake if (rc < 0) { 2642c1bb86cdSEric Blake return rc; 2643c1bb86cdSEric Blake } 2644c1bb86cdSEric Blake if (!(flags & BDRV_REQ_MAY_UNMAP)) { 2645f5a5ca79SManos Pitsidianakis return paio_submit_co(bs, s->fd, offset, NULL, bytes, 2646c1bb86cdSEric Blake QEMU_AIO_WRITE_ZEROES|QEMU_AIO_BLKDEV); 2647c1bb86cdSEric Blake } else if (s->discard_zeroes) { 2648f5a5ca79SManos Pitsidianakis return paio_submit_co(bs, s->fd, offset, NULL, bytes, 2649c1bb86cdSEric Blake QEMU_AIO_DISCARD|QEMU_AIO_BLKDEV); 2650c1bb86cdSEric Blake } 2651c1bb86cdSEric Blake return -ENOTSUP; 2652c1bb86cdSEric Blake } 2653c1bb86cdSEric Blake 2654c1bb86cdSEric Blake static int hdev_create(const char *filename, QemuOpts *opts, 2655c1bb86cdSEric Blake Error **errp) 2656c1bb86cdSEric Blake { 2657c1bb86cdSEric Blake int fd; 2658c1bb86cdSEric Blake int ret = 0; 2659c1bb86cdSEric Blake struct stat stat_buf; 2660c1bb86cdSEric Blake int64_t total_size = 0; 2661c1bb86cdSEric Blake bool has_prefix; 2662c1bb86cdSEric Blake 2663c1bb86cdSEric Blake /* This function is used by both protocol block drivers and therefore either 2664c1bb86cdSEric Blake * of these prefixes may be given. 2665c1bb86cdSEric Blake * The return value has to be stored somewhere, otherwise this is an error 2666c1bb86cdSEric Blake * due to -Werror=unused-value. */ 2667c1bb86cdSEric Blake has_prefix = 2668c1bb86cdSEric Blake strstart(filename, "host_device:", &filename) || 2669c1bb86cdSEric Blake strstart(filename, "host_cdrom:" , &filename); 2670c1bb86cdSEric Blake 2671c1bb86cdSEric Blake (void)has_prefix; 2672c1bb86cdSEric Blake 2673c1bb86cdSEric Blake ret = raw_normalize_devicepath(&filename); 2674c1bb86cdSEric Blake if (ret < 0) { 2675c1bb86cdSEric Blake error_setg_errno(errp, -ret, "Could not normalize device path"); 2676c1bb86cdSEric Blake return ret; 2677c1bb86cdSEric Blake } 2678c1bb86cdSEric Blake 2679c1bb86cdSEric Blake /* Read out options */ 2680c1bb86cdSEric Blake total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), 2681c1bb86cdSEric Blake BDRV_SECTOR_SIZE); 2682c1bb86cdSEric Blake 2683c1bb86cdSEric Blake fd = qemu_open(filename, O_WRONLY | O_BINARY); 2684c1bb86cdSEric Blake if (fd < 0) { 2685c1bb86cdSEric Blake ret = -errno; 2686c1bb86cdSEric Blake error_setg_errno(errp, -ret, "Could not open device"); 2687c1bb86cdSEric Blake return ret; 2688c1bb86cdSEric Blake } 2689c1bb86cdSEric Blake 2690c1bb86cdSEric Blake if (fstat(fd, &stat_buf) < 0) { 2691c1bb86cdSEric Blake ret = -errno; 2692c1bb86cdSEric Blake error_setg_errno(errp, -ret, "Could not stat device"); 2693c1bb86cdSEric Blake } else if (!S_ISBLK(stat_buf.st_mode) && !S_ISCHR(stat_buf.st_mode)) { 2694c1bb86cdSEric Blake error_setg(errp, 2695c1bb86cdSEric Blake "The given file is neither a block nor a character device"); 2696c1bb86cdSEric Blake ret = -ENODEV; 2697c1bb86cdSEric Blake } else if (lseek(fd, 0, SEEK_END) < total_size) { 2698c1bb86cdSEric Blake error_setg(errp, "Device is too small"); 2699c1bb86cdSEric Blake ret = -ENOSPC; 2700c1bb86cdSEric Blake } 2701c1bb86cdSEric Blake 2702c1bb86cdSEric Blake qemu_close(fd); 2703c1bb86cdSEric Blake return ret; 2704c1bb86cdSEric Blake } 2705c1bb86cdSEric Blake 2706c1bb86cdSEric Blake static BlockDriver bdrv_host_device = { 2707c1bb86cdSEric Blake .format_name = "host_device", 2708c1bb86cdSEric Blake .protocol_name = "host_device", 2709c1bb86cdSEric Blake .instance_size = sizeof(BDRVRawState), 2710c1bb86cdSEric Blake .bdrv_needs_filename = true, 2711c1bb86cdSEric Blake .bdrv_probe_device = hdev_probe_device, 2712c1bb86cdSEric Blake .bdrv_parse_filename = hdev_parse_filename, 2713c1bb86cdSEric Blake .bdrv_file_open = hdev_open, 2714c1bb86cdSEric Blake .bdrv_close = raw_close, 2715c1bb86cdSEric Blake .bdrv_reopen_prepare = raw_reopen_prepare, 2716c1bb86cdSEric Blake .bdrv_reopen_commit = raw_reopen_commit, 2717c1bb86cdSEric Blake .bdrv_reopen_abort = raw_reopen_abort, 2718c1bb86cdSEric Blake .bdrv_create = hdev_create, 2719c1bb86cdSEric Blake .create_opts = &raw_create_opts, 2720c1bb86cdSEric Blake .bdrv_co_pwrite_zeroes = hdev_co_pwrite_zeroes, 2721c1bb86cdSEric Blake 2722c1bb86cdSEric Blake .bdrv_co_preadv = raw_co_preadv, 2723c1bb86cdSEric Blake .bdrv_co_pwritev = raw_co_pwritev, 2724c1bb86cdSEric Blake .bdrv_aio_flush = raw_aio_flush, 2725c1bb86cdSEric Blake .bdrv_aio_pdiscard = hdev_aio_pdiscard, 2726c1bb86cdSEric Blake .bdrv_refresh_limits = raw_refresh_limits, 2727c1bb86cdSEric Blake .bdrv_io_plug = raw_aio_plug, 2728c1bb86cdSEric Blake .bdrv_io_unplug = raw_aio_unplug, 2729c1bb86cdSEric Blake 2730c1bb86cdSEric Blake .bdrv_truncate = raw_truncate, 2731c1bb86cdSEric Blake .bdrv_getlength = raw_getlength, 2732c1bb86cdSEric Blake .bdrv_get_info = raw_get_info, 2733c1bb86cdSEric Blake .bdrv_get_allocated_file_size 2734c1bb86cdSEric Blake = raw_get_allocated_file_size, 2735244a5668SFam Zheng .bdrv_check_perm = raw_check_perm, 2736244a5668SFam Zheng .bdrv_set_perm = raw_set_perm, 2737244a5668SFam Zheng .bdrv_abort_perm_update = raw_abort_perm_update, 2738c1bb86cdSEric Blake .bdrv_probe_blocksizes = hdev_probe_blocksizes, 2739c1bb86cdSEric Blake .bdrv_probe_geometry = hdev_probe_geometry, 2740c1bb86cdSEric Blake 2741c1bb86cdSEric Blake /* generic scsi device */ 2742c1bb86cdSEric Blake #ifdef __linux__ 2743c1bb86cdSEric Blake .bdrv_aio_ioctl = hdev_aio_ioctl, 2744c1bb86cdSEric Blake #endif 2745c1bb86cdSEric Blake }; 2746c1bb86cdSEric Blake 2747c1bb86cdSEric Blake #if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) 2748c1bb86cdSEric Blake static void cdrom_parse_filename(const char *filename, QDict *options, 2749c1bb86cdSEric Blake Error **errp) 2750c1bb86cdSEric Blake { 275103c320d8SMax Reitz bdrv_parse_filename_strip_prefix(filename, "host_cdrom:", options); 2752c1bb86cdSEric Blake } 2753c1bb86cdSEric Blake #endif 2754c1bb86cdSEric Blake 2755c1bb86cdSEric Blake #ifdef __linux__ 2756c1bb86cdSEric Blake static int cdrom_open(BlockDriverState *bs, QDict *options, int flags, 2757c1bb86cdSEric Blake Error **errp) 2758c1bb86cdSEric Blake { 2759c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2760c1bb86cdSEric Blake 2761c1bb86cdSEric Blake s->type = FTYPE_CD; 2762c1bb86cdSEric Blake 2763c1bb86cdSEric Blake /* open will not fail even if no CD is inserted, so add O_NONBLOCK */ 2764c1bb86cdSEric Blake return raw_open_common(bs, options, flags, O_NONBLOCK, errp); 2765c1bb86cdSEric Blake } 2766c1bb86cdSEric Blake 2767c1bb86cdSEric Blake static int cdrom_probe_device(const char *filename) 2768c1bb86cdSEric Blake { 2769c1bb86cdSEric Blake int fd, ret; 2770c1bb86cdSEric Blake int prio = 0; 2771c1bb86cdSEric Blake struct stat st; 2772c1bb86cdSEric Blake 2773c1bb86cdSEric Blake fd = qemu_open(filename, O_RDONLY | O_NONBLOCK); 2774c1bb86cdSEric Blake if (fd < 0) { 2775c1bb86cdSEric Blake goto out; 2776c1bb86cdSEric Blake } 2777c1bb86cdSEric Blake ret = fstat(fd, &st); 2778c1bb86cdSEric Blake if (ret == -1 || !S_ISBLK(st.st_mode)) { 2779c1bb86cdSEric Blake goto outc; 2780c1bb86cdSEric Blake } 2781c1bb86cdSEric Blake 2782c1bb86cdSEric Blake /* Attempt to detect via a CDROM specific ioctl */ 2783c1bb86cdSEric Blake ret = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT); 2784c1bb86cdSEric Blake if (ret >= 0) 2785c1bb86cdSEric Blake prio = 100; 2786c1bb86cdSEric Blake 2787c1bb86cdSEric Blake outc: 2788c1bb86cdSEric Blake qemu_close(fd); 2789c1bb86cdSEric Blake out: 2790c1bb86cdSEric Blake return prio; 2791c1bb86cdSEric Blake } 2792c1bb86cdSEric Blake 2793c1bb86cdSEric Blake static bool cdrom_is_inserted(BlockDriverState *bs) 2794c1bb86cdSEric Blake { 2795c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2796c1bb86cdSEric Blake int ret; 2797c1bb86cdSEric Blake 2798c1bb86cdSEric Blake ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT); 2799c1bb86cdSEric Blake return ret == CDS_DISC_OK; 2800c1bb86cdSEric Blake } 2801c1bb86cdSEric Blake 2802c1bb86cdSEric Blake static void cdrom_eject(BlockDriverState *bs, bool eject_flag) 2803c1bb86cdSEric Blake { 2804c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2805c1bb86cdSEric Blake 2806c1bb86cdSEric Blake if (eject_flag) { 2807c1bb86cdSEric Blake if (ioctl(s->fd, CDROMEJECT, NULL) < 0) 2808c1bb86cdSEric Blake perror("CDROMEJECT"); 2809c1bb86cdSEric Blake } else { 2810c1bb86cdSEric Blake if (ioctl(s->fd, CDROMCLOSETRAY, NULL) < 0) 2811c1bb86cdSEric Blake perror("CDROMEJECT"); 2812c1bb86cdSEric Blake } 2813c1bb86cdSEric Blake } 2814c1bb86cdSEric Blake 2815c1bb86cdSEric Blake static void cdrom_lock_medium(BlockDriverState *bs, bool locked) 2816c1bb86cdSEric Blake { 2817c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2818c1bb86cdSEric Blake 2819c1bb86cdSEric Blake if (ioctl(s->fd, CDROM_LOCKDOOR, locked) < 0) { 2820c1bb86cdSEric Blake /* 2821c1bb86cdSEric Blake * Note: an error can happen if the distribution automatically 2822c1bb86cdSEric Blake * mounts the CD-ROM 2823c1bb86cdSEric Blake */ 2824c1bb86cdSEric Blake /* perror("CDROM_LOCKDOOR"); */ 2825c1bb86cdSEric Blake } 2826c1bb86cdSEric Blake } 2827c1bb86cdSEric Blake 2828c1bb86cdSEric Blake static BlockDriver bdrv_host_cdrom = { 2829c1bb86cdSEric Blake .format_name = "host_cdrom", 2830c1bb86cdSEric Blake .protocol_name = "host_cdrom", 2831c1bb86cdSEric Blake .instance_size = sizeof(BDRVRawState), 2832c1bb86cdSEric Blake .bdrv_needs_filename = true, 2833c1bb86cdSEric Blake .bdrv_probe_device = cdrom_probe_device, 2834c1bb86cdSEric Blake .bdrv_parse_filename = cdrom_parse_filename, 2835c1bb86cdSEric Blake .bdrv_file_open = cdrom_open, 2836c1bb86cdSEric Blake .bdrv_close = raw_close, 2837c1bb86cdSEric Blake .bdrv_reopen_prepare = raw_reopen_prepare, 2838c1bb86cdSEric Blake .bdrv_reopen_commit = raw_reopen_commit, 2839c1bb86cdSEric Blake .bdrv_reopen_abort = raw_reopen_abort, 2840c1bb86cdSEric Blake .bdrv_create = hdev_create, 2841c1bb86cdSEric Blake .create_opts = &raw_create_opts, 2842c1bb86cdSEric Blake 2843c1bb86cdSEric Blake 2844c1bb86cdSEric Blake .bdrv_co_preadv = raw_co_preadv, 2845c1bb86cdSEric Blake .bdrv_co_pwritev = raw_co_pwritev, 2846c1bb86cdSEric Blake .bdrv_aio_flush = raw_aio_flush, 2847c1bb86cdSEric Blake .bdrv_refresh_limits = raw_refresh_limits, 2848c1bb86cdSEric Blake .bdrv_io_plug = raw_aio_plug, 2849c1bb86cdSEric Blake .bdrv_io_unplug = raw_aio_unplug, 2850c1bb86cdSEric Blake 2851c1bb86cdSEric Blake .bdrv_truncate = raw_truncate, 2852c1bb86cdSEric Blake .bdrv_getlength = raw_getlength, 2853c1bb86cdSEric Blake .has_variable_length = true, 2854c1bb86cdSEric Blake .bdrv_get_allocated_file_size 2855c1bb86cdSEric Blake = raw_get_allocated_file_size, 2856c1bb86cdSEric Blake 2857c1bb86cdSEric Blake /* removable device support */ 2858c1bb86cdSEric Blake .bdrv_is_inserted = cdrom_is_inserted, 2859c1bb86cdSEric Blake .bdrv_eject = cdrom_eject, 2860c1bb86cdSEric Blake .bdrv_lock_medium = cdrom_lock_medium, 2861c1bb86cdSEric Blake 2862c1bb86cdSEric Blake /* generic scsi device */ 2863c1bb86cdSEric Blake .bdrv_aio_ioctl = hdev_aio_ioctl, 2864c1bb86cdSEric Blake }; 2865c1bb86cdSEric Blake #endif /* __linux__ */ 2866c1bb86cdSEric Blake 2867c1bb86cdSEric Blake #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__) 2868c1bb86cdSEric Blake static int cdrom_open(BlockDriverState *bs, QDict *options, int flags, 2869c1bb86cdSEric Blake Error **errp) 2870c1bb86cdSEric Blake { 2871c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2872c1bb86cdSEric Blake Error *local_err = NULL; 2873c1bb86cdSEric Blake int ret; 2874c1bb86cdSEric Blake 2875c1bb86cdSEric Blake s->type = FTYPE_CD; 2876c1bb86cdSEric Blake 2877c1bb86cdSEric Blake ret = raw_open_common(bs, options, flags, 0, &local_err); 2878c1bb86cdSEric Blake if (ret) { 2879c1bb86cdSEric Blake error_propagate(errp, local_err); 2880c1bb86cdSEric Blake return ret; 2881c1bb86cdSEric Blake } 2882c1bb86cdSEric Blake 2883c1bb86cdSEric Blake /* make sure the door isn't locked at this time */ 2884c1bb86cdSEric Blake ioctl(s->fd, CDIOCALLOW); 2885c1bb86cdSEric Blake return 0; 2886c1bb86cdSEric Blake } 2887c1bb86cdSEric Blake 2888c1bb86cdSEric Blake static int cdrom_probe_device(const char *filename) 2889c1bb86cdSEric Blake { 2890c1bb86cdSEric Blake if (strstart(filename, "/dev/cd", NULL) || 2891c1bb86cdSEric Blake strstart(filename, "/dev/acd", NULL)) 2892c1bb86cdSEric Blake return 100; 2893c1bb86cdSEric Blake return 0; 2894c1bb86cdSEric Blake } 2895c1bb86cdSEric Blake 2896c1bb86cdSEric Blake static int cdrom_reopen(BlockDriverState *bs) 2897c1bb86cdSEric Blake { 2898c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2899c1bb86cdSEric Blake int fd; 2900c1bb86cdSEric Blake 2901c1bb86cdSEric Blake /* 2902c1bb86cdSEric Blake * Force reread of possibly changed/newly loaded disc, 2903c1bb86cdSEric Blake * FreeBSD seems to not notice sometimes... 2904c1bb86cdSEric Blake */ 2905c1bb86cdSEric Blake if (s->fd >= 0) 2906c1bb86cdSEric Blake qemu_close(s->fd); 2907c1bb86cdSEric Blake fd = qemu_open(bs->filename, s->open_flags, 0644); 2908c1bb86cdSEric Blake if (fd < 0) { 2909c1bb86cdSEric Blake s->fd = -1; 2910c1bb86cdSEric Blake return -EIO; 2911c1bb86cdSEric Blake } 2912c1bb86cdSEric Blake s->fd = fd; 2913c1bb86cdSEric Blake 2914c1bb86cdSEric Blake /* make sure the door isn't locked at this time */ 2915c1bb86cdSEric Blake ioctl(s->fd, CDIOCALLOW); 2916c1bb86cdSEric Blake return 0; 2917c1bb86cdSEric Blake } 2918c1bb86cdSEric Blake 2919c1bb86cdSEric Blake static bool cdrom_is_inserted(BlockDriverState *bs) 2920c1bb86cdSEric Blake { 2921c1bb86cdSEric Blake return raw_getlength(bs) > 0; 2922c1bb86cdSEric Blake } 2923c1bb86cdSEric Blake 2924c1bb86cdSEric Blake static void cdrom_eject(BlockDriverState *bs, bool eject_flag) 2925c1bb86cdSEric Blake { 2926c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2927c1bb86cdSEric Blake 2928c1bb86cdSEric Blake if (s->fd < 0) 2929c1bb86cdSEric Blake return; 2930c1bb86cdSEric Blake 2931c1bb86cdSEric Blake (void) ioctl(s->fd, CDIOCALLOW); 2932c1bb86cdSEric Blake 2933c1bb86cdSEric Blake if (eject_flag) { 2934c1bb86cdSEric Blake if (ioctl(s->fd, CDIOCEJECT) < 0) 2935c1bb86cdSEric Blake perror("CDIOCEJECT"); 2936c1bb86cdSEric Blake } else { 2937c1bb86cdSEric Blake if (ioctl(s->fd, CDIOCCLOSE) < 0) 2938c1bb86cdSEric Blake perror("CDIOCCLOSE"); 2939c1bb86cdSEric Blake } 2940c1bb86cdSEric Blake 2941c1bb86cdSEric Blake cdrom_reopen(bs); 2942c1bb86cdSEric Blake } 2943c1bb86cdSEric Blake 2944c1bb86cdSEric Blake static void cdrom_lock_medium(BlockDriverState *bs, bool locked) 2945c1bb86cdSEric Blake { 2946c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2947c1bb86cdSEric Blake 2948c1bb86cdSEric Blake if (s->fd < 0) 2949c1bb86cdSEric Blake return; 2950c1bb86cdSEric Blake if (ioctl(s->fd, (locked ? CDIOCPREVENT : CDIOCALLOW)) < 0) { 2951c1bb86cdSEric Blake /* 2952c1bb86cdSEric Blake * Note: an error can happen if the distribution automatically 2953c1bb86cdSEric Blake * mounts the CD-ROM 2954c1bb86cdSEric Blake */ 2955c1bb86cdSEric Blake /* perror("CDROM_LOCKDOOR"); */ 2956c1bb86cdSEric Blake } 2957c1bb86cdSEric Blake } 2958c1bb86cdSEric Blake 2959c1bb86cdSEric Blake static BlockDriver bdrv_host_cdrom = { 2960c1bb86cdSEric Blake .format_name = "host_cdrom", 2961c1bb86cdSEric Blake .protocol_name = "host_cdrom", 2962c1bb86cdSEric Blake .instance_size = sizeof(BDRVRawState), 2963c1bb86cdSEric Blake .bdrv_needs_filename = true, 2964c1bb86cdSEric Blake .bdrv_probe_device = cdrom_probe_device, 2965c1bb86cdSEric Blake .bdrv_parse_filename = cdrom_parse_filename, 2966c1bb86cdSEric Blake .bdrv_file_open = cdrom_open, 2967c1bb86cdSEric Blake .bdrv_close = raw_close, 2968c1bb86cdSEric Blake .bdrv_reopen_prepare = raw_reopen_prepare, 2969c1bb86cdSEric Blake .bdrv_reopen_commit = raw_reopen_commit, 2970c1bb86cdSEric Blake .bdrv_reopen_abort = raw_reopen_abort, 2971c1bb86cdSEric Blake .bdrv_create = hdev_create, 2972c1bb86cdSEric Blake .create_opts = &raw_create_opts, 2973c1bb86cdSEric Blake 2974c1bb86cdSEric Blake .bdrv_co_preadv = raw_co_preadv, 2975c1bb86cdSEric Blake .bdrv_co_pwritev = raw_co_pwritev, 2976c1bb86cdSEric Blake .bdrv_aio_flush = raw_aio_flush, 2977c1bb86cdSEric Blake .bdrv_refresh_limits = raw_refresh_limits, 2978c1bb86cdSEric Blake .bdrv_io_plug = raw_aio_plug, 2979c1bb86cdSEric Blake .bdrv_io_unplug = raw_aio_unplug, 2980c1bb86cdSEric Blake 2981c1bb86cdSEric Blake .bdrv_truncate = raw_truncate, 2982c1bb86cdSEric Blake .bdrv_getlength = raw_getlength, 2983c1bb86cdSEric Blake .has_variable_length = true, 2984c1bb86cdSEric Blake .bdrv_get_allocated_file_size 2985c1bb86cdSEric Blake = raw_get_allocated_file_size, 2986c1bb86cdSEric Blake 2987c1bb86cdSEric Blake /* removable device support */ 2988c1bb86cdSEric Blake .bdrv_is_inserted = cdrom_is_inserted, 2989c1bb86cdSEric Blake .bdrv_eject = cdrom_eject, 2990c1bb86cdSEric Blake .bdrv_lock_medium = cdrom_lock_medium, 2991c1bb86cdSEric Blake }; 2992c1bb86cdSEric Blake #endif /* __FreeBSD__ */ 2993c1bb86cdSEric Blake 2994c1bb86cdSEric Blake static void bdrv_file_init(void) 2995c1bb86cdSEric Blake { 2996c1bb86cdSEric Blake /* 2997c1bb86cdSEric Blake * Register all the drivers. Note that order is important, the driver 2998c1bb86cdSEric Blake * registered last will get probed first. 2999c1bb86cdSEric Blake */ 3000c1bb86cdSEric Blake bdrv_register(&bdrv_file); 3001c1bb86cdSEric Blake bdrv_register(&bdrv_host_device); 3002c1bb86cdSEric Blake #ifdef __linux__ 3003c1bb86cdSEric Blake bdrv_register(&bdrv_host_cdrom); 3004c1bb86cdSEric Blake #endif 3005c1bb86cdSEric Blake #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) 3006c1bb86cdSEric Blake bdrv_register(&bdrv_host_cdrom); 3007c1bb86cdSEric Blake #endif 3008c1bb86cdSEric Blake } 3009c1bb86cdSEric Blake 3010c1bb86cdSEric Blake block_init(bdrv_file_init); 3011