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