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