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