bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 1 | /* |
ths | 223d467 | 2007-12-15 17:28:36 +0000 | [diff] [blame] | 2 | * Block driver for RAW files (posix) |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3 | * |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 4 | * Copyright (c) 2006 Fabrice Bellard |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5 | * |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | */ |
pbrook | faf0796 | 2007-11-11 02:51:17 +0000 | [diff] [blame] | 24 | #include "qemu-common.h" |
pbrook | 87ecb68 | 2007-11-17 17:14:51 +0000 | [diff] [blame] | 25 | #include "qemu-timer.h" |
aliguori | baf35cb | 2008-09-10 15:45:19 +0000 | [diff] [blame] | 26 | #include "qemu-char.h" |
Blue Swirl | 0bf9e31 | 2009-07-20 17:19:25 +0000 | [diff] [blame] | 27 | #include "qemu-log.h" |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 28 | #include "block_int.h" |
Anthony Liguori | 5efa9d5 | 2009-05-09 17:03:42 -0500 | [diff] [blame] | 29 | #include "module.h" |
blueswir1 | 414f0da | 2008-08-15 18:20:52 +0000 | [diff] [blame] | 30 | #ifdef CONFIG_AIO |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 31 | #include "posix-aio-compat.h" |
blueswir1 | 414f0da | 2008-08-15 18:20:52 +0000 | [diff] [blame] | 32 | #endif |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 33 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 34 | #ifdef CONFIG_COCOA |
| 35 | #include <paths.h> |
| 36 | #include <sys/param.h> |
| 37 | #include <IOKit/IOKitLib.h> |
| 38 | #include <IOKit/IOBSD.h> |
| 39 | #include <IOKit/storage/IOMediaBSDClient.h> |
| 40 | #include <IOKit/storage/IOMedia.h> |
| 41 | #include <IOKit/storage/IOCDMedia.h> |
| 42 | //#include <IOKit/storage/IOCDTypes.h> |
| 43 | #include <CoreFoundation/CoreFoundation.h> |
| 44 | #endif |
| 45 | |
| 46 | #ifdef __sun__ |
ths | 2e9671d | 2007-01-05 17:44:41 +0000 | [diff] [blame] | 47 | #define _POSIX_PTHREAD_SEMANTICS 1 |
| 48 | #include <signal.h> |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 49 | #include <sys/dkio.h> |
| 50 | #endif |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 51 | #ifdef __linux__ |
| 52 | #include <sys/ioctl.h> |
| 53 | #include <linux/cdrom.h> |
| 54 | #include <linux/fd.h> |
| 55 | #endif |
ths | 1cb6c3f | 2006-12-21 19:14:11 +0000 | [diff] [blame] | 56 | #ifdef __FreeBSD__ |
blueswir1 | 543952c | 2008-08-24 10:43:12 +0000 | [diff] [blame] | 57 | #include <signal.h> |
ths | 1cb6c3f | 2006-12-21 19:14:11 +0000 | [diff] [blame] | 58 | #include <sys/disk.h> |
blueswir1 | 9f23011 | 2009-03-28 08:37:13 +0000 | [diff] [blame] | 59 | #include <sys/cdio.h> |
ths | 1cb6c3f | 2006-12-21 19:14:11 +0000 | [diff] [blame] | 60 | #endif |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 61 | |
blueswir1 | 128ab2f | 2008-08-15 18:33:42 +0000 | [diff] [blame] | 62 | #ifdef __OpenBSD__ |
| 63 | #include <sys/ioctl.h> |
| 64 | #include <sys/disklabel.h> |
| 65 | #include <sys/dkio.h> |
| 66 | #endif |
| 67 | |
blueswir1 | c5e9723 | 2009-03-07 20:06:23 +0000 | [diff] [blame] | 68 | #ifdef __DragonFly__ |
| 69 | #include <sys/ioctl.h> |
| 70 | #include <sys/diskslice.h> |
| 71 | #endif |
| 72 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 73 | //#define DEBUG_FLOPPY |
| 74 | |
pbrook | faf0796 | 2007-11-11 02:51:17 +0000 | [diff] [blame] | 75 | //#define DEBUG_BLOCK |
aliguori | 03ff3ca | 2008-09-15 15:51:35 +0000 | [diff] [blame] | 76 | #if defined(DEBUG_BLOCK) |
Blue Swirl | 001faf3 | 2009-05-13 17:53:17 +0000 | [diff] [blame] | 77 | #define DEBUG_BLOCK_PRINT(formatCstr, ...) do { if (qemu_log_enabled()) \ |
| 78 | { qemu_log(formatCstr, ## __VA_ARGS__); qemu_log_flush(); } } while (0) |
ths | 8c05dbf | 2007-09-13 12:29:23 +0000 | [diff] [blame] | 79 | #else |
Blue Swirl | 001faf3 | 2009-05-13 17:53:17 +0000 | [diff] [blame] | 80 | #define DEBUG_BLOCK_PRINT(formatCstr, ...) |
ths | 8c05dbf | 2007-09-13 12:29:23 +0000 | [diff] [blame] | 81 | #endif |
| 82 | |
aliguori | f646557 | 2008-10-14 18:00:38 +0000 | [diff] [blame] | 83 | /* OS X does not have O_DSYNC */ |
| 84 | #ifndef O_DSYNC |
G 3 | 1c27a8b | 2009-07-01 13:28:32 -0400 | [diff] [blame] | 85 | #ifdef O_SYNC |
aliguori | 7ab064d | 2008-10-14 18:14:47 +0000 | [diff] [blame] | 86 | #define O_DSYNC O_SYNC |
G 3 | 1c27a8b | 2009-07-01 13:28:32 -0400 | [diff] [blame] | 87 | #elif defined(O_FSYNC) |
| 88 | #define O_DSYNC O_FSYNC |
| 89 | #endif |
aliguori | f646557 | 2008-10-14 18:00:38 +0000 | [diff] [blame] | 90 | #endif |
| 91 | |
aliguori | 9f7965c | 2008-10-14 14:42:54 +0000 | [diff] [blame] | 92 | /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */ |
| 93 | #ifndef O_DIRECT |
| 94 | #define O_DIRECT O_DSYNC |
| 95 | #endif |
| 96 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 97 | #define FTYPE_FILE 0 |
| 98 | #define FTYPE_CD 1 |
| 99 | #define FTYPE_FD 2 |
| 100 | |
bellard | bed5cc5 | 2008-05-28 09:51:09 +0000 | [diff] [blame] | 101 | #define ALIGNED_BUFFER_SIZE (32 * 512) |
| 102 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 103 | /* if the FD is not accessed during that time (in ms), we try to |
| 104 | reopen it to see if the disk has been changed */ |
| 105 | #define FD_OPEN_TIMEOUT 1000 |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 106 | |
| 107 | typedef struct BDRVRawState { |
| 108 | int fd; |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 109 | int type; |
ths | 8c05dbf | 2007-09-13 12:29:23 +0000 | [diff] [blame] | 110 | unsigned int lseek_err_cnt; |
Christoph Hellwig | 0e1d8f4 | 2009-06-15 13:53:26 +0200 | [diff] [blame] | 111 | int open_flags; |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 112 | #if defined(__linux__) |
| 113 | /* linux floppy specific */ |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 114 | int64_t fd_open_time; |
| 115 | int64_t fd_error_time; |
| 116 | int fd_got_error; |
| 117 | int fd_media_changed; |
| 118 | #endif |
bellard | bed5cc5 | 2008-05-28 09:51:09 +0000 | [diff] [blame] | 119 | uint8_t* aligned_buf; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 120 | } BDRVRawState; |
| 121 | |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 122 | static int posix_aio_init(void); |
| 123 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 124 | static int fd_open(BlockDriverState *bs); |
Kevin Wolf | 22afa7b | 2009-06-26 19:51:24 +0200 | [diff] [blame] | 125 | static int64_t raw_getlength(BlockDriverState *bs); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 126 | |
blueswir1 | 9f23011 | 2009-03-28 08:37:13 +0000 | [diff] [blame] | 127 | #if defined(__FreeBSD__) |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 128 | static int cdrom_reopen(BlockDriverState *bs); |
blueswir1 | 9f23011 | 2009-03-28 08:37:13 +0000 | [diff] [blame] | 129 | #endif |
| 130 | |
Christoph Hellwig | 90babde | 2009-06-15 13:53:38 +0200 | [diff] [blame] | 131 | static int raw_open_common(BlockDriverState *bs, const char *filename, |
Blue Swirl | 19a3da7 | 2009-06-17 18:27:44 +0300 | [diff] [blame] | 132 | int bdrv_flags, int open_flags) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 133 | { |
| 134 | BDRVRawState *s = bs->opaque; |
Christoph Hellwig | 0e1d8f4 | 2009-06-15 13:53:26 +0200 | [diff] [blame] | 135 | int fd, ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 136 | |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 137 | posix_aio_init(); |
| 138 | |
ths | 8c05dbf | 2007-09-13 12:29:23 +0000 | [diff] [blame] | 139 | s->lseek_err_cnt = 0; |
| 140 | |
Blue Swirl | 19a3da7 | 2009-06-17 18:27:44 +0300 | [diff] [blame] | 141 | s->open_flags = open_flags | O_BINARY; |
Avi Kivity | 11a1feb | 2009-06-16 16:21:09 +0300 | [diff] [blame] | 142 | s->open_flags &= ~O_ACCMODE; |
Blue Swirl | 19a3da7 | 2009-06-17 18:27:44 +0300 | [diff] [blame] | 143 | if ((bdrv_flags & BDRV_O_ACCESS) == BDRV_O_RDWR) { |
Christoph Hellwig | 0e1d8f4 | 2009-06-15 13:53:26 +0200 | [diff] [blame] | 144 | s->open_flags |= O_RDWR; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 145 | } else { |
Christoph Hellwig | 0e1d8f4 | 2009-06-15 13:53:26 +0200 | [diff] [blame] | 146 | s->open_flags |= O_RDONLY; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 147 | bs->read_only = 1; |
| 148 | } |
aliguori | 9f7965c | 2008-10-14 14:42:54 +0000 | [diff] [blame] | 149 | |
| 150 | /* Use O_DSYNC for write-through caching, no flags for write-back caching, |
| 151 | * and O_DIRECT for no caching. */ |
Blue Swirl | 19a3da7 | 2009-06-17 18:27:44 +0300 | [diff] [blame] | 152 | if ((bdrv_flags & BDRV_O_NOCACHE)) |
Christoph Hellwig | 0e1d8f4 | 2009-06-15 13:53:26 +0200 | [diff] [blame] | 153 | s->open_flags |= O_DIRECT; |
Blue Swirl | 19a3da7 | 2009-06-17 18:27:44 +0300 | [diff] [blame] | 154 | else if (!(bdrv_flags & BDRV_O_CACHE_WB)) |
Christoph Hellwig | 0e1d8f4 | 2009-06-15 13:53:26 +0200 | [diff] [blame] | 155 | s->open_flags |= O_DSYNC; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 156 | |
Christoph Hellwig | 90babde | 2009-06-15 13:53:38 +0200 | [diff] [blame] | 157 | s->fd = -1; |
Christoph Hellwig | 0e1d8f4 | 2009-06-15 13:53:26 +0200 | [diff] [blame] | 158 | fd = open(filename, s->open_flags, 0644); |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 159 | if (fd < 0) { |
| 160 | ret = -errno; |
| 161 | if (ret == -EROFS) |
| 162 | ret = -EACCES; |
| 163 | return ret; |
| 164 | } |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 165 | s->fd = fd; |
bellard | bed5cc5 | 2008-05-28 09:51:09 +0000 | [diff] [blame] | 166 | s->aligned_buf = NULL; |
Blue Swirl | 19a3da7 | 2009-06-17 18:27:44 +0300 | [diff] [blame] | 167 | if ((bdrv_flags & BDRV_O_NOCACHE)) { |
aliguori | e268ca5 | 2009-04-22 20:20:00 +0000 | [diff] [blame] | 168 | s->aligned_buf = qemu_blockalign(bs, ALIGNED_BUFFER_SIZE); |
bellard | bed5cc5 | 2008-05-28 09:51:09 +0000 | [diff] [blame] | 169 | if (s->aligned_buf == NULL) { |
| 170 | ret = -errno; |
| 171 | close(fd); |
| 172 | return ret; |
| 173 | } |
| 174 | } |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 175 | return 0; |
| 176 | } |
| 177 | |
Christoph Hellwig | 90babde | 2009-06-15 13:53:38 +0200 | [diff] [blame] | 178 | static int raw_open(BlockDriverState *bs, const char *filename, int flags) |
| 179 | { |
| 180 | BDRVRawState *s = bs->opaque; |
Blue Swirl | 19a3da7 | 2009-06-17 18:27:44 +0300 | [diff] [blame] | 181 | int open_flags = 0; |
Christoph Hellwig | 90babde | 2009-06-15 13:53:38 +0200 | [diff] [blame] | 182 | |
| 183 | s->type = FTYPE_FILE; |
| 184 | if (flags & BDRV_O_CREAT) |
Blue Swirl | 19a3da7 | 2009-06-17 18:27:44 +0300 | [diff] [blame] | 185 | open_flags = O_CREAT | O_TRUNC; |
Christoph Hellwig | 90babde | 2009-06-15 13:53:38 +0200 | [diff] [blame] | 186 | |
Blue Swirl | 19a3da7 | 2009-06-17 18:27:44 +0300 | [diff] [blame] | 187 | return raw_open_common(bs, filename, flags, open_flags); |
Christoph Hellwig | 90babde | 2009-06-15 13:53:38 +0200 | [diff] [blame] | 188 | } |
| 189 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 190 | /* XXX: use host sector size if necessary with: |
| 191 | #ifdef DIOCGSECTORSIZE |
| 192 | { |
| 193 | unsigned int sectorsize = 512; |
| 194 | if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) && |
| 195 | sectorsize > bufsize) |
| 196 | bufsize = sectorsize; |
| 197 | } |
| 198 | #endif |
| 199 | #ifdef CONFIG_COCOA |
| 200 | u_int32_t blockSize = 512; |
| 201 | if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) { |
| 202 | bufsize = blockSize; |
| 203 | } |
| 204 | #endif |
| 205 | */ |
| 206 | |
bellard | bed5cc5 | 2008-05-28 09:51:09 +0000 | [diff] [blame] | 207 | /* |
| 208 | * offset and count are in bytes, but must be multiples of 512 for files |
| 209 | * opened with O_DIRECT. buf must be aligned to 512 bytes then. |
| 210 | * |
| 211 | * This function may be called without alignment if the caller ensures |
| 212 | * that O_DIRECT is not in effect. |
| 213 | */ |
| 214 | static int raw_pread_aligned(BlockDriverState *bs, int64_t offset, |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 215 | uint8_t *buf, int count) |
| 216 | { |
| 217 | BDRVRawState *s = bs->opaque; |
| 218 | int ret; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 219 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 220 | ret = fd_open(bs); |
| 221 | if (ret < 0) |
| 222 | return ret; |
| 223 | |
ths | 985a03b | 2007-12-24 16:10:43 +0000 | [diff] [blame] | 224 | if (offset >= 0 && lseek(s->fd, offset, SEEK_SET) == (off_t)-1) { |
ths | 8c05dbf | 2007-09-13 12:29:23 +0000 | [diff] [blame] | 225 | ++(s->lseek_err_cnt); |
| 226 | if(s->lseek_err_cnt <= 10) { |
j_mayer | 9286841 | 2007-09-21 06:09:39 +0000 | [diff] [blame] | 227 | DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64 |
| 228 | "] lseek failed : %d = %s\n", |
ths | 8c05dbf | 2007-09-13 12:29:23 +0000 | [diff] [blame] | 229 | s->fd, bs->filename, offset, buf, count, |
| 230 | bs->total_sectors, errno, strerror(errno)); |
| 231 | } |
| 232 | return -1; |
| 233 | } |
| 234 | s->lseek_err_cnt=0; |
| 235 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 236 | ret = read(s->fd, buf, count); |
ths | 8c05dbf | 2007-09-13 12:29:23 +0000 | [diff] [blame] | 237 | if (ret == count) |
| 238 | goto label__raw_read__success; |
| 239 | |
Kevin Wolf | 22afa7b | 2009-06-26 19:51:24 +0200 | [diff] [blame] | 240 | /* Allow reads beyond the end (needed for pwrite) */ |
| 241 | if ((ret == 0) && bs->growable) { |
| 242 | int64_t size = raw_getlength(bs); |
| 243 | if (offset >= size) { |
| 244 | memset(buf, 0, count); |
| 245 | ret = count; |
| 246 | goto label__raw_read__success; |
| 247 | } |
| 248 | } |
| 249 | |
j_mayer | 9286841 | 2007-09-21 06:09:39 +0000 | [diff] [blame] | 250 | DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64 |
| 251 | "] read failed %d : %d = %s\n", |
ths | 8c05dbf | 2007-09-13 12:29:23 +0000 | [diff] [blame] | 252 | s->fd, bs->filename, offset, buf, count, |
| 253 | bs->total_sectors, ret, errno, strerror(errno)); |
| 254 | |
| 255 | /* Try harder for CDrom. */ |
| 256 | if (bs->type == BDRV_TYPE_CDROM) { |
| 257 | lseek(s->fd, offset, SEEK_SET); |
| 258 | ret = read(s->fd, buf, count); |
| 259 | if (ret == count) |
| 260 | goto label__raw_read__success; |
| 261 | lseek(s->fd, offset, SEEK_SET); |
| 262 | ret = read(s->fd, buf, count); |
| 263 | if (ret == count) |
| 264 | goto label__raw_read__success; |
| 265 | |
j_mayer | 9286841 | 2007-09-21 06:09:39 +0000 | [diff] [blame] | 266 | DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64 |
| 267 | "] retry read failed %d : %d = %s\n", |
ths | 8c05dbf | 2007-09-13 12:29:23 +0000 | [diff] [blame] | 268 | s->fd, bs->filename, offset, buf, count, |
| 269 | bs->total_sectors, ret, errno, strerror(errno)); |
| 270 | } |
| 271 | |
ths | 8c05dbf | 2007-09-13 12:29:23 +0000 | [diff] [blame] | 272 | label__raw_read__success: |
| 273 | |
Christoph Hellwig | 94c6d6d | 2009-05-25 10:08:52 +0200 | [diff] [blame] | 274 | return (ret < 0) ? -errno : ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 275 | } |
| 276 | |
bellard | bed5cc5 | 2008-05-28 09:51:09 +0000 | [diff] [blame] | 277 | /* |
| 278 | * offset and count are in bytes, but must be multiples of 512 for files |
| 279 | * opened with O_DIRECT. buf must be aligned to 512 bytes then. |
| 280 | * |
| 281 | * This function may be called without alignment if the caller ensures |
| 282 | * that O_DIRECT is not in effect. |
| 283 | */ |
| 284 | static int raw_pwrite_aligned(BlockDriverState *bs, int64_t offset, |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 285 | const uint8_t *buf, int count) |
| 286 | { |
| 287 | BDRVRawState *s = bs->opaque; |
| 288 | int ret; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 289 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 290 | ret = fd_open(bs); |
| 291 | if (ret < 0) |
aliguori | 4141d4c | 2009-01-15 20:44:26 +0000 | [diff] [blame] | 292 | return -errno; |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 293 | |
ths | 985a03b | 2007-12-24 16:10:43 +0000 | [diff] [blame] | 294 | if (offset >= 0 && lseek(s->fd, offset, SEEK_SET) == (off_t)-1) { |
ths | 8c05dbf | 2007-09-13 12:29:23 +0000 | [diff] [blame] | 295 | ++(s->lseek_err_cnt); |
| 296 | if(s->lseek_err_cnt) { |
j_mayer | 9286841 | 2007-09-21 06:09:39 +0000 | [diff] [blame] | 297 | DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%" |
| 298 | PRId64 "] lseek failed : %d = %s\n", |
ths | 8c05dbf | 2007-09-13 12:29:23 +0000 | [diff] [blame] | 299 | s->fd, bs->filename, offset, buf, count, |
| 300 | bs->total_sectors, errno, strerror(errno)); |
| 301 | } |
aliguori | 4141d4c | 2009-01-15 20:44:26 +0000 | [diff] [blame] | 302 | return -EIO; |
ths | 8c05dbf | 2007-09-13 12:29:23 +0000 | [diff] [blame] | 303 | } |
| 304 | s->lseek_err_cnt = 0; |
| 305 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 306 | ret = write(s->fd, buf, count); |
ths | 8c05dbf | 2007-09-13 12:29:23 +0000 | [diff] [blame] | 307 | if (ret == count) |
| 308 | goto label__raw_write__success; |
| 309 | |
j_mayer | 9286841 | 2007-09-21 06:09:39 +0000 | [diff] [blame] | 310 | DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%" PRId64 |
| 311 | "] write failed %d : %d = %s\n", |
ths | 8c05dbf | 2007-09-13 12:29:23 +0000 | [diff] [blame] | 312 | s->fd, bs->filename, offset, buf, count, |
| 313 | bs->total_sectors, ret, errno, strerror(errno)); |
| 314 | |
ths | 8c05dbf | 2007-09-13 12:29:23 +0000 | [diff] [blame] | 315 | label__raw_write__success: |
| 316 | |
aliguori | 4141d4c | 2009-01-15 20:44:26 +0000 | [diff] [blame] | 317 | return (ret < 0) ? -errno : ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 318 | } |
| 319 | |
bellard | bed5cc5 | 2008-05-28 09:51:09 +0000 | [diff] [blame] | 320 | |
bellard | bed5cc5 | 2008-05-28 09:51:09 +0000 | [diff] [blame] | 321 | /* |
| 322 | * offset and count are in bytes and possibly not aligned. For files opened |
| 323 | * with O_DIRECT, necessary alignments are ensured before calling |
| 324 | * raw_pread_aligned to do the actual read. |
| 325 | */ |
| 326 | static int raw_pread(BlockDriverState *bs, int64_t offset, |
| 327 | uint8_t *buf, int count) |
| 328 | { |
| 329 | BDRVRawState *s = bs->opaque; |
| 330 | int size, ret, shift, sum; |
| 331 | |
| 332 | sum = 0; |
| 333 | |
| 334 | if (s->aligned_buf != NULL) { |
| 335 | |
| 336 | if (offset & 0x1ff) { |
| 337 | /* align offset on a 512 bytes boundary */ |
| 338 | |
| 339 | shift = offset & 0x1ff; |
| 340 | size = (shift + count + 0x1ff) & ~0x1ff; |
| 341 | if (size > ALIGNED_BUFFER_SIZE) |
| 342 | size = ALIGNED_BUFFER_SIZE; |
| 343 | ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, size); |
| 344 | if (ret < 0) |
| 345 | return ret; |
| 346 | |
| 347 | size = 512 - shift; |
| 348 | if (size > count) |
| 349 | size = count; |
| 350 | memcpy(buf, s->aligned_buf + shift, size); |
| 351 | |
| 352 | buf += size; |
| 353 | offset += size; |
| 354 | count -= size; |
| 355 | sum += size; |
| 356 | |
| 357 | if (count == 0) |
| 358 | return sum; |
| 359 | } |
| 360 | if (count & 0x1ff || (uintptr_t) buf & 0x1ff) { |
| 361 | |
| 362 | /* read on aligned buffer */ |
| 363 | |
| 364 | while (count) { |
| 365 | |
| 366 | size = (count + 0x1ff) & ~0x1ff; |
| 367 | if (size > ALIGNED_BUFFER_SIZE) |
| 368 | size = ALIGNED_BUFFER_SIZE; |
| 369 | |
| 370 | ret = raw_pread_aligned(bs, offset, s->aligned_buf, size); |
| 371 | if (ret < 0) |
| 372 | return ret; |
| 373 | |
| 374 | size = ret; |
| 375 | if (size > count) |
| 376 | size = count; |
| 377 | |
| 378 | memcpy(buf, s->aligned_buf, size); |
| 379 | |
| 380 | buf += size; |
| 381 | offset += size; |
| 382 | count -= size; |
| 383 | sum += size; |
| 384 | } |
| 385 | |
| 386 | return sum; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | return raw_pread_aligned(bs, offset, buf, count) + sum; |
| 391 | } |
| 392 | |
aliguori | eda578e | 2009-03-12 19:57:16 +0000 | [diff] [blame] | 393 | static int raw_read(BlockDriverState *bs, int64_t sector_num, |
| 394 | uint8_t *buf, int nb_sectors) |
| 395 | { |
aliguori | 537a1d4 | 2009-03-13 03:12:03 +0000 | [diff] [blame] | 396 | int ret; |
| 397 | |
| 398 | ret = raw_pread(bs, sector_num * 512, buf, nb_sectors * 512); |
| 399 | if (ret == (nb_sectors * 512)) |
| 400 | ret = 0; |
| 401 | return ret; |
aliguori | eda578e | 2009-03-12 19:57:16 +0000 | [diff] [blame] | 402 | } |
| 403 | |
bellard | bed5cc5 | 2008-05-28 09:51:09 +0000 | [diff] [blame] | 404 | /* |
| 405 | * offset and count are in bytes and possibly not aligned. For files opened |
| 406 | * with O_DIRECT, necessary alignments are ensured before calling |
| 407 | * raw_pwrite_aligned to do the actual write. |
| 408 | */ |
| 409 | static int raw_pwrite(BlockDriverState *bs, int64_t offset, |
| 410 | const uint8_t *buf, int count) |
| 411 | { |
| 412 | BDRVRawState *s = bs->opaque; |
| 413 | int size, ret, shift, sum; |
| 414 | |
| 415 | sum = 0; |
| 416 | |
| 417 | if (s->aligned_buf != NULL) { |
| 418 | |
| 419 | if (offset & 0x1ff) { |
| 420 | /* align offset on a 512 bytes boundary */ |
| 421 | shift = offset & 0x1ff; |
| 422 | ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, 512); |
| 423 | if (ret < 0) |
| 424 | return ret; |
| 425 | |
| 426 | size = 512 - shift; |
| 427 | if (size > count) |
| 428 | size = count; |
| 429 | memcpy(s->aligned_buf + shift, buf, size); |
| 430 | |
| 431 | ret = raw_pwrite_aligned(bs, offset - shift, s->aligned_buf, 512); |
| 432 | if (ret < 0) |
| 433 | return ret; |
| 434 | |
| 435 | buf += size; |
| 436 | offset += size; |
| 437 | count -= size; |
| 438 | sum += size; |
| 439 | |
| 440 | if (count == 0) |
| 441 | return sum; |
| 442 | } |
| 443 | if (count & 0x1ff || (uintptr_t) buf & 0x1ff) { |
| 444 | |
| 445 | while ((size = (count & ~0x1ff)) != 0) { |
| 446 | |
| 447 | if (size > ALIGNED_BUFFER_SIZE) |
| 448 | size = ALIGNED_BUFFER_SIZE; |
| 449 | |
| 450 | memcpy(s->aligned_buf, buf, size); |
| 451 | |
| 452 | ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, size); |
| 453 | if (ret < 0) |
| 454 | return ret; |
| 455 | |
| 456 | buf += ret; |
| 457 | offset += ret; |
| 458 | count -= ret; |
| 459 | sum += ret; |
| 460 | } |
| 461 | /* here, count < 512 because (count & ~0x1ff) == 0 */ |
| 462 | if (count) { |
| 463 | ret = raw_pread_aligned(bs, offset, s->aligned_buf, 512); |
| 464 | if (ret < 0) |
| 465 | return ret; |
| 466 | memcpy(s->aligned_buf, buf, count); |
| 467 | |
| 468 | ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, 512); |
| 469 | if (ret < 0) |
| 470 | return ret; |
| 471 | if (count < ret) |
| 472 | ret = count; |
| 473 | |
| 474 | sum += ret; |
| 475 | } |
| 476 | return sum; |
| 477 | } |
| 478 | } |
| 479 | return raw_pwrite_aligned(bs, offset, buf, count) + sum; |
| 480 | } |
| 481 | |
aliguori | eda578e | 2009-03-12 19:57:16 +0000 | [diff] [blame] | 482 | static int raw_write(BlockDriverState *bs, int64_t sector_num, |
| 483 | const uint8_t *buf, int nb_sectors) |
| 484 | { |
aliguori | 537a1d4 | 2009-03-13 03:12:03 +0000 | [diff] [blame] | 485 | int ret; |
| 486 | ret = raw_pwrite(bs, sector_num * 512, buf, nb_sectors * 512); |
| 487 | if (ret == (nb_sectors * 512)) |
| 488 | ret = 0; |
| 489 | return ret; |
aliguori | eda578e | 2009-03-12 19:57:16 +0000 | [diff] [blame] | 490 | } |
| 491 | |
blueswir1 | 414f0da | 2008-08-15 18:20:52 +0000 | [diff] [blame] | 492 | #ifdef CONFIG_AIO |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 493 | /***********************************************************/ |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 494 | /* Unix AIO using POSIX AIO */ |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 495 | |
| 496 | typedef struct RawAIOCB { |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 497 | BlockDriverAIOCB common; |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 498 | struct qemu_paiocb aiocb; |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 499 | struct RawAIOCB *next; |
bellard | bed5cc5 | 2008-05-28 09:51:09 +0000 | [diff] [blame] | 500 | int ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 501 | } RawAIOCB; |
| 502 | |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 503 | typedef struct PosixAioState |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 504 | { |
aliguori | 9e472e1 | 2008-10-08 19:50:24 +0000 | [diff] [blame] | 505 | int rfd, wfd; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 506 | RawAIOCB *first_aio; |
| 507 | } PosixAioState; |
| 508 | |
| 509 | static void posix_aio_read(void *opaque) |
| 510 | { |
| 511 | PosixAioState *s = opaque; |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 512 | RawAIOCB *acb, **pacb; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 513 | int ret; |
aliguori | 9e472e1 | 2008-10-08 19:50:24 +0000 | [diff] [blame] | 514 | ssize_t len; |
aliguori | 2c41a5f | 2008-09-11 14:32:27 +0000 | [diff] [blame] | 515 | |
aliguori | e20e830 | 2008-11-13 19:23:17 +0000 | [diff] [blame] | 516 | /* read all bytes from signal pipe */ |
| 517 | for (;;) { |
| 518 | char bytes[16]; |
aliguori | 2c41a5f | 2008-09-11 14:32:27 +0000 | [diff] [blame] | 519 | |
aliguori | e20e830 | 2008-11-13 19:23:17 +0000 | [diff] [blame] | 520 | len = read(s->rfd, bytes, sizeof(bytes)); |
aliguori | 2c41a5f | 2008-09-11 14:32:27 +0000 | [diff] [blame] | 521 | if (len == -1 && errno == EINTR) |
aliguori | e20e830 | 2008-11-13 19:23:17 +0000 | [diff] [blame] | 522 | continue; /* try again */ |
| 523 | if (len == sizeof(bytes)) |
| 524 | continue; /* more to read */ |
| 525 | break; |
| 526 | } |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 527 | |
| 528 | for(;;) { |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 529 | pacb = &s->first_aio; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 530 | for(;;) { |
| 531 | acb = *pacb; |
| 532 | if (!acb) |
| 533 | goto the_end; |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 534 | ret = qemu_paio_error(&acb->aiocb); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 535 | if (ret == ECANCELED) { |
| 536 | /* remove the request */ |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 537 | *pacb = acb->next; |
| 538 | qemu_aio_release(acb); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 539 | } else if (ret != EINPROGRESS) { |
| 540 | /* end of aio */ |
| 541 | if (ret == 0) { |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 542 | ret = qemu_paio_return(&acb->aiocb); |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 543 | if (ret == acb->aiocb.aio_nbytes) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 544 | ret = 0; |
| 545 | else |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 546 | ret = -EINVAL; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 547 | } else { |
| 548 | ret = -ret; |
| 549 | } |
| 550 | /* remove the request */ |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 551 | *pacb = acb->next; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 552 | /* call the callback */ |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 553 | acb->common.cb(acb->common.opaque, ret); |
| 554 | qemu_aio_release(acb); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 555 | break; |
| 556 | } else { |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 557 | pacb = &acb->next; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 558 | } |
| 559 | } |
| 560 | } |
| 561 | the_end: ; |
| 562 | } |
| 563 | |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 564 | static int posix_aio_flush(void *opaque) |
| 565 | { |
| 566 | PosixAioState *s = opaque; |
| 567 | return !!s->first_aio; |
| 568 | } |
| 569 | |
| 570 | static PosixAioState *posix_aio_state; |
| 571 | |
aliguori | 9e472e1 | 2008-10-08 19:50:24 +0000 | [diff] [blame] | 572 | static void aio_signal_handler(int signum) |
| 573 | { |
| 574 | if (posix_aio_state) { |
| 575 | char byte = 0; |
| 576 | |
| 577 | write(posix_aio_state->wfd, &byte, sizeof(byte)); |
| 578 | } |
| 579 | |
| 580 | qemu_service_io(); |
| 581 | } |
| 582 | |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 583 | static int posix_aio_init(void) |
aliguori | baf35cb | 2008-09-10 15:45:19 +0000 | [diff] [blame] | 584 | { |
aliguori | 9e472e1 | 2008-10-08 19:50:24 +0000 | [diff] [blame] | 585 | struct sigaction act; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 586 | PosixAioState *s; |
aliguori | 9e472e1 | 2008-10-08 19:50:24 +0000 | [diff] [blame] | 587 | int fds[2]; |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 588 | struct qemu_paioinit ai; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 589 | |
| 590 | if (posix_aio_state) |
| 591 | return 0; |
aliguori | baf35cb | 2008-09-10 15:45:19 +0000 | [diff] [blame] | 592 | |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 593 | s = qemu_malloc(sizeof(PosixAioState)); |
aliguori | baf35cb | 2008-09-10 15:45:19 +0000 | [diff] [blame] | 594 | |
aliguori | 9e472e1 | 2008-10-08 19:50:24 +0000 | [diff] [blame] | 595 | sigfillset(&act.sa_mask); |
| 596 | act.sa_flags = 0; /* do not restart syscalls to interrupt select() */ |
| 597 | act.sa_handler = aio_signal_handler; |
| 598 | sigaction(SIGUSR2, &act, NULL); |
| 599 | |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 600 | s->first_aio = NULL; |
aliguori | 9e472e1 | 2008-10-08 19:50:24 +0000 | [diff] [blame] | 601 | if (pipe(fds) == -1) { |
| 602 | fprintf(stderr, "failed to create pipe\n"); |
aliguori | 2746310 | 2008-09-27 20:58:43 +0000 | [diff] [blame] | 603 | return -errno; |
| 604 | } |
aliguori | 2c41a5f | 2008-09-11 14:32:27 +0000 | [diff] [blame] | 605 | |
aliguori | 9e472e1 | 2008-10-08 19:50:24 +0000 | [diff] [blame] | 606 | s->rfd = fds[0]; |
| 607 | s->wfd = fds[1]; |
aliguori | 2c41a5f | 2008-09-11 14:32:27 +0000 | [diff] [blame] | 608 | |
aliguori | e20e830 | 2008-11-13 19:23:17 +0000 | [diff] [blame] | 609 | fcntl(s->rfd, F_SETFL, O_NONBLOCK); |
aliguori | 9e472e1 | 2008-10-08 19:50:24 +0000 | [diff] [blame] | 610 | fcntl(s->wfd, F_SETFL, O_NONBLOCK); |
| 611 | |
| 612 | qemu_aio_set_fd_handler(s->rfd, posix_aio_read, NULL, posix_aio_flush, s); |
aliguori | baf35cb | 2008-09-10 15:45:19 +0000 | [diff] [blame] | 613 | |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 614 | memset(&ai, 0, sizeof(ai)); |
| 615 | ai.aio_threads = 64; |
| 616 | ai.aio_num = 64; |
| 617 | qemu_paio_init(&ai); |
aliguori | acce87f | 2008-09-26 16:12:14 +0000 | [diff] [blame] | 618 | |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 619 | posix_aio_state = s; |
aliguori | baf35cb | 2008-09-10 15:45:19 +0000 | [diff] [blame] | 620 | |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 621 | return 0; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Christoph Hellwig | c16b5a2 | 2009-05-25 12:37:32 +0200 | [diff] [blame] | 624 | static void raw_aio_remove(RawAIOCB *acb) |
| 625 | { |
| 626 | RawAIOCB **pacb; |
| 627 | |
| 628 | /* remove the callback from the queue */ |
| 629 | pacb = &posix_aio_state->first_aio; |
| 630 | for(;;) { |
| 631 | if (*pacb == NULL) { |
| 632 | fprintf(stderr, "raw_aio_remove: aio request not found!\n"); |
| 633 | break; |
| 634 | } else if (*pacb == acb) { |
| 635 | *pacb = acb->next; |
| 636 | qemu_aio_release(acb); |
| 637 | break; |
| 638 | } |
| 639 | pacb = &(*pacb)->next; |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | static void raw_aio_cancel(BlockDriverAIOCB *blockacb) |
| 644 | { |
| 645 | int ret; |
| 646 | RawAIOCB *acb = (RawAIOCB *)blockacb; |
| 647 | |
| 648 | ret = qemu_paio_cancel(acb->aiocb.aio_fildes, &acb->aiocb); |
| 649 | if (ret == QEMU_PAIO_NOTCANCELED) { |
| 650 | /* fail safe: if the aio could not be canceled, we wait for |
| 651 | it */ |
| 652 | while (qemu_paio_error(&acb->aiocb) == EINPROGRESS); |
| 653 | } |
| 654 | |
| 655 | raw_aio_remove(acb); |
| 656 | } |
| 657 | |
| 658 | static AIOPool raw_aio_pool = { |
| 659 | .aiocb_size = sizeof(RawAIOCB), |
| 660 | .cancel = raw_aio_cancel, |
| 661 | }; |
| 662 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 663 | static RawAIOCB *raw_aio_setup(BlockDriverState *bs, int64_t sector_num, |
| 664 | QEMUIOVector *qiov, int nb_sectors, |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 665 | BlockDriverCompletionFunc *cb, void *opaque) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 666 | { |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 667 | BDRVRawState *s = bs->opaque; |
| 668 | RawAIOCB *acb; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 669 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 670 | if (fd_open(bs) < 0) |
| 671 | return NULL; |
| 672 | |
Christoph Hellwig | c16b5a2 | 2009-05-25 12:37:32 +0200 | [diff] [blame] | 673 | acb = qemu_aio_get(&raw_aio_pool, bs, cb, opaque); |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 674 | if (!acb) |
| 675 | return NULL; |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 676 | acb->aiocb.aio_fildes = s->fd; |
blueswir1 | 55f11ca | 2009-01-24 11:54:21 +0000 | [diff] [blame] | 677 | acb->aiocb.ev_signo = SIGUSR2; |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 678 | acb->aiocb.aio_iov = qiov->iov; |
| 679 | acb->aiocb.aio_niov = qiov->niov; |
| 680 | acb->aiocb.aio_nbytes = nb_sectors * 512; |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 681 | acb->aiocb.aio_offset = sector_num * 512; |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 682 | acb->aiocb.aio_flags = 0; |
| 683 | |
| 684 | /* |
| 685 | * If O_DIRECT is used the buffer needs to be aligned on a sector |
| 686 | * boundary. Tell the low level code to ensure that in case it's |
| 687 | * not done yet. |
| 688 | */ |
| 689 | if (s->aligned_buf) |
| 690 | acb->aiocb.aio_flags |= QEMU_AIO_SECTOR_ALIGNED; |
| 691 | |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 692 | acb->next = posix_aio_state->first_aio; |
| 693 | posix_aio_state->first_aio = acb; |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 694 | return acb; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 695 | } |
| 696 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 697 | static BlockDriverAIOCB *raw_aio_readv(BlockDriverState *bs, |
| 698 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 699 | BlockDriverCompletionFunc *cb, void *opaque) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 700 | { |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 701 | RawAIOCB *acb; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 702 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 703 | acb = raw_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque); |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 704 | if (!acb) |
| 705 | return NULL; |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 706 | if (qemu_paio_read(&acb->aiocb) < 0) { |
aliguori | 22bf145 | 2009-01-29 17:02:08 +0000 | [diff] [blame] | 707 | raw_aio_remove(acb); |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 708 | return NULL; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 709 | } |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 710 | return &acb->common; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 711 | } |
| 712 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 713 | static BlockDriverAIOCB *raw_aio_writev(BlockDriverState *bs, |
| 714 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 715 | BlockDriverCompletionFunc *cb, void *opaque) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 716 | { |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 717 | RawAIOCB *acb; |
| 718 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 719 | acb = raw_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque); |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 720 | if (!acb) |
| 721 | return NULL; |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 722 | if (qemu_paio_write(&acb->aiocb) < 0) { |
aliguori | 22bf145 | 2009-01-29 17:02:08 +0000 | [diff] [blame] | 723 | raw_aio_remove(acb); |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 724 | return NULL; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 725 | } |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 726 | return &acb->common; |
| 727 | } |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 728 | #else /* CONFIG_AIO */ |
| 729 | static int posix_aio_init(void) |
blueswir1 | 414f0da | 2008-08-15 18:20:52 +0000 | [diff] [blame] | 730 | { |
blueswir1 | 674a24a | 2008-10-03 19:00:40 +0000 | [diff] [blame] | 731 | return 0; |
blueswir1 | 414f0da | 2008-08-15 18:20:52 +0000 | [diff] [blame] | 732 | } |
blueswir1 | 414f0da | 2008-08-15 18:20:52 +0000 | [diff] [blame] | 733 | #endif /* CONFIG_AIO */ |
| 734 | |
aliguori | 5353872 | 2008-09-26 15:59:29 +0000 | [diff] [blame] | 735 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 736 | static void raw_close(BlockDriverState *bs) |
| 737 | { |
| 738 | BDRVRawState *s = bs->opaque; |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 739 | if (s->fd >= 0) { |
| 740 | close(s->fd); |
| 741 | s->fd = -1; |
bellard | bed5cc5 | 2008-05-28 09:51:09 +0000 | [diff] [blame] | 742 | if (s->aligned_buf != NULL) |
| 743 | qemu_free(s->aligned_buf); |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 744 | } |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | static int raw_truncate(BlockDriverState *bs, int64_t offset) |
| 748 | { |
| 749 | BDRVRawState *s = bs->opaque; |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 750 | if (s->type != FTYPE_FILE) |
| 751 | return -ENOTSUP; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 752 | if (ftruncate(s->fd, offset) < 0) |
| 753 | return -errno; |
| 754 | return 0; |
| 755 | } |
| 756 | |
blueswir1 | 128ab2f | 2008-08-15 18:33:42 +0000 | [diff] [blame] | 757 | #ifdef __OpenBSD__ |
| 758 | static int64_t raw_getlength(BlockDriverState *bs) |
| 759 | { |
| 760 | BDRVRawState *s = bs->opaque; |
| 761 | int fd = s->fd; |
| 762 | struct stat st; |
| 763 | |
| 764 | if (fstat(fd, &st)) |
| 765 | return -1; |
| 766 | if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) { |
| 767 | struct disklabel dl; |
| 768 | |
| 769 | if (ioctl(fd, DIOCGDINFO, &dl)) |
| 770 | return -1; |
| 771 | return (uint64_t)dl.d_secsize * |
| 772 | dl.d_partitions[DISKPART(st.st_rdev)].p_size; |
| 773 | } else |
| 774 | return st.st_size; |
| 775 | } |
| 776 | #else /* !__OpenBSD__ */ |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 777 | static int64_t raw_getlength(BlockDriverState *bs) |
| 778 | { |
| 779 | BDRVRawState *s = bs->opaque; |
| 780 | int fd = s->fd; |
| 781 | int64_t size; |
Juan Quintela | 71e72a1 | 2009-07-27 16:12:56 +0200 | [diff] [blame] | 782 | #ifdef CONFIG_BSD |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 783 | struct stat sb; |
blueswir1 | 9f23011 | 2009-03-28 08:37:13 +0000 | [diff] [blame] | 784 | #ifdef __FreeBSD__ |
| 785 | int reopened = 0; |
| 786 | #endif |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 787 | #endif |
| 788 | #ifdef __sun__ |
| 789 | struct dk_minfo minfo; |
| 790 | int rv; |
| 791 | #endif |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 792 | int ret; |
| 793 | |
| 794 | ret = fd_open(bs); |
| 795 | if (ret < 0) |
| 796 | return ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 797 | |
Juan Quintela | 71e72a1 | 2009-07-27 16:12:56 +0200 | [diff] [blame] | 798 | #ifdef CONFIG_BSD |
blueswir1 | 9f23011 | 2009-03-28 08:37:13 +0000 | [diff] [blame] | 799 | #ifdef __FreeBSD__ |
| 800 | again: |
| 801 | #endif |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 802 | if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) { |
| 803 | #ifdef DIOCGMEDIASIZE |
| 804 | if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size)) |
blueswir1 | c5e9723 | 2009-03-07 20:06:23 +0000 | [diff] [blame] | 805 | #elif defined(DIOCGPART) |
| 806 | { |
| 807 | struct partinfo pi; |
| 808 | if (ioctl(fd, DIOCGPART, &pi) == 0) |
| 809 | size = pi.media_size; |
| 810 | else |
| 811 | size = 0; |
| 812 | } |
| 813 | if (size == 0) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 814 | #endif |
| 815 | #ifdef CONFIG_COCOA |
| 816 | size = LONG_LONG_MAX; |
| 817 | #else |
| 818 | size = lseek(fd, 0LL, SEEK_END); |
| 819 | #endif |
blueswir1 | 9f23011 | 2009-03-28 08:37:13 +0000 | [diff] [blame] | 820 | #ifdef __FreeBSD__ |
| 821 | switch(s->type) { |
| 822 | case FTYPE_CD: |
| 823 | /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */ |
| 824 | if (size == 2048LL * (unsigned)-1) |
| 825 | size = 0; |
| 826 | /* XXX no disc? maybe we need to reopen... */ |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 827 | if (size <= 0 && !reopened && cdrom_reopen(bs) >= 0) { |
blueswir1 | 9f23011 | 2009-03-28 08:37:13 +0000 | [diff] [blame] | 828 | reopened = 1; |
| 829 | goto again; |
| 830 | } |
| 831 | } |
| 832 | #endif |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 833 | } else |
| 834 | #endif |
| 835 | #ifdef __sun__ |
| 836 | /* |
| 837 | * use the DKIOCGMEDIAINFO ioctl to read the size. |
| 838 | */ |
| 839 | rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo ); |
| 840 | if ( rv != -1 ) { |
| 841 | size = minfo.dki_lbsize * minfo.dki_capacity; |
| 842 | } else /* there are reports that lseek on some devices |
| 843 | fails, but irc discussion said that contingency |
| 844 | on contingency was overkill */ |
| 845 | #endif |
| 846 | { |
| 847 | size = lseek(fd, 0, SEEK_END); |
| 848 | } |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 849 | return size; |
| 850 | } |
blueswir1 | 128ab2f | 2008-08-15 18:33:42 +0000 | [diff] [blame] | 851 | #endif |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 852 | |
Kevin Wolf | 0e7e198 | 2009-05-18 16:42:10 +0200 | [diff] [blame] | 853 | static int raw_create(const char *filename, QEMUOptionParameter *options) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 854 | { |
| 855 | int fd; |
Stefan Weil | 1e37d05 | 2009-07-11 16:43:37 +0200 | [diff] [blame] | 856 | int result = 0; |
Kevin Wolf | 0e7e198 | 2009-05-18 16:42:10 +0200 | [diff] [blame] | 857 | int64_t total_size = 0; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 858 | |
Kevin Wolf | 0e7e198 | 2009-05-18 16:42:10 +0200 | [diff] [blame] | 859 | /* Read out options */ |
| 860 | while (options && options->name) { |
| 861 | if (!strcmp(options->name, BLOCK_OPT_SIZE)) { |
| 862 | total_size = options->value.n / 512; |
| 863 | } |
| 864 | options++; |
| 865 | } |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 866 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 867 | fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 868 | 0644); |
Stefan Weil | 1e37d05 | 2009-07-11 16:43:37 +0200 | [diff] [blame] | 869 | if (fd < 0) { |
| 870 | result = -errno; |
| 871 | } else { |
| 872 | if (ftruncate(fd, total_size * 512) != 0) { |
| 873 | result = -errno; |
| 874 | } |
| 875 | if (close(fd) != 0) { |
| 876 | result = -errno; |
| 877 | } |
| 878 | } |
| 879 | return result; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | static void raw_flush(BlockDriverState *bs) |
| 883 | { |
| 884 | BDRVRawState *s = bs->opaque; |
| 885 | fsync(s->fd); |
| 886 | } |
| 887 | |
Kevin Wolf | 0e7e198 | 2009-05-18 16:42:10 +0200 | [diff] [blame] | 888 | |
| 889 | static QEMUOptionParameter raw_create_options[] = { |
Kevin Wolf | db08adf | 2009-06-04 15:39:38 +0200 | [diff] [blame] | 890 | { |
| 891 | .name = BLOCK_OPT_SIZE, |
| 892 | .type = OPT_SIZE, |
| 893 | .help = "Virtual disk size" |
| 894 | }, |
Kevin Wolf | 0e7e198 | 2009-05-18 16:42:10 +0200 | [diff] [blame] | 895 | { NULL } |
| 896 | }; |
| 897 | |
Anthony Liguori | 5efa9d5 | 2009-05-09 17:03:42 -0500 | [diff] [blame] | 898 | static BlockDriver bdrv_raw = { |
blueswir1 | 856ae5c | 2009-04-07 17:57:09 +0000 | [diff] [blame] | 899 | .format_name = "raw", |
| 900 | .instance_size = sizeof(BDRVRawState), |
| 901 | .bdrv_probe = NULL, /* no probe for protocols */ |
| 902 | .bdrv_open = raw_open, |
| 903 | .bdrv_read = raw_read, |
| 904 | .bdrv_write = raw_write, |
| 905 | .bdrv_close = raw_close, |
| 906 | .bdrv_create = raw_create, |
| 907 | .bdrv_flush = raw_flush, |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 908 | |
blueswir1 | 414f0da | 2008-08-15 18:20:52 +0000 | [diff] [blame] | 909 | #ifdef CONFIG_AIO |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 910 | .bdrv_aio_readv = raw_aio_readv, |
| 911 | .bdrv_aio_writev = raw_aio_writev, |
blueswir1 | 414f0da | 2008-08-15 18:20:52 +0000 | [diff] [blame] | 912 | #endif |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 913 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 914 | .bdrv_truncate = raw_truncate, |
| 915 | .bdrv_getlength = raw_getlength, |
Kevin Wolf | 0e7e198 | 2009-05-18 16:42:10 +0200 | [diff] [blame] | 916 | |
| 917 | .create_options = raw_create_options, |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 918 | }; |
| 919 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 920 | /***********************************************/ |
| 921 | /* host device */ |
| 922 | |
| 923 | #ifdef CONFIG_COCOA |
| 924 | static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator ); |
| 925 | static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize ); |
| 926 | |
| 927 | kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator ) |
| 928 | { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 929 | kern_return_t kernResult; |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 930 | mach_port_t masterPort; |
| 931 | CFMutableDictionaryRef classesToMatch; |
| 932 | |
| 933 | kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort ); |
| 934 | if ( KERN_SUCCESS != kernResult ) { |
| 935 | printf( "IOMasterPort returned %d\n", kernResult ); |
| 936 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 937 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 938 | classesToMatch = IOServiceMatching( kIOCDMediaClass ); |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 939 | if ( classesToMatch == NULL ) { |
| 940 | printf( "IOServiceMatching returned a NULL dictionary.\n" ); |
| 941 | } else { |
| 942 | CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue ); |
| 943 | } |
| 944 | kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator ); |
| 945 | if ( KERN_SUCCESS != kernResult ) |
| 946 | { |
| 947 | printf( "IOServiceGetMatchingServices returned %d\n", kernResult ); |
| 948 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 949 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 950 | return kernResult; |
| 951 | } |
| 952 | |
| 953 | kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize ) |
| 954 | { |
| 955 | io_object_t nextMedia; |
| 956 | kern_return_t kernResult = KERN_FAILURE; |
| 957 | *bsdPath = '\0'; |
| 958 | nextMedia = IOIteratorNext( mediaIterator ); |
| 959 | if ( nextMedia ) |
| 960 | { |
| 961 | CFTypeRef bsdPathAsCFString; |
| 962 | bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 ); |
| 963 | if ( bsdPathAsCFString ) { |
| 964 | size_t devPathLength; |
| 965 | strcpy( bsdPath, _PATH_DEV ); |
| 966 | strcat( bsdPath, "r" ); |
| 967 | devPathLength = strlen( bsdPath ); |
| 968 | if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) { |
| 969 | kernResult = KERN_SUCCESS; |
| 970 | } |
| 971 | CFRelease( bsdPathAsCFString ); |
| 972 | } |
| 973 | IOObjectRelease( nextMedia ); |
| 974 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 975 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 976 | return kernResult; |
| 977 | } |
| 978 | |
| 979 | #endif |
| 980 | |
Christoph Hellwig | 508c7cb | 2009-06-15 14:04:22 +0200 | [diff] [blame] | 981 | static int hdev_probe_device(const char *filename) |
| 982 | { |
| 983 | struct stat st; |
| 984 | |
| 985 | /* allow a dedicated CD-ROM driver to match with a higher priority */ |
| 986 | if (strstart(filename, "/dev/cdrom", NULL)) |
| 987 | return 50; |
| 988 | |
| 989 | if (stat(filename, &st) >= 0 && |
| 990 | (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) { |
| 991 | return 100; |
| 992 | } |
| 993 | |
| 994 | return 0; |
| 995 | } |
| 996 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 997 | static int hdev_open(BlockDriverState *bs, const char *filename, int flags) |
| 998 | { |
| 999 | BDRVRawState *s = bs->opaque; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 1000 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1001 | #ifdef CONFIG_COCOA |
| 1002 | if (strstart(filename, "/dev/cdrom", NULL)) { |
| 1003 | kern_return_t kernResult; |
| 1004 | io_iterator_t mediaIterator; |
| 1005 | char bsdPath[ MAXPATHLEN ]; |
| 1006 | int fd; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1007 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1008 | kernResult = FindEjectableCDMedia( &mediaIterator ); |
| 1009 | kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) ); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1010 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1011 | if ( bsdPath[ 0 ] != '\0' ) { |
| 1012 | strcat(bsdPath,"s0"); |
| 1013 | /* some CDs don't have a partition 0 */ |
| 1014 | fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE); |
| 1015 | if (fd < 0) { |
| 1016 | bsdPath[strlen(bsdPath)-1] = '1'; |
| 1017 | } else { |
| 1018 | close(fd); |
| 1019 | } |
| 1020 | filename = bsdPath; |
| 1021 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1022 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1023 | if ( mediaIterator ) |
| 1024 | IOObjectRelease( mediaIterator ); |
| 1025 | } |
| 1026 | #endif |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1027 | |
| 1028 | s->type = FTYPE_FILE; |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 1029 | #if defined(__linux__) && defined(CONFIG_AIO) |
| 1030 | if (strstart(filename, "/dev/sg", NULL)) { |
ths | 985a03b | 2007-12-24 16:10:43 +0000 | [diff] [blame] | 1031 | bs->sg = 1; |
blueswir1 | 9f23011 | 2009-03-28 08:37:13 +0000 | [diff] [blame] | 1032 | } |
| 1033 | #endif |
Christoph Hellwig | 90babde | 2009-06-15 13:53:38 +0200 | [diff] [blame] | 1034 | |
Blue Swirl | 19a3da7 | 2009-06-17 18:27:44 +0300 | [diff] [blame] | 1035 | return raw_open_common(bs, filename, flags, 0); |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
aliguori | 03ff3ca | 2008-09-15 15:51:35 +0000 | [diff] [blame] | 1038 | #if defined(__linux__) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1039 | /* Note: we do not have a reliable method to detect if the floppy is |
| 1040 | present. The current method is to try to open the floppy at every |
| 1041 | I/O and to keep it opened during a few hundreds of ms. */ |
| 1042 | static int fd_open(BlockDriverState *bs) |
| 1043 | { |
| 1044 | BDRVRawState *s = bs->opaque; |
| 1045 | int last_media_present; |
| 1046 | |
| 1047 | if (s->type != FTYPE_FD) |
| 1048 | return 0; |
| 1049 | last_media_present = (s->fd >= 0); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1050 | if (s->fd >= 0 && |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1051 | (qemu_get_clock(rt_clock) - s->fd_open_time) >= FD_OPEN_TIMEOUT) { |
| 1052 | close(s->fd); |
| 1053 | s->fd = -1; |
| 1054 | #ifdef DEBUG_FLOPPY |
| 1055 | printf("Floppy closed\n"); |
| 1056 | #endif |
| 1057 | } |
| 1058 | if (s->fd < 0) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1059 | if (s->fd_got_error && |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1060 | (qemu_get_clock(rt_clock) - s->fd_error_time) < FD_OPEN_TIMEOUT) { |
| 1061 | #ifdef DEBUG_FLOPPY |
| 1062 | printf("No floppy (open delayed)\n"); |
| 1063 | #endif |
| 1064 | return -EIO; |
| 1065 | } |
Christoph Hellwig | 0e1d8f4 | 2009-06-15 13:53:26 +0200 | [diff] [blame] | 1066 | s->fd = open(bs->filename, s->open_flags & ~O_NONBLOCK); |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1067 | if (s->fd < 0) { |
| 1068 | s->fd_error_time = qemu_get_clock(rt_clock); |
| 1069 | s->fd_got_error = 1; |
| 1070 | if (last_media_present) |
| 1071 | s->fd_media_changed = 1; |
| 1072 | #ifdef DEBUG_FLOPPY |
| 1073 | printf("No floppy\n"); |
| 1074 | #endif |
| 1075 | return -EIO; |
| 1076 | } |
| 1077 | #ifdef DEBUG_FLOPPY |
| 1078 | printf("Floppy opened\n"); |
| 1079 | #endif |
| 1080 | } |
| 1081 | if (!last_media_present) |
| 1082 | s->fd_media_changed = 1; |
| 1083 | s->fd_open_time = qemu_get_clock(rt_clock); |
| 1084 | s->fd_got_error = 0; |
| 1085 | return 0; |
| 1086 | } |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1087 | |
Christoph Hellwig | 63ec93d | 2009-06-15 14:04:34 +0200 | [diff] [blame] | 1088 | static int hdev_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) |
ths | 985a03b | 2007-12-24 16:10:43 +0000 | [diff] [blame] | 1089 | { |
| 1090 | BDRVRawState *s = bs->opaque; |
| 1091 | |
| 1092 | return ioctl(s->fd, req, buf); |
| 1093 | } |
aliguori | 221f715 | 2009-03-28 17:28:41 +0000 | [diff] [blame] | 1094 | |
aliguori | 8185d2c | 2009-04-05 19:10:50 +0000 | [diff] [blame] | 1095 | #ifdef CONFIG_AIO |
Christoph Hellwig | 63ec93d | 2009-06-15 14:04:34 +0200 | [diff] [blame] | 1096 | static BlockDriverAIOCB *hdev_aio_ioctl(BlockDriverState *bs, |
aliguori | 221f715 | 2009-03-28 17:28:41 +0000 | [diff] [blame] | 1097 | unsigned long int req, void *buf, |
| 1098 | BlockDriverCompletionFunc *cb, void *opaque) |
| 1099 | { |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1100 | BDRVRawState *s = bs->opaque; |
aliguori | 221f715 | 2009-03-28 17:28:41 +0000 | [diff] [blame] | 1101 | RawAIOCB *acb; |
| 1102 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1103 | if (fd_open(bs) < 0) |
aliguori | 221f715 | 2009-03-28 17:28:41 +0000 | [diff] [blame] | 1104 | return NULL; |
| 1105 | |
Christoph Hellwig | c16b5a2 | 2009-05-25 12:37:32 +0200 | [diff] [blame] | 1106 | acb = qemu_aio_get(&raw_aio_pool, bs, cb, opaque); |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1107 | if (!acb) |
| 1108 | return NULL; |
| 1109 | acb->aiocb.aio_fildes = s->fd; |
| 1110 | acb->aiocb.ev_signo = SIGUSR2; |
| 1111 | acb->aiocb.aio_offset = 0; |
| 1112 | acb->aiocb.aio_flags = 0; |
| 1113 | |
| 1114 | acb->next = posix_aio_state->first_aio; |
| 1115 | posix_aio_state->first_aio = acb; |
| 1116 | |
| 1117 | acb->aiocb.aio_ioctl_buf = buf; |
aliguori | 221f715 | 2009-03-28 17:28:41 +0000 | [diff] [blame] | 1118 | acb->aiocb.aio_ioctl_cmd = req; |
| 1119 | if (qemu_paio_ioctl(&acb->aiocb) < 0) { |
| 1120 | raw_aio_remove(acb); |
| 1121 | return NULL; |
| 1122 | } |
| 1123 | |
| 1124 | return &acb->common; |
| 1125 | } |
aliguori | 8185d2c | 2009-04-05 19:10:50 +0000 | [diff] [blame] | 1126 | #endif |
aliguori | 221f715 | 2009-03-28 17:28:41 +0000 | [diff] [blame] | 1127 | |
blueswir1 | 9f23011 | 2009-03-28 08:37:13 +0000 | [diff] [blame] | 1128 | #elif defined(__FreeBSD__) |
blueswir1 | 9f23011 | 2009-03-28 08:37:13 +0000 | [diff] [blame] | 1129 | static int fd_open(BlockDriverState *bs) |
| 1130 | { |
| 1131 | BDRVRawState *s = bs->opaque; |
| 1132 | |
| 1133 | /* this is just to ensure s->fd is sane (its called by io ops) */ |
| 1134 | if (s->fd >= 0) |
| 1135 | return 0; |
| 1136 | return -EIO; |
| 1137 | } |
blueswir1 | 9f23011 | 2009-03-28 08:37:13 +0000 | [diff] [blame] | 1138 | #else /* !linux && !FreeBSD */ |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1139 | |
aliguori | 08af02e | 2008-09-15 16:48:11 +0000 | [diff] [blame] | 1140 | static int fd_open(BlockDriverState *bs) |
| 1141 | { |
| 1142 | return 0; |
| 1143 | } |
| 1144 | |
blueswir1 | 9f23011 | 2009-03-28 08:37:13 +0000 | [diff] [blame] | 1145 | #endif /* !linux && !FreeBSD */ |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1146 | |
Kevin Wolf | 0e7e198 | 2009-05-18 16:42:10 +0200 | [diff] [blame] | 1147 | static int hdev_create(const char *filename, QEMUOptionParameter *options) |
aliguori | 93c65b4 | 2009-04-05 17:40:43 +0000 | [diff] [blame] | 1148 | { |
| 1149 | int fd; |
| 1150 | int ret = 0; |
| 1151 | struct stat stat_buf; |
Kevin Wolf | 0e7e198 | 2009-05-18 16:42:10 +0200 | [diff] [blame] | 1152 | int64_t total_size = 0; |
aliguori | 93c65b4 | 2009-04-05 17:40:43 +0000 | [diff] [blame] | 1153 | |
Kevin Wolf | 0e7e198 | 2009-05-18 16:42:10 +0200 | [diff] [blame] | 1154 | /* Read out options */ |
| 1155 | while (options && options->name) { |
| 1156 | if (!strcmp(options->name, "size")) { |
| 1157 | total_size = options->value.n / 512; |
| 1158 | } |
| 1159 | options++; |
| 1160 | } |
aliguori | 93c65b4 | 2009-04-05 17:40:43 +0000 | [diff] [blame] | 1161 | |
| 1162 | fd = open(filename, O_WRONLY | O_BINARY); |
| 1163 | if (fd < 0) |
| 1164 | return -EIO; |
| 1165 | |
| 1166 | if (fstat(fd, &stat_buf) < 0) |
| 1167 | ret = -EIO; |
Christoph Hellwig | 4099df5 | 2009-05-25 10:29:13 +0200 | [diff] [blame] | 1168 | else if (!S_ISBLK(stat_buf.st_mode) && !S_ISCHR(stat_buf.st_mode)) |
aliguori | 93c65b4 | 2009-04-05 17:40:43 +0000 | [diff] [blame] | 1169 | ret = -EIO; |
| 1170 | else if (lseek(fd, 0, SEEK_END) < total_size * 512) |
| 1171 | ret = -ENOSPC; |
| 1172 | |
| 1173 | close(fd); |
| 1174 | return ret; |
| 1175 | } |
| 1176 | |
Anthony Liguori | 5efa9d5 | 2009-05-09 17:03:42 -0500 | [diff] [blame] | 1177 | static BlockDriver bdrv_host_device = { |
aurel32 | e60f469 | 2009-03-07 22:00:29 +0000 | [diff] [blame] | 1178 | .format_name = "host_device", |
| 1179 | .instance_size = sizeof(BDRVRawState), |
Christoph Hellwig | 508c7cb | 2009-06-15 14:04:22 +0200 | [diff] [blame] | 1180 | .bdrv_probe_device = hdev_probe_device, |
aurel32 | e60f469 | 2009-03-07 22:00:29 +0000 | [diff] [blame] | 1181 | .bdrv_open = hdev_open, |
| 1182 | .bdrv_close = raw_close, |
aliguori | 93c65b4 | 2009-04-05 17:40:43 +0000 | [diff] [blame] | 1183 | .bdrv_create = hdev_create, |
aurel32 | e60f469 | 2009-03-07 22:00:29 +0000 | [diff] [blame] | 1184 | .bdrv_flush = raw_flush, |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1185 | |
blueswir1 | 414f0da | 2008-08-15 18:20:52 +0000 | [diff] [blame] | 1186 | #ifdef CONFIG_AIO |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1187 | .bdrv_aio_readv = raw_aio_readv, |
| 1188 | .bdrv_aio_writev = raw_aio_writev, |
blueswir1 | 414f0da | 2008-08-15 18:20:52 +0000 | [diff] [blame] | 1189 | #endif |
aliguori | 3c529d9 | 2008-12-12 16:41:40 +0000 | [diff] [blame] | 1190 | |
aliguori | eda578e | 2009-03-12 19:57:16 +0000 | [diff] [blame] | 1191 | .bdrv_read = raw_read, |
| 1192 | .bdrv_write = raw_write, |
aurel32 | e60f469 | 2009-03-07 22:00:29 +0000 | [diff] [blame] | 1193 | .bdrv_getlength = raw_getlength, |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1194 | |
ths | 985a03b | 2007-12-24 16:10:43 +0000 | [diff] [blame] | 1195 | /* generic scsi device */ |
Christoph Hellwig | 63ec93d | 2009-06-15 14:04:34 +0200 | [diff] [blame] | 1196 | #ifdef __linux__ |
| 1197 | .bdrv_ioctl = hdev_ioctl, |
aliguori | 8185d2c | 2009-04-05 19:10:50 +0000 | [diff] [blame] | 1198 | #ifdef CONFIG_AIO |
Christoph Hellwig | 63ec93d | 2009-06-15 14:04:34 +0200 | [diff] [blame] | 1199 | .bdrv_aio_ioctl = hdev_aio_ioctl, |
| 1200 | #endif |
aliguori | 8185d2c | 2009-04-05 19:10:50 +0000 | [diff] [blame] | 1201 | #endif |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1202 | }; |
Anthony Liguori | 5efa9d5 | 2009-05-09 17:03:42 -0500 | [diff] [blame] | 1203 | |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 1204 | #ifdef __linux__ |
| 1205 | static int floppy_open(BlockDriverState *bs, const char *filename, int flags) |
| 1206 | { |
| 1207 | BDRVRawState *s = bs->opaque; |
| 1208 | int ret; |
| 1209 | |
| 1210 | posix_aio_init(); |
| 1211 | |
| 1212 | s->type = FTYPE_FD; |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 1213 | |
Blue Swirl | 19a3da7 | 2009-06-17 18:27:44 +0300 | [diff] [blame] | 1214 | /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */ |
| 1215 | ret = raw_open_common(bs, filename, flags, O_NONBLOCK); |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 1216 | if (ret) |
| 1217 | return ret; |
| 1218 | |
| 1219 | /* close fd so that we can reopen it as needed */ |
| 1220 | close(s->fd); |
| 1221 | s->fd = -1; |
| 1222 | s->fd_media_changed = 1; |
| 1223 | |
| 1224 | return 0; |
| 1225 | } |
| 1226 | |
Christoph Hellwig | 508c7cb | 2009-06-15 14:04:22 +0200 | [diff] [blame] | 1227 | static int floppy_probe_device(const char *filename) |
| 1228 | { |
| 1229 | if (strstart(filename, "/dev/fd", NULL)) |
| 1230 | return 100; |
| 1231 | return 0; |
| 1232 | } |
| 1233 | |
| 1234 | |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 1235 | static int floppy_is_inserted(BlockDriverState *bs) |
| 1236 | { |
| 1237 | return fd_open(bs) >= 0; |
| 1238 | } |
| 1239 | |
| 1240 | static int floppy_media_changed(BlockDriverState *bs) |
| 1241 | { |
| 1242 | BDRVRawState *s = bs->opaque; |
| 1243 | int ret; |
| 1244 | |
| 1245 | /* |
| 1246 | * XXX: we do not have a true media changed indication. |
| 1247 | * It does not work if the floppy is changed without trying to read it. |
| 1248 | */ |
| 1249 | fd_open(bs); |
| 1250 | ret = s->fd_media_changed; |
| 1251 | s->fd_media_changed = 0; |
| 1252 | #ifdef DEBUG_FLOPPY |
| 1253 | printf("Floppy changed=%d\n", ret); |
| 1254 | #endif |
| 1255 | return ret; |
| 1256 | } |
| 1257 | |
| 1258 | static int floppy_eject(BlockDriverState *bs, int eject_flag) |
| 1259 | { |
| 1260 | BDRVRawState *s = bs->opaque; |
| 1261 | int fd; |
| 1262 | |
| 1263 | if (s->fd >= 0) { |
| 1264 | close(s->fd); |
| 1265 | s->fd = -1; |
| 1266 | } |
| 1267 | fd = open(bs->filename, s->open_flags | O_NONBLOCK); |
| 1268 | if (fd >= 0) { |
| 1269 | if (ioctl(fd, FDEJECT, 0) < 0) |
| 1270 | perror("FDEJECT"); |
| 1271 | close(fd); |
| 1272 | } |
| 1273 | |
| 1274 | return 0; |
| 1275 | } |
| 1276 | |
| 1277 | static BlockDriver bdrv_host_floppy = { |
| 1278 | .format_name = "host_floppy", |
| 1279 | .instance_size = sizeof(BDRVRawState), |
Christoph Hellwig | 508c7cb | 2009-06-15 14:04:22 +0200 | [diff] [blame] | 1280 | .bdrv_probe_device = floppy_probe_device, |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 1281 | .bdrv_open = floppy_open, |
| 1282 | .bdrv_close = raw_close, |
| 1283 | .bdrv_create = hdev_create, |
| 1284 | .bdrv_flush = raw_flush, |
| 1285 | |
| 1286 | #ifdef CONFIG_AIO |
| 1287 | .bdrv_aio_readv = raw_aio_readv, |
| 1288 | .bdrv_aio_writev = raw_aio_writev, |
| 1289 | #endif |
| 1290 | |
| 1291 | .bdrv_read = raw_read, |
| 1292 | .bdrv_write = raw_write, |
| 1293 | .bdrv_getlength = raw_getlength, |
| 1294 | |
| 1295 | /* removable device support */ |
| 1296 | .bdrv_is_inserted = floppy_is_inserted, |
| 1297 | .bdrv_media_changed = floppy_media_changed, |
| 1298 | .bdrv_eject = floppy_eject, |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 1299 | }; |
| 1300 | |
| 1301 | static int cdrom_open(BlockDriverState *bs, const char *filename, int flags) |
| 1302 | { |
| 1303 | BDRVRawState *s = bs->opaque; |
| 1304 | |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 1305 | s->type = FTYPE_CD; |
| 1306 | |
Blue Swirl | 19a3da7 | 2009-06-17 18:27:44 +0300 | [diff] [blame] | 1307 | /* open will not fail even if no CD is inserted, so add O_NONBLOCK */ |
| 1308 | return raw_open_common(bs, filename, flags, O_NONBLOCK); |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 1309 | } |
| 1310 | |
Christoph Hellwig | 508c7cb | 2009-06-15 14:04:22 +0200 | [diff] [blame] | 1311 | static int cdrom_probe_device(const char *filename) |
| 1312 | { |
| 1313 | if (strstart(filename, "/dev/cd", NULL)) |
| 1314 | return 100; |
| 1315 | return 0; |
| 1316 | } |
| 1317 | |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 1318 | static int cdrom_is_inserted(BlockDriverState *bs) |
| 1319 | { |
| 1320 | BDRVRawState *s = bs->opaque; |
| 1321 | int ret; |
| 1322 | |
| 1323 | ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT); |
| 1324 | if (ret == CDS_DISC_OK) |
| 1325 | return 1; |
| 1326 | return 0; |
| 1327 | } |
| 1328 | |
| 1329 | static int cdrom_eject(BlockDriverState *bs, int eject_flag) |
| 1330 | { |
| 1331 | BDRVRawState *s = bs->opaque; |
| 1332 | |
| 1333 | if (eject_flag) { |
| 1334 | if (ioctl(s->fd, CDROMEJECT, NULL) < 0) |
| 1335 | perror("CDROMEJECT"); |
| 1336 | } else { |
| 1337 | if (ioctl(s->fd, CDROMCLOSETRAY, NULL) < 0) |
| 1338 | perror("CDROMEJECT"); |
| 1339 | } |
| 1340 | |
| 1341 | return 0; |
| 1342 | } |
| 1343 | |
| 1344 | static int cdrom_set_locked(BlockDriverState *bs, int locked) |
| 1345 | { |
| 1346 | BDRVRawState *s = bs->opaque; |
| 1347 | |
| 1348 | if (ioctl(s->fd, CDROM_LOCKDOOR, locked) < 0) { |
| 1349 | /* |
| 1350 | * Note: an error can happen if the distribution automatically |
| 1351 | * mounts the CD-ROM |
| 1352 | */ |
| 1353 | /* perror("CDROM_LOCKDOOR"); */ |
| 1354 | } |
| 1355 | |
| 1356 | return 0; |
| 1357 | } |
| 1358 | |
| 1359 | static BlockDriver bdrv_host_cdrom = { |
| 1360 | .format_name = "host_cdrom", |
| 1361 | .instance_size = sizeof(BDRVRawState), |
Christoph Hellwig | 508c7cb | 2009-06-15 14:04:22 +0200 | [diff] [blame] | 1362 | .bdrv_probe_device = cdrom_probe_device, |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 1363 | .bdrv_open = cdrom_open, |
| 1364 | .bdrv_close = raw_close, |
| 1365 | .bdrv_create = hdev_create, |
| 1366 | .bdrv_flush = raw_flush, |
| 1367 | |
| 1368 | #ifdef CONFIG_AIO |
| 1369 | .bdrv_aio_readv = raw_aio_readv, |
| 1370 | .bdrv_aio_writev = raw_aio_writev, |
| 1371 | #endif |
| 1372 | |
| 1373 | .bdrv_read = raw_read, |
| 1374 | .bdrv_write = raw_write, |
| 1375 | .bdrv_getlength = raw_getlength, |
| 1376 | |
| 1377 | /* removable device support */ |
| 1378 | .bdrv_is_inserted = cdrom_is_inserted, |
| 1379 | .bdrv_eject = cdrom_eject, |
| 1380 | .bdrv_set_locked = cdrom_set_locked, |
| 1381 | |
| 1382 | /* generic scsi device */ |
Christoph Hellwig | 63ec93d | 2009-06-15 14:04:34 +0200 | [diff] [blame] | 1383 | .bdrv_ioctl = hdev_ioctl, |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 1384 | #ifdef CONFIG_AIO |
Christoph Hellwig | 63ec93d | 2009-06-15 14:04:34 +0200 | [diff] [blame] | 1385 | .bdrv_aio_ioctl = hdev_aio_ioctl, |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 1386 | #endif |
| 1387 | }; |
| 1388 | #endif /* __linux__ */ |
| 1389 | |
| 1390 | #ifdef __FreeBSD__ |
| 1391 | static int cdrom_open(BlockDriverState *bs, const char *filename, int flags) |
| 1392 | { |
| 1393 | BDRVRawState *s = bs->opaque; |
| 1394 | int ret; |
| 1395 | |
| 1396 | s->type = FTYPE_CD; |
| 1397 | |
Blue Swirl | 19a3da7 | 2009-06-17 18:27:44 +0300 | [diff] [blame] | 1398 | ret = raw_open_common(bs, filename, flags, 0); |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 1399 | if (ret) |
| 1400 | return ret; |
| 1401 | |
| 1402 | /* make sure the door isnt locked at this time */ |
| 1403 | ioctl(s->fd, CDIOCALLOW); |
| 1404 | return 0; |
| 1405 | } |
| 1406 | |
Christoph Hellwig | 508c7cb | 2009-06-15 14:04:22 +0200 | [diff] [blame] | 1407 | static int cdrom_probe_device(const char *filename) |
| 1408 | { |
| 1409 | if (strstart(filename, "/dev/cd", NULL) || |
| 1410 | strstart(filename, "/dev/acd", NULL)) |
| 1411 | return 100; |
| 1412 | return 0; |
| 1413 | } |
| 1414 | |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 1415 | static int cdrom_reopen(BlockDriverState *bs) |
| 1416 | { |
| 1417 | BDRVRawState *s = bs->opaque; |
| 1418 | int fd; |
| 1419 | |
| 1420 | /* |
| 1421 | * Force reread of possibly changed/newly loaded disc, |
| 1422 | * FreeBSD seems to not notice sometimes... |
| 1423 | */ |
| 1424 | if (s->fd >= 0) |
| 1425 | close(s->fd); |
| 1426 | fd = open(bs->filename, s->open_flags, 0644); |
| 1427 | if (fd < 0) { |
| 1428 | s->fd = -1; |
| 1429 | return -EIO; |
| 1430 | } |
| 1431 | s->fd = fd; |
| 1432 | |
| 1433 | /* make sure the door isnt locked at this time */ |
| 1434 | ioctl(s->fd, CDIOCALLOW); |
| 1435 | return 0; |
| 1436 | } |
| 1437 | |
| 1438 | static int cdrom_is_inserted(BlockDriverState *bs) |
| 1439 | { |
| 1440 | return raw_getlength(bs) > 0; |
| 1441 | } |
| 1442 | |
| 1443 | static int cdrom_eject(BlockDriverState *bs, int eject_flag) |
| 1444 | { |
| 1445 | BDRVRawState *s = bs->opaque; |
| 1446 | |
| 1447 | if (s->fd < 0) |
| 1448 | return -ENOTSUP; |
| 1449 | |
| 1450 | (void) ioctl(s->fd, CDIOCALLOW); |
| 1451 | |
| 1452 | if (eject_flag) { |
| 1453 | if (ioctl(s->fd, CDIOCEJECT) < 0) |
| 1454 | perror("CDIOCEJECT"); |
| 1455 | } else { |
| 1456 | if (ioctl(s->fd, CDIOCCLOSE) < 0) |
| 1457 | perror("CDIOCCLOSE"); |
| 1458 | } |
| 1459 | |
| 1460 | if (cdrom_reopen(bs) < 0) |
| 1461 | return -ENOTSUP; |
| 1462 | return 0; |
| 1463 | } |
| 1464 | |
| 1465 | static int cdrom_set_locked(BlockDriverState *bs, int locked) |
| 1466 | { |
| 1467 | BDRVRawState *s = bs->opaque; |
| 1468 | |
| 1469 | if (s->fd < 0) |
| 1470 | return -ENOTSUP; |
| 1471 | if (ioctl(s->fd, (locked ? CDIOCPREVENT : CDIOCALLOW)) < 0) { |
| 1472 | /* |
| 1473 | * Note: an error can happen if the distribution automatically |
| 1474 | * mounts the CD-ROM |
| 1475 | */ |
| 1476 | /* perror("CDROM_LOCKDOOR"); */ |
| 1477 | } |
| 1478 | |
| 1479 | return 0; |
| 1480 | } |
| 1481 | |
| 1482 | static BlockDriver bdrv_host_cdrom = { |
| 1483 | .format_name = "host_cdrom", |
| 1484 | .instance_size = sizeof(BDRVRawState), |
Christoph Hellwig | 508c7cb | 2009-06-15 14:04:22 +0200 | [diff] [blame] | 1485 | .bdrv_probe_device = cdrom_probe_device, |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 1486 | .bdrv_open = cdrom_open, |
| 1487 | .bdrv_close = raw_close, |
| 1488 | .bdrv_create = hdev_create, |
| 1489 | .bdrv_flush = raw_flush, |
| 1490 | |
| 1491 | #ifdef CONFIG_AIO |
| 1492 | .bdrv_aio_readv = raw_aio_readv, |
| 1493 | .bdrv_aio_writev = raw_aio_writev, |
| 1494 | #endif |
| 1495 | |
| 1496 | .bdrv_read = raw_read, |
| 1497 | .bdrv_write = raw_write, |
| 1498 | .bdrv_getlength = raw_getlength, |
| 1499 | |
| 1500 | /* removable device support */ |
| 1501 | .bdrv_is_inserted = cdrom_is_inserted, |
| 1502 | .bdrv_eject = cdrom_eject, |
| 1503 | .bdrv_set_locked = cdrom_set_locked, |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 1504 | }; |
| 1505 | #endif /* __FreeBSD__ */ |
| 1506 | |
Anthony Liguori | 5efa9d5 | 2009-05-09 17:03:42 -0500 | [diff] [blame] | 1507 | static void bdrv_raw_init(void) |
| 1508 | { |
Christoph Hellwig | 508c7cb | 2009-06-15 14:04:22 +0200 | [diff] [blame] | 1509 | /* |
| 1510 | * Register all the drivers. Note that order is important, the driver |
| 1511 | * registered last will get probed first. |
| 1512 | */ |
Anthony Liguori | 5efa9d5 | 2009-05-09 17:03:42 -0500 | [diff] [blame] | 1513 | bdrv_register(&bdrv_raw); |
| 1514 | bdrv_register(&bdrv_host_device); |
Christoph Hellwig | f3a5d3f | 2009-06-15 13:55:19 +0200 | [diff] [blame] | 1515 | #ifdef __linux__ |
| 1516 | bdrv_register(&bdrv_host_floppy); |
| 1517 | bdrv_register(&bdrv_host_cdrom); |
| 1518 | #endif |
| 1519 | #ifdef __FreeBSD__ |
| 1520 | bdrv_register(&bdrv_host_cdrom); |
| 1521 | #endif |
Anthony Liguori | 5efa9d5 | 2009-05-09 17:03:42 -0500 | [diff] [blame] | 1522 | } |
| 1523 | |
| 1524 | block_init(bdrv_raw_init); |