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" |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 27 | #include "block_int.h" |
aliguori | baf35cb | 2008-09-10 15:45:19 +0000 | [diff] [blame] | 28 | #include "compatfd.h" |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 29 | #include <assert.h> |
blueswir1 | 414f0da | 2008-08-15 18:20:52 +0000 | [diff] [blame] | 30 | #ifdef CONFIG_AIO |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 31 | #include <aio.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> |
| 59 | #endif |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 60 | |
blueswir1 | 128ab2f | 2008-08-15 18:33:42 +0000 | [diff] [blame] | 61 | #ifdef __OpenBSD__ |
| 62 | #include <sys/ioctl.h> |
| 63 | #include <sys/disklabel.h> |
| 64 | #include <sys/dkio.h> |
| 65 | #endif |
| 66 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 67 | //#define DEBUG_FLOPPY |
| 68 | |
pbrook | faf0796 | 2007-11-11 02:51:17 +0000 | [diff] [blame] | 69 | //#define DEBUG_BLOCK |
aliguori | 03ff3ca | 2008-09-15 15:51:35 +0000 | [diff] [blame] | 70 | #if defined(DEBUG_BLOCK) |
balrog | a50a628 | 2007-09-20 12:42:24 +0000 | [diff] [blame] | 71 | #define DEBUG_BLOCK_PRINT(formatCstr, args...) do { if (loglevel != 0) \ |
balrog | 2e03286 | 2007-09-20 13:24:53 +0000 | [diff] [blame] | 72 | { fprintf(logfile, formatCstr, ##args); fflush(logfile); } } while (0) |
ths | 8c05dbf | 2007-09-13 12:29:23 +0000 | [diff] [blame] | 73 | #else |
| 74 | #define DEBUG_BLOCK_PRINT(formatCstr, args...) |
| 75 | #endif |
| 76 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 77 | #define FTYPE_FILE 0 |
| 78 | #define FTYPE_CD 1 |
| 79 | #define FTYPE_FD 2 |
| 80 | |
bellard | bed5cc5 | 2008-05-28 09:51:09 +0000 | [diff] [blame] | 81 | #define ALIGNED_BUFFER_SIZE (32 * 512) |
| 82 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 83 | /* if the FD is not accessed during that time (in ms), we try to |
| 84 | reopen it to see if the disk has been changed */ |
| 85 | #define FD_OPEN_TIMEOUT 1000 |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 86 | |
aliguori | 5353872 | 2008-09-26 15:59:29 +0000 | [diff] [blame] | 87 | /* posix-aio doesn't allow multiple outstanding requests to a single file |
| 88 | * descriptor. we implement a pool of dup()'d file descriptors to work |
| 89 | * around this */ |
| 90 | #define RAW_FD_POOL_SIZE 64 |
| 91 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 92 | typedef struct BDRVRawState { |
| 93 | int fd; |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 94 | int type; |
ths | 8c05dbf | 2007-09-13 12:29:23 +0000 | [diff] [blame] | 95 | unsigned int lseek_err_cnt; |
aliguori | 5353872 | 2008-09-26 15:59:29 +0000 | [diff] [blame] | 96 | int fd_pool[RAW_FD_POOL_SIZE]; |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 97 | #if defined(__linux__) |
| 98 | /* linux floppy specific */ |
blueswir1 | 6dd2db5 | 2008-05-07 15:26:22 +0000 | [diff] [blame] | 99 | int fd_open_flags; |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 100 | int64_t fd_open_time; |
| 101 | int64_t fd_error_time; |
| 102 | int fd_got_error; |
| 103 | int fd_media_changed; |
| 104 | #endif |
aliguori | 03ff3ca | 2008-09-15 15:51:35 +0000 | [diff] [blame] | 105 | #if defined(O_DIRECT) |
bellard | bed5cc5 | 2008-05-28 09:51:09 +0000 | [diff] [blame] | 106 | uint8_t* aligned_buf; |
| 107 | #endif |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 108 | } BDRVRawState; |
| 109 | |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 110 | static int posix_aio_init(void); |
| 111 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 112 | static int fd_open(BlockDriverState *bs); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 113 | |
| 114 | static int raw_open(BlockDriverState *bs, const char *filename, int flags) |
| 115 | { |
| 116 | BDRVRawState *s = bs->opaque; |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 117 | int fd, open_flags, ret; |
aliguori | 5353872 | 2008-09-26 15:59:29 +0000 | [diff] [blame] | 118 | int i; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 119 | |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 120 | posix_aio_init(); |
| 121 | |
ths | 8c05dbf | 2007-09-13 12:29:23 +0000 | [diff] [blame] | 122 | s->lseek_err_cnt = 0; |
| 123 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 124 | open_flags = O_BINARY; |
| 125 | if ((flags & BDRV_O_ACCESS) == O_RDWR) { |
| 126 | open_flags |= O_RDWR; |
| 127 | } else { |
| 128 | open_flags |= O_RDONLY; |
| 129 | bs->read_only = 1; |
| 130 | } |
| 131 | if (flags & BDRV_O_CREAT) |
| 132 | open_flags |= O_CREAT | O_TRUNC; |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 133 | #ifdef O_DIRECT |
| 134 | if (flags & BDRV_O_DIRECT) |
| 135 | open_flags |= O_DIRECT; |
| 136 | #endif |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 137 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 138 | s->type = FTYPE_FILE; |
| 139 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 140 | fd = open(filename, open_flags, 0644); |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 141 | if (fd < 0) { |
| 142 | ret = -errno; |
| 143 | if (ret == -EROFS) |
| 144 | ret = -EACCES; |
| 145 | return ret; |
| 146 | } |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 147 | s->fd = fd; |
aliguori | 5353872 | 2008-09-26 15:59:29 +0000 | [diff] [blame] | 148 | for (i = 0; i < RAW_FD_POOL_SIZE; i++) |
| 149 | s->fd_pool[i] = -1; |
aliguori | 03ff3ca | 2008-09-15 15:51:35 +0000 | [diff] [blame] | 150 | #if defined(O_DIRECT) |
bellard | bed5cc5 | 2008-05-28 09:51:09 +0000 | [diff] [blame] | 151 | s->aligned_buf = NULL; |
| 152 | if (flags & BDRV_O_DIRECT) { |
| 153 | s->aligned_buf = qemu_memalign(512, ALIGNED_BUFFER_SIZE); |
| 154 | if (s->aligned_buf == NULL) { |
| 155 | ret = -errno; |
| 156 | close(fd); |
| 157 | return ret; |
| 158 | } |
| 159 | } |
| 160 | #endif |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | /* XXX: use host sector size if necessary with: |
| 165 | #ifdef DIOCGSECTORSIZE |
| 166 | { |
| 167 | unsigned int sectorsize = 512; |
| 168 | if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) && |
| 169 | sectorsize > bufsize) |
| 170 | bufsize = sectorsize; |
| 171 | } |
| 172 | #endif |
| 173 | #ifdef CONFIG_COCOA |
| 174 | u_int32_t blockSize = 512; |
| 175 | if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) { |
| 176 | bufsize = blockSize; |
| 177 | } |
| 178 | #endif |
| 179 | */ |
| 180 | |
bellard | bed5cc5 | 2008-05-28 09:51:09 +0000 | [diff] [blame] | 181 | /* |
| 182 | * offset and count are in bytes, but must be multiples of 512 for files |
| 183 | * opened with O_DIRECT. buf must be aligned to 512 bytes then. |
| 184 | * |
| 185 | * This function may be called without alignment if the caller ensures |
| 186 | * that O_DIRECT is not in effect. |
| 187 | */ |
| 188 | static int raw_pread_aligned(BlockDriverState *bs, int64_t offset, |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 189 | uint8_t *buf, int count) |
| 190 | { |
| 191 | BDRVRawState *s = bs->opaque; |
| 192 | int ret; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 193 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 194 | ret = fd_open(bs); |
| 195 | if (ret < 0) |
| 196 | return ret; |
| 197 | |
ths | 985a03b | 2007-12-24 16:10:43 +0000 | [diff] [blame] | 198 | if (offset >= 0 && lseek(s->fd, offset, SEEK_SET) == (off_t)-1) { |
ths | 8c05dbf | 2007-09-13 12:29:23 +0000 | [diff] [blame] | 199 | ++(s->lseek_err_cnt); |
| 200 | if(s->lseek_err_cnt <= 10) { |
j_mayer | 9286841 | 2007-09-21 06:09:39 +0000 | [diff] [blame] | 201 | DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64 |
| 202 | "] lseek failed : %d = %s\n", |
ths | 8c05dbf | 2007-09-13 12:29:23 +0000 | [diff] [blame] | 203 | s->fd, bs->filename, offset, buf, count, |
| 204 | bs->total_sectors, errno, strerror(errno)); |
| 205 | } |
| 206 | return -1; |
| 207 | } |
| 208 | s->lseek_err_cnt=0; |
| 209 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 210 | ret = read(s->fd, buf, count); |
ths | 8c05dbf | 2007-09-13 12:29:23 +0000 | [diff] [blame] | 211 | if (ret == count) |
| 212 | goto label__raw_read__success; |
| 213 | |
j_mayer | 9286841 | 2007-09-21 06:09:39 +0000 | [diff] [blame] | 214 | DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64 |
| 215 | "] read failed %d : %d = %s\n", |
ths | 8c05dbf | 2007-09-13 12:29:23 +0000 | [diff] [blame] | 216 | s->fd, bs->filename, offset, buf, count, |
| 217 | bs->total_sectors, ret, errno, strerror(errno)); |
| 218 | |
| 219 | /* Try harder for CDrom. */ |
| 220 | if (bs->type == BDRV_TYPE_CDROM) { |
| 221 | lseek(s->fd, offset, SEEK_SET); |
| 222 | ret = read(s->fd, buf, count); |
| 223 | if (ret == count) |
| 224 | goto label__raw_read__success; |
| 225 | lseek(s->fd, offset, SEEK_SET); |
| 226 | ret = read(s->fd, buf, count); |
| 227 | if (ret == count) |
| 228 | goto label__raw_read__success; |
| 229 | |
j_mayer | 9286841 | 2007-09-21 06:09:39 +0000 | [diff] [blame] | 230 | DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64 |
| 231 | "] retry read failed %d : %d = %s\n", |
ths | 8c05dbf | 2007-09-13 12:29:23 +0000 | [diff] [blame] | 232 | s->fd, bs->filename, offset, buf, count, |
| 233 | bs->total_sectors, ret, errno, strerror(errno)); |
| 234 | } |
| 235 | |
ths | 8c05dbf | 2007-09-13 12:29:23 +0000 | [diff] [blame] | 236 | label__raw_read__success: |
| 237 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 238 | return ret; |
| 239 | } |
| 240 | |
bellard | bed5cc5 | 2008-05-28 09:51:09 +0000 | [diff] [blame] | 241 | /* |
| 242 | * offset and count are in bytes, but must be multiples of 512 for files |
| 243 | * opened with O_DIRECT. buf must be aligned to 512 bytes then. |
| 244 | * |
| 245 | * This function may be called without alignment if the caller ensures |
| 246 | * that O_DIRECT is not in effect. |
| 247 | */ |
| 248 | static int raw_pwrite_aligned(BlockDriverState *bs, int64_t offset, |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 249 | const uint8_t *buf, int count) |
| 250 | { |
| 251 | BDRVRawState *s = bs->opaque; |
| 252 | int ret; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 253 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 254 | ret = fd_open(bs); |
| 255 | if (ret < 0) |
| 256 | return ret; |
| 257 | |
ths | 985a03b | 2007-12-24 16:10:43 +0000 | [diff] [blame] | 258 | if (offset >= 0 && lseek(s->fd, offset, SEEK_SET) == (off_t)-1) { |
ths | 8c05dbf | 2007-09-13 12:29:23 +0000 | [diff] [blame] | 259 | ++(s->lseek_err_cnt); |
| 260 | if(s->lseek_err_cnt) { |
j_mayer | 9286841 | 2007-09-21 06:09:39 +0000 | [diff] [blame] | 261 | DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%" |
| 262 | PRId64 "] lseek failed : %d = %s\n", |
ths | 8c05dbf | 2007-09-13 12:29:23 +0000 | [diff] [blame] | 263 | s->fd, bs->filename, offset, buf, count, |
| 264 | bs->total_sectors, errno, strerror(errno)); |
| 265 | } |
| 266 | return -1; |
| 267 | } |
| 268 | s->lseek_err_cnt = 0; |
| 269 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 270 | ret = write(s->fd, buf, count); |
ths | 8c05dbf | 2007-09-13 12:29:23 +0000 | [diff] [blame] | 271 | if (ret == count) |
| 272 | goto label__raw_write__success; |
| 273 | |
j_mayer | 9286841 | 2007-09-21 06:09:39 +0000 | [diff] [blame] | 274 | DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%" PRId64 |
| 275 | "] write failed %d : %d = %s\n", |
ths | 8c05dbf | 2007-09-13 12:29:23 +0000 | [diff] [blame] | 276 | s->fd, bs->filename, offset, buf, count, |
| 277 | bs->total_sectors, ret, errno, strerror(errno)); |
| 278 | |
ths | 8c05dbf | 2007-09-13 12:29:23 +0000 | [diff] [blame] | 279 | label__raw_write__success: |
| 280 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 281 | return ret; |
| 282 | } |
| 283 | |
bellard | bed5cc5 | 2008-05-28 09:51:09 +0000 | [diff] [blame] | 284 | |
aliguori | 03ff3ca | 2008-09-15 15:51:35 +0000 | [diff] [blame] | 285 | #if defined(O_DIRECT) |
bellard | bed5cc5 | 2008-05-28 09:51:09 +0000 | [diff] [blame] | 286 | /* |
| 287 | * offset and count are in bytes and possibly not aligned. For files opened |
| 288 | * with O_DIRECT, necessary alignments are ensured before calling |
| 289 | * raw_pread_aligned to do the actual read. |
| 290 | */ |
| 291 | static int raw_pread(BlockDriverState *bs, int64_t offset, |
| 292 | uint8_t *buf, int count) |
| 293 | { |
| 294 | BDRVRawState *s = bs->opaque; |
| 295 | int size, ret, shift, sum; |
| 296 | |
| 297 | sum = 0; |
| 298 | |
| 299 | if (s->aligned_buf != NULL) { |
| 300 | |
| 301 | if (offset & 0x1ff) { |
| 302 | /* align offset on a 512 bytes boundary */ |
| 303 | |
| 304 | shift = offset & 0x1ff; |
| 305 | size = (shift + count + 0x1ff) & ~0x1ff; |
| 306 | if (size > ALIGNED_BUFFER_SIZE) |
| 307 | size = ALIGNED_BUFFER_SIZE; |
| 308 | ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, size); |
| 309 | if (ret < 0) |
| 310 | return ret; |
| 311 | |
| 312 | size = 512 - shift; |
| 313 | if (size > count) |
| 314 | size = count; |
| 315 | memcpy(buf, s->aligned_buf + shift, size); |
| 316 | |
| 317 | buf += size; |
| 318 | offset += size; |
| 319 | count -= size; |
| 320 | sum += size; |
| 321 | |
| 322 | if (count == 0) |
| 323 | return sum; |
| 324 | } |
| 325 | if (count & 0x1ff || (uintptr_t) buf & 0x1ff) { |
| 326 | |
| 327 | /* read on aligned buffer */ |
| 328 | |
| 329 | while (count) { |
| 330 | |
| 331 | size = (count + 0x1ff) & ~0x1ff; |
| 332 | if (size > ALIGNED_BUFFER_SIZE) |
| 333 | size = ALIGNED_BUFFER_SIZE; |
| 334 | |
| 335 | ret = raw_pread_aligned(bs, offset, s->aligned_buf, size); |
| 336 | if (ret < 0) |
| 337 | return ret; |
| 338 | |
| 339 | size = ret; |
| 340 | if (size > count) |
| 341 | size = count; |
| 342 | |
| 343 | memcpy(buf, s->aligned_buf, size); |
| 344 | |
| 345 | buf += size; |
| 346 | offset += size; |
| 347 | count -= size; |
| 348 | sum += size; |
| 349 | } |
| 350 | |
| 351 | return sum; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | return raw_pread_aligned(bs, offset, buf, count) + sum; |
| 356 | } |
| 357 | |
| 358 | /* |
| 359 | * offset and count are in bytes and possibly not aligned. For files opened |
| 360 | * with O_DIRECT, necessary alignments are ensured before calling |
| 361 | * raw_pwrite_aligned to do the actual write. |
| 362 | */ |
| 363 | static int raw_pwrite(BlockDriverState *bs, int64_t offset, |
| 364 | const uint8_t *buf, int count) |
| 365 | { |
| 366 | BDRVRawState *s = bs->opaque; |
| 367 | int size, ret, shift, sum; |
| 368 | |
| 369 | sum = 0; |
| 370 | |
| 371 | if (s->aligned_buf != NULL) { |
| 372 | |
| 373 | if (offset & 0x1ff) { |
| 374 | /* align offset on a 512 bytes boundary */ |
| 375 | shift = offset & 0x1ff; |
| 376 | ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, 512); |
| 377 | if (ret < 0) |
| 378 | return ret; |
| 379 | |
| 380 | size = 512 - shift; |
| 381 | if (size > count) |
| 382 | size = count; |
| 383 | memcpy(s->aligned_buf + shift, buf, size); |
| 384 | |
| 385 | ret = raw_pwrite_aligned(bs, offset - shift, s->aligned_buf, 512); |
| 386 | if (ret < 0) |
| 387 | return ret; |
| 388 | |
| 389 | buf += size; |
| 390 | offset += size; |
| 391 | count -= size; |
| 392 | sum += size; |
| 393 | |
| 394 | if (count == 0) |
| 395 | return sum; |
| 396 | } |
| 397 | if (count & 0x1ff || (uintptr_t) buf & 0x1ff) { |
| 398 | |
| 399 | while ((size = (count & ~0x1ff)) != 0) { |
| 400 | |
| 401 | if (size > ALIGNED_BUFFER_SIZE) |
| 402 | size = ALIGNED_BUFFER_SIZE; |
| 403 | |
| 404 | memcpy(s->aligned_buf, buf, size); |
| 405 | |
| 406 | ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, size); |
| 407 | if (ret < 0) |
| 408 | return ret; |
| 409 | |
| 410 | buf += ret; |
| 411 | offset += ret; |
| 412 | count -= ret; |
| 413 | sum += ret; |
| 414 | } |
| 415 | /* here, count < 512 because (count & ~0x1ff) == 0 */ |
| 416 | if (count) { |
| 417 | ret = raw_pread_aligned(bs, offset, s->aligned_buf, 512); |
| 418 | if (ret < 0) |
| 419 | return ret; |
| 420 | memcpy(s->aligned_buf, buf, count); |
| 421 | |
| 422 | ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, 512); |
| 423 | if (ret < 0) |
| 424 | return ret; |
| 425 | if (count < ret) |
| 426 | ret = count; |
| 427 | |
| 428 | sum += ret; |
| 429 | } |
| 430 | return sum; |
| 431 | } |
| 432 | } |
| 433 | return raw_pwrite_aligned(bs, offset, buf, count) + sum; |
| 434 | } |
| 435 | |
| 436 | #else |
| 437 | #define raw_pread raw_pread_aligned |
| 438 | #define raw_pwrite raw_pwrite_aligned |
| 439 | #endif |
| 440 | |
| 441 | |
blueswir1 | 414f0da | 2008-08-15 18:20:52 +0000 | [diff] [blame] | 442 | #ifdef CONFIG_AIO |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 443 | /***********************************************************/ |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 444 | /* Unix AIO using POSIX AIO */ |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 445 | |
| 446 | typedef struct RawAIOCB { |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 447 | BlockDriverAIOCB common; |
aliguori | 5353872 | 2008-09-26 15:59:29 +0000 | [diff] [blame] | 448 | int fd; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 449 | struct aiocb aiocb; |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 450 | struct RawAIOCB *next; |
bellard | bed5cc5 | 2008-05-28 09:51:09 +0000 | [diff] [blame] | 451 | int ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 452 | } RawAIOCB; |
| 453 | |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 454 | typedef struct PosixAioState |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 455 | { |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 456 | int fd; |
| 457 | RawAIOCB *first_aio; |
| 458 | } PosixAioState; |
| 459 | |
aliguori | 5353872 | 2008-09-26 15:59:29 +0000 | [diff] [blame] | 460 | static int raw_fd_pool_get(BDRVRawState *s) |
| 461 | { |
| 462 | int i; |
| 463 | |
| 464 | for (i = 0; i < RAW_FD_POOL_SIZE; i++) { |
| 465 | /* already in use */ |
| 466 | if (s->fd_pool[i] != -1) |
| 467 | continue; |
| 468 | |
| 469 | /* try to dup file descriptor */ |
| 470 | s->fd_pool[i] = dup(s->fd); |
| 471 | if (s->fd_pool[i] != -1) |
| 472 | return s->fd_pool[i]; |
| 473 | } |
| 474 | |
| 475 | /* we couldn't dup the file descriptor so just use the main one */ |
| 476 | return s->fd; |
| 477 | } |
| 478 | |
| 479 | static void raw_fd_pool_put(RawAIOCB *acb) |
| 480 | { |
| 481 | BDRVRawState *s = acb->common.bs->opaque; |
| 482 | int i; |
| 483 | |
| 484 | for (i = 0; i < RAW_FD_POOL_SIZE; i++) { |
| 485 | if (s->fd_pool[i] == acb->fd) { |
| 486 | close(s->fd_pool[i]); |
| 487 | s->fd_pool[i] = -1; |
| 488 | } |
| 489 | } |
| 490 | } |
| 491 | |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 492 | static void posix_aio_read(void *opaque) |
| 493 | { |
| 494 | PosixAioState *s = opaque; |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 495 | RawAIOCB *acb, **pacb; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 496 | int ret; |
aliguori | 2c41a5f | 2008-09-11 14:32:27 +0000 | [diff] [blame] | 497 | size_t offset; |
| 498 | union { |
| 499 | struct qemu_signalfd_siginfo siginfo; |
| 500 | char buf[128]; |
| 501 | } sig; |
| 502 | |
| 503 | /* try to read from signalfd, don't freak out if we can't read anything */ |
| 504 | offset = 0; |
| 505 | while (offset < 128) { |
| 506 | ssize_t len; |
| 507 | |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 508 | len = read(s->fd, sig.buf + offset, 128 - offset); |
aliguori | 2c41a5f | 2008-09-11 14:32:27 +0000 | [diff] [blame] | 509 | if (len == -1 && errno == EINTR) |
| 510 | continue; |
| 511 | if (len == -1 && errno == EAGAIN) { |
| 512 | /* there is no natural reason for this to happen, |
| 513 | * so we'll spin hard until we get everything just |
| 514 | * to be on the safe side. */ |
| 515 | if (offset > 0) |
| 516 | continue; |
| 517 | } |
| 518 | |
| 519 | offset += len; |
| 520 | } |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 521 | |
| 522 | for(;;) { |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 523 | pacb = &s->first_aio; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 524 | for(;;) { |
| 525 | acb = *pacb; |
| 526 | if (!acb) |
| 527 | goto the_end; |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 528 | ret = aio_error(&acb->aiocb); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 529 | if (ret == ECANCELED) { |
| 530 | /* remove the request */ |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 531 | *pacb = acb->next; |
aliguori | 5353872 | 2008-09-26 15:59:29 +0000 | [diff] [blame] | 532 | raw_fd_pool_put(acb); |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 533 | qemu_aio_release(acb); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 534 | } else if (ret != EINPROGRESS) { |
| 535 | /* end of aio */ |
| 536 | if (ret == 0) { |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 537 | ret = aio_return(&acb->aiocb); |
| 538 | if (ret == acb->aiocb.aio_nbytes) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 539 | ret = 0; |
| 540 | else |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 541 | ret = -EINVAL; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 542 | } else { |
| 543 | ret = -ret; |
| 544 | } |
| 545 | /* remove the request */ |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 546 | *pacb = acb->next; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 547 | /* call the callback */ |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 548 | acb->common.cb(acb->common.opaque, ret); |
aliguori | 5353872 | 2008-09-26 15:59:29 +0000 | [diff] [blame] | 549 | raw_fd_pool_put(acb); |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 550 | qemu_aio_release(acb); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 551 | break; |
| 552 | } else { |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 553 | pacb = &acb->next; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 554 | } |
| 555 | } |
| 556 | } |
| 557 | the_end: ; |
| 558 | } |
| 559 | |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 560 | static int posix_aio_flush(void *opaque) |
| 561 | { |
| 562 | PosixAioState *s = opaque; |
| 563 | return !!s->first_aio; |
| 564 | } |
| 565 | |
| 566 | static PosixAioState *posix_aio_state; |
| 567 | |
| 568 | static int posix_aio_init(void) |
aliguori | baf35cb | 2008-09-10 15:45:19 +0000 | [diff] [blame] | 569 | { |
| 570 | sigset_t mask; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 571 | PosixAioState *s; |
| 572 | |
| 573 | if (posix_aio_state) |
| 574 | return 0; |
aliguori | baf35cb | 2008-09-10 15:45:19 +0000 | [diff] [blame] | 575 | |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 576 | s = qemu_malloc(sizeof(PosixAioState)); |
| 577 | if (s == NULL) |
| 578 | return -ENOMEM; |
aliguori | baf35cb | 2008-09-10 15:45:19 +0000 | [diff] [blame] | 579 | |
| 580 | /* Make sure to block AIO signal */ |
| 581 | sigemptyset(&mask); |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 582 | sigaddset(&mask, SIGUSR2); |
aliguori | baf35cb | 2008-09-10 15:45:19 +0000 | [diff] [blame] | 583 | sigprocmask(SIG_BLOCK, &mask, NULL); |
| 584 | |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 585 | s->first_aio = NULL; |
| 586 | s->fd = qemu_signalfd(&mask); |
aliguori | 2746310 | 2008-09-27 20:58:43 +0000 | [diff] [blame^] | 587 | if (s->fd == -1) { |
| 588 | fprintf(stderr, "failed to create signalfd\n"); |
| 589 | return -errno; |
| 590 | } |
aliguori | 2c41a5f | 2008-09-11 14:32:27 +0000 | [diff] [blame] | 591 | |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 592 | fcntl(s->fd, F_SETFL, O_NONBLOCK); |
aliguori | 2c41a5f | 2008-09-11 14:32:27 +0000 | [diff] [blame] | 593 | |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 594 | qemu_aio_set_fd_handler(s->fd, posix_aio_read, NULL, posix_aio_flush, s); |
aliguori | baf35cb | 2008-09-10 15:45:19 +0000 | [diff] [blame] | 595 | |
aliguori | acce87f | 2008-09-26 16:12:14 +0000 | [diff] [blame] | 596 | #if defined(__linux__) |
| 597 | { |
| 598 | struct aioinit ai; |
| 599 | |
| 600 | memset(&ai, 0, sizeof(ai)); |
| 601 | #if defined(__GLIBC_PREREQ) && __GLIBC_PREREQ(2, 4) |
| 602 | ai.aio_threads = 64; |
| 603 | ai.aio_num = 64; |
aliguori | 997306f | 2008-09-26 15:52:17 +0000 | [diff] [blame] | 604 | #else |
aliguori | acce87f | 2008-09-26 16:12:14 +0000 | [diff] [blame] | 605 | /* XXX: aio thread exit seems to hang on RedHat 9 and this init |
| 606 | seems to fix the problem. */ |
| 607 | ai.aio_threads = 1; |
| 608 | ai.aio_num = 1; |
| 609 | ai.aio_idle_time = 365 * 100000; |
aliguori | baf35cb | 2008-09-10 15:45:19 +0000 | [diff] [blame] | 610 | #endif |
aliguori | acce87f | 2008-09-26 16:12:14 +0000 | [diff] [blame] | 611 | aio_init(&ai); |
| 612 | } |
| 613 | #endif |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 614 | posix_aio_state = s; |
aliguori | baf35cb | 2008-09-10 15:45:19 +0000 | [diff] [blame] | 615 | |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 616 | return 0; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 617 | } |
| 618 | |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 619 | static RawAIOCB *raw_aio_setup(BlockDriverState *bs, |
| 620 | int64_t sector_num, uint8_t *buf, int nb_sectors, |
| 621 | BlockDriverCompletionFunc *cb, void *opaque) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 622 | { |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 623 | BDRVRawState *s = bs->opaque; |
| 624 | RawAIOCB *acb; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 625 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 626 | if (fd_open(bs) < 0) |
| 627 | return NULL; |
| 628 | |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 629 | acb = qemu_aio_get(bs, cb, opaque); |
| 630 | if (!acb) |
| 631 | return NULL; |
aliguori | 5353872 | 2008-09-26 15:59:29 +0000 | [diff] [blame] | 632 | acb->fd = raw_fd_pool_get(s); |
| 633 | acb->aiocb.aio_fildes = acb->fd; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 634 | acb->aiocb.aio_sigevent.sigev_signo = SIGUSR2; |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 635 | acb->aiocb.aio_sigevent.sigev_notify = SIGEV_SIGNAL; |
| 636 | acb->aiocb.aio_buf = buf; |
ths | 985a03b | 2007-12-24 16:10:43 +0000 | [diff] [blame] | 637 | if (nb_sectors < 0) |
| 638 | acb->aiocb.aio_nbytes = -nb_sectors; |
| 639 | else |
| 640 | acb->aiocb.aio_nbytes = nb_sectors * 512; |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 641 | acb->aiocb.aio_offset = sector_num * 512; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 642 | acb->next = posix_aio_state->first_aio; |
| 643 | posix_aio_state->first_aio = acb; |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 644 | return acb; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 645 | } |
| 646 | |
bellard | bed5cc5 | 2008-05-28 09:51:09 +0000 | [diff] [blame] | 647 | static void raw_aio_em_cb(void* opaque) |
| 648 | { |
| 649 | RawAIOCB *acb = opaque; |
| 650 | acb->common.cb(acb->common.opaque, acb->ret); |
| 651 | qemu_aio_release(acb); |
| 652 | } |
bellard | bed5cc5 | 2008-05-28 09:51:09 +0000 | [diff] [blame] | 653 | |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 654 | static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs, |
| 655 | int64_t sector_num, uint8_t *buf, int nb_sectors, |
| 656 | BlockDriverCompletionFunc *cb, void *opaque) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 657 | { |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 658 | RawAIOCB *acb; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 659 | |
bellard | bed5cc5 | 2008-05-28 09:51:09 +0000 | [diff] [blame] | 660 | /* |
| 661 | * If O_DIRECT is used and the buffer is not aligned fall back |
| 662 | * to synchronous IO. |
| 663 | */ |
aliguori | 03ff3ca | 2008-09-15 15:51:35 +0000 | [diff] [blame] | 664 | #if defined(O_DIRECT) |
bellard | bed5cc5 | 2008-05-28 09:51:09 +0000 | [diff] [blame] | 665 | BDRVRawState *s = bs->opaque; |
| 666 | |
| 667 | if (unlikely(s->aligned_buf != NULL && ((uintptr_t) buf % 512))) { |
| 668 | QEMUBH *bh; |
| 669 | acb = qemu_aio_get(bs, cb, opaque); |
| 670 | acb->ret = raw_pread(bs, 512 * sector_num, buf, 512 * nb_sectors); |
| 671 | bh = qemu_bh_new(raw_aio_em_cb, acb); |
| 672 | qemu_bh_schedule(bh); |
| 673 | return &acb->common; |
| 674 | } |
| 675 | #endif |
| 676 | |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 677 | acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque); |
| 678 | if (!acb) |
| 679 | return NULL; |
| 680 | if (aio_read(&acb->aiocb) < 0) { |
| 681 | qemu_aio_release(acb); |
| 682 | return NULL; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 683 | } |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 684 | return &acb->common; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 685 | } |
| 686 | |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 687 | static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs, |
| 688 | int64_t sector_num, const uint8_t *buf, int nb_sectors, |
| 689 | BlockDriverCompletionFunc *cb, void *opaque) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 690 | { |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 691 | RawAIOCB *acb; |
| 692 | |
bellard | bed5cc5 | 2008-05-28 09:51:09 +0000 | [diff] [blame] | 693 | /* |
| 694 | * If O_DIRECT is used and the buffer is not aligned fall back |
| 695 | * to synchronous IO. |
| 696 | */ |
aliguori | 03ff3ca | 2008-09-15 15:51:35 +0000 | [diff] [blame] | 697 | #if defined(O_DIRECT) |
bellard | bed5cc5 | 2008-05-28 09:51:09 +0000 | [diff] [blame] | 698 | BDRVRawState *s = bs->opaque; |
| 699 | |
| 700 | if (unlikely(s->aligned_buf != NULL && ((uintptr_t) buf % 512))) { |
| 701 | QEMUBH *bh; |
| 702 | acb = qemu_aio_get(bs, cb, opaque); |
| 703 | acb->ret = raw_pwrite(bs, 512 * sector_num, buf, 512 * nb_sectors); |
| 704 | bh = qemu_bh_new(raw_aio_em_cb, acb); |
| 705 | qemu_bh_schedule(bh); |
| 706 | return &acb->common; |
| 707 | } |
| 708 | #endif |
| 709 | |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 710 | acb = raw_aio_setup(bs, sector_num, (uint8_t*)buf, nb_sectors, cb, opaque); |
| 711 | if (!acb) |
| 712 | return NULL; |
| 713 | if (aio_write(&acb->aiocb) < 0) { |
| 714 | qemu_aio_release(acb); |
| 715 | return NULL; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 716 | } |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 717 | return &acb->common; |
| 718 | } |
| 719 | |
| 720 | static void raw_aio_cancel(BlockDriverAIOCB *blockacb) |
| 721 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 722 | int ret; |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 723 | RawAIOCB *acb = (RawAIOCB *)blockacb; |
| 724 | RawAIOCB **pacb; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 725 | |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 726 | ret = aio_cancel(acb->aiocb.aio_fildes, &acb->aiocb); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 727 | if (ret == AIO_NOTCANCELED) { |
| 728 | /* fail safe: if the aio could not be canceled, we wait for |
| 729 | it */ |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 730 | while (aio_error(&acb->aiocb) == EINPROGRESS); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | /* remove the callback from the queue */ |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 734 | pacb = &posix_aio_state->first_aio; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 735 | for(;;) { |
| 736 | if (*pacb == NULL) { |
| 737 | break; |
| 738 | } else if (*pacb == acb) { |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 739 | *pacb = acb->next; |
aliguori | 5353872 | 2008-09-26 15:59:29 +0000 | [diff] [blame] | 740 | raw_fd_pool_put(acb); |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 741 | qemu_aio_release(acb); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 742 | break; |
| 743 | } |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 744 | pacb = &acb->next; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 745 | } |
| 746 | } |
| 747 | |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 748 | #else /* CONFIG_AIO */ |
| 749 | static int posix_aio_init(void) |
blueswir1 | 414f0da | 2008-08-15 18:20:52 +0000 | [diff] [blame] | 750 | { |
| 751 | } |
blueswir1 | 414f0da | 2008-08-15 18:20:52 +0000 | [diff] [blame] | 752 | #endif /* CONFIG_AIO */ |
| 753 | |
aliguori | 5353872 | 2008-09-26 15:59:29 +0000 | [diff] [blame] | 754 | static void raw_close_fd_pool(BDRVRawState *s) |
| 755 | { |
| 756 | int i; |
| 757 | |
| 758 | for (i = 0; i < RAW_FD_POOL_SIZE; i++) { |
| 759 | if (s->fd_pool[i] != -1) { |
| 760 | close(s->fd_pool[i]); |
| 761 | s->fd_pool[i] = -1; |
| 762 | } |
| 763 | } |
| 764 | } |
| 765 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 766 | static void raw_close(BlockDriverState *bs) |
| 767 | { |
| 768 | BDRVRawState *s = bs->opaque; |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 769 | if (s->fd >= 0) { |
| 770 | close(s->fd); |
| 771 | s->fd = -1; |
aliguori | 03ff3ca | 2008-09-15 15:51:35 +0000 | [diff] [blame] | 772 | #if defined(O_DIRECT) |
bellard | bed5cc5 | 2008-05-28 09:51:09 +0000 | [diff] [blame] | 773 | if (s->aligned_buf != NULL) |
| 774 | qemu_free(s->aligned_buf); |
| 775 | #endif |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 776 | } |
aliguori | 5353872 | 2008-09-26 15:59:29 +0000 | [diff] [blame] | 777 | raw_close_fd_pool(s); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | static int raw_truncate(BlockDriverState *bs, int64_t offset) |
| 781 | { |
| 782 | BDRVRawState *s = bs->opaque; |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 783 | if (s->type != FTYPE_FILE) |
| 784 | return -ENOTSUP; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 785 | if (ftruncate(s->fd, offset) < 0) |
| 786 | return -errno; |
| 787 | return 0; |
| 788 | } |
| 789 | |
blueswir1 | 128ab2f | 2008-08-15 18:33:42 +0000 | [diff] [blame] | 790 | #ifdef __OpenBSD__ |
| 791 | static int64_t raw_getlength(BlockDriverState *bs) |
| 792 | { |
| 793 | BDRVRawState *s = bs->opaque; |
| 794 | int fd = s->fd; |
| 795 | struct stat st; |
| 796 | |
| 797 | if (fstat(fd, &st)) |
| 798 | return -1; |
| 799 | if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) { |
| 800 | struct disklabel dl; |
| 801 | |
| 802 | if (ioctl(fd, DIOCGDINFO, &dl)) |
| 803 | return -1; |
| 804 | return (uint64_t)dl.d_secsize * |
| 805 | dl.d_partitions[DISKPART(st.st_rdev)].p_size; |
| 806 | } else |
| 807 | return st.st_size; |
| 808 | } |
| 809 | #else /* !__OpenBSD__ */ |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 810 | static int64_t raw_getlength(BlockDriverState *bs) |
| 811 | { |
| 812 | BDRVRawState *s = bs->opaque; |
| 813 | int fd = s->fd; |
| 814 | int64_t size; |
| 815 | #ifdef _BSD |
| 816 | struct stat sb; |
| 817 | #endif |
| 818 | #ifdef __sun__ |
| 819 | struct dk_minfo minfo; |
| 820 | int rv; |
| 821 | #endif |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 822 | int ret; |
| 823 | |
| 824 | ret = fd_open(bs); |
| 825 | if (ret < 0) |
| 826 | return ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 827 | |
| 828 | #ifdef _BSD |
| 829 | if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) { |
| 830 | #ifdef DIOCGMEDIASIZE |
| 831 | if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size)) |
| 832 | #endif |
| 833 | #ifdef CONFIG_COCOA |
| 834 | size = LONG_LONG_MAX; |
| 835 | #else |
| 836 | size = lseek(fd, 0LL, SEEK_END); |
| 837 | #endif |
| 838 | } else |
| 839 | #endif |
| 840 | #ifdef __sun__ |
| 841 | /* |
| 842 | * use the DKIOCGMEDIAINFO ioctl to read the size. |
| 843 | */ |
| 844 | rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo ); |
| 845 | if ( rv != -1 ) { |
| 846 | size = minfo.dki_lbsize * minfo.dki_capacity; |
| 847 | } else /* there are reports that lseek on some devices |
| 848 | fails, but irc discussion said that contingency |
| 849 | on contingency was overkill */ |
| 850 | #endif |
| 851 | { |
| 852 | size = lseek(fd, 0, SEEK_END); |
| 853 | } |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 854 | return size; |
| 855 | } |
blueswir1 | 128ab2f | 2008-08-15 18:33:42 +0000 | [diff] [blame] | 856 | #endif |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 857 | |
| 858 | static int raw_create(const char *filename, int64_t total_size, |
| 859 | const char *backing_file, int flags) |
| 860 | { |
| 861 | int fd; |
| 862 | |
| 863 | if (flags || backing_file) |
| 864 | return -ENOTSUP; |
| 865 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 866 | fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 867 | 0644); |
| 868 | if (fd < 0) |
| 869 | return -EIO; |
| 870 | ftruncate(fd, total_size * 512); |
| 871 | close(fd); |
| 872 | return 0; |
| 873 | } |
| 874 | |
| 875 | static void raw_flush(BlockDriverState *bs) |
| 876 | { |
| 877 | BDRVRawState *s = bs->opaque; |
| 878 | fsync(s->fd); |
| 879 | } |
| 880 | |
| 881 | BlockDriver bdrv_raw = { |
| 882 | "raw", |
| 883 | sizeof(BDRVRawState), |
| 884 | NULL, /* no probe for protocols */ |
| 885 | raw_open, |
| 886 | NULL, |
| 887 | NULL, |
| 888 | raw_close, |
| 889 | raw_create, |
| 890 | raw_flush, |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 891 | |
blueswir1 | 414f0da | 2008-08-15 18:20:52 +0000 | [diff] [blame] | 892 | #ifdef CONFIG_AIO |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 893 | .bdrv_aio_read = raw_aio_read, |
| 894 | .bdrv_aio_write = raw_aio_write, |
| 895 | .bdrv_aio_cancel = raw_aio_cancel, |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 896 | .aiocb_size = sizeof(RawAIOCB), |
blueswir1 | 414f0da | 2008-08-15 18:20:52 +0000 | [diff] [blame] | 897 | #endif |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 898 | .bdrv_pread = raw_pread, |
| 899 | .bdrv_pwrite = raw_pwrite, |
| 900 | .bdrv_truncate = raw_truncate, |
| 901 | .bdrv_getlength = raw_getlength, |
| 902 | }; |
| 903 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 904 | /***********************************************/ |
| 905 | /* host device */ |
| 906 | |
| 907 | #ifdef CONFIG_COCOA |
| 908 | static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator ); |
| 909 | static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize ); |
| 910 | |
| 911 | kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator ) |
| 912 | { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 913 | kern_return_t kernResult; |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 914 | mach_port_t masterPort; |
| 915 | CFMutableDictionaryRef classesToMatch; |
| 916 | |
| 917 | kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort ); |
| 918 | if ( KERN_SUCCESS != kernResult ) { |
| 919 | printf( "IOMasterPort returned %d\n", kernResult ); |
| 920 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 921 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 922 | classesToMatch = IOServiceMatching( kIOCDMediaClass ); |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 923 | if ( classesToMatch == NULL ) { |
| 924 | printf( "IOServiceMatching returned a NULL dictionary.\n" ); |
| 925 | } else { |
| 926 | CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue ); |
| 927 | } |
| 928 | kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator ); |
| 929 | if ( KERN_SUCCESS != kernResult ) |
| 930 | { |
| 931 | printf( "IOServiceGetMatchingServices returned %d\n", kernResult ); |
| 932 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 933 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 934 | return kernResult; |
| 935 | } |
| 936 | |
| 937 | kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize ) |
| 938 | { |
| 939 | io_object_t nextMedia; |
| 940 | kern_return_t kernResult = KERN_FAILURE; |
| 941 | *bsdPath = '\0'; |
| 942 | nextMedia = IOIteratorNext( mediaIterator ); |
| 943 | if ( nextMedia ) |
| 944 | { |
| 945 | CFTypeRef bsdPathAsCFString; |
| 946 | bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 ); |
| 947 | if ( bsdPathAsCFString ) { |
| 948 | size_t devPathLength; |
| 949 | strcpy( bsdPath, _PATH_DEV ); |
| 950 | strcat( bsdPath, "r" ); |
| 951 | devPathLength = strlen( bsdPath ); |
| 952 | if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) { |
| 953 | kernResult = KERN_SUCCESS; |
| 954 | } |
| 955 | CFRelease( bsdPathAsCFString ); |
| 956 | } |
| 957 | IOObjectRelease( nextMedia ); |
| 958 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 959 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 960 | return kernResult; |
| 961 | } |
| 962 | |
| 963 | #endif |
| 964 | |
| 965 | static int hdev_open(BlockDriverState *bs, const char *filename, int flags) |
| 966 | { |
| 967 | BDRVRawState *s = bs->opaque; |
aliguori | 5353872 | 2008-09-26 15:59:29 +0000 | [diff] [blame] | 968 | int fd, open_flags, ret, i; |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 969 | |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 970 | posix_aio_init(); |
| 971 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 972 | #ifdef CONFIG_COCOA |
| 973 | if (strstart(filename, "/dev/cdrom", NULL)) { |
| 974 | kern_return_t kernResult; |
| 975 | io_iterator_t mediaIterator; |
| 976 | char bsdPath[ MAXPATHLEN ]; |
| 977 | int fd; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 978 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 979 | kernResult = FindEjectableCDMedia( &mediaIterator ); |
| 980 | kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) ); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 981 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 982 | if ( bsdPath[ 0 ] != '\0' ) { |
| 983 | strcat(bsdPath,"s0"); |
| 984 | /* some CDs don't have a partition 0 */ |
| 985 | fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE); |
| 986 | if (fd < 0) { |
| 987 | bsdPath[strlen(bsdPath)-1] = '1'; |
| 988 | } else { |
| 989 | close(fd); |
| 990 | } |
| 991 | filename = bsdPath; |
| 992 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 993 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 994 | if ( mediaIterator ) |
| 995 | IOObjectRelease( mediaIterator ); |
| 996 | } |
| 997 | #endif |
| 998 | open_flags = O_BINARY; |
| 999 | if ((flags & BDRV_O_ACCESS) == O_RDWR) { |
| 1000 | open_flags |= O_RDWR; |
| 1001 | } else { |
| 1002 | open_flags |= O_RDONLY; |
| 1003 | bs->read_only = 1; |
| 1004 | } |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 1005 | #ifdef O_DIRECT |
| 1006 | if (flags & BDRV_O_DIRECT) |
| 1007 | open_flags |= O_DIRECT; |
| 1008 | #endif |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1009 | |
| 1010 | s->type = FTYPE_FILE; |
| 1011 | #if defined(__linux__) |
| 1012 | if (strstart(filename, "/dev/cd", NULL)) { |
| 1013 | /* open will not fail even if no CD is inserted */ |
| 1014 | open_flags |= O_NONBLOCK; |
| 1015 | s->type = FTYPE_CD; |
| 1016 | } else if (strstart(filename, "/dev/fd", NULL)) { |
| 1017 | s->type = FTYPE_FD; |
blueswir1 | 6dd2db5 | 2008-05-07 15:26:22 +0000 | [diff] [blame] | 1018 | s->fd_open_flags = open_flags; |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1019 | /* open will not fail even if no floppy is inserted */ |
| 1020 | open_flags |= O_NONBLOCK; |
ths | 985a03b | 2007-12-24 16:10:43 +0000 | [diff] [blame] | 1021 | } else if (strstart(filename, "/dev/sg", NULL)) { |
| 1022 | bs->sg = 1; |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1023 | } |
| 1024 | #endif |
| 1025 | fd = open(filename, open_flags, 0644); |
| 1026 | if (fd < 0) { |
| 1027 | ret = -errno; |
| 1028 | if (ret == -EROFS) |
| 1029 | ret = -EACCES; |
| 1030 | return ret; |
| 1031 | } |
| 1032 | s->fd = fd; |
aliguori | 5353872 | 2008-09-26 15:59:29 +0000 | [diff] [blame] | 1033 | for (i = 0; i < RAW_FD_POOL_SIZE; i++) |
| 1034 | s->fd_pool[i] = -1; |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1035 | #if defined(__linux__) |
| 1036 | /* close fd so that we can reopen it as needed */ |
| 1037 | if (s->type == FTYPE_FD) { |
| 1038 | close(s->fd); |
| 1039 | s->fd = -1; |
| 1040 | s->fd_media_changed = 1; |
| 1041 | } |
| 1042 | #endif |
| 1043 | return 0; |
| 1044 | } |
| 1045 | |
aliguori | 03ff3ca | 2008-09-15 15:51:35 +0000 | [diff] [blame] | 1046 | #if defined(__linux__) |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1047 | /* Note: we do not have a reliable method to detect if the floppy is |
| 1048 | present. The current method is to try to open the floppy at every |
| 1049 | I/O and to keep it opened during a few hundreds of ms. */ |
| 1050 | static int fd_open(BlockDriverState *bs) |
| 1051 | { |
| 1052 | BDRVRawState *s = bs->opaque; |
| 1053 | int last_media_present; |
| 1054 | |
| 1055 | if (s->type != FTYPE_FD) |
| 1056 | return 0; |
| 1057 | last_media_present = (s->fd >= 0); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1058 | if (s->fd >= 0 && |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1059 | (qemu_get_clock(rt_clock) - s->fd_open_time) >= FD_OPEN_TIMEOUT) { |
| 1060 | close(s->fd); |
| 1061 | s->fd = -1; |
aliguori | 5353872 | 2008-09-26 15:59:29 +0000 | [diff] [blame] | 1062 | raw_close_fd_pool(s); |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1063 | #ifdef DEBUG_FLOPPY |
| 1064 | printf("Floppy closed\n"); |
| 1065 | #endif |
| 1066 | } |
| 1067 | if (s->fd < 0) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1068 | if (s->fd_got_error && |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1069 | (qemu_get_clock(rt_clock) - s->fd_error_time) < FD_OPEN_TIMEOUT) { |
| 1070 | #ifdef DEBUG_FLOPPY |
| 1071 | printf("No floppy (open delayed)\n"); |
| 1072 | #endif |
| 1073 | return -EIO; |
| 1074 | } |
blueswir1 | 6dd2db5 | 2008-05-07 15:26:22 +0000 | [diff] [blame] | 1075 | s->fd = open(bs->filename, s->fd_open_flags); |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1076 | if (s->fd < 0) { |
| 1077 | s->fd_error_time = qemu_get_clock(rt_clock); |
| 1078 | s->fd_got_error = 1; |
| 1079 | if (last_media_present) |
| 1080 | s->fd_media_changed = 1; |
| 1081 | #ifdef DEBUG_FLOPPY |
| 1082 | printf("No floppy\n"); |
| 1083 | #endif |
| 1084 | return -EIO; |
| 1085 | } |
| 1086 | #ifdef DEBUG_FLOPPY |
| 1087 | printf("Floppy opened\n"); |
| 1088 | #endif |
| 1089 | } |
| 1090 | if (!last_media_present) |
| 1091 | s->fd_media_changed = 1; |
| 1092 | s->fd_open_time = qemu_get_clock(rt_clock); |
| 1093 | s->fd_got_error = 0; |
| 1094 | return 0; |
| 1095 | } |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1096 | |
| 1097 | static int raw_is_inserted(BlockDriverState *bs) |
| 1098 | { |
| 1099 | BDRVRawState *s = bs->opaque; |
| 1100 | int ret; |
| 1101 | |
| 1102 | switch(s->type) { |
| 1103 | case FTYPE_CD: |
| 1104 | ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT); |
| 1105 | if (ret == CDS_DISC_OK) |
| 1106 | return 1; |
| 1107 | else |
| 1108 | return 0; |
| 1109 | break; |
| 1110 | case FTYPE_FD: |
| 1111 | ret = fd_open(bs); |
| 1112 | return (ret >= 0); |
| 1113 | default: |
| 1114 | return 1; |
| 1115 | } |
| 1116 | } |
| 1117 | |
| 1118 | /* currently only used by fdc.c, but a CD version would be good too */ |
| 1119 | static int raw_media_changed(BlockDriverState *bs) |
| 1120 | { |
| 1121 | BDRVRawState *s = bs->opaque; |
| 1122 | |
| 1123 | switch(s->type) { |
| 1124 | case FTYPE_FD: |
| 1125 | { |
| 1126 | int ret; |
| 1127 | /* XXX: we do not have a true media changed indication. It |
| 1128 | does not work if the floppy is changed without trying |
| 1129 | to read it */ |
| 1130 | fd_open(bs); |
| 1131 | ret = s->fd_media_changed; |
| 1132 | s->fd_media_changed = 0; |
| 1133 | #ifdef DEBUG_FLOPPY |
| 1134 | printf("Floppy changed=%d\n", ret); |
| 1135 | #endif |
| 1136 | return ret; |
| 1137 | } |
| 1138 | default: |
| 1139 | return -ENOTSUP; |
| 1140 | } |
| 1141 | } |
| 1142 | |
| 1143 | static int raw_eject(BlockDriverState *bs, int eject_flag) |
| 1144 | { |
| 1145 | BDRVRawState *s = bs->opaque; |
| 1146 | |
| 1147 | switch(s->type) { |
| 1148 | case FTYPE_CD: |
| 1149 | if (eject_flag) { |
| 1150 | if (ioctl (s->fd, CDROMEJECT, NULL) < 0) |
| 1151 | perror("CDROMEJECT"); |
| 1152 | } else { |
| 1153 | if (ioctl (s->fd, CDROMCLOSETRAY, NULL) < 0) |
| 1154 | perror("CDROMEJECT"); |
| 1155 | } |
| 1156 | break; |
| 1157 | case FTYPE_FD: |
| 1158 | { |
| 1159 | int fd; |
| 1160 | if (s->fd >= 0) { |
| 1161 | close(s->fd); |
| 1162 | s->fd = -1; |
aliguori | 5353872 | 2008-09-26 15:59:29 +0000 | [diff] [blame] | 1163 | raw_close_fd_pool(s); |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1164 | } |
blueswir1 | 6dd2db5 | 2008-05-07 15:26:22 +0000 | [diff] [blame] | 1165 | fd = open(bs->filename, s->fd_open_flags | O_NONBLOCK); |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1166 | if (fd >= 0) { |
| 1167 | if (ioctl(fd, FDEJECT, 0) < 0) |
| 1168 | perror("FDEJECT"); |
| 1169 | close(fd); |
| 1170 | } |
| 1171 | } |
| 1172 | break; |
| 1173 | default: |
| 1174 | return -ENOTSUP; |
| 1175 | } |
| 1176 | return 0; |
| 1177 | } |
| 1178 | |
| 1179 | static int raw_set_locked(BlockDriverState *bs, int locked) |
| 1180 | { |
| 1181 | BDRVRawState *s = bs->opaque; |
| 1182 | |
| 1183 | switch(s->type) { |
| 1184 | case FTYPE_CD: |
| 1185 | if (ioctl (s->fd, CDROM_LOCKDOOR, locked) < 0) { |
| 1186 | /* Note: an error can happen if the distribution automatically |
| 1187 | mounts the CD-ROM */ |
| 1188 | // perror("CDROM_LOCKDOOR"); |
| 1189 | } |
| 1190 | break; |
| 1191 | default: |
| 1192 | return -ENOTSUP; |
| 1193 | } |
| 1194 | return 0; |
| 1195 | } |
| 1196 | |
ths | 985a03b | 2007-12-24 16:10:43 +0000 | [diff] [blame] | 1197 | static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) |
| 1198 | { |
| 1199 | BDRVRawState *s = bs->opaque; |
| 1200 | |
| 1201 | return ioctl(s->fd, req, buf); |
| 1202 | } |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1203 | #else |
| 1204 | |
aliguori | 08af02e | 2008-09-15 16:48:11 +0000 | [diff] [blame] | 1205 | static int fd_open(BlockDriverState *bs) |
| 1206 | { |
| 1207 | return 0; |
| 1208 | } |
| 1209 | |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1210 | static int raw_is_inserted(BlockDriverState *bs) |
| 1211 | { |
| 1212 | return 1; |
| 1213 | } |
| 1214 | |
| 1215 | static int raw_media_changed(BlockDriverState *bs) |
| 1216 | { |
| 1217 | return -ENOTSUP; |
| 1218 | } |
| 1219 | |
| 1220 | static int raw_eject(BlockDriverState *bs, int eject_flag) |
| 1221 | { |
| 1222 | return -ENOTSUP; |
| 1223 | } |
| 1224 | |
| 1225 | static int raw_set_locked(BlockDriverState *bs, int locked) |
| 1226 | { |
| 1227 | return -ENOTSUP; |
| 1228 | } |
| 1229 | |
ths | 985a03b | 2007-12-24 16:10:43 +0000 | [diff] [blame] | 1230 | static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) |
| 1231 | { |
| 1232 | return -ENOTSUP; |
| 1233 | } |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1234 | #endif /* !linux */ |
| 1235 | |
| 1236 | BlockDriver bdrv_host_device = { |
| 1237 | "host_device", |
| 1238 | sizeof(BDRVRawState), |
| 1239 | NULL, /* no probe for protocols */ |
| 1240 | hdev_open, |
| 1241 | NULL, |
| 1242 | NULL, |
| 1243 | raw_close, |
| 1244 | NULL, |
| 1245 | raw_flush, |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1246 | |
blueswir1 | 414f0da | 2008-08-15 18:20:52 +0000 | [diff] [blame] | 1247 | #ifdef CONFIG_AIO |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1248 | .bdrv_aio_read = raw_aio_read, |
| 1249 | .bdrv_aio_write = raw_aio_write, |
| 1250 | .bdrv_aio_cancel = raw_aio_cancel, |
| 1251 | .aiocb_size = sizeof(RawAIOCB), |
blueswir1 | 414f0da | 2008-08-15 18:20:52 +0000 | [diff] [blame] | 1252 | #endif |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1253 | .bdrv_pread = raw_pread, |
| 1254 | .bdrv_pwrite = raw_pwrite, |
| 1255 | .bdrv_getlength = raw_getlength, |
| 1256 | |
| 1257 | /* removable device support */ |
| 1258 | .bdrv_is_inserted = raw_is_inserted, |
| 1259 | .bdrv_media_changed = raw_media_changed, |
| 1260 | .bdrv_eject = raw_eject, |
| 1261 | .bdrv_set_locked = raw_set_locked, |
ths | 985a03b | 2007-12-24 16:10:43 +0000 | [diff] [blame] | 1262 | /* generic scsi device */ |
| 1263 | .bdrv_ioctl = raw_ioctl, |
bellard | 19cb373 | 2006-08-19 11:45:59 +0000 | [diff] [blame] | 1264 | }; |