]>
Commit | Line | Data |
---|---|---|
83f64091 | 1 | /* |
223d4670 | 2 | * Block driver for RAW files (posix) |
5fafdf24 | 3 | * |
83f64091 | 4 | * Copyright (c) 2006 Fabrice Bellard |
5fafdf24 | 5 | * |
83f64091 FB |
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 | */ | |
faf07963 | 24 | #include "qemu-common.h" |
87ecb68b | 25 | #include "qemu-timer.h" |
baf35cb9 | 26 | #include "qemu-char.h" |
0bf9e31a | 27 | #include "qemu-log.h" |
83f64091 | 28 | #include "block_int.h" |
5efa9d5a | 29 | #include "module.h" |
9ef91a67 | 30 | #include "block/raw-posix-aio.h" |
83f64091 | 31 | |
83f64091 FB |
32 | #ifdef CONFIG_COCOA |
33 | #include <paths.h> | |
34 | #include <sys/param.h> | |
35 | #include <IOKit/IOKitLib.h> | |
36 | #include <IOKit/IOBSD.h> | |
37 | #include <IOKit/storage/IOMediaBSDClient.h> | |
38 | #include <IOKit/storage/IOMedia.h> | |
39 | #include <IOKit/storage/IOCDMedia.h> | |
40 | //#include <IOKit/storage/IOCDTypes.h> | |
41 | #include <CoreFoundation/CoreFoundation.h> | |
42 | #endif | |
43 | ||
44 | #ifdef __sun__ | |
2e9671da TS |
45 | #define _POSIX_PTHREAD_SEMANTICS 1 |
46 | #include <signal.h> | |
83f64091 FB |
47 | #include <sys/dkio.h> |
48 | #endif | |
19cb3738 FB |
49 | #ifdef __linux__ |
50 | #include <sys/ioctl.h> | |
05acda4d | 51 | #include <sys/param.h> |
19cb3738 FB |
52 | #include <linux/cdrom.h> |
53 | #include <linux/fd.h> | |
54 | #endif | |
a167ba50 | 55 | #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__) |
543952ca | 56 | #include <signal.h> |
1cb6c3fd | 57 | #include <sys/disk.h> |
9f23011a | 58 | #include <sys/cdio.h> |
1cb6c3fd | 59 | #endif |
83f64091 | 60 | |
128ab2ff BS |
61 | #ifdef __OpenBSD__ |
62 | #include <sys/ioctl.h> | |
63 | #include <sys/disklabel.h> | |
64 | #include <sys/dkio.h> | |
65 | #endif | |
66 | ||
c5e97233 BS |
67 | #ifdef __DragonFly__ |
68 | #include <sys/ioctl.h> | |
69 | #include <sys/diskslice.h> | |
70 | #endif | |
71 | ||
19cb3738 | 72 | //#define DEBUG_FLOPPY |
83f64091 | 73 | |
faf07963 | 74 | //#define DEBUG_BLOCK |
03ff3ca3 | 75 | #if defined(DEBUG_BLOCK) |
001faf32 BS |
76 | #define DEBUG_BLOCK_PRINT(formatCstr, ...) do { if (qemu_log_enabled()) \ |
77 | { qemu_log(formatCstr, ## __VA_ARGS__); qemu_log_flush(); } } while (0) | |
8c05dbf9 | 78 | #else |
001faf32 | 79 | #define DEBUG_BLOCK_PRINT(formatCstr, ...) |
8c05dbf9 TS |
80 | #endif |
81 | ||
f6465578 AL |
82 | /* OS X does not have O_DSYNC */ |
83 | #ifndef O_DSYNC | |
1c27a8b3 | 84 | #ifdef O_SYNC |
7ab064d2 | 85 | #define O_DSYNC O_SYNC |
1c27a8b3 | 86 | #elif defined(O_FSYNC) |
87 | #define O_DSYNC O_FSYNC | |
88 | #endif | |
f6465578 AL |
89 | #endif |
90 | ||
9f7965c7 AL |
91 | /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */ |
92 | #ifndef O_DIRECT | |
93 | #define O_DIRECT O_DSYNC | |
94 | #endif | |
95 | ||
19cb3738 FB |
96 | #define FTYPE_FILE 0 |
97 | #define FTYPE_CD 1 | |
98 | #define FTYPE_FD 2 | |
83f64091 | 99 | |
bed5cc52 FB |
100 | #define ALIGNED_BUFFER_SIZE (32 * 512) |
101 | ||
19cb3738 FB |
102 | /* if the FD is not accessed during that time (in ms), we try to |
103 | reopen it to see if the disk has been changed */ | |
104 | #define FD_OPEN_TIMEOUT 1000 | |
83f64091 | 105 | |
19cb3738 FB |
106 | typedef struct BDRVRawState { |
107 | int fd; | |
108 | int type; | |
0e1d8f4c | 109 | int open_flags; |
19cb3738 FB |
110 | #if defined(__linux__) |
111 | /* linux floppy specific */ | |
19cb3738 FB |
112 | int64_t fd_open_time; |
113 | int64_t fd_error_time; | |
114 | int fd_got_error; | |
115 | int fd_media_changed; | |
83f64091 | 116 | #endif |
e44bd6fc | 117 | #ifdef CONFIG_LINUX_AIO |
5c6c3a6c | 118 | int use_aio; |
1e5b9d2f | 119 | void *aio_ctx; |
e44bd6fc | 120 | #endif |
bed5cc52 | 121 | uint8_t* aligned_buf; |
19cb3738 FB |
122 | } BDRVRawState; |
123 | ||
124 | static int fd_open(BlockDriverState *bs); | |
22afa7b5 | 125 | static int64_t raw_getlength(BlockDriverState *bs); |
83f64091 | 126 | |
a167ba50 | 127 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
f3a5d3f8 | 128 | static int cdrom_reopen(BlockDriverState *bs); |
9f23011a BS |
129 | #endif |
130 | ||
90babde0 | 131 | static int raw_open_common(BlockDriverState *bs, const char *filename, |
19a3da7f | 132 | int bdrv_flags, int open_flags) |
83f64091 FB |
133 | { |
134 | BDRVRawState *s = bs->opaque; | |
0e1d8f4c | 135 | int fd, ret; |
83f64091 | 136 | |
19a3da7f | 137 | s->open_flags = open_flags | O_BINARY; |
11a1feb6 | 138 | s->open_flags &= ~O_ACCMODE; |
f5edb014 | 139 | if (bdrv_flags & BDRV_O_RDWR) { |
0e1d8f4c | 140 | s->open_flags |= O_RDWR; |
83f64091 | 141 | } else { |
0e1d8f4c | 142 | s->open_flags |= O_RDONLY; |
83f64091 | 143 | } |
9f7965c7 AL |
144 | |
145 | /* Use O_DSYNC for write-through caching, no flags for write-back caching, | |
146 | * and O_DIRECT for no caching. */ | |
19a3da7f | 147 | if ((bdrv_flags & BDRV_O_NOCACHE)) |
0e1d8f4c | 148 | s->open_flags |= O_DIRECT; |
19a3da7f | 149 | else if (!(bdrv_flags & BDRV_O_CACHE_WB)) |
0e1d8f4c | 150 | s->open_flags |= O_DSYNC; |
83f64091 | 151 | |
90babde0 | 152 | s->fd = -1; |
40ff6d7e | 153 | fd = qemu_open(filename, s->open_flags, 0644); |
19cb3738 FB |
154 | if (fd < 0) { |
155 | ret = -errno; | |
156 | if (ret == -EROFS) | |
157 | ret = -EACCES; | |
158 | return ret; | |
159 | } | |
83f64091 | 160 | s->fd = fd; |
bed5cc52 | 161 | s->aligned_buf = NULL; |
5c6c3a6c | 162 | |
19a3da7f | 163 | if ((bdrv_flags & BDRV_O_NOCACHE)) { |
e268ca52 | 164 | s->aligned_buf = qemu_blockalign(bs, ALIGNED_BUFFER_SIZE); |
bed5cc52 | 165 | if (s->aligned_buf == NULL) { |
9ef91a67 | 166 | goto out_close; |
bed5cc52 FB |
167 | } |
168 | } | |
9ef91a67 | 169 | |
5c6c3a6c CH |
170 | #ifdef CONFIG_LINUX_AIO |
171 | if ((bdrv_flags & (BDRV_O_NOCACHE|BDRV_O_NATIVE_AIO)) == | |
172 | (BDRV_O_NOCACHE|BDRV_O_NATIVE_AIO)) { | |
d2e46345 KW |
173 | |
174 | /* We're falling back to POSIX AIO in some cases */ | |
175 | paio_init(); | |
176 | ||
5c6c3a6c CH |
177 | s->aio_ctx = laio_init(); |
178 | if (!s->aio_ctx) { | |
179 | goto out_free_buf; | |
180 | } | |
181 | s->use_aio = 1; | |
182 | } else | |
183 | #endif | |
184 | { | |
1e5b9d2f | 185 | if (paio_init() < 0) { |
5c6c3a6c CH |
186 | goto out_free_buf; |
187 | } | |
e44bd6fc | 188 | #ifdef CONFIG_LINUX_AIO |
5c6c3a6c | 189 | s->use_aio = 0; |
e44bd6fc | 190 | #endif |
9ef91a67 CH |
191 | } |
192 | ||
83f64091 | 193 | return 0; |
9ef91a67 CH |
194 | |
195 | out_free_buf: | |
196 | qemu_vfree(s->aligned_buf); | |
197 | out_close: | |
198 | close(fd); | |
199 | return -errno; | |
83f64091 FB |
200 | } |
201 | ||
90babde0 CH |
202 | static int raw_open(BlockDriverState *bs, const char *filename, int flags) |
203 | { | |
204 | BDRVRawState *s = bs->opaque; | |
205 | ||
206 | s->type = FTYPE_FILE; | |
9a2d77ad | 207 | return raw_open_common(bs, filename, flags, 0); |
90babde0 CH |
208 | } |
209 | ||
83f64091 FB |
210 | /* XXX: use host sector size if necessary with: |
211 | #ifdef DIOCGSECTORSIZE | |
212 | { | |
213 | unsigned int sectorsize = 512; | |
214 | if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) && | |
215 | sectorsize > bufsize) | |
216 | bufsize = sectorsize; | |
217 | } | |
218 | #endif | |
219 | #ifdef CONFIG_COCOA | |
2ee9fb48 | 220 | uint32_t blockSize = 512; |
83f64091 FB |
221 | if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) { |
222 | bufsize = blockSize; | |
223 | } | |
224 | #endif | |
225 | */ | |
226 | ||
bed5cc52 FB |
227 | /* |
228 | * offset and count are in bytes, but must be multiples of 512 for files | |
229 | * opened with O_DIRECT. buf must be aligned to 512 bytes then. | |
230 | * | |
231 | * This function may be called without alignment if the caller ensures | |
232 | * that O_DIRECT is not in effect. | |
233 | */ | |
234 | static int raw_pread_aligned(BlockDriverState *bs, int64_t offset, | |
83f64091 FB |
235 | uint8_t *buf, int count) |
236 | { | |
237 | BDRVRawState *s = bs->opaque; | |
238 | int ret; | |
3b46e624 | 239 | |
19cb3738 FB |
240 | ret = fd_open(bs); |
241 | if (ret < 0) | |
242 | return ret; | |
243 | ||
4899d10d | 244 | ret = pread(s->fd, buf, count, offset); |
8c05dbf9 | 245 | if (ret == count) |
65d21bc7 | 246 | return ret; |
8c05dbf9 | 247 | |
22afa7b5 KW |
248 | /* Allow reads beyond the end (needed for pwrite) */ |
249 | if ((ret == 0) && bs->growable) { | |
250 | int64_t size = raw_getlength(bs); | |
251 | if (offset >= size) { | |
252 | memset(buf, 0, count); | |
65d21bc7 | 253 | return count; |
22afa7b5 KW |
254 | } |
255 | } | |
256 | ||
92868412 JM |
257 | DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64 |
258 | "] read failed %d : %d = %s\n", | |
8c05dbf9 TS |
259 | s->fd, bs->filename, offset, buf, count, |
260 | bs->total_sectors, ret, errno, strerror(errno)); | |
261 | ||
262 | /* Try harder for CDrom. */ | |
65d21bc7 | 263 | if (s->type != FTYPE_FILE) { |
4899d10d | 264 | ret = pread(s->fd, buf, count, offset); |
8c05dbf9 | 265 | if (ret == count) |
65d21bc7 | 266 | return ret; |
4899d10d | 267 | ret = pread(s->fd, buf, count, offset); |
8c05dbf9 | 268 | if (ret == count) |
65d21bc7 | 269 | return ret; |
8c05dbf9 | 270 | |
92868412 JM |
271 | DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64 |
272 | "] retry read failed %d : %d = %s\n", | |
8c05dbf9 TS |
273 | s->fd, bs->filename, offset, buf, count, |
274 | bs->total_sectors, ret, errno, strerror(errno)); | |
275 | } | |
276 | ||
94c6d6d8 | 277 | return (ret < 0) ? -errno : ret; |
83f64091 FB |
278 | } |
279 | ||
bed5cc52 FB |
280 | /* |
281 | * offset and count are in bytes, but must be multiples of 512 for files | |
282 | * opened with O_DIRECT. buf must be aligned to 512 bytes then. | |
283 | * | |
284 | * This function may be called without alignment if the caller ensures | |
285 | * that O_DIRECT is not in effect. | |
286 | */ | |
287 | static int raw_pwrite_aligned(BlockDriverState *bs, int64_t offset, | |
83f64091 FB |
288 | const uint8_t *buf, int count) |
289 | { | |
290 | BDRVRawState *s = bs->opaque; | |
291 | int ret; | |
3b46e624 | 292 | |
19cb3738 FB |
293 | ret = fd_open(bs); |
294 | if (ret < 0) | |
4141d4c2 | 295 | return -errno; |
19cb3738 | 296 | |
4899d10d | 297 | ret = pwrite(s->fd, buf, count, offset); |
8c05dbf9 | 298 | if (ret == count) |
65d21bc7 | 299 | return ret; |
8c05dbf9 | 300 | |
92868412 JM |
301 | DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%" PRId64 |
302 | "] write failed %d : %d = %s\n", | |
8c05dbf9 TS |
303 | s->fd, bs->filename, offset, buf, count, |
304 | bs->total_sectors, ret, errno, strerror(errno)); | |
305 | ||
4141d4c2 | 306 | return (ret < 0) ? -errno : ret; |
83f64091 FB |
307 | } |
308 | ||
bed5cc52 | 309 | |
bed5cc52 FB |
310 | /* |
311 | * offset and count are in bytes and possibly not aligned. For files opened | |
312 | * with O_DIRECT, necessary alignments are ensured before calling | |
313 | * raw_pread_aligned to do the actual read. | |
314 | */ | |
315 | static int raw_pread(BlockDriverState *bs, int64_t offset, | |
316 | uint8_t *buf, int count) | |
317 | { | |
318 | BDRVRawState *s = bs->opaque; | |
319 | int size, ret, shift, sum; | |
320 | ||
321 | sum = 0; | |
322 | ||
323 | if (s->aligned_buf != NULL) { | |
324 | ||
325 | if (offset & 0x1ff) { | |
326 | /* align offset on a 512 bytes boundary */ | |
327 | ||
328 | shift = offset & 0x1ff; | |
329 | size = (shift + count + 0x1ff) & ~0x1ff; | |
330 | if (size > ALIGNED_BUFFER_SIZE) | |
331 | size = ALIGNED_BUFFER_SIZE; | |
332 | ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, size); | |
333 | if (ret < 0) | |
334 | return ret; | |
335 | ||
336 | size = 512 - shift; | |
337 | if (size > count) | |
338 | size = count; | |
339 | memcpy(buf, s->aligned_buf + shift, size); | |
340 | ||
341 | buf += size; | |
342 | offset += size; | |
343 | count -= size; | |
344 | sum += size; | |
345 | ||
346 | if (count == 0) | |
347 | return sum; | |
348 | } | |
349 | if (count & 0x1ff || (uintptr_t) buf & 0x1ff) { | |
350 | ||
351 | /* read on aligned buffer */ | |
352 | ||
353 | while (count) { | |
354 | ||
355 | size = (count + 0x1ff) & ~0x1ff; | |
356 | if (size > ALIGNED_BUFFER_SIZE) | |
357 | size = ALIGNED_BUFFER_SIZE; | |
358 | ||
359 | ret = raw_pread_aligned(bs, offset, s->aligned_buf, size); | |
053965c7 | 360 | if (ret < 0) { |
bed5cc52 | 361 | return ret; |
053965c7 KW |
362 | } else if (ret == 0) { |
363 | fprintf(stderr, "raw_pread: read beyond end of file\n"); | |
364 | abort(); | |
365 | } | |
bed5cc52 FB |
366 | |
367 | size = ret; | |
368 | if (size > count) | |
369 | size = count; | |
370 | ||
371 | memcpy(buf, s->aligned_buf, size); | |
372 | ||
373 | buf += size; | |
374 | offset += size; | |
375 | count -= size; | |
376 | sum += size; | |
377 | } | |
378 | ||
379 | return sum; | |
380 | } | |
381 | } | |
382 | ||
383 | return raw_pread_aligned(bs, offset, buf, count) + sum; | |
384 | } | |
385 | ||
eda578e5 AL |
386 | static int raw_read(BlockDriverState *bs, int64_t sector_num, |
387 | uint8_t *buf, int nb_sectors) | |
388 | { | |
537a1d4b AL |
389 | int ret; |
390 | ||
9040385d JS |
391 | ret = raw_pread(bs, sector_num * BDRV_SECTOR_SIZE, buf, |
392 | nb_sectors * BDRV_SECTOR_SIZE); | |
393 | if (ret == (nb_sectors * BDRV_SECTOR_SIZE)) | |
537a1d4b AL |
394 | ret = 0; |
395 | return ret; | |
eda578e5 AL |
396 | } |
397 | ||
bed5cc52 FB |
398 | /* |
399 | * offset and count are in bytes and possibly not aligned. For files opened | |
400 | * with O_DIRECT, necessary alignments are ensured before calling | |
401 | * raw_pwrite_aligned to do the actual write. | |
402 | */ | |
403 | static int raw_pwrite(BlockDriverState *bs, int64_t offset, | |
404 | const uint8_t *buf, int count) | |
405 | { | |
406 | BDRVRawState *s = bs->opaque; | |
407 | int size, ret, shift, sum; | |
408 | ||
409 | sum = 0; | |
410 | ||
411 | if (s->aligned_buf != NULL) { | |
412 | ||
413 | if (offset & 0x1ff) { | |
414 | /* align offset on a 512 bytes boundary */ | |
415 | shift = offset & 0x1ff; | |
416 | ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, 512); | |
417 | if (ret < 0) | |
418 | return ret; | |
419 | ||
420 | size = 512 - shift; | |
421 | if (size > count) | |
422 | size = count; | |
423 | memcpy(s->aligned_buf + shift, buf, size); | |
424 | ||
425 | ret = raw_pwrite_aligned(bs, offset - shift, s->aligned_buf, 512); | |
426 | if (ret < 0) | |
427 | return ret; | |
428 | ||
429 | buf += size; | |
430 | offset += size; | |
431 | count -= size; | |
432 | sum += size; | |
433 | ||
434 | if (count == 0) | |
435 | return sum; | |
436 | } | |
437 | if (count & 0x1ff || (uintptr_t) buf & 0x1ff) { | |
438 | ||
439 | while ((size = (count & ~0x1ff)) != 0) { | |
440 | ||
441 | if (size > ALIGNED_BUFFER_SIZE) | |
442 | size = ALIGNED_BUFFER_SIZE; | |
443 | ||
444 | memcpy(s->aligned_buf, buf, size); | |
445 | ||
446 | ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, size); | |
447 | if (ret < 0) | |
448 | return ret; | |
449 | ||
450 | buf += ret; | |
451 | offset += ret; | |
452 | count -= ret; | |
453 | sum += ret; | |
454 | } | |
455 | /* here, count < 512 because (count & ~0x1ff) == 0 */ | |
456 | if (count) { | |
457 | ret = raw_pread_aligned(bs, offset, s->aligned_buf, 512); | |
458 | if (ret < 0) | |
459 | return ret; | |
460 | memcpy(s->aligned_buf, buf, count); | |
461 | ||
462 | ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, 512); | |
463 | if (ret < 0) | |
464 | return ret; | |
465 | if (count < ret) | |
466 | ret = count; | |
467 | ||
468 | sum += ret; | |
469 | } | |
470 | return sum; | |
471 | } | |
472 | } | |
473 | return raw_pwrite_aligned(bs, offset, buf, count) + sum; | |
474 | } | |
475 | ||
eda578e5 AL |
476 | static int raw_write(BlockDriverState *bs, int64_t sector_num, |
477 | const uint8_t *buf, int nb_sectors) | |
478 | { | |
537a1d4b | 479 | int ret; |
9040385d JS |
480 | ret = raw_pwrite(bs, sector_num * BDRV_SECTOR_SIZE, buf, |
481 | nb_sectors * BDRV_SECTOR_SIZE); | |
482 | if (ret == (nb_sectors * BDRV_SECTOR_SIZE)) | |
537a1d4b AL |
483 | ret = 0; |
484 | return ret; | |
eda578e5 AL |
485 | } |
486 | ||
9ef91a67 CH |
487 | /* |
488 | * Check if all memory in this vector is sector aligned. | |
489 | */ | |
490 | static int qiov_is_aligned(QEMUIOVector *qiov) | |
a76bab49 | 491 | { |
9ef91a67 | 492 | int i; |
83f64091 | 493 | |
9ef91a67 | 494 | for (i = 0; i < qiov->niov; i++) { |
9040385d | 495 | if ((uintptr_t) qiov->iov[i].iov_base % BDRV_SECTOR_SIZE) { |
9ef91a67 | 496 | return 0; |
c16b5a2c | 497 | } |
c16b5a2c | 498 | } |
c16b5a2c | 499 | |
9ef91a67 | 500 | return 1; |
c16b5a2c CH |
501 | } |
502 | ||
9ef91a67 CH |
503 | static BlockDriverAIOCB *raw_aio_submit(BlockDriverState *bs, |
504 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, | |
505 | BlockDriverCompletionFunc *cb, void *opaque, int type) | |
83f64091 | 506 | { |
ce1a14dc | 507 | BDRVRawState *s = bs->opaque; |
ce1a14dc | 508 | |
19cb3738 FB |
509 | if (fd_open(bs) < 0) |
510 | return NULL; | |
511 | ||
f141eafe AL |
512 | /* |
513 | * If O_DIRECT is used the buffer needs to be aligned on a sector | |
9ef91a67 CH |
514 | * boundary. Check if this is the case or telll the low-level |
515 | * driver that it needs to copy the buffer. | |
f141eafe | 516 | */ |
5c6c3a6c CH |
517 | if (s->aligned_buf) { |
518 | if (!qiov_is_aligned(qiov)) { | |
519 | type |= QEMU_AIO_MISALIGNED; | |
e44bd6fc | 520 | #ifdef CONFIG_LINUX_AIO |
5c6c3a6c CH |
521 | } else if (s->use_aio) { |
522 | return laio_submit(bs, s->aio_ctx, s->fd, sector_num, qiov, | |
e44bd6fc SW |
523 | nb_sectors, cb, opaque, type); |
524 | #endif | |
5c6c3a6c | 525 | } |
9ef91a67 | 526 | } |
f141eafe | 527 | |
1e5b9d2f | 528 | return paio_submit(bs, s->fd, sector_num, qiov, nb_sectors, |
9ef91a67 | 529 | cb, opaque, type); |
83f64091 FB |
530 | } |
531 | ||
f141eafe AL |
532 | static BlockDriverAIOCB *raw_aio_readv(BlockDriverState *bs, |
533 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, | |
ce1a14dc | 534 | BlockDriverCompletionFunc *cb, void *opaque) |
83f64091 | 535 | { |
9ef91a67 CH |
536 | return raw_aio_submit(bs, sector_num, qiov, nb_sectors, |
537 | cb, opaque, QEMU_AIO_READ); | |
83f64091 FB |
538 | } |
539 | ||
f141eafe AL |
540 | static BlockDriverAIOCB *raw_aio_writev(BlockDriverState *bs, |
541 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, | |
ce1a14dc | 542 | BlockDriverCompletionFunc *cb, void *opaque) |
83f64091 | 543 | { |
9ef91a67 CH |
544 | return raw_aio_submit(bs, sector_num, qiov, nb_sectors, |
545 | cb, opaque, QEMU_AIO_WRITE); | |
83f64091 | 546 | } |
53538725 | 547 | |
b2e12bc6 CH |
548 | static BlockDriverAIOCB *raw_aio_flush(BlockDriverState *bs, |
549 | BlockDriverCompletionFunc *cb, void *opaque) | |
550 | { | |
551 | BDRVRawState *s = bs->opaque; | |
552 | ||
553 | if (fd_open(bs) < 0) | |
554 | return NULL; | |
555 | ||
1e5b9d2f | 556 | return paio_submit(bs, s->fd, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH); |
b2e12bc6 CH |
557 | } |
558 | ||
83f64091 FB |
559 | static void raw_close(BlockDriverState *bs) |
560 | { | |
561 | BDRVRawState *s = bs->opaque; | |
19cb3738 FB |
562 | if (s->fd >= 0) { |
563 | close(s->fd); | |
564 | s->fd = -1; | |
bed5cc52 | 565 | if (s->aligned_buf != NULL) |
f8a83245 | 566 | qemu_vfree(s->aligned_buf); |
19cb3738 | 567 | } |
83f64091 FB |
568 | } |
569 | ||
570 | static int raw_truncate(BlockDriverState *bs, int64_t offset) | |
571 | { | |
572 | BDRVRawState *s = bs->opaque; | |
19cb3738 FB |
573 | if (s->type != FTYPE_FILE) |
574 | return -ENOTSUP; | |
83f64091 FB |
575 | if (ftruncate(s->fd, offset) < 0) |
576 | return -errno; | |
577 | return 0; | |
578 | } | |
579 | ||
128ab2ff BS |
580 | #ifdef __OpenBSD__ |
581 | static int64_t raw_getlength(BlockDriverState *bs) | |
582 | { | |
583 | BDRVRawState *s = bs->opaque; | |
584 | int fd = s->fd; | |
585 | struct stat st; | |
586 | ||
587 | if (fstat(fd, &st)) | |
588 | return -1; | |
589 | if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) { | |
590 | struct disklabel dl; | |
591 | ||
592 | if (ioctl(fd, DIOCGDINFO, &dl)) | |
593 | return -1; | |
594 | return (uint64_t)dl.d_secsize * | |
595 | dl.d_partitions[DISKPART(st.st_rdev)].p_size; | |
596 | } else | |
597 | return st.st_size; | |
598 | } | |
50779cc2 CH |
599 | #elif defined(__sun__) |
600 | static int64_t raw_getlength(BlockDriverState *bs) | |
601 | { | |
602 | BDRVRawState *s = bs->opaque; | |
603 | struct dk_minfo minfo; | |
604 | int ret; | |
605 | ||
606 | ret = fd_open(bs); | |
607 | if (ret < 0) { | |
608 | return ret; | |
609 | } | |
610 | ||
611 | /* | |
612 | * Use the DKIOCGMEDIAINFO ioctl to read the size. | |
613 | */ | |
614 | ret = ioctl(s->fd, DKIOCGMEDIAINFO, &minfo); | |
615 | if (ret != -1) { | |
616 | return minfo.dki_lbsize * minfo.dki_capacity; | |
617 | } | |
618 | ||
619 | /* | |
620 | * There are reports that lseek on some devices fails, but | |
621 | * irc discussion said that contingency on contingency was overkill. | |
622 | */ | |
623 | return lseek(s->fd, 0, SEEK_END); | |
624 | } | |
625 | #elif defined(CONFIG_BSD) | |
626 | static int64_t raw_getlength(BlockDriverState *bs) | |
83f64091 FB |
627 | { |
628 | BDRVRawState *s = bs->opaque; | |
629 | int fd = s->fd; | |
630 | int64_t size; | |
83f64091 | 631 | struct stat sb; |
a167ba50 | 632 | #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__) |
9f23011a | 633 | int reopened = 0; |
83f64091 | 634 | #endif |
19cb3738 FB |
635 | int ret; |
636 | ||
637 | ret = fd_open(bs); | |
638 | if (ret < 0) | |
639 | return ret; | |
83f64091 | 640 | |
a167ba50 | 641 | #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__) |
9f23011a BS |
642 | again: |
643 | #endif | |
83f64091 FB |
644 | if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) { |
645 | #ifdef DIOCGMEDIASIZE | |
646 | if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size)) | |
c5e97233 BS |
647 | #elif defined(DIOCGPART) |
648 | { | |
649 | struct partinfo pi; | |
650 | if (ioctl(fd, DIOCGPART, &pi) == 0) | |
651 | size = pi.media_size; | |
652 | else | |
653 | size = 0; | |
654 | } | |
655 | if (size == 0) | |
83f64091 FB |
656 | #endif |
657 | #ifdef CONFIG_COCOA | |
658 | size = LONG_LONG_MAX; | |
659 | #else | |
660 | size = lseek(fd, 0LL, SEEK_END); | |
9f23011a | 661 | #endif |
a167ba50 | 662 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
9f23011a BS |
663 | switch(s->type) { |
664 | case FTYPE_CD: | |
665 | /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */ | |
666 | if (size == 2048LL * (unsigned)-1) | |
667 | size = 0; | |
668 | /* XXX no disc? maybe we need to reopen... */ | |
f3a5d3f8 | 669 | if (size <= 0 && !reopened && cdrom_reopen(bs) >= 0) { |
9f23011a BS |
670 | reopened = 1; |
671 | goto again; | |
672 | } | |
673 | } | |
83f64091 | 674 | #endif |
50779cc2 | 675 | } else { |
83f64091 FB |
676 | size = lseek(fd, 0, SEEK_END); |
677 | } | |
83f64091 FB |
678 | return size; |
679 | } | |
50779cc2 CH |
680 | #else |
681 | static int64_t raw_getlength(BlockDriverState *bs) | |
682 | { | |
683 | BDRVRawState *s = bs->opaque; | |
684 | int ret; | |
685 | ||
686 | ret = fd_open(bs); | |
687 | if (ret < 0) { | |
688 | return ret; | |
689 | } | |
690 | ||
691 | return lseek(s->fd, 0, SEEK_END); | |
692 | } | |
128ab2ff | 693 | #endif |
83f64091 | 694 | |
0e7e1989 | 695 | static int raw_create(const char *filename, QEMUOptionParameter *options) |
83f64091 FB |
696 | { |
697 | int fd; | |
1e37d059 | 698 | int result = 0; |
0e7e1989 | 699 | int64_t total_size = 0; |
83f64091 | 700 | |
0e7e1989 KW |
701 | /* Read out options */ |
702 | while (options && options->name) { | |
703 | if (!strcmp(options->name, BLOCK_OPT_SIZE)) { | |
9040385d | 704 | total_size = options->value.n / BDRV_SECTOR_SIZE; |
0e7e1989 KW |
705 | } |
706 | options++; | |
707 | } | |
83f64091 | 708 | |
5fafdf24 | 709 | fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, |
83f64091 | 710 | 0644); |
1e37d059 SW |
711 | if (fd < 0) { |
712 | result = -errno; | |
713 | } else { | |
9040385d | 714 | if (ftruncate(fd, total_size * BDRV_SECTOR_SIZE) != 0) { |
1e37d059 SW |
715 | result = -errno; |
716 | } | |
717 | if (close(fd) != 0) { | |
718 | result = -errno; | |
719 | } | |
720 | } | |
721 | return result; | |
83f64091 FB |
722 | } |
723 | ||
724 | static void raw_flush(BlockDriverState *bs) | |
725 | { | |
726 | BDRVRawState *s = bs->opaque; | |
6f1953c4 | 727 | qemu_fdatasync(s->fd); |
83f64091 FB |
728 | } |
729 | ||
0e7e1989 KW |
730 | |
731 | static QEMUOptionParameter raw_create_options[] = { | |
db08adf5 KW |
732 | { |
733 | .name = BLOCK_OPT_SIZE, | |
734 | .type = OPT_SIZE, | |
735 | .help = "Virtual disk size" | |
736 | }, | |
0e7e1989 KW |
737 | { NULL } |
738 | }; | |
739 | ||
84a12e66 CH |
740 | static BlockDriver bdrv_file = { |
741 | .format_name = "file", | |
742 | .protocol_name = "file", | |
856ae5c3 BS |
743 | .instance_size = sizeof(BDRVRawState), |
744 | .bdrv_probe = NULL, /* no probe for protocols */ | |
66f82cee | 745 | .bdrv_file_open = raw_open, |
856ae5c3 BS |
746 | .bdrv_read = raw_read, |
747 | .bdrv_write = raw_write, | |
748 | .bdrv_close = raw_close, | |
749 | .bdrv_create = raw_create, | |
750 | .bdrv_flush = raw_flush, | |
3b46e624 | 751 | |
f141eafe AL |
752 | .bdrv_aio_readv = raw_aio_readv, |
753 | .bdrv_aio_writev = raw_aio_writev, | |
b2e12bc6 | 754 | .bdrv_aio_flush = raw_aio_flush, |
3c529d93 | 755 | |
83f64091 FB |
756 | .bdrv_truncate = raw_truncate, |
757 | .bdrv_getlength = raw_getlength, | |
0e7e1989 KW |
758 | |
759 | .create_options = raw_create_options, | |
83f64091 FB |
760 | }; |
761 | ||
19cb3738 FB |
762 | /***********************************************/ |
763 | /* host device */ | |
764 | ||
765 | #ifdef CONFIG_COCOA | |
766 | static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator ); | |
767 | static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize ); | |
768 | ||
769 | kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator ) | |
770 | { | |
5fafdf24 | 771 | kern_return_t kernResult; |
19cb3738 FB |
772 | mach_port_t masterPort; |
773 | CFMutableDictionaryRef classesToMatch; | |
774 | ||
775 | kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort ); | |
776 | if ( KERN_SUCCESS != kernResult ) { | |
777 | printf( "IOMasterPort returned %d\n", kernResult ); | |
778 | } | |
3b46e624 | 779 | |
5fafdf24 | 780 | classesToMatch = IOServiceMatching( kIOCDMediaClass ); |
19cb3738 FB |
781 | if ( classesToMatch == NULL ) { |
782 | printf( "IOServiceMatching returned a NULL dictionary.\n" ); | |
783 | } else { | |
784 | CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue ); | |
785 | } | |
786 | kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator ); | |
787 | if ( KERN_SUCCESS != kernResult ) | |
788 | { | |
789 | printf( "IOServiceGetMatchingServices returned %d\n", kernResult ); | |
790 | } | |
3b46e624 | 791 | |
19cb3738 FB |
792 | return kernResult; |
793 | } | |
794 | ||
795 | kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize ) | |
796 | { | |
797 | io_object_t nextMedia; | |
798 | kern_return_t kernResult = KERN_FAILURE; | |
799 | *bsdPath = '\0'; | |
800 | nextMedia = IOIteratorNext( mediaIterator ); | |
801 | if ( nextMedia ) | |
802 | { | |
803 | CFTypeRef bsdPathAsCFString; | |
804 | bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 ); | |
805 | if ( bsdPathAsCFString ) { | |
806 | size_t devPathLength; | |
807 | strcpy( bsdPath, _PATH_DEV ); | |
808 | strcat( bsdPath, "r" ); | |
809 | devPathLength = strlen( bsdPath ); | |
810 | if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) { | |
811 | kernResult = KERN_SUCCESS; | |
812 | } | |
813 | CFRelease( bsdPathAsCFString ); | |
814 | } | |
815 | IOObjectRelease( nextMedia ); | |
816 | } | |
3b46e624 | 817 | |
19cb3738 FB |
818 | return kernResult; |
819 | } | |
820 | ||
821 | #endif | |
822 | ||
508c7cb3 CH |
823 | static int hdev_probe_device(const char *filename) |
824 | { | |
825 | struct stat st; | |
826 | ||
827 | /* allow a dedicated CD-ROM driver to match with a higher priority */ | |
828 | if (strstart(filename, "/dev/cdrom", NULL)) | |
829 | return 50; | |
830 | ||
831 | if (stat(filename, &st) >= 0 && | |
832 | (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) { | |
833 | return 100; | |
834 | } | |
835 | ||
836 | return 0; | |
837 | } | |
838 | ||
19cb3738 FB |
839 | static int hdev_open(BlockDriverState *bs, const char *filename, int flags) |
840 | { | |
841 | BDRVRawState *s = bs->opaque; | |
a76bab49 | 842 | |
19cb3738 FB |
843 | #ifdef CONFIG_COCOA |
844 | if (strstart(filename, "/dev/cdrom", NULL)) { | |
845 | kern_return_t kernResult; | |
846 | io_iterator_t mediaIterator; | |
847 | char bsdPath[ MAXPATHLEN ]; | |
848 | int fd; | |
5fafdf24 | 849 | |
19cb3738 FB |
850 | kernResult = FindEjectableCDMedia( &mediaIterator ); |
851 | kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) ); | |
3b46e624 | 852 | |
19cb3738 FB |
853 | if ( bsdPath[ 0 ] != '\0' ) { |
854 | strcat(bsdPath,"s0"); | |
855 | /* some CDs don't have a partition 0 */ | |
856 | fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE); | |
857 | if (fd < 0) { | |
858 | bsdPath[strlen(bsdPath)-1] = '1'; | |
859 | } else { | |
860 | close(fd); | |
861 | } | |
862 | filename = bsdPath; | |
863 | } | |
3b46e624 | 864 | |
19cb3738 FB |
865 | if ( mediaIterator ) |
866 | IOObjectRelease( mediaIterator ); | |
867 | } | |
868 | #endif | |
19cb3738 FB |
869 | |
870 | s->type = FTYPE_FILE; | |
4dd75c70 | 871 | #if defined(__linux__) |
05acda4d BK |
872 | { |
873 | char resolved_path[ MAXPATHLEN ], *temp; | |
874 | ||
875 | temp = realpath(filename, resolved_path); | |
876 | if (temp && strstart(temp, "/dev/sg", NULL)) { | |
877 | bs->sg = 1; | |
878 | } | |
19cb3738 FB |
879 | } |
880 | #endif | |
90babde0 | 881 | |
19a3da7f | 882 | return raw_open_common(bs, filename, flags, 0); |
19cb3738 FB |
883 | } |
884 | ||
03ff3ca3 | 885 | #if defined(__linux__) |
19cb3738 FB |
886 | /* Note: we do not have a reliable method to detect if the floppy is |
887 | present. The current method is to try to open the floppy at every | |
888 | I/O and to keep it opened during a few hundreds of ms. */ | |
889 | static int fd_open(BlockDriverState *bs) | |
890 | { | |
891 | BDRVRawState *s = bs->opaque; | |
892 | int last_media_present; | |
893 | ||
894 | if (s->type != FTYPE_FD) | |
895 | return 0; | |
896 | last_media_present = (s->fd >= 0); | |
5fafdf24 | 897 | if (s->fd >= 0 && |
19cb3738 FB |
898 | (qemu_get_clock(rt_clock) - s->fd_open_time) >= FD_OPEN_TIMEOUT) { |
899 | close(s->fd); | |
900 | s->fd = -1; | |
901 | #ifdef DEBUG_FLOPPY | |
902 | printf("Floppy closed\n"); | |
903 | #endif | |
904 | } | |
905 | if (s->fd < 0) { | |
5fafdf24 | 906 | if (s->fd_got_error && |
19cb3738 FB |
907 | (qemu_get_clock(rt_clock) - s->fd_error_time) < FD_OPEN_TIMEOUT) { |
908 | #ifdef DEBUG_FLOPPY | |
909 | printf("No floppy (open delayed)\n"); | |
910 | #endif | |
911 | return -EIO; | |
912 | } | |
0e1d8f4c | 913 | s->fd = open(bs->filename, s->open_flags & ~O_NONBLOCK); |
19cb3738 FB |
914 | if (s->fd < 0) { |
915 | s->fd_error_time = qemu_get_clock(rt_clock); | |
916 | s->fd_got_error = 1; | |
917 | if (last_media_present) | |
918 | s->fd_media_changed = 1; | |
919 | #ifdef DEBUG_FLOPPY | |
920 | printf("No floppy\n"); | |
921 | #endif | |
922 | return -EIO; | |
923 | } | |
924 | #ifdef DEBUG_FLOPPY | |
925 | printf("Floppy opened\n"); | |
926 | #endif | |
927 | } | |
928 | if (!last_media_present) | |
929 | s->fd_media_changed = 1; | |
930 | s->fd_open_time = qemu_get_clock(rt_clock); | |
931 | s->fd_got_error = 0; | |
932 | return 0; | |
933 | } | |
19cb3738 | 934 | |
63ec93db | 935 | static int hdev_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) |
985a03b0 TS |
936 | { |
937 | BDRVRawState *s = bs->opaque; | |
938 | ||
939 | return ioctl(s->fd, req, buf); | |
940 | } | |
221f715d | 941 | |
63ec93db | 942 | static BlockDriverAIOCB *hdev_aio_ioctl(BlockDriverState *bs, |
221f715d AL |
943 | unsigned long int req, void *buf, |
944 | BlockDriverCompletionFunc *cb, void *opaque) | |
945 | { | |
f141eafe | 946 | BDRVRawState *s = bs->opaque; |
221f715d | 947 | |
f141eafe AL |
948 | if (fd_open(bs) < 0) |
949 | return NULL; | |
9ef91a67 | 950 | return paio_ioctl(bs, s->fd, req, buf, cb, opaque); |
221f715d AL |
951 | } |
952 | ||
a167ba50 | 953 | #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
9f23011a BS |
954 | static int fd_open(BlockDriverState *bs) |
955 | { | |
956 | BDRVRawState *s = bs->opaque; | |
957 | ||
958 | /* this is just to ensure s->fd is sane (its called by io ops) */ | |
959 | if (s->fd >= 0) | |
960 | return 0; | |
961 | return -EIO; | |
962 | } | |
9f23011a | 963 | #else /* !linux && !FreeBSD */ |
19cb3738 | 964 | |
08af02e2 AL |
965 | static int fd_open(BlockDriverState *bs) |
966 | { | |
967 | return 0; | |
968 | } | |
969 | ||
221f715d | 970 | #endif /* !linux && !FreeBSD */ |
04eeb8b6 | 971 | |
0e7e1989 | 972 | static int hdev_create(const char *filename, QEMUOptionParameter *options) |
93c65b47 AL |
973 | { |
974 | int fd; | |
975 | int ret = 0; | |
976 | struct stat stat_buf; | |
0e7e1989 | 977 | int64_t total_size = 0; |
93c65b47 | 978 | |
0e7e1989 KW |
979 | /* Read out options */ |
980 | while (options && options->name) { | |
981 | if (!strcmp(options->name, "size")) { | |
9040385d | 982 | total_size = options->value.n / BDRV_SECTOR_SIZE; |
0e7e1989 KW |
983 | } |
984 | options++; | |
985 | } | |
93c65b47 AL |
986 | |
987 | fd = open(filename, O_WRONLY | O_BINARY); | |
988 | if (fd < 0) | |
57e69b7d | 989 | return -errno; |
93c65b47 AL |
990 | |
991 | if (fstat(fd, &stat_buf) < 0) | |
57e69b7d | 992 | ret = -errno; |
4099df58 | 993 | else if (!S_ISBLK(stat_buf.st_mode) && !S_ISCHR(stat_buf.st_mode)) |
57e69b7d | 994 | ret = -ENODEV; |
9040385d | 995 | else if (lseek(fd, 0, SEEK_END) < total_size * BDRV_SECTOR_SIZE) |
93c65b47 AL |
996 | ret = -ENOSPC; |
997 | ||
998 | close(fd); | |
999 | return ret; | |
1000 | } | |
1001 | ||
336c1c12 KW |
1002 | static int hdev_has_zero_init(BlockDriverState *bs) |
1003 | { | |
1004 | return 0; | |
1005 | } | |
1006 | ||
5efa9d5a | 1007 | static BlockDriver bdrv_host_device = { |
0b4ce02e | 1008 | .format_name = "host_device", |
84a12e66 | 1009 | .protocol_name = "host_device", |
0b4ce02e KW |
1010 | .instance_size = sizeof(BDRVRawState), |
1011 | .bdrv_probe_device = hdev_probe_device, | |
66f82cee | 1012 | .bdrv_file_open = hdev_open, |
0b4ce02e | 1013 | .bdrv_close = raw_close, |
93c65b47 | 1014 | .bdrv_create = hdev_create, |
0b4ce02e | 1015 | .create_options = raw_create_options, |
336c1c12 | 1016 | .bdrv_has_zero_init = hdev_has_zero_init, |
0b4ce02e | 1017 | .bdrv_flush = raw_flush, |
3b46e624 | 1018 | |
f141eafe AL |
1019 | .bdrv_aio_readv = raw_aio_readv, |
1020 | .bdrv_aio_writev = raw_aio_writev, | |
b2e12bc6 | 1021 | .bdrv_aio_flush = raw_aio_flush, |
3c529d93 | 1022 | |
eda578e5 AL |
1023 | .bdrv_read = raw_read, |
1024 | .bdrv_write = raw_write, | |
e60f469c | 1025 | .bdrv_getlength = raw_getlength, |
19cb3738 | 1026 | |
f3a5d3f8 | 1027 | /* generic scsi device */ |
63ec93db CH |
1028 | #ifdef __linux__ |
1029 | .bdrv_ioctl = hdev_ioctl, | |
63ec93db CH |
1030 | .bdrv_aio_ioctl = hdev_aio_ioctl, |
1031 | #endif | |
f3a5d3f8 CH |
1032 | }; |
1033 | ||
1034 | #ifdef __linux__ | |
1035 | static int floppy_open(BlockDriverState *bs, const char *filename, int flags) | |
1036 | { | |
1037 | BDRVRawState *s = bs->opaque; | |
1038 | int ret; | |
1039 | ||
f3a5d3f8 | 1040 | s->type = FTYPE_FD; |
f3a5d3f8 | 1041 | |
19a3da7f BS |
1042 | /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */ |
1043 | ret = raw_open_common(bs, filename, flags, O_NONBLOCK); | |
f3a5d3f8 CH |
1044 | if (ret) |
1045 | return ret; | |
1046 | ||
1047 | /* close fd so that we can reopen it as needed */ | |
1048 | close(s->fd); | |
1049 | s->fd = -1; | |
1050 | s->fd_media_changed = 1; | |
1051 | ||
1052 | return 0; | |
1053 | } | |
1054 | ||
508c7cb3 CH |
1055 | static int floppy_probe_device(const char *filename) |
1056 | { | |
2ebf7c4b CR |
1057 | int fd, ret; |
1058 | int prio = 0; | |
1059 | struct floppy_struct fdparam; | |
1060 | ||
508c7cb3 | 1061 | if (strstart(filename, "/dev/fd", NULL)) |
2ebf7c4b CR |
1062 | prio = 50; |
1063 | ||
1064 | fd = open(filename, O_RDONLY | O_NONBLOCK); | |
1065 | if (fd < 0) { | |
1066 | goto out; | |
1067 | } | |
1068 | ||
1069 | /* Attempt to detect via a floppy specific ioctl */ | |
1070 | ret = ioctl(fd, FDGETPRM, &fdparam); | |
1071 | if (ret >= 0) | |
1072 | prio = 100; | |
1073 | ||
1074 | close(fd); | |
1075 | out: | |
1076 | return prio; | |
508c7cb3 CH |
1077 | } |
1078 | ||
1079 | ||
f3a5d3f8 CH |
1080 | static int floppy_is_inserted(BlockDriverState *bs) |
1081 | { | |
1082 | return fd_open(bs) >= 0; | |
1083 | } | |
1084 | ||
1085 | static int floppy_media_changed(BlockDriverState *bs) | |
1086 | { | |
1087 | BDRVRawState *s = bs->opaque; | |
1088 | int ret; | |
1089 | ||
1090 | /* | |
1091 | * XXX: we do not have a true media changed indication. | |
1092 | * It does not work if the floppy is changed without trying to read it. | |
1093 | */ | |
1094 | fd_open(bs); | |
1095 | ret = s->fd_media_changed; | |
1096 | s->fd_media_changed = 0; | |
1097 | #ifdef DEBUG_FLOPPY | |
1098 | printf("Floppy changed=%d\n", ret); | |
1099 | #endif | |
1100 | return ret; | |
1101 | } | |
1102 | ||
1103 | static int floppy_eject(BlockDriverState *bs, int eject_flag) | |
1104 | { | |
1105 | BDRVRawState *s = bs->opaque; | |
1106 | int fd; | |
1107 | ||
1108 | if (s->fd >= 0) { | |
1109 | close(s->fd); | |
1110 | s->fd = -1; | |
1111 | } | |
1112 | fd = open(bs->filename, s->open_flags | O_NONBLOCK); | |
1113 | if (fd >= 0) { | |
1114 | if (ioctl(fd, FDEJECT, 0) < 0) | |
1115 | perror("FDEJECT"); | |
1116 | close(fd); | |
1117 | } | |
1118 | ||
1119 | return 0; | |
1120 | } | |
1121 | ||
1122 | static BlockDriver bdrv_host_floppy = { | |
1123 | .format_name = "host_floppy", | |
84a12e66 | 1124 | .protocol_name = "host_floppy", |
f3a5d3f8 | 1125 | .instance_size = sizeof(BDRVRawState), |
508c7cb3 | 1126 | .bdrv_probe_device = floppy_probe_device, |
66f82cee | 1127 | .bdrv_file_open = floppy_open, |
f3a5d3f8 CH |
1128 | .bdrv_close = raw_close, |
1129 | .bdrv_create = hdev_create, | |
0b4ce02e | 1130 | .create_options = raw_create_options, |
336c1c12 | 1131 | .bdrv_has_zero_init = hdev_has_zero_init, |
f3a5d3f8 CH |
1132 | .bdrv_flush = raw_flush, |
1133 | ||
f3a5d3f8 CH |
1134 | .bdrv_aio_readv = raw_aio_readv, |
1135 | .bdrv_aio_writev = raw_aio_writev, | |
b2e12bc6 | 1136 | .bdrv_aio_flush = raw_aio_flush, |
f3a5d3f8 CH |
1137 | |
1138 | .bdrv_read = raw_read, | |
1139 | .bdrv_write = raw_write, | |
1140 | .bdrv_getlength = raw_getlength, | |
1141 | ||
1142 | /* removable device support */ | |
1143 | .bdrv_is_inserted = floppy_is_inserted, | |
1144 | .bdrv_media_changed = floppy_media_changed, | |
1145 | .bdrv_eject = floppy_eject, | |
f3a5d3f8 CH |
1146 | }; |
1147 | ||
1148 | static int cdrom_open(BlockDriverState *bs, const char *filename, int flags) | |
1149 | { | |
1150 | BDRVRawState *s = bs->opaque; | |
1151 | ||
f3a5d3f8 CH |
1152 | s->type = FTYPE_CD; |
1153 | ||
19a3da7f BS |
1154 | /* open will not fail even if no CD is inserted, so add O_NONBLOCK */ |
1155 | return raw_open_common(bs, filename, flags, O_NONBLOCK); | |
f3a5d3f8 CH |
1156 | } |
1157 | ||
508c7cb3 CH |
1158 | static int cdrom_probe_device(const char *filename) |
1159 | { | |
3baf720e CR |
1160 | int fd, ret; |
1161 | int prio = 0; | |
1162 | ||
3baf720e CR |
1163 | fd = open(filename, O_RDONLY | O_NONBLOCK); |
1164 | if (fd < 0) { | |
1165 | goto out; | |
1166 | } | |
1167 | ||
1168 | /* Attempt to detect via a CDROM specific ioctl */ | |
1169 | ret = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT); | |
1170 | if (ret >= 0) | |
1171 | prio = 100; | |
1172 | ||
1173 | close(fd); | |
1174 | out: | |
1175 | return prio; | |
508c7cb3 CH |
1176 | } |
1177 | ||
f3a5d3f8 CH |
1178 | static int cdrom_is_inserted(BlockDriverState *bs) |
1179 | { | |
1180 | BDRVRawState *s = bs->opaque; | |
1181 | int ret; | |
1182 | ||
1183 | ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT); | |
1184 | if (ret == CDS_DISC_OK) | |
1185 | return 1; | |
1186 | return 0; | |
1187 | } | |
1188 | ||
1189 | static int cdrom_eject(BlockDriverState *bs, int eject_flag) | |
1190 | { | |
1191 | BDRVRawState *s = bs->opaque; | |
1192 | ||
1193 | if (eject_flag) { | |
1194 | if (ioctl(s->fd, CDROMEJECT, NULL) < 0) | |
1195 | perror("CDROMEJECT"); | |
1196 | } else { | |
1197 | if (ioctl(s->fd, CDROMCLOSETRAY, NULL) < 0) | |
1198 | perror("CDROMEJECT"); | |
1199 | } | |
1200 | ||
1201 | return 0; | |
1202 | } | |
1203 | ||
1204 | static int cdrom_set_locked(BlockDriverState *bs, int locked) | |
1205 | { | |
1206 | BDRVRawState *s = bs->opaque; | |
1207 | ||
1208 | if (ioctl(s->fd, CDROM_LOCKDOOR, locked) < 0) { | |
1209 | /* | |
1210 | * Note: an error can happen if the distribution automatically | |
1211 | * mounts the CD-ROM | |
1212 | */ | |
1213 | /* perror("CDROM_LOCKDOOR"); */ | |
1214 | } | |
1215 | ||
1216 | return 0; | |
1217 | } | |
1218 | ||
1219 | static BlockDriver bdrv_host_cdrom = { | |
1220 | .format_name = "host_cdrom", | |
84a12e66 | 1221 | .protocol_name = "host_cdrom", |
f3a5d3f8 | 1222 | .instance_size = sizeof(BDRVRawState), |
508c7cb3 | 1223 | .bdrv_probe_device = cdrom_probe_device, |
66f82cee | 1224 | .bdrv_file_open = cdrom_open, |
f3a5d3f8 CH |
1225 | .bdrv_close = raw_close, |
1226 | .bdrv_create = hdev_create, | |
0b4ce02e | 1227 | .create_options = raw_create_options, |
336c1c12 | 1228 | .bdrv_has_zero_init = hdev_has_zero_init, |
f3a5d3f8 CH |
1229 | .bdrv_flush = raw_flush, |
1230 | ||
f3a5d3f8 CH |
1231 | .bdrv_aio_readv = raw_aio_readv, |
1232 | .bdrv_aio_writev = raw_aio_writev, | |
b2e12bc6 | 1233 | .bdrv_aio_flush = raw_aio_flush, |
f3a5d3f8 CH |
1234 | |
1235 | .bdrv_read = raw_read, | |
1236 | .bdrv_write = raw_write, | |
1237 | .bdrv_getlength = raw_getlength, | |
1238 | ||
1239 | /* removable device support */ | |
1240 | .bdrv_is_inserted = cdrom_is_inserted, | |
1241 | .bdrv_eject = cdrom_eject, | |
1242 | .bdrv_set_locked = cdrom_set_locked, | |
1243 | ||
1244 | /* generic scsi device */ | |
63ec93db | 1245 | .bdrv_ioctl = hdev_ioctl, |
63ec93db | 1246 | .bdrv_aio_ioctl = hdev_aio_ioctl, |
f3a5d3f8 CH |
1247 | }; |
1248 | #endif /* __linux__ */ | |
1249 | ||
a167ba50 | 1250 | #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__) |
f3a5d3f8 CH |
1251 | static int cdrom_open(BlockDriverState *bs, const char *filename, int flags) |
1252 | { | |
1253 | BDRVRawState *s = bs->opaque; | |
1254 | int ret; | |
1255 | ||
1256 | s->type = FTYPE_CD; | |
1257 | ||
19a3da7f | 1258 | ret = raw_open_common(bs, filename, flags, 0); |
f3a5d3f8 CH |
1259 | if (ret) |
1260 | return ret; | |
1261 | ||
1262 | /* make sure the door isnt locked at this time */ | |
1263 | ioctl(s->fd, CDIOCALLOW); | |
1264 | return 0; | |
1265 | } | |
1266 | ||
508c7cb3 CH |
1267 | static int cdrom_probe_device(const char *filename) |
1268 | { | |
1269 | if (strstart(filename, "/dev/cd", NULL) || | |
1270 | strstart(filename, "/dev/acd", NULL)) | |
1271 | return 100; | |
1272 | return 0; | |
1273 | } | |
1274 | ||
f3a5d3f8 CH |
1275 | static int cdrom_reopen(BlockDriverState *bs) |
1276 | { | |
1277 | BDRVRawState *s = bs->opaque; | |
1278 | int fd; | |
1279 | ||
1280 | /* | |
1281 | * Force reread of possibly changed/newly loaded disc, | |
1282 | * FreeBSD seems to not notice sometimes... | |
1283 | */ | |
1284 | if (s->fd >= 0) | |
1285 | close(s->fd); | |
1286 | fd = open(bs->filename, s->open_flags, 0644); | |
1287 | if (fd < 0) { | |
1288 | s->fd = -1; | |
1289 | return -EIO; | |
1290 | } | |
1291 | s->fd = fd; | |
1292 | ||
1293 | /* make sure the door isnt locked at this time */ | |
1294 | ioctl(s->fd, CDIOCALLOW); | |
1295 | return 0; | |
1296 | } | |
1297 | ||
1298 | static int cdrom_is_inserted(BlockDriverState *bs) | |
1299 | { | |
1300 | return raw_getlength(bs) > 0; | |
1301 | } | |
1302 | ||
1303 | static int cdrom_eject(BlockDriverState *bs, int eject_flag) | |
1304 | { | |
1305 | BDRVRawState *s = bs->opaque; | |
1306 | ||
1307 | if (s->fd < 0) | |
1308 | return -ENOTSUP; | |
1309 | ||
1310 | (void) ioctl(s->fd, CDIOCALLOW); | |
1311 | ||
1312 | if (eject_flag) { | |
1313 | if (ioctl(s->fd, CDIOCEJECT) < 0) | |
1314 | perror("CDIOCEJECT"); | |
1315 | } else { | |
1316 | if (ioctl(s->fd, CDIOCCLOSE) < 0) | |
1317 | perror("CDIOCCLOSE"); | |
1318 | } | |
1319 | ||
1320 | if (cdrom_reopen(bs) < 0) | |
1321 | return -ENOTSUP; | |
1322 | return 0; | |
1323 | } | |
1324 | ||
1325 | static int cdrom_set_locked(BlockDriverState *bs, int locked) | |
1326 | { | |
1327 | BDRVRawState *s = bs->opaque; | |
1328 | ||
1329 | if (s->fd < 0) | |
1330 | return -ENOTSUP; | |
1331 | if (ioctl(s->fd, (locked ? CDIOCPREVENT : CDIOCALLOW)) < 0) { | |
1332 | /* | |
1333 | * Note: an error can happen if the distribution automatically | |
1334 | * mounts the CD-ROM | |
1335 | */ | |
1336 | /* perror("CDROM_LOCKDOOR"); */ | |
1337 | } | |
1338 | ||
1339 | return 0; | |
1340 | } | |
1341 | ||
1342 | static BlockDriver bdrv_host_cdrom = { | |
1343 | .format_name = "host_cdrom", | |
84a12e66 | 1344 | .protocol_name = "host_cdrom", |
f3a5d3f8 | 1345 | .instance_size = sizeof(BDRVRawState), |
508c7cb3 | 1346 | .bdrv_probe_device = cdrom_probe_device, |
66f82cee | 1347 | .bdrv_file_open = cdrom_open, |
f3a5d3f8 CH |
1348 | .bdrv_close = raw_close, |
1349 | .bdrv_create = hdev_create, | |
0b4ce02e | 1350 | .create_options = raw_create_options, |
336c1c12 | 1351 | .bdrv_has_zero_init = hdev_has_zero_init, |
f3a5d3f8 CH |
1352 | .bdrv_flush = raw_flush, |
1353 | ||
f3a5d3f8 CH |
1354 | .bdrv_aio_readv = raw_aio_readv, |
1355 | .bdrv_aio_writev = raw_aio_writev, | |
b2e12bc6 | 1356 | .bdrv_aio_flush = raw_aio_flush, |
f3a5d3f8 CH |
1357 | |
1358 | .bdrv_read = raw_read, | |
1359 | .bdrv_write = raw_write, | |
1360 | .bdrv_getlength = raw_getlength, | |
1361 | ||
19cb3738 | 1362 | /* removable device support */ |
f3a5d3f8 CH |
1363 | .bdrv_is_inserted = cdrom_is_inserted, |
1364 | .bdrv_eject = cdrom_eject, | |
1365 | .bdrv_set_locked = cdrom_set_locked, | |
19cb3738 | 1366 | }; |
f3a5d3f8 | 1367 | #endif /* __FreeBSD__ */ |
5efa9d5a | 1368 | |
84a12e66 | 1369 | static void bdrv_file_init(void) |
5efa9d5a | 1370 | { |
508c7cb3 CH |
1371 | /* |
1372 | * Register all the drivers. Note that order is important, the driver | |
1373 | * registered last will get probed first. | |
1374 | */ | |
84a12e66 | 1375 | bdrv_register(&bdrv_file); |
5efa9d5a | 1376 | bdrv_register(&bdrv_host_device); |
f3a5d3f8 CH |
1377 | #ifdef __linux__ |
1378 | bdrv_register(&bdrv_host_floppy); | |
1379 | bdrv_register(&bdrv_host_cdrom); | |
1380 | #endif | |
a167ba50 | 1381 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
f3a5d3f8 CH |
1382 | bdrv_register(&bdrv_host_cdrom); |
1383 | #endif | |
5efa9d5a AL |
1384 | } |
1385 | ||
84a12e66 | 1386 | block_init(bdrv_file_init); |