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