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