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