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 "qemu/timer.h" 29c1bb86cdSEric Blake #include "qemu/log.h" 30c1bb86cdSEric Blake #include "block/block_int.h" 31c1bb86cdSEric Blake #include "qemu/module.h" 32c1bb86cdSEric Blake #include "trace.h" 33c1bb86cdSEric Blake #include "block/thread-pool.h" 34c1bb86cdSEric Blake #include "qemu/iov.h" 35c1bb86cdSEric Blake #include "block/raw-aio.h" 36c1bb86cdSEric Blake #include "qapi/util.h" 37c1bb86cdSEric Blake #include "qapi/qmp/qstring.h" 38c1bb86cdSEric Blake 39c1bb86cdSEric Blake #if defined(__APPLE__) && (__MACH__) 40c1bb86cdSEric Blake #include <paths.h> 41c1bb86cdSEric Blake #include <sys/param.h> 42c1bb86cdSEric Blake #include <IOKit/IOKitLib.h> 43c1bb86cdSEric Blake #include <IOKit/IOBSD.h> 44c1bb86cdSEric Blake #include <IOKit/storage/IOMediaBSDClient.h> 45c1bb86cdSEric Blake #include <IOKit/storage/IOMedia.h> 46c1bb86cdSEric Blake #include <IOKit/storage/IOCDMedia.h> 47c1bb86cdSEric Blake //#include <IOKit/storage/IOCDTypes.h> 48c1bb86cdSEric Blake #include <IOKit/storage/IODVDMedia.h> 49c1bb86cdSEric Blake #include <CoreFoundation/CoreFoundation.h> 50c1bb86cdSEric Blake #endif 51c1bb86cdSEric Blake 52c1bb86cdSEric Blake #ifdef __sun__ 53c1bb86cdSEric Blake #define _POSIX_PTHREAD_SEMANTICS 1 54c1bb86cdSEric Blake #include <sys/dkio.h> 55c1bb86cdSEric Blake #endif 56c1bb86cdSEric Blake #ifdef __linux__ 57c1bb86cdSEric Blake #include <sys/ioctl.h> 58c1bb86cdSEric Blake #include <sys/param.h> 59c1bb86cdSEric Blake #include <linux/cdrom.h> 60c1bb86cdSEric Blake #include <linux/fd.h> 61c1bb86cdSEric Blake #include <linux/fs.h> 62c1bb86cdSEric Blake #include <linux/hdreg.h> 63c1bb86cdSEric Blake #include <scsi/sg.h> 64c1bb86cdSEric Blake #ifdef __s390__ 65c1bb86cdSEric Blake #include <asm/dasd.h> 66c1bb86cdSEric Blake #endif 67c1bb86cdSEric Blake #ifndef FS_NOCOW_FL 68c1bb86cdSEric Blake #define FS_NOCOW_FL 0x00800000 /* Do not cow file */ 69c1bb86cdSEric Blake #endif 70c1bb86cdSEric Blake #endif 71c1bb86cdSEric Blake #if defined(CONFIG_FALLOCATE_PUNCH_HOLE) || defined(CONFIG_FALLOCATE_ZERO_RANGE) 72c1bb86cdSEric Blake #include <linux/falloc.h> 73c1bb86cdSEric Blake #endif 74c1bb86cdSEric Blake #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__) 75c1bb86cdSEric Blake #include <sys/disk.h> 76c1bb86cdSEric Blake #include <sys/cdio.h> 77c1bb86cdSEric Blake #endif 78c1bb86cdSEric Blake 79c1bb86cdSEric Blake #ifdef __OpenBSD__ 80c1bb86cdSEric Blake #include <sys/ioctl.h> 81c1bb86cdSEric Blake #include <sys/disklabel.h> 82c1bb86cdSEric Blake #include <sys/dkio.h> 83c1bb86cdSEric Blake #endif 84c1bb86cdSEric Blake 85c1bb86cdSEric Blake #ifdef __NetBSD__ 86c1bb86cdSEric Blake #include <sys/ioctl.h> 87c1bb86cdSEric Blake #include <sys/disklabel.h> 88c1bb86cdSEric Blake #include <sys/dkio.h> 89c1bb86cdSEric Blake #include <sys/disk.h> 90c1bb86cdSEric Blake #endif 91c1bb86cdSEric Blake 92c1bb86cdSEric Blake #ifdef __DragonFly__ 93c1bb86cdSEric Blake #include <sys/ioctl.h> 94c1bb86cdSEric Blake #include <sys/diskslice.h> 95c1bb86cdSEric Blake #endif 96c1bb86cdSEric Blake 97c1bb86cdSEric Blake #ifdef CONFIG_XFS 98c1bb86cdSEric Blake #include <xfs/xfs.h> 99c1bb86cdSEric Blake #endif 100c1bb86cdSEric Blake 101c1bb86cdSEric Blake //#define DEBUG_BLOCK 102c1bb86cdSEric Blake 103c1bb86cdSEric Blake #ifdef DEBUG_BLOCK 104c1bb86cdSEric Blake # define DEBUG_BLOCK_PRINT 1 105c1bb86cdSEric Blake #else 106c1bb86cdSEric Blake # define DEBUG_BLOCK_PRINT 0 107c1bb86cdSEric Blake #endif 108c1bb86cdSEric Blake #define DPRINTF(fmt, ...) \ 109c1bb86cdSEric Blake do { \ 110c1bb86cdSEric Blake if (DEBUG_BLOCK_PRINT) { \ 111c1bb86cdSEric Blake printf(fmt, ## __VA_ARGS__); \ 112c1bb86cdSEric Blake } \ 113c1bb86cdSEric Blake } while (0) 114c1bb86cdSEric Blake 115c1bb86cdSEric Blake /* OS X does not have O_DSYNC */ 116c1bb86cdSEric Blake #ifndef O_DSYNC 117c1bb86cdSEric Blake #ifdef O_SYNC 118c1bb86cdSEric Blake #define O_DSYNC O_SYNC 119c1bb86cdSEric Blake #elif defined(O_FSYNC) 120c1bb86cdSEric Blake #define O_DSYNC O_FSYNC 121c1bb86cdSEric Blake #endif 122c1bb86cdSEric Blake #endif 123c1bb86cdSEric Blake 124c1bb86cdSEric Blake /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */ 125c1bb86cdSEric Blake #ifndef O_DIRECT 126c1bb86cdSEric Blake #define O_DIRECT O_DSYNC 127c1bb86cdSEric Blake #endif 128c1bb86cdSEric Blake 129c1bb86cdSEric Blake #define FTYPE_FILE 0 130c1bb86cdSEric Blake #define FTYPE_CD 1 131c1bb86cdSEric Blake 132c1bb86cdSEric Blake #define MAX_BLOCKSIZE 4096 133c1bb86cdSEric Blake 134c1bb86cdSEric Blake typedef struct BDRVRawState { 135c1bb86cdSEric Blake int fd; 136c1bb86cdSEric Blake int type; 137c1bb86cdSEric Blake int open_flags; 138c1bb86cdSEric Blake size_t buf_align; 139c1bb86cdSEric Blake 140c1bb86cdSEric Blake #ifdef CONFIG_XFS 141c1bb86cdSEric Blake bool is_xfs:1; 142c1bb86cdSEric Blake #endif 143c1bb86cdSEric Blake bool has_discard:1; 144c1bb86cdSEric Blake bool has_write_zeroes:1; 145c1bb86cdSEric Blake bool discard_zeroes:1; 146c1bb86cdSEric Blake bool use_linux_aio:1; 147e5bcf967SKevin Wolf bool page_cache_inconsistent:1; 148c1bb86cdSEric Blake bool has_fallocate; 149c1bb86cdSEric Blake bool needs_alignment; 150c1bb86cdSEric Blake } BDRVRawState; 151c1bb86cdSEric Blake 152c1bb86cdSEric Blake typedef struct BDRVRawReopenState { 153c1bb86cdSEric Blake int fd; 154c1bb86cdSEric Blake int open_flags; 155c1bb86cdSEric Blake } BDRVRawReopenState; 156c1bb86cdSEric Blake 157c1bb86cdSEric Blake static int fd_open(BlockDriverState *bs); 158c1bb86cdSEric Blake static int64_t raw_getlength(BlockDriverState *bs); 159c1bb86cdSEric Blake 160c1bb86cdSEric Blake typedef struct RawPosixAIOData { 161c1bb86cdSEric Blake BlockDriverState *bs; 162c1bb86cdSEric Blake int aio_fildes; 163c1bb86cdSEric Blake union { 164c1bb86cdSEric Blake struct iovec *aio_iov; 165c1bb86cdSEric Blake void *aio_ioctl_buf; 166c1bb86cdSEric Blake }; 167c1bb86cdSEric Blake int aio_niov; 168c1bb86cdSEric Blake uint64_t aio_nbytes; 169c1bb86cdSEric Blake #define aio_ioctl_cmd aio_nbytes /* for QEMU_AIO_IOCTL */ 170c1bb86cdSEric Blake off_t aio_offset; 171c1bb86cdSEric Blake int aio_type; 172c1bb86cdSEric Blake } RawPosixAIOData; 173c1bb86cdSEric Blake 174c1bb86cdSEric Blake #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) 175c1bb86cdSEric Blake static int cdrom_reopen(BlockDriverState *bs); 176c1bb86cdSEric Blake #endif 177c1bb86cdSEric Blake 178c1bb86cdSEric Blake #if defined(__NetBSD__) 179c1bb86cdSEric Blake static int raw_normalize_devicepath(const char **filename) 180c1bb86cdSEric Blake { 181c1bb86cdSEric Blake static char namebuf[PATH_MAX]; 182c1bb86cdSEric Blake const char *dp, *fname; 183c1bb86cdSEric Blake struct stat sb; 184c1bb86cdSEric Blake 185c1bb86cdSEric Blake fname = *filename; 186c1bb86cdSEric Blake dp = strrchr(fname, '/'); 187c1bb86cdSEric Blake if (lstat(fname, &sb) < 0) { 188c1bb86cdSEric Blake fprintf(stderr, "%s: stat failed: %s\n", 189c1bb86cdSEric Blake fname, strerror(errno)); 190c1bb86cdSEric Blake return -errno; 191c1bb86cdSEric Blake } 192c1bb86cdSEric Blake 193c1bb86cdSEric Blake if (!S_ISBLK(sb.st_mode)) { 194c1bb86cdSEric Blake return 0; 195c1bb86cdSEric Blake } 196c1bb86cdSEric Blake 197c1bb86cdSEric Blake if (dp == NULL) { 198c1bb86cdSEric Blake snprintf(namebuf, PATH_MAX, "r%s", fname); 199c1bb86cdSEric Blake } else { 200c1bb86cdSEric Blake snprintf(namebuf, PATH_MAX, "%.*s/r%s", 201c1bb86cdSEric Blake (int)(dp - fname), fname, dp + 1); 202c1bb86cdSEric Blake } 203c1bb86cdSEric Blake fprintf(stderr, "%s is a block device", fname); 204c1bb86cdSEric Blake *filename = namebuf; 205c1bb86cdSEric Blake fprintf(stderr, ", using %s\n", *filename); 206c1bb86cdSEric Blake 207c1bb86cdSEric Blake return 0; 208c1bb86cdSEric Blake } 209c1bb86cdSEric Blake #else 210c1bb86cdSEric Blake static int raw_normalize_devicepath(const char **filename) 211c1bb86cdSEric Blake { 212c1bb86cdSEric Blake return 0; 213c1bb86cdSEric Blake } 214c1bb86cdSEric Blake #endif 215c1bb86cdSEric Blake 216c1bb86cdSEric Blake /* 217c1bb86cdSEric Blake * Get logical block size via ioctl. On success store it in @sector_size_p. 218c1bb86cdSEric Blake */ 219c1bb86cdSEric Blake static int probe_logical_blocksize(int fd, unsigned int *sector_size_p) 220c1bb86cdSEric Blake { 221c1bb86cdSEric Blake unsigned int sector_size; 222c1bb86cdSEric Blake bool success = false; 223*700f9ce0SPeter Maydell int i; 224c1bb86cdSEric Blake 225c1bb86cdSEric Blake errno = ENOTSUP; 226*700f9ce0SPeter Maydell static const unsigned long ioctl_list[] = { 227c1bb86cdSEric Blake #ifdef BLKSSZGET 228*700f9ce0SPeter Maydell BLKSSZGET, 229c1bb86cdSEric Blake #endif 230c1bb86cdSEric Blake #ifdef DKIOCGETBLOCKSIZE 231*700f9ce0SPeter Maydell DKIOCGETBLOCKSIZE, 232c1bb86cdSEric Blake #endif 233c1bb86cdSEric Blake #ifdef DIOCGSECTORSIZE 234*700f9ce0SPeter Maydell DIOCGSECTORSIZE, 235*700f9ce0SPeter Maydell #endif 236*700f9ce0SPeter Maydell }; 237*700f9ce0SPeter Maydell 238*700f9ce0SPeter Maydell /* Try a few ioctls to get the right size */ 239*700f9ce0SPeter Maydell for (i = 0; i < (int)ARRAY_SIZE(ioctl_list); i++) { 240*700f9ce0SPeter Maydell if (ioctl(fd, ioctl_list[i], §or_size) >= 0) { 241c1bb86cdSEric Blake *sector_size_p = sector_size; 242c1bb86cdSEric Blake success = true; 243c1bb86cdSEric Blake } 244*700f9ce0SPeter Maydell } 245c1bb86cdSEric Blake 246c1bb86cdSEric Blake return success ? 0 : -errno; 247c1bb86cdSEric Blake } 248c1bb86cdSEric Blake 249c1bb86cdSEric Blake /** 250c1bb86cdSEric Blake * Get physical block size of @fd. 251c1bb86cdSEric Blake * On success, store it in @blk_size and return 0. 252c1bb86cdSEric Blake * On failure, return -errno. 253c1bb86cdSEric Blake */ 254c1bb86cdSEric Blake static int probe_physical_blocksize(int fd, unsigned int *blk_size) 255c1bb86cdSEric Blake { 256c1bb86cdSEric Blake #ifdef BLKPBSZGET 257c1bb86cdSEric Blake if (ioctl(fd, BLKPBSZGET, blk_size) < 0) { 258c1bb86cdSEric Blake return -errno; 259c1bb86cdSEric Blake } 260c1bb86cdSEric Blake return 0; 261c1bb86cdSEric Blake #else 262c1bb86cdSEric Blake return -ENOTSUP; 263c1bb86cdSEric Blake #endif 264c1bb86cdSEric Blake } 265c1bb86cdSEric Blake 266c1bb86cdSEric Blake /* Check if read is allowed with given memory buffer and length. 267c1bb86cdSEric Blake * 268c1bb86cdSEric Blake * This function is used to check O_DIRECT memory buffer and request alignment. 269c1bb86cdSEric Blake */ 270c1bb86cdSEric Blake static bool raw_is_io_aligned(int fd, void *buf, size_t len) 271c1bb86cdSEric Blake { 272c1bb86cdSEric Blake ssize_t ret = pread(fd, buf, len, 0); 273c1bb86cdSEric Blake 274c1bb86cdSEric Blake if (ret >= 0) { 275c1bb86cdSEric Blake return true; 276c1bb86cdSEric Blake } 277c1bb86cdSEric Blake 278c1bb86cdSEric Blake #ifdef __linux__ 279c1bb86cdSEric Blake /* The Linux kernel returns EINVAL for misaligned O_DIRECT reads. Ignore 280c1bb86cdSEric Blake * other errors (e.g. real I/O error), which could happen on a failed 281c1bb86cdSEric Blake * drive, since we only care about probing alignment. 282c1bb86cdSEric Blake */ 283c1bb86cdSEric Blake if (errno != EINVAL) { 284c1bb86cdSEric Blake return true; 285c1bb86cdSEric Blake } 286c1bb86cdSEric Blake #endif 287c1bb86cdSEric Blake 288c1bb86cdSEric Blake return false; 289c1bb86cdSEric Blake } 290c1bb86cdSEric Blake 291c1bb86cdSEric Blake static void raw_probe_alignment(BlockDriverState *bs, int fd, Error **errp) 292c1bb86cdSEric Blake { 293c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 294c1bb86cdSEric Blake char *buf; 295c1bb86cdSEric Blake size_t max_align = MAX(MAX_BLOCKSIZE, getpagesize()); 296c1bb86cdSEric Blake 297c1bb86cdSEric Blake /* For SCSI generic devices the alignment is not really used. 298c1bb86cdSEric Blake With buffered I/O, we don't have any restrictions. */ 299c1bb86cdSEric Blake if (bdrv_is_sg(bs) || !s->needs_alignment) { 300c1bb86cdSEric Blake bs->bl.request_alignment = 1; 301c1bb86cdSEric Blake s->buf_align = 1; 302c1bb86cdSEric Blake return; 303c1bb86cdSEric Blake } 304c1bb86cdSEric Blake 305c1bb86cdSEric Blake bs->bl.request_alignment = 0; 306c1bb86cdSEric Blake s->buf_align = 0; 307c1bb86cdSEric Blake /* Let's try to use the logical blocksize for the alignment. */ 308c1bb86cdSEric Blake if (probe_logical_blocksize(fd, &bs->bl.request_alignment) < 0) { 309c1bb86cdSEric Blake bs->bl.request_alignment = 0; 310c1bb86cdSEric Blake } 311c1bb86cdSEric Blake #ifdef CONFIG_XFS 312c1bb86cdSEric Blake if (s->is_xfs) { 313c1bb86cdSEric Blake struct dioattr da; 314c1bb86cdSEric Blake if (xfsctl(NULL, fd, XFS_IOC_DIOINFO, &da) >= 0) { 315c1bb86cdSEric Blake bs->bl.request_alignment = da.d_miniosz; 316c1bb86cdSEric Blake /* The kernel returns wrong information for d_mem */ 317c1bb86cdSEric Blake /* s->buf_align = da.d_mem; */ 318c1bb86cdSEric Blake } 319c1bb86cdSEric Blake } 320c1bb86cdSEric Blake #endif 321c1bb86cdSEric Blake 322c1bb86cdSEric Blake /* If we could not get the sizes so far, we can only guess them */ 323c1bb86cdSEric Blake if (!s->buf_align) { 324c1bb86cdSEric Blake size_t align; 325c1bb86cdSEric Blake buf = qemu_memalign(max_align, 2 * max_align); 326c1bb86cdSEric Blake for (align = 512; align <= max_align; align <<= 1) { 327c1bb86cdSEric Blake if (raw_is_io_aligned(fd, buf + align, max_align)) { 328c1bb86cdSEric Blake s->buf_align = align; 329c1bb86cdSEric Blake break; 330c1bb86cdSEric Blake } 331c1bb86cdSEric Blake } 332c1bb86cdSEric Blake qemu_vfree(buf); 333c1bb86cdSEric Blake } 334c1bb86cdSEric Blake 335c1bb86cdSEric Blake if (!bs->bl.request_alignment) { 336c1bb86cdSEric Blake size_t align; 337c1bb86cdSEric Blake buf = qemu_memalign(s->buf_align, max_align); 338c1bb86cdSEric Blake for (align = 512; align <= max_align; align <<= 1) { 339c1bb86cdSEric Blake if (raw_is_io_aligned(fd, buf, align)) { 340c1bb86cdSEric Blake bs->bl.request_alignment = align; 341c1bb86cdSEric Blake break; 342c1bb86cdSEric Blake } 343c1bb86cdSEric Blake } 344c1bb86cdSEric Blake qemu_vfree(buf); 345c1bb86cdSEric Blake } 346c1bb86cdSEric Blake 347c1bb86cdSEric Blake if (!s->buf_align || !bs->bl.request_alignment) { 348c1bb86cdSEric Blake error_setg(errp, "Could not find working O_DIRECT alignment"); 349c1bb86cdSEric Blake error_append_hint(errp, "Try cache.direct=off\n"); 350c1bb86cdSEric Blake } 351c1bb86cdSEric Blake } 352c1bb86cdSEric Blake 353c1bb86cdSEric Blake static void raw_parse_flags(int bdrv_flags, int *open_flags) 354c1bb86cdSEric Blake { 355c1bb86cdSEric Blake assert(open_flags != NULL); 356c1bb86cdSEric Blake 357c1bb86cdSEric Blake *open_flags |= O_BINARY; 358c1bb86cdSEric Blake *open_flags &= ~O_ACCMODE; 359c1bb86cdSEric Blake if (bdrv_flags & BDRV_O_RDWR) { 360c1bb86cdSEric Blake *open_flags |= O_RDWR; 361c1bb86cdSEric Blake } else { 362c1bb86cdSEric Blake *open_flags |= O_RDONLY; 363c1bb86cdSEric Blake } 364c1bb86cdSEric Blake 365c1bb86cdSEric Blake /* Use O_DSYNC for write-through caching, no flags for write-back caching, 366c1bb86cdSEric Blake * and O_DIRECT for no caching. */ 367c1bb86cdSEric Blake if ((bdrv_flags & BDRV_O_NOCACHE)) { 368c1bb86cdSEric Blake *open_flags |= O_DIRECT; 369c1bb86cdSEric Blake } 370c1bb86cdSEric Blake } 371c1bb86cdSEric Blake 372c1bb86cdSEric Blake static void raw_parse_filename(const char *filename, QDict *options, 373c1bb86cdSEric Blake Error **errp) 374c1bb86cdSEric Blake { 375c1bb86cdSEric Blake /* The filename does not have to be prefixed by the protocol name, since 376c1bb86cdSEric Blake * "file" is the default protocol; therefore, the return value of this 377c1bb86cdSEric Blake * function call can be ignored. */ 378c1bb86cdSEric Blake strstart(filename, "file:", &filename); 379c1bb86cdSEric Blake 380c1bb86cdSEric Blake qdict_put_obj(options, "filename", QOBJECT(qstring_from_str(filename))); 381c1bb86cdSEric Blake } 382c1bb86cdSEric Blake 383c1bb86cdSEric Blake static QemuOptsList raw_runtime_opts = { 384c1bb86cdSEric Blake .name = "raw", 385c1bb86cdSEric Blake .head = QTAILQ_HEAD_INITIALIZER(raw_runtime_opts.head), 386c1bb86cdSEric Blake .desc = { 387c1bb86cdSEric Blake { 388c1bb86cdSEric Blake .name = "filename", 389c1bb86cdSEric Blake .type = QEMU_OPT_STRING, 390c1bb86cdSEric Blake .help = "File name of the image", 391c1bb86cdSEric Blake }, 392c1bb86cdSEric Blake { 393c1bb86cdSEric Blake .name = "aio", 394c1bb86cdSEric Blake .type = QEMU_OPT_STRING, 395c1bb86cdSEric Blake .help = "host AIO implementation (threads, native)", 396c1bb86cdSEric Blake }, 397c1bb86cdSEric Blake { /* end of list */ } 398c1bb86cdSEric Blake }, 399c1bb86cdSEric Blake }; 400c1bb86cdSEric Blake 401c1bb86cdSEric Blake static int raw_open_common(BlockDriverState *bs, QDict *options, 402c1bb86cdSEric Blake int bdrv_flags, int open_flags, Error **errp) 403c1bb86cdSEric Blake { 404c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 405c1bb86cdSEric Blake QemuOpts *opts; 406c1bb86cdSEric Blake Error *local_err = NULL; 407c1bb86cdSEric Blake const char *filename = NULL; 408c1bb86cdSEric Blake BlockdevAioOptions aio, aio_default; 409c1bb86cdSEric Blake int fd, ret; 410c1bb86cdSEric Blake struct stat st; 411c1bb86cdSEric Blake 412c1bb86cdSEric Blake opts = qemu_opts_create(&raw_runtime_opts, NULL, 0, &error_abort); 413c1bb86cdSEric Blake qemu_opts_absorb_qdict(opts, options, &local_err); 414c1bb86cdSEric Blake if (local_err) { 415c1bb86cdSEric Blake error_propagate(errp, local_err); 416c1bb86cdSEric Blake ret = -EINVAL; 417c1bb86cdSEric Blake goto fail; 418c1bb86cdSEric Blake } 419c1bb86cdSEric Blake 420c1bb86cdSEric Blake filename = qemu_opt_get(opts, "filename"); 421c1bb86cdSEric Blake 422c1bb86cdSEric Blake ret = raw_normalize_devicepath(&filename); 423c1bb86cdSEric Blake if (ret != 0) { 424c1bb86cdSEric Blake error_setg_errno(errp, -ret, "Could not normalize device path"); 425c1bb86cdSEric Blake goto fail; 426c1bb86cdSEric Blake } 427c1bb86cdSEric Blake 428c1bb86cdSEric Blake aio_default = (bdrv_flags & BDRV_O_NATIVE_AIO) 429c1bb86cdSEric Blake ? BLOCKDEV_AIO_OPTIONS_NATIVE 430c1bb86cdSEric Blake : BLOCKDEV_AIO_OPTIONS_THREADS; 431c1bb86cdSEric Blake aio = qapi_enum_parse(BlockdevAioOptions_lookup, qemu_opt_get(opts, "aio"), 432c1bb86cdSEric Blake BLOCKDEV_AIO_OPTIONS__MAX, aio_default, &local_err); 433c1bb86cdSEric Blake if (local_err) { 434c1bb86cdSEric Blake error_propagate(errp, local_err); 435c1bb86cdSEric Blake ret = -EINVAL; 436c1bb86cdSEric Blake goto fail; 437c1bb86cdSEric Blake } 438c1bb86cdSEric Blake s->use_linux_aio = (aio == BLOCKDEV_AIO_OPTIONS_NATIVE); 439c1bb86cdSEric Blake 440c1bb86cdSEric Blake s->open_flags = open_flags; 441c1bb86cdSEric Blake raw_parse_flags(bdrv_flags, &s->open_flags); 442c1bb86cdSEric Blake 443c1bb86cdSEric Blake s->fd = -1; 444c1bb86cdSEric Blake fd = qemu_open(filename, s->open_flags, 0644); 445c1bb86cdSEric Blake if (fd < 0) { 446c1bb86cdSEric Blake ret = -errno; 447c1bb86cdSEric Blake error_setg_errno(errp, errno, "Could not open '%s'", filename); 448c1bb86cdSEric Blake if (ret == -EROFS) { 449c1bb86cdSEric Blake ret = -EACCES; 450c1bb86cdSEric Blake } 451c1bb86cdSEric Blake goto fail; 452c1bb86cdSEric Blake } 453c1bb86cdSEric Blake s->fd = fd; 454c1bb86cdSEric Blake 455c1bb86cdSEric Blake #ifdef CONFIG_LINUX_AIO 456c1bb86cdSEric Blake /* Currently Linux does AIO only for files opened with O_DIRECT */ 457c1bb86cdSEric Blake if (s->use_linux_aio && !(s->open_flags & O_DIRECT)) { 458c1bb86cdSEric Blake error_setg(errp, "aio=native was specified, but it requires " 459c1bb86cdSEric Blake "cache.direct=on, which was not specified."); 460c1bb86cdSEric Blake ret = -EINVAL; 461c1bb86cdSEric Blake goto fail; 462c1bb86cdSEric Blake } 463c1bb86cdSEric Blake #else 464c1bb86cdSEric Blake if (s->use_linux_aio) { 465c1bb86cdSEric Blake error_setg(errp, "aio=native was specified, but is not supported " 466c1bb86cdSEric Blake "in this build."); 467c1bb86cdSEric Blake ret = -EINVAL; 468c1bb86cdSEric Blake goto fail; 469c1bb86cdSEric Blake } 470c1bb86cdSEric Blake #endif /* !defined(CONFIG_LINUX_AIO) */ 471c1bb86cdSEric Blake 472c1bb86cdSEric Blake s->has_discard = true; 473c1bb86cdSEric Blake s->has_write_zeroes = true; 474c1bb86cdSEric Blake bs->supported_zero_flags = BDRV_REQ_MAY_UNMAP; 475c1bb86cdSEric Blake if ((bs->open_flags & BDRV_O_NOCACHE) != 0) { 476c1bb86cdSEric Blake s->needs_alignment = true; 477c1bb86cdSEric Blake } 478c1bb86cdSEric Blake 479c1bb86cdSEric Blake if (fstat(s->fd, &st) < 0) { 480c1bb86cdSEric Blake ret = -errno; 481c1bb86cdSEric Blake error_setg_errno(errp, errno, "Could not stat file"); 482c1bb86cdSEric Blake goto fail; 483c1bb86cdSEric Blake } 484c1bb86cdSEric Blake if (S_ISREG(st.st_mode)) { 485c1bb86cdSEric Blake s->discard_zeroes = true; 486c1bb86cdSEric Blake s->has_fallocate = true; 487c1bb86cdSEric Blake } 488c1bb86cdSEric Blake if (S_ISBLK(st.st_mode)) { 489c1bb86cdSEric Blake #ifdef BLKDISCARDZEROES 490c1bb86cdSEric Blake unsigned int arg; 491c1bb86cdSEric Blake if (ioctl(s->fd, BLKDISCARDZEROES, &arg) == 0 && arg) { 492c1bb86cdSEric Blake s->discard_zeroes = true; 493c1bb86cdSEric Blake } 494c1bb86cdSEric Blake #endif 495c1bb86cdSEric Blake #ifdef __linux__ 496c1bb86cdSEric Blake /* On Linux 3.10, BLKDISCARD leaves stale data in the page cache. Do 497c1bb86cdSEric Blake * not rely on the contents of discarded blocks unless using O_DIRECT. 498c1bb86cdSEric Blake * Same for BLKZEROOUT. 499c1bb86cdSEric Blake */ 500c1bb86cdSEric Blake if (!(bs->open_flags & BDRV_O_NOCACHE)) { 501c1bb86cdSEric Blake s->discard_zeroes = false; 502c1bb86cdSEric Blake s->has_write_zeroes = false; 503c1bb86cdSEric Blake } 504c1bb86cdSEric Blake #endif 505c1bb86cdSEric Blake } 506c1bb86cdSEric Blake #ifdef __FreeBSD__ 507c1bb86cdSEric Blake if (S_ISCHR(st.st_mode)) { 508c1bb86cdSEric Blake /* 509c1bb86cdSEric Blake * The file is a char device (disk), which on FreeBSD isn't behind 510c1bb86cdSEric Blake * a pager, so force all requests to be aligned. This is needed 511c1bb86cdSEric Blake * so QEMU makes sure all IO operations on the device are aligned 512c1bb86cdSEric Blake * to sector size, or else FreeBSD will reject them with EINVAL. 513c1bb86cdSEric Blake */ 514c1bb86cdSEric Blake s->needs_alignment = true; 515c1bb86cdSEric Blake } 516c1bb86cdSEric Blake #endif 517c1bb86cdSEric Blake 518c1bb86cdSEric Blake #ifdef CONFIG_XFS 519c1bb86cdSEric Blake if (platform_test_xfs_fd(s->fd)) { 520c1bb86cdSEric Blake s->is_xfs = true; 521c1bb86cdSEric Blake } 522c1bb86cdSEric Blake #endif 523c1bb86cdSEric Blake 524c1bb86cdSEric Blake ret = 0; 525c1bb86cdSEric Blake fail: 526c1bb86cdSEric Blake if (filename && (bdrv_flags & BDRV_O_TEMPORARY)) { 527c1bb86cdSEric Blake unlink(filename); 528c1bb86cdSEric Blake } 529c1bb86cdSEric Blake qemu_opts_del(opts); 530c1bb86cdSEric Blake return ret; 531c1bb86cdSEric Blake } 532c1bb86cdSEric Blake 533c1bb86cdSEric Blake static int raw_open(BlockDriverState *bs, QDict *options, int flags, 534c1bb86cdSEric Blake Error **errp) 535c1bb86cdSEric Blake { 536c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 537c1bb86cdSEric Blake 538c1bb86cdSEric Blake s->type = FTYPE_FILE; 539c1bb86cdSEric Blake return raw_open_common(bs, options, flags, 0, errp); 540c1bb86cdSEric Blake } 541c1bb86cdSEric Blake 542c1bb86cdSEric Blake static int raw_reopen_prepare(BDRVReopenState *state, 543c1bb86cdSEric Blake BlockReopenQueue *queue, Error **errp) 544c1bb86cdSEric Blake { 545c1bb86cdSEric Blake BDRVRawState *s; 546c1bb86cdSEric Blake BDRVRawReopenState *rs; 547c1bb86cdSEric Blake int ret = 0; 548c1bb86cdSEric Blake Error *local_err = NULL; 549c1bb86cdSEric Blake 550c1bb86cdSEric Blake assert(state != NULL); 551c1bb86cdSEric Blake assert(state->bs != NULL); 552c1bb86cdSEric Blake 553c1bb86cdSEric Blake s = state->bs->opaque; 554c1bb86cdSEric Blake 555c1bb86cdSEric Blake state->opaque = g_new0(BDRVRawReopenState, 1); 556c1bb86cdSEric Blake rs = state->opaque; 557c1bb86cdSEric Blake 558c1bb86cdSEric Blake if (s->type == FTYPE_CD) { 559c1bb86cdSEric Blake rs->open_flags |= O_NONBLOCK; 560c1bb86cdSEric Blake } 561c1bb86cdSEric Blake 562c1bb86cdSEric Blake raw_parse_flags(state->flags, &rs->open_flags); 563c1bb86cdSEric Blake 564c1bb86cdSEric Blake rs->fd = -1; 565c1bb86cdSEric Blake 566c1bb86cdSEric Blake int fcntl_flags = O_APPEND | O_NONBLOCK; 567c1bb86cdSEric Blake #ifdef O_NOATIME 568c1bb86cdSEric Blake fcntl_flags |= O_NOATIME; 569c1bb86cdSEric Blake #endif 570c1bb86cdSEric Blake 571c1bb86cdSEric Blake #ifdef O_ASYNC 572c1bb86cdSEric Blake /* Not all operating systems have O_ASYNC, and those that don't 573c1bb86cdSEric Blake * will not let us track the state into rs->open_flags (typically 574c1bb86cdSEric Blake * you achieve the same effect with an ioctl, for example I_SETSIG 575c1bb86cdSEric Blake * on Solaris). But we do not use O_ASYNC, so that's fine. 576c1bb86cdSEric Blake */ 577c1bb86cdSEric Blake assert((s->open_flags & O_ASYNC) == 0); 578c1bb86cdSEric Blake #endif 579c1bb86cdSEric Blake 580c1bb86cdSEric Blake if ((rs->open_flags & ~fcntl_flags) == (s->open_flags & ~fcntl_flags)) { 581c1bb86cdSEric Blake /* dup the original fd */ 582c1bb86cdSEric Blake rs->fd = qemu_dup(s->fd); 583c1bb86cdSEric Blake if (rs->fd >= 0) { 584c1bb86cdSEric Blake ret = fcntl_setfl(rs->fd, rs->open_flags); 585c1bb86cdSEric Blake if (ret) { 586c1bb86cdSEric Blake qemu_close(rs->fd); 587c1bb86cdSEric Blake rs->fd = -1; 588c1bb86cdSEric Blake } 589c1bb86cdSEric Blake } 590c1bb86cdSEric Blake } 591c1bb86cdSEric Blake 592c1bb86cdSEric Blake /* If we cannot use fcntl, or fcntl failed, fall back to qemu_open() */ 593c1bb86cdSEric Blake if (rs->fd == -1) { 594c1bb86cdSEric Blake const char *normalized_filename = state->bs->filename; 595c1bb86cdSEric Blake ret = raw_normalize_devicepath(&normalized_filename); 596c1bb86cdSEric Blake if (ret < 0) { 597c1bb86cdSEric Blake error_setg_errno(errp, -ret, "Could not normalize device path"); 598c1bb86cdSEric Blake } else { 599c1bb86cdSEric Blake assert(!(rs->open_flags & O_CREAT)); 600c1bb86cdSEric Blake rs->fd = qemu_open(normalized_filename, rs->open_flags); 601c1bb86cdSEric Blake if (rs->fd == -1) { 602c1bb86cdSEric Blake error_setg_errno(errp, errno, "Could not reopen file"); 603c1bb86cdSEric Blake ret = -1; 604c1bb86cdSEric Blake } 605c1bb86cdSEric Blake } 606c1bb86cdSEric Blake } 607c1bb86cdSEric Blake 608c1bb86cdSEric Blake /* Fail already reopen_prepare() if we can't get a working O_DIRECT 609c1bb86cdSEric Blake * alignment with the new fd. */ 610c1bb86cdSEric Blake if (rs->fd != -1) { 611c1bb86cdSEric Blake raw_probe_alignment(state->bs, rs->fd, &local_err); 612c1bb86cdSEric Blake if (local_err) { 613c1bb86cdSEric Blake qemu_close(rs->fd); 614c1bb86cdSEric Blake rs->fd = -1; 615c1bb86cdSEric Blake error_propagate(errp, local_err); 616c1bb86cdSEric Blake ret = -EINVAL; 617c1bb86cdSEric Blake } 618c1bb86cdSEric Blake } 619c1bb86cdSEric Blake 620c1bb86cdSEric Blake return ret; 621c1bb86cdSEric Blake } 622c1bb86cdSEric Blake 623c1bb86cdSEric Blake static void raw_reopen_commit(BDRVReopenState *state) 624c1bb86cdSEric Blake { 625c1bb86cdSEric Blake BDRVRawReopenState *rs = state->opaque; 626c1bb86cdSEric Blake BDRVRawState *s = state->bs->opaque; 627c1bb86cdSEric Blake 628c1bb86cdSEric Blake s->open_flags = rs->open_flags; 629c1bb86cdSEric Blake 630c1bb86cdSEric Blake qemu_close(s->fd); 631c1bb86cdSEric Blake s->fd = rs->fd; 632c1bb86cdSEric Blake 633c1bb86cdSEric Blake g_free(state->opaque); 634c1bb86cdSEric Blake state->opaque = NULL; 635c1bb86cdSEric Blake } 636c1bb86cdSEric Blake 637c1bb86cdSEric Blake 638c1bb86cdSEric Blake static void raw_reopen_abort(BDRVReopenState *state) 639c1bb86cdSEric Blake { 640c1bb86cdSEric Blake BDRVRawReopenState *rs = state->opaque; 641c1bb86cdSEric Blake 642c1bb86cdSEric Blake /* nothing to do if NULL, we didn't get far enough */ 643c1bb86cdSEric Blake if (rs == NULL) { 644c1bb86cdSEric Blake return; 645c1bb86cdSEric Blake } 646c1bb86cdSEric Blake 647c1bb86cdSEric Blake if (rs->fd >= 0) { 648c1bb86cdSEric Blake qemu_close(rs->fd); 649c1bb86cdSEric Blake rs->fd = -1; 650c1bb86cdSEric Blake } 651c1bb86cdSEric Blake g_free(state->opaque); 652c1bb86cdSEric Blake state->opaque = NULL; 653c1bb86cdSEric Blake } 654c1bb86cdSEric Blake 65548265250SEric Farman static int hdev_get_max_transfer_length(BlockDriverState *bs, int fd) 656c1bb86cdSEric Blake { 657c1bb86cdSEric Blake #ifdef BLKSECTGET 65848265250SEric Farman int max_bytes = 0; 65948265250SEric Farman short max_sectors = 0; 66048265250SEric Farman if (bs->sg && ioctl(fd, BLKSECTGET, &max_bytes) == 0) { 66148265250SEric Farman return max_bytes; 66248265250SEric Farman } else if (!bs->sg && ioctl(fd, BLKSECTGET, &max_sectors) == 0) { 66348265250SEric Farman return max_sectors << BDRV_SECTOR_BITS; 664c1bb86cdSEric Blake } else { 665c1bb86cdSEric Blake return -errno; 666c1bb86cdSEric Blake } 667c1bb86cdSEric Blake #else 668c1bb86cdSEric Blake return -ENOSYS; 669c1bb86cdSEric Blake #endif 670c1bb86cdSEric Blake } 671c1bb86cdSEric Blake 6729103f1ceSFam Zheng static int hdev_get_max_segments(const struct stat *st) 6739103f1ceSFam Zheng { 6749103f1ceSFam Zheng #ifdef CONFIG_LINUX 6759103f1ceSFam Zheng char buf[32]; 6769103f1ceSFam Zheng const char *end; 6779103f1ceSFam Zheng char *sysfspath; 6789103f1ceSFam Zheng int ret; 6799103f1ceSFam Zheng int fd = -1; 6809103f1ceSFam Zheng long max_segments; 6819103f1ceSFam Zheng 6829103f1ceSFam Zheng sysfspath = g_strdup_printf("/sys/dev/block/%u:%u/queue/max_segments", 6839103f1ceSFam Zheng major(st->st_rdev), minor(st->st_rdev)); 6849103f1ceSFam Zheng fd = open(sysfspath, O_RDONLY); 6859103f1ceSFam Zheng if (fd == -1) { 6869103f1ceSFam Zheng ret = -errno; 6879103f1ceSFam Zheng goto out; 6889103f1ceSFam Zheng } 6899103f1ceSFam Zheng do { 69069583490SStefan Hajnoczi ret = read(fd, buf, sizeof(buf) - 1); 6919103f1ceSFam Zheng } while (ret == -1 && errno == EINTR); 6929103f1ceSFam Zheng if (ret < 0) { 6939103f1ceSFam Zheng ret = -errno; 6949103f1ceSFam Zheng goto out; 6959103f1ceSFam Zheng } else if (ret == 0) { 6969103f1ceSFam Zheng ret = -EIO; 6979103f1ceSFam Zheng goto out; 6989103f1ceSFam Zheng } 6999103f1ceSFam Zheng buf[ret] = 0; 7009103f1ceSFam Zheng /* The file is ended with '\n', pass 'end' to accept that. */ 7019103f1ceSFam Zheng ret = qemu_strtol(buf, &end, 10, &max_segments); 7029103f1ceSFam Zheng if (ret == 0 && end && *end == '\n') { 7039103f1ceSFam Zheng ret = max_segments; 7049103f1ceSFam Zheng } 7059103f1ceSFam Zheng 7069103f1ceSFam Zheng out: 707fed414dfSFam Zheng if (fd != -1) { 708fed414dfSFam Zheng close(fd); 709fed414dfSFam Zheng } 7109103f1ceSFam Zheng g_free(sysfspath); 7119103f1ceSFam Zheng return ret; 7129103f1ceSFam Zheng #else 7139103f1ceSFam Zheng return -ENOTSUP; 7149103f1ceSFam Zheng #endif 7159103f1ceSFam Zheng } 7169103f1ceSFam Zheng 717c1bb86cdSEric Blake static void raw_refresh_limits(BlockDriverState *bs, Error **errp) 718c1bb86cdSEric Blake { 719c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 720c1bb86cdSEric Blake struct stat st; 721c1bb86cdSEric Blake 722c1bb86cdSEric Blake if (!fstat(s->fd, &st)) { 723c4c41a0aSEric Farman if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode)) { 72448265250SEric Farman int ret = hdev_get_max_transfer_length(bs, s->fd); 72548265250SEric Farman if (ret > 0 && ret <= BDRV_REQUEST_MAX_BYTES) { 72648265250SEric Farman bs->bl.max_transfer = pow2floor(ret); 727c1bb86cdSEric Blake } 7289103f1ceSFam Zheng ret = hdev_get_max_segments(&st); 7299103f1ceSFam Zheng if (ret > 0) { 7309103f1ceSFam Zheng bs->bl.max_transfer = MIN(bs->bl.max_transfer, 7319103f1ceSFam Zheng ret * getpagesize()); 7329103f1ceSFam Zheng } 733c1bb86cdSEric Blake } 734c1bb86cdSEric Blake } 735c1bb86cdSEric Blake 736c1bb86cdSEric Blake raw_probe_alignment(bs, s->fd, errp); 737c1bb86cdSEric Blake bs->bl.min_mem_alignment = s->buf_align; 738c1bb86cdSEric Blake bs->bl.opt_mem_alignment = MAX(s->buf_align, getpagesize()); 739c1bb86cdSEric Blake } 740c1bb86cdSEric Blake 741c1bb86cdSEric Blake static int check_for_dasd(int fd) 742c1bb86cdSEric Blake { 743c1bb86cdSEric Blake #ifdef BIODASDINFO2 744c1bb86cdSEric Blake struct dasd_information2_t info = {0}; 745c1bb86cdSEric Blake 746c1bb86cdSEric Blake return ioctl(fd, BIODASDINFO2, &info); 747c1bb86cdSEric Blake #else 748c1bb86cdSEric Blake return -1; 749c1bb86cdSEric Blake #endif 750c1bb86cdSEric Blake } 751c1bb86cdSEric Blake 752c1bb86cdSEric Blake /** 753c1bb86cdSEric Blake * Try to get @bs's logical and physical block size. 754c1bb86cdSEric Blake * On success, store them in @bsz and return zero. 755c1bb86cdSEric Blake * On failure, return negative errno. 756c1bb86cdSEric Blake */ 757c1bb86cdSEric Blake static int hdev_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz) 758c1bb86cdSEric Blake { 759c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 760c1bb86cdSEric Blake int ret; 761c1bb86cdSEric Blake 762c1bb86cdSEric Blake /* If DASD, get blocksizes */ 763c1bb86cdSEric Blake if (check_for_dasd(s->fd) < 0) { 764c1bb86cdSEric Blake return -ENOTSUP; 765c1bb86cdSEric Blake } 766c1bb86cdSEric Blake ret = probe_logical_blocksize(s->fd, &bsz->log); 767c1bb86cdSEric Blake if (ret < 0) { 768c1bb86cdSEric Blake return ret; 769c1bb86cdSEric Blake } 770c1bb86cdSEric Blake return probe_physical_blocksize(s->fd, &bsz->phys); 771c1bb86cdSEric Blake } 772c1bb86cdSEric Blake 773c1bb86cdSEric Blake /** 774c1bb86cdSEric Blake * Try to get @bs's geometry: cyls, heads, sectors. 775c1bb86cdSEric Blake * On success, store them in @geo and return 0. 776c1bb86cdSEric Blake * On failure return -errno. 777c1bb86cdSEric Blake * (Allows block driver to assign default geometry values that guest sees) 778c1bb86cdSEric Blake */ 779c1bb86cdSEric Blake #ifdef __linux__ 780c1bb86cdSEric Blake static int hdev_probe_geometry(BlockDriverState *bs, HDGeometry *geo) 781c1bb86cdSEric Blake { 782c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 783c1bb86cdSEric Blake struct hd_geometry ioctl_geo = {0}; 784c1bb86cdSEric Blake 785c1bb86cdSEric Blake /* If DASD, get its geometry */ 786c1bb86cdSEric Blake if (check_for_dasd(s->fd) < 0) { 787c1bb86cdSEric Blake return -ENOTSUP; 788c1bb86cdSEric Blake } 789c1bb86cdSEric Blake if (ioctl(s->fd, HDIO_GETGEO, &ioctl_geo) < 0) { 790c1bb86cdSEric Blake return -errno; 791c1bb86cdSEric Blake } 792c1bb86cdSEric Blake /* HDIO_GETGEO may return success even though geo contains zeros 793c1bb86cdSEric Blake (e.g. certain multipath setups) */ 794c1bb86cdSEric Blake if (!ioctl_geo.heads || !ioctl_geo.sectors || !ioctl_geo.cylinders) { 795c1bb86cdSEric Blake return -ENOTSUP; 796c1bb86cdSEric Blake } 797c1bb86cdSEric Blake /* Do not return a geometry for partition */ 798c1bb86cdSEric Blake if (ioctl_geo.start != 0) { 799c1bb86cdSEric Blake return -ENOTSUP; 800c1bb86cdSEric Blake } 801c1bb86cdSEric Blake geo->heads = ioctl_geo.heads; 802c1bb86cdSEric Blake geo->sectors = ioctl_geo.sectors; 803c1bb86cdSEric Blake geo->cylinders = ioctl_geo.cylinders; 804c1bb86cdSEric Blake 805c1bb86cdSEric Blake return 0; 806c1bb86cdSEric Blake } 807c1bb86cdSEric Blake #else /* __linux__ */ 808c1bb86cdSEric Blake static int hdev_probe_geometry(BlockDriverState *bs, HDGeometry *geo) 809c1bb86cdSEric Blake { 810c1bb86cdSEric Blake return -ENOTSUP; 811c1bb86cdSEric Blake } 812c1bb86cdSEric Blake #endif 813c1bb86cdSEric Blake 814c1bb86cdSEric Blake static ssize_t handle_aiocb_ioctl(RawPosixAIOData *aiocb) 815c1bb86cdSEric Blake { 816c1bb86cdSEric Blake int ret; 817c1bb86cdSEric Blake 818c1bb86cdSEric Blake ret = ioctl(aiocb->aio_fildes, aiocb->aio_ioctl_cmd, aiocb->aio_ioctl_buf); 819c1bb86cdSEric Blake if (ret == -1) { 820c1bb86cdSEric Blake return -errno; 821c1bb86cdSEric Blake } 822c1bb86cdSEric Blake 823c1bb86cdSEric Blake return 0; 824c1bb86cdSEric Blake } 825c1bb86cdSEric Blake 826c1bb86cdSEric Blake static ssize_t handle_aiocb_flush(RawPosixAIOData *aiocb) 827c1bb86cdSEric Blake { 828e5bcf967SKevin Wolf BDRVRawState *s = aiocb->bs->opaque; 829c1bb86cdSEric Blake int ret; 830c1bb86cdSEric Blake 831e5bcf967SKevin Wolf if (s->page_cache_inconsistent) { 832e5bcf967SKevin Wolf return -EIO; 833e5bcf967SKevin Wolf } 834e5bcf967SKevin Wolf 835c1bb86cdSEric Blake ret = qemu_fdatasync(aiocb->aio_fildes); 836c1bb86cdSEric Blake if (ret == -1) { 837e5bcf967SKevin Wolf /* There is no clear definition of the semantics of a failing fsync(), 838e5bcf967SKevin Wolf * so we may have to assume the worst. The sad truth is that this 839e5bcf967SKevin Wolf * assumption is correct for Linux. Some pages are now probably marked 840e5bcf967SKevin Wolf * clean in the page cache even though they are inconsistent with the 841e5bcf967SKevin Wolf * on-disk contents. The next fdatasync() call would succeed, but no 842e5bcf967SKevin Wolf * further writeback attempt will be made. We can't get back to a state 843e5bcf967SKevin Wolf * in which we know what is on disk (we would have to rewrite 844e5bcf967SKevin Wolf * everything that was touched since the last fdatasync() at least), so 845e5bcf967SKevin Wolf * make bdrv_flush() fail permanently. Given that the behaviour isn't 846e5bcf967SKevin Wolf * really defined, I have little hope that other OSes are doing better. 847e5bcf967SKevin Wolf * 848e5bcf967SKevin Wolf * Obviously, this doesn't affect O_DIRECT, which bypasses the page 849e5bcf967SKevin Wolf * cache. */ 850e5bcf967SKevin Wolf if ((s->open_flags & O_DIRECT) == 0) { 851e5bcf967SKevin Wolf s->page_cache_inconsistent = true; 852e5bcf967SKevin Wolf } 853c1bb86cdSEric Blake return -errno; 854c1bb86cdSEric Blake } 855c1bb86cdSEric Blake return 0; 856c1bb86cdSEric Blake } 857c1bb86cdSEric Blake 858c1bb86cdSEric Blake #ifdef CONFIG_PREADV 859c1bb86cdSEric Blake 860c1bb86cdSEric Blake static bool preadv_present = true; 861c1bb86cdSEric Blake 862c1bb86cdSEric Blake static ssize_t 863c1bb86cdSEric Blake qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset) 864c1bb86cdSEric Blake { 865c1bb86cdSEric Blake return preadv(fd, iov, nr_iov, offset); 866c1bb86cdSEric Blake } 867c1bb86cdSEric Blake 868c1bb86cdSEric Blake static ssize_t 869c1bb86cdSEric Blake qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset) 870c1bb86cdSEric Blake { 871c1bb86cdSEric Blake return pwritev(fd, iov, nr_iov, offset); 872c1bb86cdSEric Blake } 873c1bb86cdSEric Blake 874c1bb86cdSEric Blake #else 875c1bb86cdSEric Blake 876c1bb86cdSEric Blake static bool preadv_present = false; 877c1bb86cdSEric Blake 878c1bb86cdSEric Blake static ssize_t 879c1bb86cdSEric Blake qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset) 880c1bb86cdSEric Blake { 881c1bb86cdSEric Blake return -ENOSYS; 882c1bb86cdSEric Blake } 883c1bb86cdSEric Blake 884c1bb86cdSEric Blake static ssize_t 885c1bb86cdSEric Blake qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset) 886c1bb86cdSEric Blake { 887c1bb86cdSEric Blake return -ENOSYS; 888c1bb86cdSEric Blake } 889c1bb86cdSEric Blake 890c1bb86cdSEric Blake #endif 891c1bb86cdSEric Blake 892c1bb86cdSEric Blake static ssize_t handle_aiocb_rw_vector(RawPosixAIOData *aiocb) 893c1bb86cdSEric Blake { 894c1bb86cdSEric Blake ssize_t len; 895c1bb86cdSEric Blake 896c1bb86cdSEric Blake do { 897c1bb86cdSEric Blake if (aiocb->aio_type & QEMU_AIO_WRITE) 898c1bb86cdSEric Blake len = qemu_pwritev(aiocb->aio_fildes, 899c1bb86cdSEric Blake aiocb->aio_iov, 900c1bb86cdSEric Blake aiocb->aio_niov, 901c1bb86cdSEric Blake aiocb->aio_offset); 902c1bb86cdSEric Blake else 903c1bb86cdSEric Blake len = qemu_preadv(aiocb->aio_fildes, 904c1bb86cdSEric Blake aiocb->aio_iov, 905c1bb86cdSEric Blake aiocb->aio_niov, 906c1bb86cdSEric Blake aiocb->aio_offset); 907c1bb86cdSEric Blake } while (len == -1 && errno == EINTR); 908c1bb86cdSEric Blake 909c1bb86cdSEric Blake if (len == -1) { 910c1bb86cdSEric Blake return -errno; 911c1bb86cdSEric Blake } 912c1bb86cdSEric Blake return len; 913c1bb86cdSEric Blake } 914c1bb86cdSEric Blake 915c1bb86cdSEric Blake /* 916c1bb86cdSEric Blake * Read/writes the data to/from a given linear buffer. 917c1bb86cdSEric Blake * 918c1bb86cdSEric Blake * Returns the number of bytes handles or -errno in case of an error. Short 919c1bb86cdSEric Blake * reads are only returned if the end of the file is reached. 920c1bb86cdSEric Blake */ 921c1bb86cdSEric Blake static ssize_t handle_aiocb_rw_linear(RawPosixAIOData *aiocb, char *buf) 922c1bb86cdSEric Blake { 923c1bb86cdSEric Blake ssize_t offset = 0; 924c1bb86cdSEric Blake ssize_t len; 925c1bb86cdSEric Blake 926c1bb86cdSEric Blake while (offset < aiocb->aio_nbytes) { 927c1bb86cdSEric Blake if (aiocb->aio_type & QEMU_AIO_WRITE) { 928c1bb86cdSEric Blake len = pwrite(aiocb->aio_fildes, 929c1bb86cdSEric Blake (const char *)buf + offset, 930c1bb86cdSEric Blake aiocb->aio_nbytes - offset, 931c1bb86cdSEric Blake aiocb->aio_offset + offset); 932c1bb86cdSEric Blake } else { 933c1bb86cdSEric Blake len = pread(aiocb->aio_fildes, 934c1bb86cdSEric Blake buf + offset, 935c1bb86cdSEric Blake aiocb->aio_nbytes - offset, 936c1bb86cdSEric Blake aiocb->aio_offset + offset); 937c1bb86cdSEric Blake } 938c1bb86cdSEric Blake if (len == -1 && errno == EINTR) { 939c1bb86cdSEric Blake continue; 940c1bb86cdSEric Blake } else if (len == -1 && errno == EINVAL && 941c1bb86cdSEric Blake (aiocb->bs->open_flags & BDRV_O_NOCACHE) && 942c1bb86cdSEric Blake !(aiocb->aio_type & QEMU_AIO_WRITE) && 943c1bb86cdSEric Blake offset > 0) { 944c1bb86cdSEric Blake /* O_DIRECT pread() may fail with EINVAL when offset is unaligned 945c1bb86cdSEric Blake * after a short read. Assume that O_DIRECT short reads only occur 946c1bb86cdSEric Blake * at EOF. Therefore this is a short read, not an I/O error. 947c1bb86cdSEric Blake */ 948c1bb86cdSEric Blake break; 949c1bb86cdSEric Blake } else if (len == -1) { 950c1bb86cdSEric Blake offset = -errno; 951c1bb86cdSEric Blake break; 952c1bb86cdSEric Blake } else if (len == 0) { 953c1bb86cdSEric Blake break; 954c1bb86cdSEric Blake } 955c1bb86cdSEric Blake offset += len; 956c1bb86cdSEric Blake } 957c1bb86cdSEric Blake 958c1bb86cdSEric Blake return offset; 959c1bb86cdSEric Blake } 960c1bb86cdSEric Blake 961c1bb86cdSEric Blake static ssize_t handle_aiocb_rw(RawPosixAIOData *aiocb) 962c1bb86cdSEric Blake { 963c1bb86cdSEric Blake ssize_t nbytes; 964c1bb86cdSEric Blake char *buf; 965c1bb86cdSEric Blake 966c1bb86cdSEric Blake if (!(aiocb->aio_type & QEMU_AIO_MISALIGNED)) { 967c1bb86cdSEric Blake /* 968c1bb86cdSEric Blake * If there is just a single buffer, and it is properly aligned 969c1bb86cdSEric Blake * we can just use plain pread/pwrite without any problems. 970c1bb86cdSEric Blake */ 971c1bb86cdSEric Blake if (aiocb->aio_niov == 1) { 972c1bb86cdSEric Blake return handle_aiocb_rw_linear(aiocb, aiocb->aio_iov->iov_base); 973c1bb86cdSEric Blake } 974c1bb86cdSEric Blake /* 975c1bb86cdSEric Blake * We have more than one iovec, and all are properly aligned. 976c1bb86cdSEric Blake * 977c1bb86cdSEric Blake * Try preadv/pwritev first and fall back to linearizing the 978c1bb86cdSEric Blake * buffer if it's not supported. 979c1bb86cdSEric Blake */ 980c1bb86cdSEric Blake if (preadv_present) { 981c1bb86cdSEric Blake nbytes = handle_aiocb_rw_vector(aiocb); 982c1bb86cdSEric Blake if (nbytes == aiocb->aio_nbytes || 983c1bb86cdSEric Blake (nbytes < 0 && nbytes != -ENOSYS)) { 984c1bb86cdSEric Blake return nbytes; 985c1bb86cdSEric Blake } 986c1bb86cdSEric Blake preadv_present = false; 987c1bb86cdSEric Blake } 988c1bb86cdSEric Blake 989c1bb86cdSEric Blake /* 990c1bb86cdSEric Blake * XXX(hch): short read/write. no easy way to handle the reminder 991c1bb86cdSEric Blake * using these interfaces. For now retry using plain 992c1bb86cdSEric Blake * pread/pwrite? 993c1bb86cdSEric Blake */ 994c1bb86cdSEric Blake } 995c1bb86cdSEric Blake 996c1bb86cdSEric Blake /* 997c1bb86cdSEric Blake * Ok, we have to do it the hard way, copy all segments into 998c1bb86cdSEric Blake * a single aligned buffer. 999c1bb86cdSEric Blake */ 1000c1bb86cdSEric Blake buf = qemu_try_blockalign(aiocb->bs, aiocb->aio_nbytes); 1001c1bb86cdSEric Blake if (buf == NULL) { 1002c1bb86cdSEric Blake return -ENOMEM; 1003c1bb86cdSEric Blake } 1004c1bb86cdSEric Blake 1005c1bb86cdSEric Blake if (aiocb->aio_type & QEMU_AIO_WRITE) { 1006c1bb86cdSEric Blake char *p = buf; 1007c1bb86cdSEric Blake int i; 1008c1bb86cdSEric Blake 1009c1bb86cdSEric Blake for (i = 0; i < aiocb->aio_niov; ++i) { 1010c1bb86cdSEric Blake memcpy(p, aiocb->aio_iov[i].iov_base, aiocb->aio_iov[i].iov_len); 1011c1bb86cdSEric Blake p += aiocb->aio_iov[i].iov_len; 1012c1bb86cdSEric Blake } 1013c1bb86cdSEric Blake assert(p - buf == aiocb->aio_nbytes); 1014c1bb86cdSEric Blake } 1015c1bb86cdSEric Blake 1016c1bb86cdSEric Blake nbytes = handle_aiocb_rw_linear(aiocb, buf); 1017c1bb86cdSEric Blake if (!(aiocb->aio_type & QEMU_AIO_WRITE)) { 1018c1bb86cdSEric Blake char *p = buf; 1019c1bb86cdSEric Blake size_t count = aiocb->aio_nbytes, copy; 1020c1bb86cdSEric Blake int i; 1021c1bb86cdSEric Blake 1022c1bb86cdSEric Blake for (i = 0; i < aiocb->aio_niov && count; ++i) { 1023c1bb86cdSEric Blake copy = count; 1024c1bb86cdSEric Blake if (copy > aiocb->aio_iov[i].iov_len) { 1025c1bb86cdSEric Blake copy = aiocb->aio_iov[i].iov_len; 1026c1bb86cdSEric Blake } 1027c1bb86cdSEric Blake memcpy(aiocb->aio_iov[i].iov_base, p, copy); 1028c1bb86cdSEric Blake assert(count >= copy); 1029c1bb86cdSEric Blake p += copy; 1030c1bb86cdSEric Blake count -= copy; 1031c1bb86cdSEric Blake } 1032c1bb86cdSEric Blake assert(count == 0); 1033c1bb86cdSEric Blake } 1034c1bb86cdSEric Blake qemu_vfree(buf); 1035c1bb86cdSEric Blake 1036c1bb86cdSEric Blake return nbytes; 1037c1bb86cdSEric Blake } 1038c1bb86cdSEric Blake 1039c1bb86cdSEric Blake #ifdef CONFIG_XFS 1040c1bb86cdSEric Blake static int xfs_write_zeroes(BDRVRawState *s, int64_t offset, uint64_t bytes) 1041c1bb86cdSEric Blake { 1042c1bb86cdSEric Blake struct xfs_flock64 fl; 1043c1bb86cdSEric Blake int err; 1044c1bb86cdSEric Blake 1045c1bb86cdSEric Blake memset(&fl, 0, sizeof(fl)); 1046c1bb86cdSEric Blake fl.l_whence = SEEK_SET; 1047c1bb86cdSEric Blake fl.l_start = offset; 1048c1bb86cdSEric Blake fl.l_len = bytes; 1049c1bb86cdSEric Blake 1050c1bb86cdSEric Blake if (xfsctl(NULL, s->fd, XFS_IOC_ZERO_RANGE, &fl) < 0) { 1051c1bb86cdSEric Blake err = errno; 1052c1bb86cdSEric Blake DPRINTF("cannot write zero range (%s)\n", strerror(errno)); 1053c1bb86cdSEric Blake return -err; 1054c1bb86cdSEric Blake } 1055c1bb86cdSEric Blake 1056c1bb86cdSEric Blake return 0; 1057c1bb86cdSEric Blake } 1058c1bb86cdSEric Blake 1059c1bb86cdSEric Blake static int xfs_discard(BDRVRawState *s, int64_t offset, uint64_t bytes) 1060c1bb86cdSEric Blake { 1061c1bb86cdSEric Blake struct xfs_flock64 fl; 1062c1bb86cdSEric Blake int err; 1063c1bb86cdSEric Blake 1064c1bb86cdSEric Blake memset(&fl, 0, sizeof(fl)); 1065c1bb86cdSEric Blake fl.l_whence = SEEK_SET; 1066c1bb86cdSEric Blake fl.l_start = offset; 1067c1bb86cdSEric Blake fl.l_len = bytes; 1068c1bb86cdSEric Blake 1069c1bb86cdSEric Blake if (xfsctl(NULL, s->fd, XFS_IOC_UNRESVSP64, &fl) < 0) { 1070c1bb86cdSEric Blake err = errno; 1071c1bb86cdSEric Blake DPRINTF("cannot punch hole (%s)\n", strerror(errno)); 1072c1bb86cdSEric Blake return -err; 1073c1bb86cdSEric Blake } 1074c1bb86cdSEric Blake 1075c1bb86cdSEric Blake return 0; 1076c1bb86cdSEric Blake } 1077c1bb86cdSEric Blake #endif 1078c1bb86cdSEric Blake 1079c1bb86cdSEric Blake static int translate_err(int err) 1080c1bb86cdSEric Blake { 1081c1bb86cdSEric Blake if (err == -ENODEV || err == -ENOSYS || err == -EOPNOTSUPP || 1082c1bb86cdSEric Blake err == -ENOTTY) { 1083c1bb86cdSEric Blake err = -ENOTSUP; 1084c1bb86cdSEric Blake } 1085c1bb86cdSEric Blake return err; 1086c1bb86cdSEric Blake } 1087c1bb86cdSEric Blake 1088c1bb86cdSEric Blake #ifdef CONFIG_FALLOCATE 1089c1bb86cdSEric Blake static int do_fallocate(int fd, int mode, off_t offset, off_t len) 1090c1bb86cdSEric Blake { 1091c1bb86cdSEric Blake do { 1092c1bb86cdSEric Blake if (fallocate(fd, mode, offset, len) == 0) { 1093c1bb86cdSEric Blake return 0; 1094c1bb86cdSEric Blake } 1095c1bb86cdSEric Blake } while (errno == EINTR); 1096c1bb86cdSEric Blake return translate_err(-errno); 1097c1bb86cdSEric Blake } 1098c1bb86cdSEric Blake #endif 1099c1bb86cdSEric Blake 1100c1bb86cdSEric Blake static ssize_t handle_aiocb_write_zeroes_block(RawPosixAIOData *aiocb) 1101c1bb86cdSEric Blake { 1102c1bb86cdSEric Blake int ret = -ENOTSUP; 1103c1bb86cdSEric Blake BDRVRawState *s = aiocb->bs->opaque; 1104c1bb86cdSEric Blake 1105c1bb86cdSEric Blake if (!s->has_write_zeroes) { 1106c1bb86cdSEric Blake return -ENOTSUP; 1107c1bb86cdSEric Blake } 1108c1bb86cdSEric Blake 1109c1bb86cdSEric Blake #ifdef BLKZEROOUT 1110c1bb86cdSEric Blake do { 1111c1bb86cdSEric Blake uint64_t range[2] = { aiocb->aio_offset, aiocb->aio_nbytes }; 1112c1bb86cdSEric Blake if (ioctl(aiocb->aio_fildes, BLKZEROOUT, range) == 0) { 1113c1bb86cdSEric Blake return 0; 1114c1bb86cdSEric Blake } 1115c1bb86cdSEric Blake } while (errno == EINTR); 1116c1bb86cdSEric Blake 1117c1bb86cdSEric Blake ret = translate_err(-errno); 1118c1bb86cdSEric Blake #endif 1119c1bb86cdSEric Blake 1120c1bb86cdSEric Blake if (ret == -ENOTSUP) { 1121c1bb86cdSEric Blake s->has_write_zeroes = false; 1122c1bb86cdSEric Blake } 1123c1bb86cdSEric Blake return ret; 1124c1bb86cdSEric Blake } 1125c1bb86cdSEric Blake 1126c1bb86cdSEric Blake static ssize_t handle_aiocb_write_zeroes(RawPosixAIOData *aiocb) 1127c1bb86cdSEric Blake { 1128c1bb86cdSEric Blake #if defined(CONFIG_FALLOCATE) || defined(CONFIG_XFS) 1129c1bb86cdSEric Blake BDRVRawState *s = aiocb->bs->opaque; 1130c1bb86cdSEric Blake #endif 1131c1bb86cdSEric Blake 1132c1bb86cdSEric Blake if (aiocb->aio_type & QEMU_AIO_BLKDEV) { 1133c1bb86cdSEric Blake return handle_aiocb_write_zeroes_block(aiocb); 1134c1bb86cdSEric Blake } 1135c1bb86cdSEric Blake 1136c1bb86cdSEric Blake #ifdef CONFIG_XFS 1137c1bb86cdSEric Blake if (s->is_xfs) { 1138c1bb86cdSEric Blake return xfs_write_zeroes(s, aiocb->aio_offset, aiocb->aio_nbytes); 1139c1bb86cdSEric Blake } 1140c1bb86cdSEric Blake #endif 1141c1bb86cdSEric Blake 1142c1bb86cdSEric Blake #ifdef CONFIG_FALLOCATE_ZERO_RANGE 1143c1bb86cdSEric Blake if (s->has_write_zeroes) { 1144c1bb86cdSEric Blake int ret = do_fallocate(s->fd, FALLOC_FL_ZERO_RANGE, 1145c1bb86cdSEric Blake aiocb->aio_offset, aiocb->aio_nbytes); 1146c1bb86cdSEric Blake if (ret == 0 || ret != -ENOTSUP) { 1147c1bb86cdSEric Blake return ret; 1148c1bb86cdSEric Blake } 1149c1bb86cdSEric Blake s->has_write_zeroes = false; 1150c1bb86cdSEric Blake } 1151c1bb86cdSEric Blake #endif 1152c1bb86cdSEric Blake 1153c1bb86cdSEric Blake #ifdef CONFIG_FALLOCATE_PUNCH_HOLE 1154c1bb86cdSEric Blake if (s->has_discard && s->has_fallocate) { 1155c1bb86cdSEric Blake int ret = do_fallocate(s->fd, 1156c1bb86cdSEric Blake FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 1157c1bb86cdSEric Blake aiocb->aio_offset, aiocb->aio_nbytes); 1158c1bb86cdSEric Blake if (ret == 0) { 1159c1bb86cdSEric Blake ret = do_fallocate(s->fd, 0, aiocb->aio_offset, aiocb->aio_nbytes); 1160c1bb86cdSEric Blake if (ret == 0 || ret != -ENOTSUP) { 1161c1bb86cdSEric Blake return ret; 1162c1bb86cdSEric Blake } 1163c1bb86cdSEric Blake s->has_fallocate = false; 1164c1bb86cdSEric Blake } else if (ret != -ENOTSUP) { 1165c1bb86cdSEric Blake return ret; 1166c1bb86cdSEric Blake } else { 1167c1bb86cdSEric Blake s->has_discard = false; 1168c1bb86cdSEric Blake } 1169c1bb86cdSEric Blake } 1170c1bb86cdSEric Blake #endif 1171c1bb86cdSEric Blake 1172c1bb86cdSEric Blake #ifdef CONFIG_FALLOCATE 1173c1bb86cdSEric Blake if (s->has_fallocate && aiocb->aio_offset >= bdrv_getlength(aiocb->bs)) { 1174c1bb86cdSEric Blake int ret = do_fallocate(s->fd, 0, aiocb->aio_offset, aiocb->aio_nbytes); 1175c1bb86cdSEric Blake if (ret == 0 || ret != -ENOTSUP) { 1176c1bb86cdSEric Blake return ret; 1177c1bb86cdSEric Blake } 1178c1bb86cdSEric Blake s->has_fallocate = false; 1179c1bb86cdSEric Blake } 1180c1bb86cdSEric Blake #endif 1181c1bb86cdSEric Blake 1182c1bb86cdSEric Blake return -ENOTSUP; 1183c1bb86cdSEric Blake } 1184c1bb86cdSEric Blake 1185c1bb86cdSEric Blake static ssize_t handle_aiocb_discard(RawPosixAIOData *aiocb) 1186c1bb86cdSEric Blake { 1187c1bb86cdSEric Blake int ret = -EOPNOTSUPP; 1188c1bb86cdSEric Blake BDRVRawState *s = aiocb->bs->opaque; 1189c1bb86cdSEric Blake 1190c1bb86cdSEric Blake if (!s->has_discard) { 1191c1bb86cdSEric Blake return -ENOTSUP; 1192c1bb86cdSEric Blake } 1193c1bb86cdSEric Blake 1194c1bb86cdSEric Blake if (aiocb->aio_type & QEMU_AIO_BLKDEV) { 1195c1bb86cdSEric Blake #ifdef BLKDISCARD 1196c1bb86cdSEric Blake do { 1197c1bb86cdSEric Blake uint64_t range[2] = { aiocb->aio_offset, aiocb->aio_nbytes }; 1198c1bb86cdSEric Blake if (ioctl(aiocb->aio_fildes, BLKDISCARD, range) == 0) { 1199c1bb86cdSEric Blake return 0; 1200c1bb86cdSEric Blake } 1201c1bb86cdSEric Blake } while (errno == EINTR); 1202c1bb86cdSEric Blake 1203c1bb86cdSEric Blake ret = -errno; 1204c1bb86cdSEric Blake #endif 1205c1bb86cdSEric Blake } else { 1206c1bb86cdSEric Blake #ifdef CONFIG_XFS 1207c1bb86cdSEric Blake if (s->is_xfs) { 1208c1bb86cdSEric Blake return xfs_discard(s, aiocb->aio_offset, aiocb->aio_nbytes); 1209c1bb86cdSEric Blake } 1210c1bb86cdSEric Blake #endif 1211c1bb86cdSEric Blake 1212c1bb86cdSEric Blake #ifdef CONFIG_FALLOCATE_PUNCH_HOLE 1213c1bb86cdSEric Blake ret = do_fallocate(s->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 1214c1bb86cdSEric Blake aiocb->aio_offset, aiocb->aio_nbytes); 1215c1bb86cdSEric Blake #endif 1216c1bb86cdSEric Blake } 1217c1bb86cdSEric Blake 1218c1bb86cdSEric Blake ret = translate_err(ret); 1219c1bb86cdSEric Blake if (ret == -ENOTSUP) { 1220c1bb86cdSEric Blake s->has_discard = false; 1221c1bb86cdSEric Blake } 1222c1bb86cdSEric Blake return ret; 1223c1bb86cdSEric Blake } 1224c1bb86cdSEric Blake 1225c1bb86cdSEric Blake static int aio_worker(void *arg) 1226c1bb86cdSEric Blake { 1227c1bb86cdSEric Blake RawPosixAIOData *aiocb = arg; 1228c1bb86cdSEric Blake ssize_t ret = 0; 1229c1bb86cdSEric Blake 1230c1bb86cdSEric Blake switch (aiocb->aio_type & QEMU_AIO_TYPE_MASK) { 1231c1bb86cdSEric Blake case QEMU_AIO_READ: 1232c1bb86cdSEric Blake ret = handle_aiocb_rw(aiocb); 1233c1bb86cdSEric Blake if (ret >= 0 && ret < aiocb->aio_nbytes) { 1234c1bb86cdSEric Blake iov_memset(aiocb->aio_iov, aiocb->aio_niov, ret, 1235c1bb86cdSEric Blake 0, aiocb->aio_nbytes - ret); 1236c1bb86cdSEric Blake 1237c1bb86cdSEric Blake ret = aiocb->aio_nbytes; 1238c1bb86cdSEric Blake } 1239c1bb86cdSEric Blake if (ret == aiocb->aio_nbytes) { 1240c1bb86cdSEric Blake ret = 0; 1241c1bb86cdSEric Blake } else if (ret >= 0 && ret < aiocb->aio_nbytes) { 1242c1bb86cdSEric Blake ret = -EINVAL; 1243c1bb86cdSEric Blake } 1244c1bb86cdSEric Blake break; 1245c1bb86cdSEric Blake case QEMU_AIO_WRITE: 1246c1bb86cdSEric Blake ret = handle_aiocb_rw(aiocb); 1247c1bb86cdSEric Blake if (ret == aiocb->aio_nbytes) { 1248c1bb86cdSEric Blake ret = 0; 1249c1bb86cdSEric Blake } else if (ret >= 0 && ret < aiocb->aio_nbytes) { 1250c1bb86cdSEric Blake ret = -EINVAL; 1251c1bb86cdSEric Blake } 1252c1bb86cdSEric Blake break; 1253c1bb86cdSEric Blake case QEMU_AIO_FLUSH: 1254c1bb86cdSEric Blake ret = handle_aiocb_flush(aiocb); 1255c1bb86cdSEric Blake break; 1256c1bb86cdSEric Blake case QEMU_AIO_IOCTL: 1257c1bb86cdSEric Blake ret = handle_aiocb_ioctl(aiocb); 1258c1bb86cdSEric Blake break; 1259c1bb86cdSEric Blake case QEMU_AIO_DISCARD: 1260c1bb86cdSEric Blake ret = handle_aiocb_discard(aiocb); 1261c1bb86cdSEric Blake break; 1262c1bb86cdSEric Blake case QEMU_AIO_WRITE_ZEROES: 1263c1bb86cdSEric Blake ret = handle_aiocb_write_zeroes(aiocb); 1264c1bb86cdSEric Blake break; 1265c1bb86cdSEric Blake default: 1266c1bb86cdSEric Blake fprintf(stderr, "invalid aio request (0x%x)\n", aiocb->aio_type); 1267c1bb86cdSEric Blake ret = -EINVAL; 1268c1bb86cdSEric Blake break; 1269c1bb86cdSEric Blake } 1270c1bb86cdSEric Blake 1271c1bb86cdSEric Blake g_free(aiocb); 1272c1bb86cdSEric Blake return ret; 1273c1bb86cdSEric Blake } 1274c1bb86cdSEric Blake 1275c1bb86cdSEric Blake static int paio_submit_co(BlockDriverState *bs, int fd, 1276c1bb86cdSEric Blake int64_t offset, QEMUIOVector *qiov, 1277c1bb86cdSEric Blake int count, int type) 1278c1bb86cdSEric Blake { 1279c1bb86cdSEric Blake RawPosixAIOData *acb = g_new(RawPosixAIOData, 1); 1280c1bb86cdSEric Blake ThreadPool *pool; 1281c1bb86cdSEric Blake 1282c1bb86cdSEric Blake acb->bs = bs; 1283c1bb86cdSEric Blake acb->aio_type = type; 1284c1bb86cdSEric Blake acb->aio_fildes = fd; 1285c1bb86cdSEric Blake 1286c1bb86cdSEric Blake acb->aio_nbytes = count; 1287c1bb86cdSEric Blake acb->aio_offset = offset; 1288c1bb86cdSEric Blake 1289c1bb86cdSEric Blake if (qiov) { 1290c1bb86cdSEric Blake acb->aio_iov = qiov->iov; 1291c1bb86cdSEric Blake acb->aio_niov = qiov->niov; 1292c1bb86cdSEric Blake assert(qiov->size == count); 1293c1bb86cdSEric Blake } 1294c1bb86cdSEric Blake 1295c1bb86cdSEric Blake trace_paio_submit_co(offset, count, type); 1296c1bb86cdSEric Blake pool = aio_get_thread_pool(bdrv_get_aio_context(bs)); 1297c1bb86cdSEric Blake return thread_pool_submit_co(pool, aio_worker, acb); 1298c1bb86cdSEric Blake } 1299c1bb86cdSEric Blake 1300c1bb86cdSEric Blake static BlockAIOCB *paio_submit(BlockDriverState *bs, int fd, 1301c1bb86cdSEric Blake int64_t offset, QEMUIOVector *qiov, int count, 1302c1bb86cdSEric Blake BlockCompletionFunc *cb, void *opaque, int type) 1303c1bb86cdSEric Blake { 1304c1bb86cdSEric Blake RawPosixAIOData *acb = g_new(RawPosixAIOData, 1); 1305c1bb86cdSEric Blake ThreadPool *pool; 1306c1bb86cdSEric Blake 1307c1bb86cdSEric Blake acb->bs = bs; 1308c1bb86cdSEric Blake acb->aio_type = type; 1309c1bb86cdSEric Blake acb->aio_fildes = fd; 1310c1bb86cdSEric Blake 1311c1bb86cdSEric Blake acb->aio_nbytes = count; 1312c1bb86cdSEric Blake acb->aio_offset = offset; 1313c1bb86cdSEric Blake 1314c1bb86cdSEric Blake if (qiov) { 1315c1bb86cdSEric Blake acb->aio_iov = qiov->iov; 1316c1bb86cdSEric Blake acb->aio_niov = qiov->niov; 1317c1bb86cdSEric Blake assert(qiov->size == acb->aio_nbytes); 1318c1bb86cdSEric Blake } 1319c1bb86cdSEric Blake 1320c1bb86cdSEric Blake trace_paio_submit(acb, opaque, offset, count, type); 1321c1bb86cdSEric Blake pool = aio_get_thread_pool(bdrv_get_aio_context(bs)); 1322c1bb86cdSEric Blake return thread_pool_submit_aio(pool, aio_worker, acb, cb, opaque); 1323c1bb86cdSEric Blake } 1324c1bb86cdSEric Blake 1325c1bb86cdSEric Blake static int coroutine_fn raw_co_prw(BlockDriverState *bs, uint64_t offset, 1326c1bb86cdSEric Blake uint64_t bytes, QEMUIOVector *qiov, int type) 1327c1bb86cdSEric Blake { 1328c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 1329c1bb86cdSEric Blake 1330c1bb86cdSEric Blake if (fd_open(bs) < 0) 1331c1bb86cdSEric Blake return -EIO; 1332c1bb86cdSEric Blake 1333c1bb86cdSEric Blake /* 1334c1bb86cdSEric Blake * Check if the underlying device requires requests to be aligned, 1335c1bb86cdSEric Blake * and if the request we are trying to submit is aligned or not. 1336c1bb86cdSEric Blake * If this is the case tell the low-level driver that it needs 1337c1bb86cdSEric Blake * to copy the buffer. 1338c1bb86cdSEric Blake */ 1339c1bb86cdSEric Blake if (s->needs_alignment) { 1340c1bb86cdSEric Blake if (!bdrv_qiov_is_aligned(bs, qiov)) { 1341c1bb86cdSEric Blake type |= QEMU_AIO_MISALIGNED; 1342c1bb86cdSEric Blake #ifdef CONFIG_LINUX_AIO 1343c1bb86cdSEric Blake } else if (s->use_linux_aio) { 1344c1bb86cdSEric Blake LinuxAioState *aio = aio_get_linux_aio(bdrv_get_aio_context(bs)); 1345c1bb86cdSEric Blake assert(qiov->size == bytes); 1346c1bb86cdSEric Blake return laio_co_submit(bs, aio, s->fd, offset, qiov, type); 1347c1bb86cdSEric Blake #endif 1348c1bb86cdSEric Blake } 1349c1bb86cdSEric Blake } 1350c1bb86cdSEric Blake 1351c1bb86cdSEric Blake return paio_submit_co(bs, s->fd, offset, qiov, bytes, type); 1352c1bb86cdSEric Blake } 1353c1bb86cdSEric Blake 1354c1bb86cdSEric Blake static int coroutine_fn raw_co_preadv(BlockDriverState *bs, uint64_t offset, 1355c1bb86cdSEric Blake uint64_t bytes, QEMUIOVector *qiov, 1356c1bb86cdSEric Blake int flags) 1357c1bb86cdSEric Blake { 1358c1bb86cdSEric Blake return raw_co_prw(bs, offset, bytes, qiov, QEMU_AIO_READ); 1359c1bb86cdSEric Blake } 1360c1bb86cdSEric Blake 1361c1bb86cdSEric Blake static int coroutine_fn raw_co_pwritev(BlockDriverState *bs, uint64_t offset, 1362c1bb86cdSEric Blake uint64_t bytes, QEMUIOVector *qiov, 1363c1bb86cdSEric Blake int flags) 1364c1bb86cdSEric Blake { 1365c1bb86cdSEric Blake assert(flags == 0); 1366c1bb86cdSEric Blake return raw_co_prw(bs, offset, bytes, qiov, QEMU_AIO_WRITE); 1367c1bb86cdSEric Blake } 1368c1bb86cdSEric Blake 1369c1bb86cdSEric Blake static void raw_aio_plug(BlockDriverState *bs) 1370c1bb86cdSEric Blake { 1371c1bb86cdSEric Blake #ifdef CONFIG_LINUX_AIO 1372c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 1373c1bb86cdSEric Blake if (s->use_linux_aio) { 1374c1bb86cdSEric Blake LinuxAioState *aio = aio_get_linux_aio(bdrv_get_aio_context(bs)); 1375c1bb86cdSEric Blake laio_io_plug(bs, aio); 1376c1bb86cdSEric Blake } 1377c1bb86cdSEric Blake #endif 1378c1bb86cdSEric Blake } 1379c1bb86cdSEric Blake 1380c1bb86cdSEric Blake static void raw_aio_unplug(BlockDriverState *bs) 1381c1bb86cdSEric Blake { 1382c1bb86cdSEric Blake #ifdef CONFIG_LINUX_AIO 1383c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 1384c1bb86cdSEric Blake if (s->use_linux_aio) { 1385c1bb86cdSEric Blake LinuxAioState *aio = aio_get_linux_aio(bdrv_get_aio_context(bs)); 1386c1bb86cdSEric Blake laio_io_unplug(bs, aio); 1387c1bb86cdSEric Blake } 1388c1bb86cdSEric Blake #endif 1389c1bb86cdSEric Blake } 1390c1bb86cdSEric Blake 1391c1bb86cdSEric Blake static BlockAIOCB *raw_aio_flush(BlockDriverState *bs, 1392c1bb86cdSEric Blake BlockCompletionFunc *cb, void *opaque) 1393c1bb86cdSEric Blake { 1394c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 1395c1bb86cdSEric Blake 1396c1bb86cdSEric Blake if (fd_open(bs) < 0) 1397c1bb86cdSEric Blake return NULL; 1398c1bb86cdSEric Blake 1399c1bb86cdSEric Blake return paio_submit(bs, s->fd, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH); 1400c1bb86cdSEric Blake } 1401c1bb86cdSEric Blake 1402c1bb86cdSEric Blake static void raw_close(BlockDriverState *bs) 1403c1bb86cdSEric Blake { 1404c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 1405c1bb86cdSEric Blake 1406c1bb86cdSEric Blake if (s->fd >= 0) { 1407c1bb86cdSEric Blake qemu_close(s->fd); 1408c1bb86cdSEric Blake s->fd = -1; 1409c1bb86cdSEric Blake } 1410c1bb86cdSEric Blake } 1411c1bb86cdSEric Blake 1412c1bb86cdSEric Blake static int raw_truncate(BlockDriverState *bs, int64_t offset) 1413c1bb86cdSEric Blake { 1414c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 1415c1bb86cdSEric Blake struct stat st; 1416c1bb86cdSEric Blake 1417c1bb86cdSEric Blake if (fstat(s->fd, &st)) { 1418c1bb86cdSEric Blake return -errno; 1419c1bb86cdSEric Blake } 1420c1bb86cdSEric Blake 1421c1bb86cdSEric Blake if (S_ISREG(st.st_mode)) { 1422c1bb86cdSEric Blake if (ftruncate(s->fd, offset) < 0) { 1423c1bb86cdSEric Blake return -errno; 1424c1bb86cdSEric Blake } 1425c1bb86cdSEric Blake } else if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) { 1426c1bb86cdSEric Blake if (offset > raw_getlength(bs)) { 1427c1bb86cdSEric Blake return -EINVAL; 1428c1bb86cdSEric Blake } 1429c1bb86cdSEric Blake } else { 1430c1bb86cdSEric Blake return -ENOTSUP; 1431c1bb86cdSEric Blake } 1432c1bb86cdSEric Blake 1433c1bb86cdSEric Blake return 0; 1434c1bb86cdSEric Blake } 1435c1bb86cdSEric Blake 1436c1bb86cdSEric Blake #ifdef __OpenBSD__ 1437c1bb86cdSEric Blake static int64_t raw_getlength(BlockDriverState *bs) 1438c1bb86cdSEric Blake { 1439c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 1440c1bb86cdSEric Blake int fd = s->fd; 1441c1bb86cdSEric Blake struct stat st; 1442c1bb86cdSEric Blake 1443c1bb86cdSEric Blake if (fstat(fd, &st)) 1444c1bb86cdSEric Blake return -errno; 1445c1bb86cdSEric Blake if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) { 1446c1bb86cdSEric Blake struct disklabel dl; 1447c1bb86cdSEric Blake 1448c1bb86cdSEric Blake if (ioctl(fd, DIOCGDINFO, &dl)) 1449c1bb86cdSEric Blake return -errno; 1450c1bb86cdSEric Blake return (uint64_t)dl.d_secsize * 1451c1bb86cdSEric Blake dl.d_partitions[DISKPART(st.st_rdev)].p_size; 1452c1bb86cdSEric Blake } else 1453c1bb86cdSEric Blake return st.st_size; 1454c1bb86cdSEric Blake } 1455c1bb86cdSEric Blake #elif defined(__NetBSD__) 1456c1bb86cdSEric Blake static int64_t raw_getlength(BlockDriverState *bs) 1457c1bb86cdSEric Blake { 1458c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 1459c1bb86cdSEric Blake int fd = s->fd; 1460c1bb86cdSEric Blake struct stat st; 1461c1bb86cdSEric Blake 1462c1bb86cdSEric Blake if (fstat(fd, &st)) 1463c1bb86cdSEric Blake return -errno; 1464c1bb86cdSEric Blake if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) { 1465c1bb86cdSEric Blake struct dkwedge_info dkw; 1466c1bb86cdSEric Blake 1467c1bb86cdSEric Blake if (ioctl(fd, DIOCGWEDGEINFO, &dkw) != -1) { 1468c1bb86cdSEric Blake return dkw.dkw_size * 512; 1469c1bb86cdSEric Blake } else { 1470c1bb86cdSEric Blake struct disklabel dl; 1471c1bb86cdSEric Blake 1472c1bb86cdSEric Blake if (ioctl(fd, DIOCGDINFO, &dl)) 1473c1bb86cdSEric Blake return -errno; 1474c1bb86cdSEric Blake return (uint64_t)dl.d_secsize * 1475c1bb86cdSEric Blake dl.d_partitions[DISKPART(st.st_rdev)].p_size; 1476c1bb86cdSEric Blake } 1477c1bb86cdSEric Blake } else 1478c1bb86cdSEric Blake return st.st_size; 1479c1bb86cdSEric Blake } 1480c1bb86cdSEric Blake #elif defined(__sun__) 1481c1bb86cdSEric Blake static int64_t raw_getlength(BlockDriverState *bs) 1482c1bb86cdSEric Blake { 1483c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 1484c1bb86cdSEric Blake struct dk_minfo minfo; 1485c1bb86cdSEric Blake int ret; 1486c1bb86cdSEric Blake int64_t size; 1487c1bb86cdSEric Blake 1488c1bb86cdSEric Blake ret = fd_open(bs); 1489c1bb86cdSEric Blake if (ret < 0) { 1490c1bb86cdSEric Blake return ret; 1491c1bb86cdSEric Blake } 1492c1bb86cdSEric Blake 1493c1bb86cdSEric Blake /* 1494c1bb86cdSEric Blake * Use the DKIOCGMEDIAINFO ioctl to read the size. 1495c1bb86cdSEric Blake */ 1496c1bb86cdSEric Blake ret = ioctl(s->fd, DKIOCGMEDIAINFO, &minfo); 1497c1bb86cdSEric Blake if (ret != -1) { 1498c1bb86cdSEric Blake return minfo.dki_lbsize * minfo.dki_capacity; 1499c1bb86cdSEric Blake } 1500c1bb86cdSEric Blake 1501c1bb86cdSEric Blake /* 1502c1bb86cdSEric Blake * There are reports that lseek on some devices fails, but 1503c1bb86cdSEric Blake * irc discussion said that contingency on contingency was overkill. 1504c1bb86cdSEric Blake */ 1505c1bb86cdSEric Blake size = lseek(s->fd, 0, SEEK_END); 1506c1bb86cdSEric Blake if (size < 0) { 1507c1bb86cdSEric Blake return -errno; 1508c1bb86cdSEric Blake } 1509c1bb86cdSEric Blake return size; 1510c1bb86cdSEric Blake } 1511c1bb86cdSEric Blake #elif defined(CONFIG_BSD) 1512c1bb86cdSEric Blake static int64_t raw_getlength(BlockDriverState *bs) 1513c1bb86cdSEric Blake { 1514c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 1515c1bb86cdSEric Blake int fd = s->fd; 1516c1bb86cdSEric Blake int64_t size; 1517c1bb86cdSEric Blake struct stat sb; 1518c1bb86cdSEric Blake #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__) 1519c1bb86cdSEric Blake int reopened = 0; 1520c1bb86cdSEric Blake #endif 1521c1bb86cdSEric Blake int ret; 1522c1bb86cdSEric Blake 1523c1bb86cdSEric Blake ret = fd_open(bs); 1524c1bb86cdSEric Blake if (ret < 0) 1525c1bb86cdSEric Blake return ret; 1526c1bb86cdSEric Blake 1527c1bb86cdSEric Blake #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__) 1528c1bb86cdSEric Blake again: 1529c1bb86cdSEric Blake #endif 1530c1bb86cdSEric Blake if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) { 1531c1bb86cdSEric Blake #ifdef DIOCGMEDIASIZE 1532c1bb86cdSEric Blake if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size)) 1533c1bb86cdSEric Blake #elif defined(DIOCGPART) 1534c1bb86cdSEric Blake { 1535c1bb86cdSEric Blake struct partinfo pi; 1536c1bb86cdSEric Blake if (ioctl(fd, DIOCGPART, &pi) == 0) 1537c1bb86cdSEric Blake size = pi.media_size; 1538c1bb86cdSEric Blake else 1539c1bb86cdSEric Blake size = 0; 1540c1bb86cdSEric Blake } 1541c1bb86cdSEric Blake if (size == 0) 1542c1bb86cdSEric Blake #endif 1543c1bb86cdSEric Blake #if defined(__APPLE__) && defined(__MACH__) 1544c1bb86cdSEric Blake { 1545c1bb86cdSEric Blake uint64_t sectors = 0; 1546c1bb86cdSEric Blake uint32_t sector_size = 0; 1547c1bb86cdSEric Blake 1548c1bb86cdSEric Blake if (ioctl(fd, DKIOCGETBLOCKCOUNT, §ors) == 0 1549c1bb86cdSEric Blake && ioctl(fd, DKIOCGETBLOCKSIZE, §or_size) == 0) { 1550c1bb86cdSEric Blake size = sectors * sector_size; 1551c1bb86cdSEric Blake } else { 1552c1bb86cdSEric Blake size = lseek(fd, 0LL, SEEK_END); 1553c1bb86cdSEric Blake if (size < 0) { 1554c1bb86cdSEric Blake return -errno; 1555c1bb86cdSEric Blake } 1556c1bb86cdSEric Blake } 1557c1bb86cdSEric Blake } 1558c1bb86cdSEric Blake #else 1559c1bb86cdSEric Blake size = lseek(fd, 0LL, SEEK_END); 1560c1bb86cdSEric Blake if (size < 0) { 1561c1bb86cdSEric Blake return -errno; 1562c1bb86cdSEric Blake } 1563c1bb86cdSEric Blake #endif 1564c1bb86cdSEric Blake #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) 1565c1bb86cdSEric Blake switch(s->type) { 1566c1bb86cdSEric Blake case FTYPE_CD: 1567c1bb86cdSEric Blake /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */ 1568c1bb86cdSEric Blake if (size == 2048LL * (unsigned)-1) 1569c1bb86cdSEric Blake size = 0; 1570c1bb86cdSEric Blake /* XXX no disc? maybe we need to reopen... */ 1571c1bb86cdSEric Blake if (size <= 0 && !reopened && cdrom_reopen(bs) >= 0) { 1572c1bb86cdSEric Blake reopened = 1; 1573c1bb86cdSEric Blake goto again; 1574c1bb86cdSEric Blake } 1575c1bb86cdSEric Blake } 1576c1bb86cdSEric Blake #endif 1577c1bb86cdSEric Blake } else { 1578c1bb86cdSEric Blake size = lseek(fd, 0, SEEK_END); 1579c1bb86cdSEric Blake if (size < 0) { 1580c1bb86cdSEric Blake return -errno; 1581c1bb86cdSEric Blake } 1582c1bb86cdSEric Blake } 1583c1bb86cdSEric Blake return size; 1584c1bb86cdSEric Blake } 1585c1bb86cdSEric Blake #else 1586c1bb86cdSEric Blake static int64_t raw_getlength(BlockDriverState *bs) 1587c1bb86cdSEric Blake { 1588c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 1589c1bb86cdSEric Blake int ret; 1590c1bb86cdSEric Blake int64_t size; 1591c1bb86cdSEric Blake 1592c1bb86cdSEric Blake ret = fd_open(bs); 1593c1bb86cdSEric Blake if (ret < 0) { 1594c1bb86cdSEric Blake return ret; 1595c1bb86cdSEric Blake } 1596c1bb86cdSEric Blake 1597c1bb86cdSEric Blake size = lseek(s->fd, 0, SEEK_END); 1598c1bb86cdSEric Blake if (size < 0) { 1599c1bb86cdSEric Blake return -errno; 1600c1bb86cdSEric Blake } 1601c1bb86cdSEric Blake return size; 1602c1bb86cdSEric Blake } 1603c1bb86cdSEric Blake #endif 1604c1bb86cdSEric Blake 1605c1bb86cdSEric Blake static int64_t raw_get_allocated_file_size(BlockDriverState *bs) 1606c1bb86cdSEric Blake { 1607c1bb86cdSEric Blake struct stat st; 1608c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 1609c1bb86cdSEric Blake 1610c1bb86cdSEric Blake if (fstat(s->fd, &st) < 0) { 1611c1bb86cdSEric Blake return -errno; 1612c1bb86cdSEric Blake } 1613c1bb86cdSEric Blake return (int64_t)st.st_blocks * 512; 1614c1bb86cdSEric Blake } 1615c1bb86cdSEric Blake 1616c1bb86cdSEric Blake static int raw_create(const char *filename, QemuOpts *opts, Error **errp) 1617c1bb86cdSEric Blake { 1618c1bb86cdSEric Blake int fd; 1619c1bb86cdSEric Blake int result = 0; 1620c1bb86cdSEric Blake int64_t total_size = 0; 1621c1bb86cdSEric Blake bool nocow = false; 1622c1bb86cdSEric Blake PreallocMode prealloc; 1623c1bb86cdSEric Blake char *buf = NULL; 1624c1bb86cdSEric Blake Error *local_err = NULL; 1625c1bb86cdSEric Blake 1626c1bb86cdSEric Blake strstart(filename, "file:", &filename); 1627c1bb86cdSEric Blake 1628c1bb86cdSEric Blake /* Read out options */ 1629c1bb86cdSEric Blake total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), 1630c1bb86cdSEric Blake BDRV_SECTOR_SIZE); 1631c1bb86cdSEric Blake nocow = qemu_opt_get_bool(opts, BLOCK_OPT_NOCOW, false); 1632c1bb86cdSEric Blake buf = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC); 1633c1bb86cdSEric Blake prealloc = qapi_enum_parse(PreallocMode_lookup, buf, 1634c1bb86cdSEric Blake PREALLOC_MODE__MAX, PREALLOC_MODE_OFF, 1635c1bb86cdSEric Blake &local_err); 1636c1bb86cdSEric Blake g_free(buf); 1637c1bb86cdSEric Blake if (local_err) { 1638c1bb86cdSEric Blake error_propagate(errp, local_err); 1639c1bb86cdSEric Blake result = -EINVAL; 1640c1bb86cdSEric Blake goto out; 1641c1bb86cdSEric Blake } 1642c1bb86cdSEric Blake 1643c1bb86cdSEric Blake fd = qemu_open(filename, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 1644c1bb86cdSEric Blake 0644); 1645c1bb86cdSEric Blake if (fd < 0) { 1646c1bb86cdSEric Blake result = -errno; 1647c1bb86cdSEric Blake error_setg_errno(errp, -result, "Could not create file"); 1648c1bb86cdSEric Blake goto out; 1649c1bb86cdSEric Blake } 1650c1bb86cdSEric Blake 1651c1bb86cdSEric Blake if (nocow) { 1652c1bb86cdSEric Blake #ifdef __linux__ 1653c1bb86cdSEric Blake /* Set NOCOW flag to solve performance issue on fs like btrfs. 1654c1bb86cdSEric Blake * This is an optimisation. The FS_IOC_SETFLAGS ioctl return value 1655c1bb86cdSEric Blake * will be ignored since any failure of this operation should not 1656c1bb86cdSEric Blake * block the left work. 1657c1bb86cdSEric Blake */ 1658c1bb86cdSEric Blake int attr; 1659c1bb86cdSEric Blake if (ioctl(fd, FS_IOC_GETFLAGS, &attr) == 0) { 1660c1bb86cdSEric Blake attr |= FS_NOCOW_FL; 1661c1bb86cdSEric Blake ioctl(fd, FS_IOC_SETFLAGS, &attr); 1662c1bb86cdSEric Blake } 1663c1bb86cdSEric Blake #endif 1664c1bb86cdSEric Blake } 1665c1bb86cdSEric Blake 1666c1bb86cdSEric Blake switch (prealloc) { 1667c1bb86cdSEric Blake #ifdef CONFIG_POSIX_FALLOCATE 1668c1bb86cdSEric Blake case PREALLOC_MODE_FALLOC: 1669c6ccc2c5SNir Soffer /* 1670c6ccc2c5SNir Soffer * Truncating before posix_fallocate() makes it about twice slower on 1671c6ccc2c5SNir Soffer * file systems that do not support fallocate(), trying to check if a 1672c6ccc2c5SNir Soffer * block is allocated before allocating it, so don't do that here. 1673c6ccc2c5SNir Soffer */ 1674c1bb86cdSEric Blake result = -posix_fallocate(fd, 0, total_size); 1675c1bb86cdSEric Blake if (result != 0) { 1676c6ccc2c5SNir Soffer /* posix_fallocate() doesn't set errno. */ 1677c1bb86cdSEric Blake error_setg_errno(errp, -result, 1678c1bb86cdSEric Blake "Could not preallocate data for the new file"); 1679c1bb86cdSEric Blake } 1680c1bb86cdSEric Blake break; 1681c1bb86cdSEric Blake #endif 1682c1bb86cdSEric Blake case PREALLOC_MODE_FULL: 1683c1bb86cdSEric Blake { 16845a1dad9dSNir Soffer /* 16855a1dad9dSNir Soffer * Knowing the final size from the beginning could allow the file 16865a1dad9dSNir Soffer * system driver to do less allocations and possibly avoid 16875a1dad9dSNir Soffer * fragmentation of the file. 16885a1dad9dSNir Soffer */ 16895a1dad9dSNir Soffer if (ftruncate(fd, total_size) != 0) { 16905a1dad9dSNir Soffer result = -errno; 16915a1dad9dSNir Soffer error_setg_errno(errp, -result, "Could not resize file"); 16925a1dad9dSNir Soffer goto out_close; 16935a1dad9dSNir Soffer } 16945a1dad9dSNir Soffer 1695c1bb86cdSEric Blake int64_t num = 0, left = total_size; 1696c1bb86cdSEric Blake buf = g_malloc0(65536); 1697c1bb86cdSEric Blake 1698c1bb86cdSEric Blake while (left > 0) { 1699c1bb86cdSEric Blake num = MIN(left, 65536); 1700c1bb86cdSEric Blake result = write(fd, buf, num); 1701c1bb86cdSEric Blake if (result < 0) { 1702c1bb86cdSEric Blake result = -errno; 1703c1bb86cdSEric Blake error_setg_errno(errp, -result, 1704c1bb86cdSEric Blake "Could not write to the new file"); 1705c1bb86cdSEric Blake break; 1706c1bb86cdSEric Blake } 1707c1bb86cdSEric Blake left -= result; 1708c1bb86cdSEric Blake } 1709c1bb86cdSEric Blake if (result >= 0) { 1710c1bb86cdSEric Blake result = fsync(fd); 1711c1bb86cdSEric Blake if (result < 0) { 1712c1bb86cdSEric Blake result = -errno; 1713c1bb86cdSEric Blake error_setg_errno(errp, -result, 1714c1bb86cdSEric Blake "Could not flush new file to disk"); 1715c1bb86cdSEric Blake } 1716c1bb86cdSEric Blake } 1717c1bb86cdSEric Blake g_free(buf); 1718c1bb86cdSEric Blake break; 1719c1bb86cdSEric Blake } 1720c1bb86cdSEric Blake case PREALLOC_MODE_OFF: 1721f6a72404SNir Soffer if (ftruncate(fd, total_size) != 0) { 1722f6a72404SNir Soffer result = -errno; 1723f6a72404SNir Soffer error_setg_errno(errp, -result, "Could not resize file"); 1724f6a72404SNir Soffer } 1725c1bb86cdSEric Blake break; 1726c1bb86cdSEric Blake default: 1727c1bb86cdSEric Blake result = -EINVAL; 1728c1bb86cdSEric Blake error_setg(errp, "Unsupported preallocation mode: %s", 1729c1bb86cdSEric Blake PreallocMode_lookup[prealloc]); 1730c1bb86cdSEric Blake break; 1731c1bb86cdSEric Blake } 1732c1bb86cdSEric Blake 17335a1dad9dSNir Soffer out_close: 1734c1bb86cdSEric Blake if (qemu_close(fd) != 0 && result == 0) { 1735c1bb86cdSEric Blake result = -errno; 1736c1bb86cdSEric Blake error_setg_errno(errp, -result, "Could not close the new file"); 1737c1bb86cdSEric Blake } 1738c1bb86cdSEric Blake out: 1739c1bb86cdSEric Blake return result; 1740c1bb86cdSEric Blake } 1741c1bb86cdSEric Blake 1742c1bb86cdSEric Blake /* 1743c1bb86cdSEric Blake * Find allocation range in @bs around offset @start. 1744c1bb86cdSEric Blake * May change underlying file descriptor's file offset. 1745c1bb86cdSEric Blake * If @start is not in a hole, store @start in @data, and the 1746c1bb86cdSEric Blake * beginning of the next hole in @hole, and return 0. 1747c1bb86cdSEric Blake * If @start is in a non-trailing hole, store @start in @hole and the 1748c1bb86cdSEric Blake * beginning of the next non-hole in @data, and return 0. 1749c1bb86cdSEric Blake * If @start is in a trailing hole or beyond EOF, return -ENXIO. 1750c1bb86cdSEric Blake * If we can't find out, return a negative errno other than -ENXIO. 1751c1bb86cdSEric Blake */ 1752c1bb86cdSEric Blake static int find_allocation(BlockDriverState *bs, off_t start, 1753c1bb86cdSEric Blake off_t *data, off_t *hole) 1754c1bb86cdSEric Blake { 1755c1bb86cdSEric Blake #if defined SEEK_HOLE && defined SEEK_DATA 1756c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 1757c1bb86cdSEric Blake off_t offs; 1758c1bb86cdSEric Blake 1759c1bb86cdSEric Blake /* 1760c1bb86cdSEric Blake * SEEK_DATA cases: 1761c1bb86cdSEric Blake * D1. offs == start: start is in data 1762c1bb86cdSEric Blake * D2. offs > start: start is in a hole, next data at offs 1763c1bb86cdSEric Blake * D3. offs < 0, errno = ENXIO: either start is in a trailing hole 1764c1bb86cdSEric Blake * or start is beyond EOF 1765c1bb86cdSEric Blake * If the latter happens, the file has been truncated behind 1766c1bb86cdSEric Blake * our back since we opened it. All bets are off then. 1767c1bb86cdSEric Blake * Treating like a trailing hole is simplest. 1768c1bb86cdSEric Blake * D4. offs < 0, errno != ENXIO: we learned nothing 1769c1bb86cdSEric Blake */ 1770c1bb86cdSEric Blake offs = lseek(s->fd, start, SEEK_DATA); 1771c1bb86cdSEric Blake if (offs < 0) { 1772c1bb86cdSEric Blake return -errno; /* D3 or D4 */ 1773c1bb86cdSEric Blake } 1774c1bb86cdSEric Blake assert(offs >= start); 1775c1bb86cdSEric Blake 1776c1bb86cdSEric Blake if (offs > start) { 1777c1bb86cdSEric Blake /* D2: in hole, next data at offs */ 1778c1bb86cdSEric Blake *hole = start; 1779c1bb86cdSEric Blake *data = offs; 1780c1bb86cdSEric Blake return 0; 1781c1bb86cdSEric Blake } 1782c1bb86cdSEric Blake 1783c1bb86cdSEric Blake /* D1: in data, end not yet known */ 1784c1bb86cdSEric Blake 1785c1bb86cdSEric Blake /* 1786c1bb86cdSEric Blake * SEEK_HOLE cases: 1787c1bb86cdSEric Blake * H1. offs == start: start is in a hole 1788c1bb86cdSEric Blake * If this happens here, a hole has been dug behind our back 1789c1bb86cdSEric Blake * since the previous lseek(). 1790c1bb86cdSEric Blake * H2. offs > start: either start is in data, next hole at offs, 1791c1bb86cdSEric Blake * or start is in trailing hole, EOF at offs 1792c1bb86cdSEric Blake * Linux treats trailing holes like any other hole: offs == 1793c1bb86cdSEric Blake * start. Solaris seeks to EOF instead: offs > start (blech). 1794c1bb86cdSEric Blake * If that happens here, a hole has been dug behind our back 1795c1bb86cdSEric Blake * since the previous lseek(). 1796c1bb86cdSEric Blake * H3. offs < 0, errno = ENXIO: start is beyond EOF 1797c1bb86cdSEric Blake * If this happens, the file has been truncated behind our 1798c1bb86cdSEric Blake * back since we opened it. Treat it like a trailing hole. 1799c1bb86cdSEric Blake * H4. offs < 0, errno != ENXIO: we learned nothing 1800c1bb86cdSEric Blake * Pretend we know nothing at all, i.e. "forget" about D1. 1801c1bb86cdSEric Blake */ 1802c1bb86cdSEric Blake offs = lseek(s->fd, start, SEEK_HOLE); 1803c1bb86cdSEric Blake if (offs < 0) { 1804c1bb86cdSEric Blake return -errno; /* D1 and (H3 or H4) */ 1805c1bb86cdSEric Blake } 1806c1bb86cdSEric Blake assert(offs >= start); 1807c1bb86cdSEric Blake 1808c1bb86cdSEric Blake if (offs > start) { 1809c1bb86cdSEric Blake /* 1810c1bb86cdSEric Blake * D1 and H2: either in data, next hole at offs, or it was in 1811c1bb86cdSEric Blake * data but is now in a trailing hole. In the latter case, 1812c1bb86cdSEric Blake * all bets are off. Treating it as if it there was data all 1813c1bb86cdSEric Blake * the way to EOF is safe, so simply do that. 1814c1bb86cdSEric Blake */ 1815c1bb86cdSEric Blake *data = start; 1816c1bb86cdSEric Blake *hole = offs; 1817c1bb86cdSEric Blake return 0; 1818c1bb86cdSEric Blake } 1819c1bb86cdSEric Blake 1820c1bb86cdSEric Blake /* D1 and H1 */ 1821c1bb86cdSEric Blake return -EBUSY; 1822c1bb86cdSEric Blake #else 1823c1bb86cdSEric Blake return -ENOTSUP; 1824c1bb86cdSEric Blake #endif 1825c1bb86cdSEric Blake } 1826c1bb86cdSEric Blake 1827c1bb86cdSEric Blake /* 1828c1bb86cdSEric Blake * Returns the allocation status of the specified sectors. 1829c1bb86cdSEric Blake * 1830c1bb86cdSEric Blake * If 'sector_num' is beyond the end of the disk image the return value is 0 1831c1bb86cdSEric Blake * and 'pnum' is set to 0. 1832c1bb86cdSEric Blake * 1833c1bb86cdSEric Blake * 'pnum' is set to the number of sectors (including and immediately following 1834c1bb86cdSEric Blake * the specified sector) that are known to be in the same 1835c1bb86cdSEric Blake * allocated/unallocated state. 1836c1bb86cdSEric Blake * 1837c1bb86cdSEric Blake * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes 1838c1bb86cdSEric Blake * beyond the end of the disk image it will be clamped. 1839c1bb86cdSEric Blake */ 1840c1bb86cdSEric Blake static int64_t coroutine_fn raw_co_get_block_status(BlockDriverState *bs, 1841c1bb86cdSEric Blake int64_t sector_num, 1842c1bb86cdSEric Blake int nb_sectors, int *pnum, 1843c1bb86cdSEric Blake BlockDriverState **file) 1844c1bb86cdSEric Blake { 1845c1bb86cdSEric Blake off_t start, data = 0, hole = 0; 1846c1bb86cdSEric Blake int64_t total_size; 1847c1bb86cdSEric Blake int ret; 1848c1bb86cdSEric Blake 1849c1bb86cdSEric Blake ret = fd_open(bs); 1850c1bb86cdSEric Blake if (ret < 0) { 1851c1bb86cdSEric Blake return ret; 1852c1bb86cdSEric Blake } 1853c1bb86cdSEric Blake 1854c1bb86cdSEric Blake start = sector_num * BDRV_SECTOR_SIZE; 1855c1bb86cdSEric Blake total_size = bdrv_getlength(bs); 1856c1bb86cdSEric Blake if (total_size < 0) { 1857c1bb86cdSEric Blake return total_size; 1858c1bb86cdSEric Blake } else if (start >= total_size) { 1859c1bb86cdSEric Blake *pnum = 0; 1860c1bb86cdSEric Blake return 0; 1861c1bb86cdSEric Blake } else if (start + nb_sectors * BDRV_SECTOR_SIZE > total_size) { 1862c1bb86cdSEric Blake nb_sectors = DIV_ROUND_UP(total_size - start, BDRV_SECTOR_SIZE); 1863c1bb86cdSEric Blake } 1864c1bb86cdSEric Blake 1865c1bb86cdSEric Blake ret = find_allocation(bs, start, &data, &hole); 1866c1bb86cdSEric Blake if (ret == -ENXIO) { 1867c1bb86cdSEric Blake /* Trailing hole */ 1868c1bb86cdSEric Blake *pnum = nb_sectors; 1869c1bb86cdSEric Blake ret = BDRV_BLOCK_ZERO; 1870c1bb86cdSEric Blake } else if (ret < 0) { 1871c1bb86cdSEric Blake /* No info available, so pretend there are no holes */ 1872c1bb86cdSEric Blake *pnum = nb_sectors; 1873c1bb86cdSEric Blake ret = BDRV_BLOCK_DATA; 1874c1bb86cdSEric Blake } else if (data == start) { 1875c1bb86cdSEric Blake /* On a data extent, compute sectors to the end of the extent, 1876c1bb86cdSEric Blake * possibly including a partial sector at EOF. */ 1877c1bb86cdSEric Blake *pnum = MIN(nb_sectors, DIV_ROUND_UP(hole - start, BDRV_SECTOR_SIZE)); 1878c1bb86cdSEric Blake ret = BDRV_BLOCK_DATA; 1879c1bb86cdSEric Blake } else { 1880c1bb86cdSEric Blake /* On a hole, compute sectors to the beginning of the next extent. */ 1881c1bb86cdSEric Blake assert(hole == start); 1882c1bb86cdSEric Blake *pnum = MIN(nb_sectors, (data - start) / BDRV_SECTOR_SIZE); 1883c1bb86cdSEric Blake ret = BDRV_BLOCK_ZERO; 1884c1bb86cdSEric Blake } 1885c1bb86cdSEric Blake *file = bs; 1886c1bb86cdSEric Blake return ret | BDRV_BLOCK_OFFSET_VALID | start; 1887c1bb86cdSEric Blake } 1888c1bb86cdSEric Blake 1889c1bb86cdSEric Blake static coroutine_fn BlockAIOCB *raw_aio_pdiscard(BlockDriverState *bs, 1890c1bb86cdSEric Blake int64_t offset, int count, 1891c1bb86cdSEric Blake BlockCompletionFunc *cb, void *opaque) 1892c1bb86cdSEric Blake { 1893c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 1894c1bb86cdSEric Blake 1895c1bb86cdSEric Blake return paio_submit(bs, s->fd, offset, NULL, count, 1896c1bb86cdSEric Blake cb, opaque, QEMU_AIO_DISCARD); 1897c1bb86cdSEric Blake } 1898c1bb86cdSEric Blake 1899c1bb86cdSEric Blake static int coroutine_fn raw_co_pwrite_zeroes( 1900c1bb86cdSEric Blake BlockDriverState *bs, int64_t offset, 1901c1bb86cdSEric Blake int count, BdrvRequestFlags flags) 1902c1bb86cdSEric Blake { 1903c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 1904c1bb86cdSEric Blake 1905c1bb86cdSEric Blake if (!(flags & BDRV_REQ_MAY_UNMAP)) { 1906c1bb86cdSEric Blake return paio_submit_co(bs, s->fd, offset, NULL, count, 1907c1bb86cdSEric Blake QEMU_AIO_WRITE_ZEROES); 1908c1bb86cdSEric Blake } else if (s->discard_zeroes) { 1909c1bb86cdSEric Blake return paio_submit_co(bs, s->fd, offset, NULL, count, 1910c1bb86cdSEric Blake QEMU_AIO_DISCARD); 1911c1bb86cdSEric Blake } 1912c1bb86cdSEric Blake return -ENOTSUP; 1913c1bb86cdSEric Blake } 1914c1bb86cdSEric Blake 1915c1bb86cdSEric Blake static int raw_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 1916c1bb86cdSEric Blake { 1917c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 1918c1bb86cdSEric Blake 1919c1bb86cdSEric Blake bdi->unallocated_blocks_are_zero = s->discard_zeroes; 1920c1bb86cdSEric Blake bdi->can_write_zeroes_with_unmap = s->discard_zeroes; 1921c1bb86cdSEric Blake return 0; 1922c1bb86cdSEric Blake } 1923c1bb86cdSEric Blake 1924c1bb86cdSEric Blake static QemuOptsList raw_create_opts = { 1925c1bb86cdSEric Blake .name = "raw-create-opts", 1926c1bb86cdSEric Blake .head = QTAILQ_HEAD_INITIALIZER(raw_create_opts.head), 1927c1bb86cdSEric Blake .desc = { 1928c1bb86cdSEric Blake { 1929c1bb86cdSEric Blake .name = BLOCK_OPT_SIZE, 1930c1bb86cdSEric Blake .type = QEMU_OPT_SIZE, 1931c1bb86cdSEric Blake .help = "Virtual disk size" 1932c1bb86cdSEric Blake }, 1933c1bb86cdSEric Blake { 1934c1bb86cdSEric Blake .name = BLOCK_OPT_NOCOW, 1935c1bb86cdSEric Blake .type = QEMU_OPT_BOOL, 1936c1bb86cdSEric Blake .help = "Turn off copy-on-write (valid only on btrfs)" 1937c1bb86cdSEric Blake }, 1938c1bb86cdSEric Blake { 1939c1bb86cdSEric Blake .name = BLOCK_OPT_PREALLOC, 1940c1bb86cdSEric Blake .type = QEMU_OPT_STRING, 1941c1bb86cdSEric Blake .help = "Preallocation mode (allowed values: off, falloc, full)" 1942c1bb86cdSEric Blake }, 1943c1bb86cdSEric Blake { /* end of list */ } 1944c1bb86cdSEric Blake } 1945c1bb86cdSEric Blake }; 1946c1bb86cdSEric Blake 1947c1bb86cdSEric Blake BlockDriver bdrv_file = { 1948c1bb86cdSEric Blake .format_name = "file", 1949c1bb86cdSEric Blake .protocol_name = "file", 1950c1bb86cdSEric Blake .instance_size = sizeof(BDRVRawState), 1951c1bb86cdSEric Blake .bdrv_needs_filename = true, 1952c1bb86cdSEric Blake .bdrv_probe = NULL, /* no probe for protocols */ 1953c1bb86cdSEric Blake .bdrv_parse_filename = raw_parse_filename, 1954c1bb86cdSEric Blake .bdrv_file_open = raw_open, 1955c1bb86cdSEric Blake .bdrv_reopen_prepare = raw_reopen_prepare, 1956c1bb86cdSEric Blake .bdrv_reopen_commit = raw_reopen_commit, 1957c1bb86cdSEric Blake .bdrv_reopen_abort = raw_reopen_abort, 1958c1bb86cdSEric Blake .bdrv_close = raw_close, 1959c1bb86cdSEric Blake .bdrv_create = raw_create, 1960c1bb86cdSEric Blake .bdrv_has_zero_init = bdrv_has_zero_init_1, 1961c1bb86cdSEric Blake .bdrv_co_get_block_status = raw_co_get_block_status, 1962c1bb86cdSEric Blake .bdrv_co_pwrite_zeroes = raw_co_pwrite_zeroes, 1963c1bb86cdSEric Blake 1964c1bb86cdSEric Blake .bdrv_co_preadv = raw_co_preadv, 1965c1bb86cdSEric Blake .bdrv_co_pwritev = raw_co_pwritev, 1966c1bb86cdSEric Blake .bdrv_aio_flush = raw_aio_flush, 1967c1bb86cdSEric Blake .bdrv_aio_pdiscard = raw_aio_pdiscard, 1968c1bb86cdSEric Blake .bdrv_refresh_limits = raw_refresh_limits, 1969c1bb86cdSEric Blake .bdrv_io_plug = raw_aio_plug, 1970c1bb86cdSEric Blake .bdrv_io_unplug = raw_aio_unplug, 1971c1bb86cdSEric Blake 1972c1bb86cdSEric Blake .bdrv_truncate = raw_truncate, 1973c1bb86cdSEric Blake .bdrv_getlength = raw_getlength, 1974c1bb86cdSEric Blake .bdrv_get_info = raw_get_info, 1975c1bb86cdSEric Blake .bdrv_get_allocated_file_size 1976c1bb86cdSEric Blake = raw_get_allocated_file_size, 1977c1bb86cdSEric Blake 1978c1bb86cdSEric Blake .create_opts = &raw_create_opts, 1979c1bb86cdSEric Blake }; 1980c1bb86cdSEric Blake 1981c1bb86cdSEric Blake /***********************************************/ 1982c1bb86cdSEric Blake /* host device */ 1983c1bb86cdSEric Blake 1984c1bb86cdSEric Blake #if defined(__APPLE__) && defined(__MACH__) 1985c1bb86cdSEric Blake static kern_return_t GetBSDPath(io_iterator_t mediaIterator, char *bsdPath, 1986c1bb86cdSEric Blake CFIndex maxPathSize, int flags); 1987c1bb86cdSEric Blake static char *FindEjectableOpticalMedia(io_iterator_t *mediaIterator) 1988c1bb86cdSEric Blake { 1989c1bb86cdSEric Blake kern_return_t kernResult = KERN_FAILURE; 1990c1bb86cdSEric Blake mach_port_t masterPort; 1991c1bb86cdSEric Blake CFMutableDictionaryRef classesToMatch; 1992c1bb86cdSEric Blake const char *matching_array[] = {kIODVDMediaClass, kIOCDMediaClass}; 1993c1bb86cdSEric Blake char *mediaType = NULL; 1994c1bb86cdSEric Blake 1995c1bb86cdSEric Blake kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort ); 1996c1bb86cdSEric Blake if ( KERN_SUCCESS != kernResult ) { 1997c1bb86cdSEric Blake printf( "IOMasterPort returned %d\n", kernResult ); 1998c1bb86cdSEric Blake } 1999c1bb86cdSEric Blake 2000c1bb86cdSEric Blake int index; 2001c1bb86cdSEric Blake for (index = 0; index < ARRAY_SIZE(matching_array); index++) { 2002c1bb86cdSEric Blake classesToMatch = IOServiceMatching(matching_array[index]); 2003c1bb86cdSEric Blake if (classesToMatch == NULL) { 2004c1bb86cdSEric Blake error_report("IOServiceMatching returned NULL for %s", 2005c1bb86cdSEric Blake matching_array[index]); 2006c1bb86cdSEric Blake continue; 2007c1bb86cdSEric Blake } 2008c1bb86cdSEric Blake CFDictionarySetValue(classesToMatch, CFSTR(kIOMediaEjectableKey), 2009c1bb86cdSEric Blake kCFBooleanTrue); 2010c1bb86cdSEric Blake kernResult = IOServiceGetMatchingServices(masterPort, classesToMatch, 2011c1bb86cdSEric Blake mediaIterator); 2012c1bb86cdSEric Blake if (kernResult != KERN_SUCCESS) { 2013c1bb86cdSEric Blake error_report("Note: IOServiceGetMatchingServices returned %d", 2014c1bb86cdSEric Blake kernResult); 2015c1bb86cdSEric Blake continue; 2016c1bb86cdSEric Blake } 2017c1bb86cdSEric Blake 2018c1bb86cdSEric Blake /* If a match was found, leave the loop */ 2019c1bb86cdSEric Blake if (*mediaIterator != 0) { 2020c1bb86cdSEric Blake DPRINTF("Matching using %s\n", matching_array[index]); 2021c1bb86cdSEric Blake mediaType = g_strdup(matching_array[index]); 2022c1bb86cdSEric Blake break; 2023c1bb86cdSEric Blake } 2024c1bb86cdSEric Blake } 2025c1bb86cdSEric Blake return mediaType; 2026c1bb86cdSEric Blake } 2027c1bb86cdSEric Blake 2028c1bb86cdSEric Blake kern_return_t GetBSDPath(io_iterator_t mediaIterator, char *bsdPath, 2029c1bb86cdSEric Blake CFIndex maxPathSize, int flags) 2030c1bb86cdSEric Blake { 2031c1bb86cdSEric Blake io_object_t nextMedia; 2032c1bb86cdSEric Blake kern_return_t kernResult = KERN_FAILURE; 2033c1bb86cdSEric Blake *bsdPath = '\0'; 2034c1bb86cdSEric Blake nextMedia = IOIteratorNext( mediaIterator ); 2035c1bb86cdSEric Blake if ( nextMedia ) 2036c1bb86cdSEric Blake { 2037c1bb86cdSEric Blake CFTypeRef bsdPathAsCFString; 2038c1bb86cdSEric Blake bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 ); 2039c1bb86cdSEric Blake if ( bsdPathAsCFString ) { 2040c1bb86cdSEric Blake size_t devPathLength; 2041c1bb86cdSEric Blake strcpy( bsdPath, _PATH_DEV ); 2042c1bb86cdSEric Blake if (flags & BDRV_O_NOCACHE) { 2043c1bb86cdSEric Blake strcat(bsdPath, "r"); 2044c1bb86cdSEric Blake } 2045c1bb86cdSEric Blake devPathLength = strlen( bsdPath ); 2046c1bb86cdSEric Blake if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) { 2047c1bb86cdSEric Blake kernResult = KERN_SUCCESS; 2048c1bb86cdSEric Blake } 2049c1bb86cdSEric Blake CFRelease( bsdPathAsCFString ); 2050c1bb86cdSEric Blake } 2051c1bb86cdSEric Blake IOObjectRelease( nextMedia ); 2052c1bb86cdSEric Blake } 2053c1bb86cdSEric Blake 2054c1bb86cdSEric Blake return kernResult; 2055c1bb86cdSEric Blake } 2056c1bb86cdSEric Blake 2057c1bb86cdSEric Blake /* Sets up a real cdrom for use in QEMU */ 2058c1bb86cdSEric Blake static bool setup_cdrom(char *bsd_path, Error **errp) 2059c1bb86cdSEric Blake { 2060c1bb86cdSEric Blake int index, num_of_test_partitions = 2, fd; 2061c1bb86cdSEric Blake char test_partition[MAXPATHLEN]; 2062c1bb86cdSEric Blake bool partition_found = false; 2063c1bb86cdSEric Blake 2064c1bb86cdSEric Blake /* look for a working partition */ 2065c1bb86cdSEric Blake for (index = 0; index < num_of_test_partitions; index++) { 2066c1bb86cdSEric Blake snprintf(test_partition, sizeof(test_partition), "%ss%d", bsd_path, 2067c1bb86cdSEric Blake index); 2068c1bb86cdSEric Blake fd = qemu_open(test_partition, O_RDONLY | O_BINARY | O_LARGEFILE); 2069c1bb86cdSEric Blake if (fd >= 0) { 2070c1bb86cdSEric Blake partition_found = true; 2071c1bb86cdSEric Blake qemu_close(fd); 2072c1bb86cdSEric Blake break; 2073c1bb86cdSEric Blake } 2074c1bb86cdSEric Blake } 2075c1bb86cdSEric Blake 2076c1bb86cdSEric Blake /* if a working partition on the device was not found */ 2077c1bb86cdSEric Blake if (partition_found == false) { 2078c1bb86cdSEric Blake error_setg(errp, "Failed to find a working partition on disc"); 2079c1bb86cdSEric Blake } else { 2080c1bb86cdSEric Blake DPRINTF("Using %s as optical disc\n", test_partition); 2081c1bb86cdSEric Blake pstrcpy(bsd_path, MAXPATHLEN, test_partition); 2082c1bb86cdSEric Blake } 2083c1bb86cdSEric Blake return partition_found; 2084c1bb86cdSEric Blake } 2085c1bb86cdSEric Blake 2086c1bb86cdSEric Blake /* Prints directions on mounting and unmounting a device */ 2087c1bb86cdSEric Blake static void print_unmounting_directions(const char *file_name) 2088c1bb86cdSEric Blake { 2089c1bb86cdSEric Blake error_report("If device %s is mounted on the desktop, unmount" 2090c1bb86cdSEric Blake " it first before using it in QEMU", file_name); 2091c1bb86cdSEric Blake error_report("Command to unmount device: diskutil unmountDisk %s", 2092c1bb86cdSEric Blake file_name); 2093c1bb86cdSEric Blake error_report("Command to mount device: diskutil mountDisk %s", file_name); 2094c1bb86cdSEric Blake } 2095c1bb86cdSEric Blake 2096c1bb86cdSEric Blake #endif /* defined(__APPLE__) && defined(__MACH__) */ 2097c1bb86cdSEric Blake 2098c1bb86cdSEric Blake static int hdev_probe_device(const char *filename) 2099c1bb86cdSEric Blake { 2100c1bb86cdSEric Blake struct stat st; 2101c1bb86cdSEric Blake 2102c1bb86cdSEric Blake /* allow a dedicated CD-ROM driver to match with a higher priority */ 2103c1bb86cdSEric Blake if (strstart(filename, "/dev/cdrom", NULL)) 2104c1bb86cdSEric Blake return 50; 2105c1bb86cdSEric Blake 2106c1bb86cdSEric Blake if (stat(filename, &st) >= 0 && 2107c1bb86cdSEric Blake (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) { 2108c1bb86cdSEric Blake return 100; 2109c1bb86cdSEric Blake } 2110c1bb86cdSEric Blake 2111c1bb86cdSEric Blake return 0; 2112c1bb86cdSEric Blake } 2113c1bb86cdSEric Blake 2114c1bb86cdSEric Blake static int check_hdev_writable(BDRVRawState *s) 2115c1bb86cdSEric Blake { 2116c1bb86cdSEric Blake #if defined(BLKROGET) 2117c1bb86cdSEric Blake /* Linux block devices can be configured "read-only" using blockdev(8). 2118c1bb86cdSEric Blake * This is independent of device node permissions and therefore open(2) 2119c1bb86cdSEric Blake * with O_RDWR succeeds. Actual writes fail with EPERM. 2120c1bb86cdSEric Blake * 2121c1bb86cdSEric Blake * bdrv_open() is supposed to fail if the disk is read-only. Explicitly 2122c1bb86cdSEric Blake * check for read-only block devices so that Linux block devices behave 2123c1bb86cdSEric Blake * properly. 2124c1bb86cdSEric Blake */ 2125c1bb86cdSEric Blake struct stat st; 2126c1bb86cdSEric Blake int readonly = 0; 2127c1bb86cdSEric Blake 2128c1bb86cdSEric Blake if (fstat(s->fd, &st)) { 2129c1bb86cdSEric Blake return -errno; 2130c1bb86cdSEric Blake } 2131c1bb86cdSEric Blake 2132c1bb86cdSEric Blake if (!S_ISBLK(st.st_mode)) { 2133c1bb86cdSEric Blake return 0; 2134c1bb86cdSEric Blake } 2135c1bb86cdSEric Blake 2136c1bb86cdSEric Blake if (ioctl(s->fd, BLKROGET, &readonly) < 0) { 2137c1bb86cdSEric Blake return -errno; 2138c1bb86cdSEric Blake } 2139c1bb86cdSEric Blake 2140c1bb86cdSEric Blake if (readonly) { 2141c1bb86cdSEric Blake return -EACCES; 2142c1bb86cdSEric Blake } 2143c1bb86cdSEric Blake #endif /* defined(BLKROGET) */ 2144c1bb86cdSEric Blake return 0; 2145c1bb86cdSEric Blake } 2146c1bb86cdSEric Blake 2147c1bb86cdSEric Blake static void hdev_parse_filename(const char *filename, QDict *options, 2148c1bb86cdSEric Blake Error **errp) 2149c1bb86cdSEric Blake { 2150c1bb86cdSEric Blake /* The prefix is optional, just as for "file". */ 2151c1bb86cdSEric Blake strstart(filename, "host_device:", &filename); 2152c1bb86cdSEric Blake 2153c1bb86cdSEric Blake qdict_put_obj(options, "filename", QOBJECT(qstring_from_str(filename))); 2154c1bb86cdSEric Blake } 2155c1bb86cdSEric Blake 2156c1bb86cdSEric Blake static bool hdev_is_sg(BlockDriverState *bs) 2157c1bb86cdSEric Blake { 2158c1bb86cdSEric Blake 2159c1bb86cdSEric Blake #if defined(__linux__) 2160c1bb86cdSEric Blake 2161c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2162c1bb86cdSEric Blake struct stat st; 2163c1bb86cdSEric Blake struct sg_scsi_id scsiid; 2164c1bb86cdSEric Blake int sg_version; 2165c1bb86cdSEric Blake int ret; 2166c1bb86cdSEric Blake 2167c1bb86cdSEric Blake if (stat(bs->filename, &st) < 0 || !S_ISCHR(st.st_mode)) { 2168c1bb86cdSEric Blake return false; 2169c1bb86cdSEric Blake } 2170c1bb86cdSEric Blake 2171c1bb86cdSEric Blake ret = ioctl(s->fd, SG_GET_VERSION_NUM, &sg_version); 2172c1bb86cdSEric Blake if (ret < 0) { 2173c1bb86cdSEric Blake return false; 2174c1bb86cdSEric Blake } 2175c1bb86cdSEric Blake 2176c1bb86cdSEric Blake ret = ioctl(s->fd, SG_GET_SCSI_ID, &scsiid); 2177c1bb86cdSEric Blake if (ret >= 0) { 2178c1bb86cdSEric Blake DPRINTF("SG device found: type=%d, version=%d\n", 2179c1bb86cdSEric Blake scsiid.scsi_type, sg_version); 2180c1bb86cdSEric Blake return true; 2181c1bb86cdSEric Blake } 2182c1bb86cdSEric Blake 2183c1bb86cdSEric Blake #endif 2184c1bb86cdSEric Blake 2185c1bb86cdSEric Blake return false; 2186c1bb86cdSEric Blake } 2187c1bb86cdSEric Blake 2188c1bb86cdSEric Blake static int hdev_open(BlockDriverState *bs, QDict *options, int flags, 2189c1bb86cdSEric Blake Error **errp) 2190c1bb86cdSEric Blake { 2191c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2192c1bb86cdSEric Blake Error *local_err = NULL; 2193c1bb86cdSEric Blake int ret; 2194c1bb86cdSEric Blake 2195c1bb86cdSEric Blake #if defined(__APPLE__) && defined(__MACH__) 2196c1bb86cdSEric Blake const char *filename = qdict_get_str(options, "filename"); 2197c1bb86cdSEric Blake char bsd_path[MAXPATHLEN] = ""; 2198c1bb86cdSEric Blake bool error_occurred = false; 2199c1bb86cdSEric Blake 2200c1bb86cdSEric Blake /* If using a real cdrom */ 2201c1bb86cdSEric Blake if (strcmp(filename, "/dev/cdrom") == 0) { 2202c1bb86cdSEric Blake char *mediaType = NULL; 2203c1bb86cdSEric Blake kern_return_t ret_val; 2204c1bb86cdSEric Blake io_iterator_t mediaIterator = 0; 2205c1bb86cdSEric Blake 2206c1bb86cdSEric Blake mediaType = FindEjectableOpticalMedia(&mediaIterator); 2207c1bb86cdSEric Blake if (mediaType == NULL) { 2208c1bb86cdSEric Blake error_setg(errp, "Please make sure your CD/DVD is in the optical" 2209c1bb86cdSEric Blake " drive"); 2210c1bb86cdSEric Blake error_occurred = true; 2211c1bb86cdSEric Blake goto hdev_open_Mac_error; 2212c1bb86cdSEric Blake } 2213c1bb86cdSEric Blake 2214c1bb86cdSEric Blake ret_val = GetBSDPath(mediaIterator, bsd_path, sizeof(bsd_path), flags); 2215c1bb86cdSEric Blake if (ret_val != KERN_SUCCESS) { 2216c1bb86cdSEric Blake error_setg(errp, "Could not get BSD path for optical drive"); 2217c1bb86cdSEric Blake error_occurred = true; 2218c1bb86cdSEric Blake goto hdev_open_Mac_error; 2219c1bb86cdSEric Blake } 2220c1bb86cdSEric Blake 2221c1bb86cdSEric Blake /* If a real optical drive was not found */ 2222c1bb86cdSEric Blake if (bsd_path[0] == '\0') { 2223c1bb86cdSEric Blake error_setg(errp, "Failed to obtain bsd path for optical drive"); 2224c1bb86cdSEric Blake error_occurred = true; 2225c1bb86cdSEric Blake goto hdev_open_Mac_error; 2226c1bb86cdSEric Blake } 2227c1bb86cdSEric Blake 2228c1bb86cdSEric Blake /* If using a cdrom disc and finding a partition on the disc failed */ 2229c1bb86cdSEric Blake if (strncmp(mediaType, kIOCDMediaClass, 9) == 0 && 2230c1bb86cdSEric Blake setup_cdrom(bsd_path, errp) == false) { 2231c1bb86cdSEric Blake print_unmounting_directions(bsd_path); 2232c1bb86cdSEric Blake error_occurred = true; 2233c1bb86cdSEric Blake goto hdev_open_Mac_error; 2234c1bb86cdSEric Blake } 2235c1bb86cdSEric Blake 2236c1bb86cdSEric Blake qdict_put(options, "filename", qstring_from_str(bsd_path)); 2237c1bb86cdSEric Blake 2238c1bb86cdSEric Blake hdev_open_Mac_error: 2239c1bb86cdSEric Blake g_free(mediaType); 2240c1bb86cdSEric Blake if (mediaIterator) { 2241c1bb86cdSEric Blake IOObjectRelease(mediaIterator); 2242c1bb86cdSEric Blake } 2243c1bb86cdSEric Blake if (error_occurred) { 2244c1bb86cdSEric Blake return -ENOENT; 2245c1bb86cdSEric Blake } 2246c1bb86cdSEric Blake } 2247c1bb86cdSEric Blake #endif /* defined(__APPLE__) && defined(__MACH__) */ 2248c1bb86cdSEric Blake 2249c1bb86cdSEric Blake s->type = FTYPE_FILE; 2250c1bb86cdSEric Blake 2251c1bb86cdSEric Blake ret = raw_open_common(bs, options, flags, 0, &local_err); 2252c1bb86cdSEric Blake if (ret < 0) { 2253c1bb86cdSEric Blake error_propagate(errp, local_err); 2254c1bb86cdSEric Blake #if defined(__APPLE__) && defined(__MACH__) 2255c1bb86cdSEric Blake if (*bsd_path) { 2256c1bb86cdSEric Blake filename = bsd_path; 2257c1bb86cdSEric Blake } 2258c1bb86cdSEric Blake /* if a physical device experienced an error while being opened */ 2259c1bb86cdSEric Blake if (strncmp(filename, "/dev/", 5) == 0) { 2260c1bb86cdSEric Blake print_unmounting_directions(filename); 2261c1bb86cdSEric Blake } 2262c1bb86cdSEric Blake #endif /* defined(__APPLE__) && defined(__MACH__) */ 2263c1bb86cdSEric Blake return ret; 2264c1bb86cdSEric Blake } 2265c1bb86cdSEric Blake 2266c1bb86cdSEric Blake /* Since this does ioctl the device must be already opened */ 2267c1bb86cdSEric Blake bs->sg = hdev_is_sg(bs); 2268c1bb86cdSEric Blake 2269c1bb86cdSEric Blake if (flags & BDRV_O_RDWR) { 2270c1bb86cdSEric Blake ret = check_hdev_writable(s); 2271c1bb86cdSEric Blake if (ret < 0) { 2272c1bb86cdSEric Blake raw_close(bs); 2273c1bb86cdSEric Blake error_setg_errno(errp, -ret, "The device is not writable"); 2274c1bb86cdSEric Blake return ret; 2275c1bb86cdSEric Blake } 2276c1bb86cdSEric Blake } 2277c1bb86cdSEric Blake 2278c1bb86cdSEric Blake return ret; 2279c1bb86cdSEric Blake } 2280c1bb86cdSEric Blake 2281c1bb86cdSEric Blake #if defined(__linux__) 2282c1bb86cdSEric Blake 2283c1bb86cdSEric Blake static BlockAIOCB *hdev_aio_ioctl(BlockDriverState *bs, 2284c1bb86cdSEric Blake unsigned long int req, void *buf, 2285c1bb86cdSEric Blake BlockCompletionFunc *cb, void *opaque) 2286c1bb86cdSEric Blake { 2287c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2288c1bb86cdSEric Blake RawPosixAIOData *acb; 2289c1bb86cdSEric Blake ThreadPool *pool; 2290c1bb86cdSEric Blake 2291c1bb86cdSEric Blake if (fd_open(bs) < 0) 2292c1bb86cdSEric Blake return NULL; 2293c1bb86cdSEric Blake 2294c1bb86cdSEric Blake acb = g_new(RawPosixAIOData, 1); 2295c1bb86cdSEric Blake acb->bs = bs; 2296c1bb86cdSEric Blake acb->aio_type = QEMU_AIO_IOCTL; 2297c1bb86cdSEric Blake acb->aio_fildes = s->fd; 2298c1bb86cdSEric Blake acb->aio_offset = 0; 2299c1bb86cdSEric Blake acb->aio_ioctl_buf = buf; 2300c1bb86cdSEric Blake acb->aio_ioctl_cmd = req; 2301c1bb86cdSEric Blake pool = aio_get_thread_pool(bdrv_get_aio_context(bs)); 2302c1bb86cdSEric Blake return thread_pool_submit_aio(pool, aio_worker, acb, cb, opaque); 2303c1bb86cdSEric Blake } 2304c1bb86cdSEric Blake #endif /* linux */ 2305c1bb86cdSEric Blake 2306c1bb86cdSEric Blake static int fd_open(BlockDriverState *bs) 2307c1bb86cdSEric Blake { 2308c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2309c1bb86cdSEric Blake 2310c1bb86cdSEric Blake /* this is just to ensure s->fd is sane (its called by io ops) */ 2311c1bb86cdSEric Blake if (s->fd >= 0) 2312c1bb86cdSEric Blake return 0; 2313c1bb86cdSEric Blake return -EIO; 2314c1bb86cdSEric Blake } 2315c1bb86cdSEric Blake 2316c1bb86cdSEric Blake static coroutine_fn BlockAIOCB *hdev_aio_pdiscard(BlockDriverState *bs, 2317c1bb86cdSEric Blake int64_t offset, int count, 2318c1bb86cdSEric Blake BlockCompletionFunc *cb, void *opaque) 2319c1bb86cdSEric Blake { 2320c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2321c1bb86cdSEric Blake 2322c1bb86cdSEric Blake if (fd_open(bs) < 0) { 2323c1bb86cdSEric Blake return NULL; 2324c1bb86cdSEric Blake } 2325c1bb86cdSEric Blake return paio_submit(bs, s->fd, offset, NULL, count, 2326c1bb86cdSEric Blake cb, opaque, QEMU_AIO_DISCARD|QEMU_AIO_BLKDEV); 2327c1bb86cdSEric Blake } 2328c1bb86cdSEric Blake 2329c1bb86cdSEric Blake static coroutine_fn int hdev_co_pwrite_zeroes(BlockDriverState *bs, 2330c1bb86cdSEric Blake int64_t offset, int count, BdrvRequestFlags flags) 2331c1bb86cdSEric Blake { 2332c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2333c1bb86cdSEric Blake int rc; 2334c1bb86cdSEric Blake 2335c1bb86cdSEric Blake rc = fd_open(bs); 2336c1bb86cdSEric Blake if (rc < 0) { 2337c1bb86cdSEric Blake return rc; 2338c1bb86cdSEric Blake } 2339c1bb86cdSEric Blake if (!(flags & BDRV_REQ_MAY_UNMAP)) { 2340c1bb86cdSEric Blake return paio_submit_co(bs, s->fd, offset, NULL, count, 2341c1bb86cdSEric Blake QEMU_AIO_WRITE_ZEROES|QEMU_AIO_BLKDEV); 2342c1bb86cdSEric Blake } else if (s->discard_zeroes) { 2343c1bb86cdSEric Blake return paio_submit_co(bs, s->fd, offset, NULL, count, 2344c1bb86cdSEric Blake QEMU_AIO_DISCARD|QEMU_AIO_BLKDEV); 2345c1bb86cdSEric Blake } 2346c1bb86cdSEric Blake return -ENOTSUP; 2347c1bb86cdSEric Blake } 2348c1bb86cdSEric Blake 2349c1bb86cdSEric Blake static int hdev_create(const char *filename, QemuOpts *opts, 2350c1bb86cdSEric Blake Error **errp) 2351c1bb86cdSEric Blake { 2352c1bb86cdSEric Blake int fd; 2353c1bb86cdSEric Blake int ret = 0; 2354c1bb86cdSEric Blake struct stat stat_buf; 2355c1bb86cdSEric Blake int64_t total_size = 0; 2356c1bb86cdSEric Blake bool has_prefix; 2357c1bb86cdSEric Blake 2358c1bb86cdSEric Blake /* This function is used by both protocol block drivers and therefore either 2359c1bb86cdSEric Blake * of these prefixes may be given. 2360c1bb86cdSEric Blake * The return value has to be stored somewhere, otherwise this is an error 2361c1bb86cdSEric Blake * due to -Werror=unused-value. */ 2362c1bb86cdSEric Blake has_prefix = 2363c1bb86cdSEric Blake strstart(filename, "host_device:", &filename) || 2364c1bb86cdSEric Blake strstart(filename, "host_cdrom:" , &filename); 2365c1bb86cdSEric Blake 2366c1bb86cdSEric Blake (void)has_prefix; 2367c1bb86cdSEric Blake 2368c1bb86cdSEric Blake ret = raw_normalize_devicepath(&filename); 2369c1bb86cdSEric Blake if (ret < 0) { 2370c1bb86cdSEric Blake error_setg_errno(errp, -ret, "Could not normalize device path"); 2371c1bb86cdSEric Blake return ret; 2372c1bb86cdSEric Blake } 2373c1bb86cdSEric Blake 2374c1bb86cdSEric Blake /* Read out options */ 2375c1bb86cdSEric Blake total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), 2376c1bb86cdSEric Blake BDRV_SECTOR_SIZE); 2377c1bb86cdSEric Blake 2378c1bb86cdSEric Blake fd = qemu_open(filename, O_WRONLY | O_BINARY); 2379c1bb86cdSEric Blake if (fd < 0) { 2380c1bb86cdSEric Blake ret = -errno; 2381c1bb86cdSEric Blake error_setg_errno(errp, -ret, "Could not open device"); 2382c1bb86cdSEric Blake return ret; 2383c1bb86cdSEric Blake } 2384c1bb86cdSEric Blake 2385c1bb86cdSEric Blake if (fstat(fd, &stat_buf) < 0) { 2386c1bb86cdSEric Blake ret = -errno; 2387c1bb86cdSEric Blake error_setg_errno(errp, -ret, "Could not stat device"); 2388c1bb86cdSEric Blake } else if (!S_ISBLK(stat_buf.st_mode) && !S_ISCHR(stat_buf.st_mode)) { 2389c1bb86cdSEric Blake error_setg(errp, 2390c1bb86cdSEric Blake "The given file is neither a block nor a character device"); 2391c1bb86cdSEric Blake ret = -ENODEV; 2392c1bb86cdSEric Blake } else if (lseek(fd, 0, SEEK_END) < total_size) { 2393c1bb86cdSEric Blake error_setg(errp, "Device is too small"); 2394c1bb86cdSEric Blake ret = -ENOSPC; 2395c1bb86cdSEric Blake } 2396c1bb86cdSEric Blake 2397c1bb86cdSEric Blake qemu_close(fd); 2398c1bb86cdSEric Blake return ret; 2399c1bb86cdSEric Blake } 2400c1bb86cdSEric Blake 2401c1bb86cdSEric Blake static BlockDriver bdrv_host_device = { 2402c1bb86cdSEric Blake .format_name = "host_device", 2403c1bb86cdSEric Blake .protocol_name = "host_device", 2404c1bb86cdSEric Blake .instance_size = sizeof(BDRVRawState), 2405c1bb86cdSEric Blake .bdrv_needs_filename = true, 2406c1bb86cdSEric Blake .bdrv_probe_device = hdev_probe_device, 2407c1bb86cdSEric Blake .bdrv_parse_filename = hdev_parse_filename, 2408c1bb86cdSEric Blake .bdrv_file_open = hdev_open, 2409c1bb86cdSEric Blake .bdrv_close = raw_close, 2410c1bb86cdSEric Blake .bdrv_reopen_prepare = raw_reopen_prepare, 2411c1bb86cdSEric Blake .bdrv_reopen_commit = raw_reopen_commit, 2412c1bb86cdSEric Blake .bdrv_reopen_abort = raw_reopen_abort, 2413c1bb86cdSEric Blake .bdrv_create = hdev_create, 2414c1bb86cdSEric Blake .create_opts = &raw_create_opts, 2415c1bb86cdSEric Blake .bdrv_co_pwrite_zeroes = hdev_co_pwrite_zeroes, 2416c1bb86cdSEric Blake 2417c1bb86cdSEric Blake .bdrv_co_preadv = raw_co_preadv, 2418c1bb86cdSEric Blake .bdrv_co_pwritev = raw_co_pwritev, 2419c1bb86cdSEric Blake .bdrv_aio_flush = raw_aio_flush, 2420c1bb86cdSEric Blake .bdrv_aio_pdiscard = hdev_aio_pdiscard, 2421c1bb86cdSEric Blake .bdrv_refresh_limits = raw_refresh_limits, 2422c1bb86cdSEric Blake .bdrv_io_plug = raw_aio_plug, 2423c1bb86cdSEric Blake .bdrv_io_unplug = raw_aio_unplug, 2424c1bb86cdSEric Blake 2425c1bb86cdSEric Blake .bdrv_truncate = raw_truncate, 2426c1bb86cdSEric Blake .bdrv_getlength = raw_getlength, 2427c1bb86cdSEric Blake .bdrv_get_info = raw_get_info, 2428c1bb86cdSEric Blake .bdrv_get_allocated_file_size 2429c1bb86cdSEric Blake = raw_get_allocated_file_size, 2430c1bb86cdSEric Blake .bdrv_probe_blocksizes = hdev_probe_blocksizes, 2431c1bb86cdSEric Blake .bdrv_probe_geometry = hdev_probe_geometry, 2432c1bb86cdSEric Blake 2433c1bb86cdSEric Blake /* generic scsi device */ 2434c1bb86cdSEric Blake #ifdef __linux__ 2435c1bb86cdSEric Blake .bdrv_aio_ioctl = hdev_aio_ioctl, 2436c1bb86cdSEric Blake #endif 2437c1bb86cdSEric Blake }; 2438c1bb86cdSEric Blake 2439c1bb86cdSEric Blake #if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) 2440c1bb86cdSEric Blake static void cdrom_parse_filename(const char *filename, QDict *options, 2441c1bb86cdSEric Blake Error **errp) 2442c1bb86cdSEric Blake { 2443c1bb86cdSEric Blake /* The prefix is optional, just as for "file". */ 2444c1bb86cdSEric Blake strstart(filename, "host_cdrom:", &filename); 2445c1bb86cdSEric Blake 2446c1bb86cdSEric Blake qdict_put_obj(options, "filename", QOBJECT(qstring_from_str(filename))); 2447c1bb86cdSEric Blake } 2448c1bb86cdSEric Blake #endif 2449c1bb86cdSEric Blake 2450c1bb86cdSEric Blake #ifdef __linux__ 2451c1bb86cdSEric Blake static int cdrom_open(BlockDriverState *bs, QDict *options, int flags, 2452c1bb86cdSEric Blake Error **errp) 2453c1bb86cdSEric Blake { 2454c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2455c1bb86cdSEric Blake 2456c1bb86cdSEric Blake s->type = FTYPE_CD; 2457c1bb86cdSEric Blake 2458c1bb86cdSEric Blake /* open will not fail even if no CD is inserted, so add O_NONBLOCK */ 2459c1bb86cdSEric Blake return raw_open_common(bs, options, flags, O_NONBLOCK, errp); 2460c1bb86cdSEric Blake } 2461c1bb86cdSEric Blake 2462c1bb86cdSEric Blake static int cdrom_probe_device(const char *filename) 2463c1bb86cdSEric Blake { 2464c1bb86cdSEric Blake int fd, ret; 2465c1bb86cdSEric Blake int prio = 0; 2466c1bb86cdSEric Blake struct stat st; 2467c1bb86cdSEric Blake 2468c1bb86cdSEric Blake fd = qemu_open(filename, O_RDONLY | O_NONBLOCK); 2469c1bb86cdSEric Blake if (fd < 0) { 2470c1bb86cdSEric Blake goto out; 2471c1bb86cdSEric Blake } 2472c1bb86cdSEric Blake ret = fstat(fd, &st); 2473c1bb86cdSEric Blake if (ret == -1 || !S_ISBLK(st.st_mode)) { 2474c1bb86cdSEric Blake goto outc; 2475c1bb86cdSEric Blake } 2476c1bb86cdSEric Blake 2477c1bb86cdSEric Blake /* Attempt to detect via a CDROM specific ioctl */ 2478c1bb86cdSEric Blake ret = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT); 2479c1bb86cdSEric Blake if (ret >= 0) 2480c1bb86cdSEric Blake prio = 100; 2481c1bb86cdSEric Blake 2482c1bb86cdSEric Blake outc: 2483c1bb86cdSEric Blake qemu_close(fd); 2484c1bb86cdSEric Blake out: 2485c1bb86cdSEric Blake return prio; 2486c1bb86cdSEric Blake } 2487c1bb86cdSEric Blake 2488c1bb86cdSEric Blake static bool cdrom_is_inserted(BlockDriverState *bs) 2489c1bb86cdSEric Blake { 2490c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2491c1bb86cdSEric Blake int ret; 2492c1bb86cdSEric Blake 2493c1bb86cdSEric Blake ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT); 2494c1bb86cdSEric Blake return ret == CDS_DISC_OK; 2495c1bb86cdSEric Blake } 2496c1bb86cdSEric Blake 2497c1bb86cdSEric Blake static void cdrom_eject(BlockDriverState *bs, bool eject_flag) 2498c1bb86cdSEric Blake { 2499c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2500c1bb86cdSEric Blake 2501c1bb86cdSEric Blake if (eject_flag) { 2502c1bb86cdSEric Blake if (ioctl(s->fd, CDROMEJECT, NULL) < 0) 2503c1bb86cdSEric Blake perror("CDROMEJECT"); 2504c1bb86cdSEric Blake } else { 2505c1bb86cdSEric Blake if (ioctl(s->fd, CDROMCLOSETRAY, NULL) < 0) 2506c1bb86cdSEric Blake perror("CDROMEJECT"); 2507c1bb86cdSEric Blake } 2508c1bb86cdSEric Blake } 2509c1bb86cdSEric Blake 2510c1bb86cdSEric Blake static void cdrom_lock_medium(BlockDriverState *bs, bool locked) 2511c1bb86cdSEric Blake { 2512c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2513c1bb86cdSEric Blake 2514c1bb86cdSEric Blake if (ioctl(s->fd, CDROM_LOCKDOOR, locked) < 0) { 2515c1bb86cdSEric Blake /* 2516c1bb86cdSEric Blake * Note: an error can happen if the distribution automatically 2517c1bb86cdSEric Blake * mounts the CD-ROM 2518c1bb86cdSEric Blake */ 2519c1bb86cdSEric Blake /* perror("CDROM_LOCKDOOR"); */ 2520c1bb86cdSEric Blake } 2521c1bb86cdSEric Blake } 2522c1bb86cdSEric Blake 2523c1bb86cdSEric Blake static BlockDriver bdrv_host_cdrom = { 2524c1bb86cdSEric Blake .format_name = "host_cdrom", 2525c1bb86cdSEric Blake .protocol_name = "host_cdrom", 2526c1bb86cdSEric Blake .instance_size = sizeof(BDRVRawState), 2527c1bb86cdSEric Blake .bdrv_needs_filename = true, 2528c1bb86cdSEric Blake .bdrv_probe_device = cdrom_probe_device, 2529c1bb86cdSEric Blake .bdrv_parse_filename = cdrom_parse_filename, 2530c1bb86cdSEric Blake .bdrv_file_open = cdrom_open, 2531c1bb86cdSEric Blake .bdrv_close = raw_close, 2532c1bb86cdSEric Blake .bdrv_reopen_prepare = raw_reopen_prepare, 2533c1bb86cdSEric Blake .bdrv_reopen_commit = raw_reopen_commit, 2534c1bb86cdSEric Blake .bdrv_reopen_abort = raw_reopen_abort, 2535c1bb86cdSEric Blake .bdrv_create = hdev_create, 2536c1bb86cdSEric Blake .create_opts = &raw_create_opts, 2537c1bb86cdSEric Blake 2538c1bb86cdSEric Blake 2539c1bb86cdSEric Blake .bdrv_co_preadv = raw_co_preadv, 2540c1bb86cdSEric Blake .bdrv_co_pwritev = raw_co_pwritev, 2541c1bb86cdSEric Blake .bdrv_aio_flush = raw_aio_flush, 2542c1bb86cdSEric Blake .bdrv_refresh_limits = raw_refresh_limits, 2543c1bb86cdSEric Blake .bdrv_io_plug = raw_aio_plug, 2544c1bb86cdSEric Blake .bdrv_io_unplug = raw_aio_unplug, 2545c1bb86cdSEric Blake 2546c1bb86cdSEric Blake .bdrv_truncate = raw_truncate, 2547c1bb86cdSEric Blake .bdrv_getlength = raw_getlength, 2548c1bb86cdSEric Blake .has_variable_length = true, 2549c1bb86cdSEric Blake .bdrv_get_allocated_file_size 2550c1bb86cdSEric Blake = raw_get_allocated_file_size, 2551c1bb86cdSEric Blake 2552c1bb86cdSEric Blake /* removable device support */ 2553c1bb86cdSEric Blake .bdrv_is_inserted = cdrom_is_inserted, 2554c1bb86cdSEric Blake .bdrv_eject = cdrom_eject, 2555c1bb86cdSEric Blake .bdrv_lock_medium = cdrom_lock_medium, 2556c1bb86cdSEric Blake 2557c1bb86cdSEric Blake /* generic scsi device */ 2558c1bb86cdSEric Blake .bdrv_aio_ioctl = hdev_aio_ioctl, 2559c1bb86cdSEric Blake }; 2560c1bb86cdSEric Blake #endif /* __linux__ */ 2561c1bb86cdSEric Blake 2562c1bb86cdSEric Blake #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__) 2563c1bb86cdSEric Blake static int cdrom_open(BlockDriverState *bs, QDict *options, int flags, 2564c1bb86cdSEric Blake Error **errp) 2565c1bb86cdSEric Blake { 2566c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2567c1bb86cdSEric Blake Error *local_err = NULL; 2568c1bb86cdSEric Blake int ret; 2569c1bb86cdSEric Blake 2570c1bb86cdSEric Blake s->type = FTYPE_CD; 2571c1bb86cdSEric Blake 2572c1bb86cdSEric Blake ret = raw_open_common(bs, options, flags, 0, &local_err); 2573c1bb86cdSEric Blake if (ret) { 2574c1bb86cdSEric Blake error_propagate(errp, local_err); 2575c1bb86cdSEric Blake return ret; 2576c1bb86cdSEric Blake } 2577c1bb86cdSEric Blake 2578c1bb86cdSEric Blake /* make sure the door isn't locked at this time */ 2579c1bb86cdSEric Blake ioctl(s->fd, CDIOCALLOW); 2580c1bb86cdSEric Blake return 0; 2581c1bb86cdSEric Blake } 2582c1bb86cdSEric Blake 2583c1bb86cdSEric Blake static int cdrom_probe_device(const char *filename) 2584c1bb86cdSEric Blake { 2585c1bb86cdSEric Blake if (strstart(filename, "/dev/cd", NULL) || 2586c1bb86cdSEric Blake strstart(filename, "/dev/acd", NULL)) 2587c1bb86cdSEric Blake return 100; 2588c1bb86cdSEric Blake return 0; 2589c1bb86cdSEric Blake } 2590c1bb86cdSEric Blake 2591c1bb86cdSEric Blake static int cdrom_reopen(BlockDriverState *bs) 2592c1bb86cdSEric Blake { 2593c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2594c1bb86cdSEric Blake int fd; 2595c1bb86cdSEric Blake 2596c1bb86cdSEric Blake /* 2597c1bb86cdSEric Blake * Force reread of possibly changed/newly loaded disc, 2598c1bb86cdSEric Blake * FreeBSD seems to not notice sometimes... 2599c1bb86cdSEric Blake */ 2600c1bb86cdSEric Blake if (s->fd >= 0) 2601c1bb86cdSEric Blake qemu_close(s->fd); 2602c1bb86cdSEric Blake fd = qemu_open(bs->filename, s->open_flags, 0644); 2603c1bb86cdSEric Blake if (fd < 0) { 2604c1bb86cdSEric Blake s->fd = -1; 2605c1bb86cdSEric Blake return -EIO; 2606c1bb86cdSEric Blake } 2607c1bb86cdSEric Blake s->fd = fd; 2608c1bb86cdSEric Blake 2609c1bb86cdSEric Blake /* make sure the door isn't locked at this time */ 2610c1bb86cdSEric Blake ioctl(s->fd, CDIOCALLOW); 2611c1bb86cdSEric Blake return 0; 2612c1bb86cdSEric Blake } 2613c1bb86cdSEric Blake 2614c1bb86cdSEric Blake static bool cdrom_is_inserted(BlockDriverState *bs) 2615c1bb86cdSEric Blake { 2616c1bb86cdSEric Blake return raw_getlength(bs) > 0; 2617c1bb86cdSEric Blake } 2618c1bb86cdSEric Blake 2619c1bb86cdSEric Blake static void cdrom_eject(BlockDriverState *bs, bool eject_flag) 2620c1bb86cdSEric Blake { 2621c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2622c1bb86cdSEric Blake 2623c1bb86cdSEric Blake if (s->fd < 0) 2624c1bb86cdSEric Blake return; 2625c1bb86cdSEric Blake 2626c1bb86cdSEric Blake (void) ioctl(s->fd, CDIOCALLOW); 2627c1bb86cdSEric Blake 2628c1bb86cdSEric Blake if (eject_flag) { 2629c1bb86cdSEric Blake if (ioctl(s->fd, CDIOCEJECT) < 0) 2630c1bb86cdSEric Blake perror("CDIOCEJECT"); 2631c1bb86cdSEric Blake } else { 2632c1bb86cdSEric Blake if (ioctl(s->fd, CDIOCCLOSE) < 0) 2633c1bb86cdSEric Blake perror("CDIOCCLOSE"); 2634c1bb86cdSEric Blake } 2635c1bb86cdSEric Blake 2636c1bb86cdSEric Blake cdrom_reopen(bs); 2637c1bb86cdSEric Blake } 2638c1bb86cdSEric Blake 2639c1bb86cdSEric Blake static void cdrom_lock_medium(BlockDriverState *bs, bool locked) 2640c1bb86cdSEric Blake { 2641c1bb86cdSEric Blake BDRVRawState *s = bs->opaque; 2642c1bb86cdSEric Blake 2643c1bb86cdSEric Blake if (s->fd < 0) 2644c1bb86cdSEric Blake return; 2645c1bb86cdSEric Blake if (ioctl(s->fd, (locked ? CDIOCPREVENT : CDIOCALLOW)) < 0) { 2646c1bb86cdSEric Blake /* 2647c1bb86cdSEric Blake * Note: an error can happen if the distribution automatically 2648c1bb86cdSEric Blake * mounts the CD-ROM 2649c1bb86cdSEric Blake */ 2650c1bb86cdSEric Blake /* perror("CDROM_LOCKDOOR"); */ 2651c1bb86cdSEric Blake } 2652c1bb86cdSEric Blake } 2653c1bb86cdSEric Blake 2654c1bb86cdSEric Blake static BlockDriver bdrv_host_cdrom = { 2655c1bb86cdSEric Blake .format_name = "host_cdrom", 2656c1bb86cdSEric Blake .protocol_name = "host_cdrom", 2657c1bb86cdSEric Blake .instance_size = sizeof(BDRVRawState), 2658c1bb86cdSEric Blake .bdrv_needs_filename = true, 2659c1bb86cdSEric Blake .bdrv_probe_device = cdrom_probe_device, 2660c1bb86cdSEric Blake .bdrv_parse_filename = cdrom_parse_filename, 2661c1bb86cdSEric Blake .bdrv_file_open = cdrom_open, 2662c1bb86cdSEric Blake .bdrv_close = raw_close, 2663c1bb86cdSEric Blake .bdrv_reopen_prepare = raw_reopen_prepare, 2664c1bb86cdSEric Blake .bdrv_reopen_commit = raw_reopen_commit, 2665c1bb86cdSEric Blake .bdrv_reopen_abort = raw_reopen_abort, 2666c1bb86cdSEric Blake .bdrv_create = hdev_create, 2667c1bb86cdSEric Blake .create_opts = &raw_create_opts, 2668c1bb86cdSEric Blake 2669c1bb86cdSEric Blake .bdrv_co_preadv = raw_co_preadv, 2670c1bb86cdSEric Blake .bdrv_co_pwritev = raw_co_pwritev, 2671c1bb86cdSEric Blake .bdrv_aio_flush = raw_aio_flush, 2672c1bb86cdSEric Blake .bdrv_refresh_limits = raw_refresh_limits, 2673c1bb86cdSEric Blake .bdrv_io_plug = raw_aio_plug, 2674c1bb86cdSEric Blake .bdrv_io_unplug = raw_aio_unplug, 2675c1bb86cdSEric Blake 2676c1bb86cdSEric Blake .bdrv_truncate = raw_truncate, 2677c1bb86cdSEric Blake .bdrv_getlength = raw_getlength, 2678c1bb86cdSEric Blake .has_variable_length = true, 2679c1bb86cdSEric Blake .bdrv_get_allocated_file_size 2680c1bb86cdSEric Blake = raw_get_allocated_file_size, 2681c1bb86cdSEric Blake 2682c1bb86cdSEric Blake /* removable device support */ 2683c1bb86cdSEric Blake .bdrv_is_inserted = cdrom_is_inserted, 2684c1bb86cdSEric Blake .bdrv_eject = cdrom_eject, 2685c1bb86cdSEric Blake .bdrv_lock_medium = cdrom_lock_medium, 2686c1bb86cdSEric Blake }; 2687c1bb86cdSEric Blake #endif /* __FreeBSD__ */ 2688c1bb86cdSEric Blake 2689c1bb86cdSEric Blake static void bdrv_file_init(void) 2690c1bb86cdSEric Blake { 2691c1bb86cdSEric Blake /* 2692c1bb86cdSEric Blake * Register all the drivers. Note that order is important, the driver 2693c1bb86cdSEric Blake * registered last will get probed first. 2694c1bb86cdSEric Blake */ 2695c1bb86cdSEric Blake bdrv_register(&bdrv_file); 2696c1bb86cdSEric Blake bdrv_register(&bdrv_host_device); 2697c1bb86cdSEric Blake #ifdef __linux__ 2698c1bb86cdSEric Blake bdrv_register(&bdrv_host_cdrom); 2699c1bb86cdSEric Blake #endif 2700c1bb86cdSEric Blake #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) 2701c1bb86cdSEric Blake bdrv_register(&bdrv_host_cdrom); 2702c1bb86cdSEric Blake #endif 2703c1bb86cdSEric Blake } 2704c1bb86cdSEric Blake 2705c1bb86cdSEric Blake block_init(bdrv_file_init); 2706