]>
Commit | Line | Data |
---|---|---|
83f64091 | 1 | /* |
223d4670 | 2 | * Block driver for RAW files (posix) |
5fafdf24 | 3 | * |
83f64091 | 4 | * Copyright (c) 2006 Fabrice Bellard |
5fafdf24 | 5 | * |
83f64091 FB |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
faf07963 | 24 | #include "qemu-common.h" |
87ecb68b | 25 | #include "qemu-timer.h" |
baf35cb9 | 26 | #include "qemu-char.h" |
83f64091 | 27 | #include "block_int.h" |
5efa9d5a | 28 | #include "module.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 | 57 | #include <sys/disk.h> |
9f23011a | 58 | #include <sys/cdio.h> |
1cb6c3fd | 59 | #endif |
83f64091 | 60 | |
128ab2ff BS |
61 | #ifdef __OpenBSD__ |
62 | #include <sys/ioctl.h> | |
63 | #include <sys/disklabel.h> | |
64 | #include <sys/dkio.h> | |
65 | #endif | |
66 | ||
c5e97233 BS |
67 | #ifdef __DragonFly__ |
68 | #include <sys/ioctl.h> | |
69 | #include <sys/diskslice.h> | |
70 | #endif | |
71 | ||
19cb3738 | 72 | //#define DEBUG_FLOPPY |
83f64091 | 73 | |
faf07963 | 74 | //#define DEBUG_BLOCK |
03ff3ca3 | 75 | #if defined(DEBUG_BLOCK) |
001faf32 BS |
76 | #define DEBUG_BLOCK_PRINT(formatCstr, ...) do { if (qemu_log_enabled()) \ |
77 | { qemu_log(formatCstr, ## __VA_ARGS__); qemu_log_flush(); } } while (0) | |
8c05dbf9 | 78 | #else |
001faf32 | 79 | #define DEBUG_BLOCK_PRINT(formatCstr, ...) |
8c05dbf9 TS |
80 | #endif |
81 | ||
f6465578 AL |
82 | /* OS X does not have O_DSYNC */ |
83 | #ifndef O_DSYNC | |
7ab064d2 | 84 | #define O_DSYNC O_SYNC |
f6465578 AL |
85 | #endif |
86 | ||
9f7965c7 AL |
87 | /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */ |
88 | #ifndef O_DIRECT | |
89 | #define O_DIRECT O_DSYNC | |
90 | #endif | |
91 | ||
19cb3738 FB |
92 | #define FTYPE_FILE 0 |
93 | #define FTYPE_CD 1 | |
94 | #define FTYPE_FD 2 | |
83f64091 | 95 | |
bed5cc52 FB |
96 | #define ALIGNED_BUFFER_SIZE (32 * 512) |
97 | ||
19cb3738 FB |
98 | /* if the FD is not accessed during that time (in ms), we try to |
99 | reopen it to see if the disk has been changed */ | |
100 | #define FD_OPEN_TIMEOUT 1000 | |
83f64091 | 101 | |
19cb3738 FB |
102 | typedef struct BDRVRawState { |
103 | int fd; | |
104 | int type; | |
8c05dbf9 | 105 | unsigned int lseek_err_cnt; |
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; | |
9f23011a BS |
113 | #endif |
114 | #if defined(__FreeBSD__) | |
115 | int cd_open_flags; | |
83f64091 | 116 | #endif |
bed5cc52 | 117 | uint8_t* aligned_buf; |
19cb3738 FB |
118 | } BDRVRawState; |
119 | ||
a76bab49 AL |
120 | static int posix_aio_init(void); |
121 | ||
19cb3738 | 122 | static int fd_open(BlockDriverState *bs); |
83f64091 | 123 | |
9f23011a BS |
124 | #if defined(__FreeBSD__) |
125 | static int cd_open(BlockDriverState *bs); | |
126 | #endif | |
127 | ||
128 | static int raw_is_inserted(BlockDriverState *bs); | |
129 | ||
83f64091 FB |
130 | static int raw_open(BlockDriverState *bs, const char *filename, int flags) |
131 | { | |
132 | BDRVRawState *s = bs->opaque; | |
19cb3738 | 133 | int fd, open_flags, ret; |
83f64091 | 134 | |
a76bab49 AL |
135 | posix_aio_init(); |
136 | ||
8c05dbf9 TS |
137 | s->lseek_err_cnt = 0; |
138 | ||
83f64091 FB |
139 | open_flags = O_BINARY; |
140 | if ((flags & BDRV_O_ACCESS) == O_RDWR) { | |
141 | open_flags |= O_RDWR; | |
142 | } else { | |
143 | open_flags |= O_RDONLY; | |
144 | bs->read_only = 1; | |
145 | } | |
146 | if (flags & BDRV_O_CREAT) | |
147 | open_flags |= O_CREAT | O_TRUNC; | |
9f7965c7 AL |
148 | |
149 | /* Use O_DSYNC for write-through caching, no flags for write-back caching, | |
150 | * and O_DIRECT for no caching. */ | |
151 | if ((flags & BDRV_O_NOCACHE)) | |
33f00271 | 152 | open_flags |= O_DIRECT; |
9f7965c7 AL |
153 | else if (!(flags & BDRV_O_CACHE_WB)) |
154 | open_flags |= O_DSYNC; | |
83f64091 | 155 | |
19cb3738 FB |
156 | s->type = FTYPE_FILE; |
157 | ||
83f64091 | 158 | fd = open(filename, open_flags, 0644); |
19cb3738 FB |
159 | if (fd < 0) { |
160 | ret = -errno; | |
161 | if (ret == -EROFS) | |
162 | ret = -EACCES; | |
163 | return ret; | |
164 | } | |
83f64091 | 165 | s->fd = fd; |
bed5cc52 | 166 | s->aligned_buf = NULL; |
9f7965c7 | 167 | if ((flags & BDRV_O_NOCACHE)) { |
e268ca52 | 168 | s->aligned_buf = qemu_blockalign(bs, ALIGNED_BUFFER_SIZE); |
bed5cc52 FB |
169 | if (s->aligned_buf == NULL) { |
170 | ret = -errno; | |
171 | close(fd); | |
172 | return ret; | |
173 | } | |
174 | } | |
83f64091 FB |
175 | return 0; |
176 | } | |
177 | ||
178 | /* XXX: use host sector size if necessary with: | |
179 | #ifdef DIOCGSECTORSIZE | |
180 | { | |
181 | unsigned int sectorsize = 512; | |
182 | if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) && | |
183 | sectorsize > bufsize) | |
184 | bufsize = sectorsize; | |
185 | } | |
186 | #endif | |
187 | #ifdef CONFIG_COCOA | |
188 | u_int32_t blockSize = 512; | |
189 | if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) { | |
190 | bufsize = blockSize; | |
191 | } | |
192 | #endif | |
193 | */ | |
194 | ||
bed5cc52 FB |
195 | /* |
196 | * offset and count are in bytes, but must be multiples of 512 for files | |
197 | * opened with O_DIRECT. buf must be aligned to 512 bytes then. | |
198 | * | |
199 | * This function may be called without alignment if the caller ensures | |
200 | * that O_DIRECT is not in effect. | |
201 | */ | |
202 | static int raw_pread_aligned(BlockDriverState *bs, int64_t offset, | |
83f64091 FB |
203 | uint8_t *buf, int count) |
204 | { | |
205 | BDRVRawState *s = bs->opaque; | |
206 | int ret; | |
3b46e624 | 207 | |
19cb3738 FB |
208 | ret = fd_open(bs); |
209 | if (ret < 0) | |
210 | return ret; | |
211 | ||
985a03b0 | 212 | if (offset >= 0 && lseek(s->fd, offset, SEEK_SET) == (off_t)-1) { |
8c05dbf9 TS |
213 | ++(s->lseek_err_cnt); |
214 | if(s->lseek_err_cnt <= 10) { | |
92868412 JM |
215 | DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64 |
216 | "] lseek failed : %d = %s\n", | |
8c05dbf9 TS |
217 | s->fd, bs->filename, offset, buf, count, |
218 | bs->total_sectors, errno, strerror(errno)); | |
219 | } | |
220 | return -1; | |
221 | } | |
222 | s->lseek_err_cnt=0; | |
223 | ||
83f64091 | 224 | ret = read(s->fd, buf, count); |
8c05dbf9 TS |
225 | if (ret == count) |
226 | goto label__raw_read__success; | |
227 | ||
92868412 JM |
228 | DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64 |
229 | "] read failed %d : %d = %s\n", | |
8c05dbf9 TS |
230 | s->fd, bs->filename, offset, buf, count, |
231 | bs->total_sectors, ret, errno, strerror(errno)); | |
232 | ||
233 | /* Try harder for CDrom. */ | |
234 | if (bs->type == BDRV_TYPE_CDROM) { | |
235 | lseek(s->fd, offset, SEEK_SET); | |
236 | ret = read(s->fd, buf, count); | |
237 | if (ret == count) | |
238 | goto label__raw_read__success; | |
239 | lseek(s->fd, offset, SEEK_SET); | |
240 | ret = read(s->fd, buf, count); | |
241 | if (ret == count) | |
242 | goto label__raw_read__success; | |
243 | ||
92868412 JM |
244 | DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64 |
245 | "] retry read failed %d : %d = %s\n", | |
8c05dbf9 TS |
246 | s->fd, bs->filename, offset, buf, count, |
247 | bs->total_sectors, ret, errno, strerror(errno)); | |
248 | } | |
249 | ||
8c05dbf9 TS |
250 | label__raw_read__success: |
251 | ||
94c6d6d8 | 252 | return (ret < 0) ? -errno : ret; |
83f64091 FB |
253 | } |
254 | ||
bed5cc52 FB |
255 | /* |
256 | * offset and count are in bytes, but must be multiples of 512 for files | |
257 | * opened with O_DIRECT. buf must be aligned to 512 bytes then. | |
258 | * | |
259 | * This function may be called without alignment if the caller ensures | |
260 | * that O_DIRECT is not in effect. | |
261 | */ | |
262 | static int raw_pwrite_aligned(BlockDriverState *bs, int64_t offset, | |
83f64091 FB |
263 | const uint8_t *buf, int count) |
264 | { | |
265 | BDRVRawState *s = bs->opaque; | |
266 | int ret; | |
3b46e624 | 267 | |
19cb3738 FB |
268 | ret = fd_open(bs); |
269 | if (ret < 0) | |
4141d4c2 | 270 | return -errno; |
19cb3738 | 271 | |
985a03b0 | 272 | if (offset >= 0 && lseek(s->fd, offset, SEEK_SET) == (off_t)-1) { |
8c05dbf9 TS |
273 | ++(s->lseek_err_cnt); |
274 | if(s->lseek_err_cnt) { | |
92868412 JM |
275 | DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%" |
276 | PRId64 "] lseek failed : %d = %s\n", | |
8c05dbf9 TS |
277 | s->fd, bs->filename, offset, buf, count, |
278 | bs->total_sectors, errno, strerror(errno)); | |
279 | } | |
4141d4c2 | 280 | return -EIO; |
8c05dbf9 TS |
281 | } |
282 | s->lseek_err_cnt = 0; | |
283 | ||
83f64091 | 284 | ret = write(s->fd, buf, count); |
8c05dbf9 TS |
285 | if (ret == count) |
286 | goto label__raw_write__success; | |
287 | ||
92868412 JM |
288 | DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%" PRId64 |
289 | "] write failed %d : %d = %s\n", | |
8c05dbf9 TS |
290 | s->fd, bs->filename, offset, buf, count, |
291 | bs->total_sectors, ret, errno, strerror(errno)); | |
292 | ||
8c05dbf9 TS |
293 | label__raw_write__success: |
294 | ||
4141d4c2 | 295 | return (ret < 0) ? -errno : ret; |
83f64091 FB |
296 | } |
297 | ||
bed5cc52 | 298 | |
bed5cc52 FB |
299 | /* |
300 | * offset and count are in bytes and possibly not aligned. For files opened | |
301 | * with O_DIRECT, necessary alignments are ensured before calling | |
302 | * raw_pread_aligned to do the actual read. | |
303 | */ | |
304 | static int raw_pread(BlockDriverState *bs, int64_t offset, | |
305 | uint8_t *buf, int count) | |
306 | { | |
307 | BDRVRawState *s = bs->opaque; | |
308 | int size, ret, shift, sum; | |
309 | ||
310 | sum = 0; | |
311 | ||
312 | if (s->aligned_buf != NULL) { | |
313 | ||
314 | if (offset & 0x1ff) { | |
315 | /* align offset on a 512 bytes boundary */ | |
316 | ||
317 | shift = offset & 0x1ff; | |
318 | size = (shift + count + 0x1ff) & ~0x1ff; | |
319 | if (size > ALIGNED_BUFFER_SIZE) | |
320 | size = ALIGNED_BUFFER_SIZE; | |
321 | ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, size); | |
322 | if (ret < 0) | |
323 | return ret; | |
324 | ||
325 | size = 512 - shift; | |
326 | if (size > count) | |
327 | size = count; | |
328 | memcpy(buf, s->aligned_buf + shift, size); | |
329 | ||
330 | buf += size; | |
331 | offset += size; | |
332 | count -= size; | |
333 | sum += size; | |
334 | ||
335 | if (count == 0) | |
336 | return sum; | |
337 | } | |
338 | if (count & 0x1ff || (uintptr_t) buf & 0x1ff) { | |
339 | ||
340 | /* read on aligned buffer */ | |
341 | ||
342 | while (count) { | |
343 | ||
344 | size = (count + 0x1ff) & ~0x1ff; | |
345 | if (size > ALIGNED_BUFFER_SIZE) | |
346 | size = ALIGNED_BUFFER_SIZE; | |
347 | ||
348 | ret = raw_pread_aligned(bs, offset, s->aligned_buf, size); | |
349 | if (ret < 0) | |
350 | return ret; | |
351 | ||
352 | size = ret; | |
353 | if (size > count) | |
354 | size = count; | |
355 | ||
356 | memcpy(buf, s->aligned_buf, size); | |
357 | ||
358 | buf += size; | |
359 | offset += size; | |
360 | count -= size; | |
361 | sum += size; | |
362 | } | |
363 | ||
364 | return sum; | |
365 | } | |
366 | } | |
367 | ||
368 | return raw_pread_aligned(bs, offset, buf, count) + sum; | |
369 | } | |
370 | ||
eda578e5 AL |
371 | static int raw_read(BlockDriverState *bs, int64_t sector_num, |
372 | uint8_t *buf, int nb_sectors) | |
373 | { | |
537a1d4b AL |
374 | int ret; |
375 | ||
376 | ret = raw_pread(bs, sector_num * 512, buf, nb_sectors * 512); | |
377 | if (ret == (nb_sectors * 512)) | |
378 | ret = 0; | |
379 | return ret; | |
eda578e5 AL |
380 | } |
381 | ||
bed5cc52 FB |
382 | /* |
383 | * offset and count are in bytes and possibly not aligned. For files opened | |
384 | * with O_DIRECT, necessary alignments are ensured before calling | |
385 | * raw_pwrite_aligned to do the actual write. | |
386 | */ | |
387 | static int raw_pwrite(BlockDriverState *bs, int64_t offset, | |
388 | const uint8_t *buf, int count) | |
389 | { | |
390 | BDRVRawState *s = bs->opaque; | |
391 | int size, ret, shift, sum; | |
392 | ||
393 | sum = 0; | |
394 | ||
395 | if (s->aligned_buf != NULL) { | |
396 | ||
397 | if (offset & 0x1ff) { | |
398 | /* align offset on a 512 bytes boundary */ | |
399 | shift = offset & 0x1ff; | |
400 | ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, 512); | |
401 | if (ret < 0) | |
402 | return ret; | |
403 | ||
404 | size = 512 - shift; | |
405 | if (size > count) | |
406 | size = count; | |
407 | memcpy(s->aligned_buf + shift, buf, size); | |
408 | ||
409 | ret = raw_pwrite_aligned(bs, offset - shift, s->aligned_buf, 512); | |
410 | if (ret < 0) | |
411 | return ret; | |
412 | ||
413 | buf += size; | |
414 | offset += size; | |
415 | count -= size; | |
416 | sum += size; | |
417 | ||
418 | if (count == 0) | |
419 | return sum; | |
420 | } | |
421 | if (count & 0x1ff || (uintptr_t) buf & 0x1ff) { | |
422 | ||
423 | while ((size = (count & ~0x1ff)) != 0) { | |
424 | ||
425 | if (size > ALIGNED_BUFFER_SIZE) | |
426 | size = ALIGNED_BUFFER_SIZE; | |
427 | ||
428 | memcpy(s->aligned_buf, buf, size); | |
429 | ||
430 | ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, size); | |
431 | if (ret < 0) | |
432 | return ret; | |
433 | ||
434 | buf += ret; | |
435 | offset += ret; | |
436 | count -= ret; | |
437 | sum += ret; | |
438 | } | |
439 | /* here, count < 512 because (count & ~0x1ff) == 0 */ | |
440 | if (count) { | |
441 | ret = raw_pread_aligned(bs, offset, s->aligned_buf, 512); | |
442 | if (ret < 0) | |
443 | return ret; | |
444 | memcpy(s->aligned_buf, buf, count); | |
445 | ||
446 | ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, 512); | |
447 | if (ret < 0) | |
448 | return ret; | |
449 | if (count < ret) | |
450 | ret = count; | |
451 | ||
452 | sum += ret; | |
453 | } | |
454 | return sum; | |
455 | } | |
456 | } | |
457 | return raw_pwrite_aligned(bs, offset, buf, count) + sum; | |
458 | } | |
459 | ||
eda578e5 AL |
460 | static int raw_write(BlockDriverState *bs, int64_t sector_num, |
461 | const uint8_t *buf, int nb_sectors) | |
462 | { | |
537a1d4b AL |
463 | int ret; |
464 | ret = raw_pwrite(bs, sector_num * 512, buf, nb_sectors * 512); | |
465 | if (ret == (nb_sectors * 512)) | |
466 | ret = 0; | |
467 | return ret; | |
eda578e5 AL |
468 | } |
469 | ||
414f0dab | 470 | #ifdef CONFIG_AIO |
83f64091 | 471 | /***********************************************************/ |
19cb3738 | 472 | /* Unix AIO using POSIX AIO */ |
83f64091 FB |
473 | |
474 | typedef struct RawAIOCB { | |
ce1a14dc | 475 | BlockDriverAIOCB common; |
3c529d93 | 476 | struct qemu_paiocb aiocb; |
ce1a14dc | 477 | struct RawAIOCB *next; |
bed5cc52 | 478 | int ret; |
83f64091 FB |
479 | } RawAIOCB; |
480 | ||
a76bab49 AL |
481 | typedef struct PosixAioState |
482 | { | |
9e472e10 | 483 | int rfd, wfd; |
a76bab49 AL |
484 | RawAIOCB *first_aio; |
485 | } PosixAioState; | |
83f64091 | 486 | |
a76bab49 | 487 | static void posix_aio_read(void *opaque) |
83f64091 | 488 | { |
a76bab49 | 489 | PosixAioState *s = opaque; |
ce1a14dc | 490 | RawAIOCB *acb, **pacb; |
83f64091 | 491 | int ret; |
9e472e10 AL |
492 | ssize_t len; |
493 | ||
e20e830b AL |
494 | /* read all bytes from signal pipe */ |
495 | for (;;) { | |
496 | char bytes[16]; | |
9e472e10 | 497 | |
e20e830b | 498 | len = read(s->rfd, bytes, sizeof(bytes)); |
2c41a5f9 | 499 | if (len == -1 && errno == EINTR) |
e20e830b AL |
500 | continue; /* try again */ |
501 | if (len == sizeof(bytes)) | |
502 | continue; /* more to read */ | |
503 | break; | |
504 | } | |
83f64091 FB |
505 | |
506 | for(;;) { | |
a76bab49 | 507 | pacb = &s->first_aio; |
83f64091 FB |
508 | for(;;) { |
509 | acb = *pacb; | |
510 | if (!acb) | |
511 | goto the_end; | |
3c529d93 | 512 | ret = qemu_paio_error(&acb->aiocb); |
83f64091 FB |
513 | if (ret == ECANCELED) { |
514 | /* remove the request */ | |
ce1a14dc PB |
515 | *pacb = acb->next; |
516 | qemu_aio_release(acb); | |
83f64091 FB |
517 | } else if (ret != EINPROGRESS) { |
518 | /* end of aio */ | |
519 | if (ret == 0) { | |
3c529d93 | 520 | ret = qemu_paio_return(&acb->aiocb); |
ce1a14dc | 521 | if (ret == acb->aiocb.aio_nbytes) |
83f64091 FB |
522 | ret = 0; |
523 | else | |
19cb3738 | 524 | ret = -EINVAL; |
83f64091 FB |
525 | } else { |
526 | ret = -ret; | |
527 | } | |
528 | /* remove the request */ | |
ce1a14dc | 529 | *pacb = acb->next; |
83f64091 | 530 | /* call the callback */ |
ce1a14dc PB |
531 | acb->common.cb(acb->common.opaque, ret); |
532 | qemu_aio_release(acb); | |
83f64091 FB |
533 | break; |
534 | } else { | |
ce1a14dc | 535 | pacb = &acb->next; |
83f64091 FB |
536 | } |
537 | } | |
538 | } | |
539 | the_end: ; | |
540 | } | |
541 | ||
a76bab49 | 542 | static int posix_aio_flush(void *opaque) |
baf35cb9 | 543 | { |
a76bab49 AL |
544 | PosixAioState *s = opaque; |
545 | return !!s->first_aio; | |
546 | } | |
547 | ||
548 | static PosixAioState *posix_aio_state; | |
baf35cb9 | 549 | |
9e472e10 AL |
550 | static void aio_signal_handler(int signum) |
551 | { | |
552 | if (posix_aio_state) { | |
553 | char byte = 0; | |
554 | ||
555 | write(posix_aio_state->wfd, &byte, sizeof(byte)); | |
556 | } | |
557 | ||
558 | qemu_service_io(); | |
559 | } | |
560 | ||
a76bab49 AL |
561 | static int posix_aio_init(void) |
562 | { | |
9e472e10 | 563 | struct sigaction act; |
a76bab49 | 564 | PosixAioState *s; |
9e472e10 | 565 | int fds[2]; |
3c529d93 | 566 | struct qemu_paioinit ai; |
a76bab49 AL |
567 | |
568 | if (posix_aio_state) | |
569 | return 0; | |
ad02ad6f | 570 | |
a76bab49 | 571 | s = qemu_malloc(sizeof(PosixAioState)); |
baf35cb9 | 572 | |
9e472e10 AL |
573 | sigfillset(&act.sa_mask); |
574 | act.sa_flags = 0; /* do not restart syscalls to interrupt select() */ | |
575 | act.sa_handler = aio_signal_handler; | |
576 | sigaction(SIGUSR2, &act, NULL); | |
577 | ||
a76bab49 | 578 | s->first_aio = NULL; |
9e472e10 AL |
579 | if (pipe(fds) == -1) { |
580 | fprintf(stderr, "failed to create pipe\n"); | |
27463101 AL |
581 | return -errno; |
582 | } | |
2c41a5f9 | 583 | |
9e472e10 AL |
584 | s->rfd = fds[0]; |
585 | s->wfd = fds[1]; | |
586 | ||
e20e830b | 587 | fcntl(s->rfd, F_SETFL, O_NONBLOCK); |
9e472e10 | 588 | fcntl(s->wfd, F_SETFL, O_NONBLOCK); |
2c41a5f9 | 589 | |
9e472e10 | 590 | qemu_aio_set_fd_handler(s->rfd, posix_aio_read, NULL, posix_aio_flush, s); |
baf35cb9 | 591 | |
3c529d93 AL |
592 | memset(&ai, 0, sizeof(ai)); |
593 | ai.aio_threads = 64; | |
594 | ai.aio_num = 64; | |
595 | qemu_paio_init(&ai); | |
acce87f9 | 596 | |
a76bab49 | 597 | posix_aio_state = s; |
6eb5733a | 598 | |
a76bab49 | 599 | return 0; |
83f64091 FB |
600 | } |
601 | ||
c16b5a2c CH |
602 | static void raw_aio_remove(RawAIOCB *acb) |
603 | { | |
604 | RawAIOCB **pacb; | |
605 | ||
606 | /* remove the callback from the queue */ | |
607 | pacb = &posix_aio_state->first_aio; | |
608 | for(;;) { | |
609 | if (*pacb == NULL) { | |
610 | fprintf(stderr, "raw_aio_remove: aio request not found!\n"); | |
611 | break; | |
612 | } else if (*pacb == acb) { | |
613 | *pacb = acb->next; | |
614 | qemu_aio_release(acb); | |
615 | break; | |
616 | } | |
617 | pacb = &(*pacb)->next; | |
618 | } | |
619 | } | |
620 | ||
621 | static void raw_aio_cancel(BlockDriverAIOCB *blockacb) | |
622 | { | |
623 | int ret; | |
624 | RawAIOCB *acb = (RawAIOCB *)blockacb; | |
625 | ||
626 | ret = qemu_paio_cancel(acb->aiocb.aio_fildes, &acb->aiocb); | |
627 | if (ret == QEMU_PAIO_NOTCANCELED) { | |
628 | /* fail safe: if the aio could not be canceled, we wait for | |
629 | it */ | |
630 | while (qemu_paio_error(&acb->aiocb) == EINPROGRESS); | |
631 | } | |
632 | ||
633 | raw_aio_remove(acb); | |
634 | } | |
635 | ||
636 | static AIOPool raw_aio_pool = { | |
637 | .aiocb_size = sizeof(RawAIOCB), | |
638 | .cancel = raw_aio_cancel, | |
639 | }; | |
640 | ||
f141eafe AL |
641 | static RawAIOCB *raw_aio_setup(BlockDriverState *bs, int64_t sector_num, |
642 | QEMUIOVector *qiov, int nb_sectors, | |
ce1a14dc | 643 | BlockDriverCompletionFunc *cb, void *opaque) |
83f64091 | 644 | { |
ce1a14dc PB |
645 | BDRVRawState *s = bs->opaque; |
646 | RawAIOCB *acb; | |
647 | ||
19cb3738 FB |
648 | if (fd_open(bs) < 0) |
649 | return NULL; | |
650 | ||
c16b5a2c | 651 | acb = qemu_aio_get(&raw_aio_pool, bs, cb, opaque); |
ce1a14dc PB |
652 | if (!acb) |
653 | return NULL; | |
3c529d93 | 654 | acb->aiocb.aio_fildes = s->fd; |
55f11ca3 | 655 | acb->aiocb.ev_signo = SIGUSR2; |
f141eafe AL |
656 | acb->aiocb.aio_iov = qiov->iov; |
657 | acb->aiocb.aio_niov = qiov->niov; | |
658 | acb->aiocb.aio_nbytes = nb_sectors * 512; | |
ce1a14dc | 659 | acb->aiocb.aio_offset = sector_num * 512; |
f141eafe AL |
660 | acb->aiocb.aio_flags = 0; |
661 | ||
662 | /* | |
663 | * If O_DIRECT is used the buffer needs to be aligned on a sector | |
664 | * boundary. Tell the low level code to ensure that in case it's | |
665 | * not done yet. | |
666 | */ | |
667 | if (s->aligned_buf) | |
668 | acb->aiocb.aio_flags |= QEMU_AIO_SECTOR_ALIGNED; | |
669 | ||
a76bab49 AL |
670 | acb->next = posix_aio_state->first_aio; |
671 | posix_aio_state->first_aio = acb; | |
ce1a14dc | 672 | return acb; |
83f64091 FB |
673 | } |
674 | ||
f141eafe AL |
675 | static BlockDriverAIOCB *raw_aio_readv(BlockDriverState *bs, |
676 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, | |
ce1a14dc | 677 | BlockDriverCompletionFunc *cb, void *opaque) |
83f64091 | 678 | { |
ce1a14dc | 679 | RawAIOCB *acb; |
83f64091 | 680 | |
f141eafe | 681 | acb = raw_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque); |
ce1a14dc PB |
682 | if (!acb) |
683 | return NULL; | |
3c529d93 | 684 | if (qemu_paio_read(&acb->aiocb) < 0) { |
22bf1458 | 685 | raw_aio_remove(acb); |
ce1a14dc | 686 | return NULL; |
5fafdf24 | 687 | } |
ce1a14dc | 688 | return &acb->common; |
83f64091 FB |
689 | } |
690 | ||
f141eafe AL |
691 | static BlockDriverAIOCB *raw_aio_writev(BlockDriverState *bs, |
692 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, | |
ce1a14dc | 693 | BlockDriverCompletionFunc *cb, void *opaque) |
83f64091 | 694 | { |
ce1a14dc | 695 | RawAIOCB *acb; |
83f64091 | 696 | |
f141eafe | 697 | acb = raw_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque); |
ce1a14dc PB |
698 | if (!acb) |
699 | return NULL; | |
3c529d93 | 700 | if (qemu_paio_write(&acb->aiocb) < 0) { |
22bf1458 | 701 | raw_aio_remove(acb); |
ce1a14dc | 702 | return NULL; |
5fafdf24 | 703 | } |
ce1a14dc | 704 | return &acb->common; |
83f64091 | 705 | } |
a76bab49 AL |
706 | #else /* CONFIG_AIO */ |
707 | static int posix_aio_init(void) | |
414f0dab | 708 | { |
674a24ac | 709 | return 0; |
414f0dab | 710 | } |
414f0dab BS |
711 | #endif /* CONFIG_AIO */ |
712 | ||
53538725 | 713 | |
83f64091 FB |
714 | static void raw_close(BlockDriverState *bs) |
715 | { | |
716 | BDRVRawState *s = bs->opaque; | |
19cb3738 FB |
717 | if (s->fd >= 0) { |
718 | close(s->fd); | |
719 | s->fd = -1; | |
bed5cc52 FB |
720 | if (s->aligned_buf != NULL) |
721 | qemu_free(s->aligned_buf); | |
19cb3738 | 722 | } |
83f64091 FB |
723 | } |
724 | ||
725 | static int raw_truncate(BlockDriverState *bs, int64_t offset) | |
726 | { | |
727 | BDRVRawState *s = bs->opaque; | |
19cb3738 FB |
728 | if (s->type != FTYPE_FILE) |
729 | return -ENOTSUP; | |
83f64091 FB |
730 | if (ftruncate(s->fd, offset) < 0) |
731 | return -errno; | |
732 | return 0; | |
733 | } | |
734 | ||
128ab2ff BS |
735 | #ifdef __OpenBSD__ |
736 | static int64_t raw_getlength(BlockDriverState *bs) | |
737 | { | |
738 | BDRVRawState *s = bs->opaque; | |
739 | int fd = s->fd; | |
740 | struct stat st; | |
741 | ||
742 | if (fstat(fd, &st)) | |
743 | return -1; | |
744 | if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) { | |
745 | struct disklabel dl; | |
746 | ||
747 | if (ioctl(fd, DIOCGDINFO, &dl)) | |
748 | return -1; | |
749 | return (uint64_t)dl.d_secsize * | |
750 | dl.d_partitions[DISKPART(st.st_rdev)].p_size; | |
751 | } else | |
752 | return st.st_size; | |
753 | } | |
754 | #else /* !__OpenBSD__ */ | |
83f64091 FB |
755 | static int64_t raw_getlength(BlockDriverState *bs) |
756 | { | |
757 | BDRVRawState *s = bs->opaque; | |
758 | int fd = s->fd; | |
759 | int64_t size; | |
179a2c19 | 760 | #ifdef HOST_BSD |
83f64091 | 761 | struct stat sb; |
9f23011a BS |
762 | #ifdef __FreeBSD__ |
763 | int reopened = 0; | |
764 | #endif | |
83f64091 FB |
765 | #endif |
766 | #ifdef __sun__ | |
767 | struct dk_minfo minfo; | |
768 | int rv; | |
769 | #endif | |
19cb3738 FB |
770 | int ret; |
771 | ||
772 | ret = fd_open(bs); | |
773 | if (ret < 0) | |
774 | return ret; | |
83f64091 | 775 | |
179a2c19 | 776 | #ifdef HOST_BSD |
9f23011a BS |
777 | #ifdef __FreeBSD__ |
778 | again: | |
779 | #endif | |
83f64091 FB |
780 | if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) { |
781 | #ifdef DIOCGMEDIASIZE | |
782 | if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size)) | |
c5e97233 BS |
783 | #elif defined(DIOCGPART) |
784 | { | |
785 | struct partinfo pi; | |
786 | if (ioctl(fd, DIOCGPART, &pi) == 0) | |
787 | size = pi.media_size; | |
788 | else | |
789 | size = 0; | |
790 | } | |
791 | if (size == 0) | |
83f64091 FB |
792 | #endif |
793 | #ifdef CONFIG_COCOA | |
794 | size = LONG_LONG_MAX; | |
795 | #else | |
796 | size = lseek(fd, 0LL, SEEK_END); | |
9f23011a BS |
797 | #endif |
798 | #ifdef __FreeBSD__ | |
799 | switch(s->type) { | |
800 | case FTYPE_CD: | |
801 | /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */ | |
802 | if (size == 2048LL * (unsigned)-1) | |
803 | size = 0; | |
804 | /* XXX no disc? maybe we need to reopen... */ | |
805 | if (size <= 0 && !reopened && cd_open(bs) >= 0) { | |
806 | reopened = 1; | |
807 | goto again; | |
808 | } | |
809 | } | |
83f64091 FB |
810 | #endif |
811 | } else | |
812 | #endif | |
813 | #ifdef __sun__ | |
814 | /* | |
815 | * use the DKIOCGMEDIAINFO ioctl to read the size. | |
816 | */ | |
817 | rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo ); | |
818 | if ( rv != -1 ) { | |
819 | size = minfo.dki_lbsize * minfo.dki_capacity; | |
820 | } else /* there are reports that lseek on some devices | |
821 | fails, but irc discussion said that contingency | |
822 | on contingency was overkill */ | |
823 | #endif | |
824 | { | |
825 | size = lseek(fd, 0, SEEK_END); | |
826 | } | |
83f64091 FB |
827 | return size; |
828 | } | |
128ab2ff | 829 | #endif |
83f64091 | 830 | |
0e7e1989 | 831 | static int raw_create(const char *filename, QEMUOptionParameter *options) |
83f64091 FB |
832 | { |
833 | int fd; | |
0e7e1989 | 834 | int64_t total_size = 0; |
83f64091 | 835 | |
0e7e1989 KW |
836 | /* Read out options */ |
837 | while (options && options->name) { | |
838 | if (!strcmp(options->name, BLOCK_OPT_SIZE)) { | |
839 | total_size = options->value.n / 512; | |
840 | } | |
841 | options++; | |
842 | } | |
83f64091 | 843 | |
5fafdf24 | 844 | fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, |
83f64091 FB |
845 | 0644); |
846 | if (fd < 0) | |
847 | return -EIO; | |
848 | ftruncate(fd, total_size * 512); | |
849 | close(fd); | |
850 | return 0; | |
851 | } | |
852 | ||
853 | static void raw_flush(BlockDriverState *bs) | |
854 | { | |
855 | BDRVRawState *s = bs->opaque; | |
856 | fsync(s->fd); | |
857 | } | |
858 | ||
0e7e1989 KW |
859 | |
860 | static QEMUOptionParameter raw_create_options[] = { | |
db08adf5 KW |
861 | { |
862 | .name = BLOCK_OPT_SIZE, | |
863 | .type = OPT_SIZE, | |
864 | .help = "Virtual disk size" | |
865 | }, | |
0e7e1989 KW |
866 | { NULL } |
867 | }; | |
868 | ||
5efa9d5a | 869 | static BlockDriver bdrv_raw = { |
856ae5c3 BS |
870 | .format_name = "raw", |
871 | .instance_size = sizeof(BDRVRawState), | |
872 | .bdrv_probe = NULL, /* no probe for protocols */ | |
873 | .bdrv_open = raw_open, | |
874 | .bdrv_read = raw_read, | |
875 | .bdrv_write = raw_write, | |
876 | .bdrv_close = raw_close, | |
877 | .bdrv_create = raw_create, | |
878 | .bdrv_flush = raw_flush, | |
3b46e624 | 879 | |
414f0dab | 880 | #ifdef CONFIG_AIO |
f141eafe AL |
881 | .bdrv_aio_readv = raw_aio_readv, |
882 | .bdrv_aio_writev = raw_aio_writev, | |
414f0dab | 883 | #endif |
3c529d93 | 884 | |
83f64091 FB |
885 | .bdrv_truncate = raw_truncate, |
886 | .bdrv_getlength = raw_getlength, | |
0e7e1989 KW |
887 | |
888 | .create_options = raw_create_options, | |
83f64091 FB |
889 | }; |
890 | ||
19cb3738 FB |
891 | /***********************************************/ |
892 | /* host device */ | |
893 | ||
894 | #ifdef CONFIG_COCOA | |
895 | static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator ); | |
896 | static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize ); | |
897 | ||
898 | kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator ) | |
899 | { | |
5fafdf24 | 900 | kern_return_t kernResult; |
19cb3738 FB |
901 | mach_port_t masterPort; |
902 | CFMutableDictionaryRef classesToMatch; | |
903 | ||
904 | kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort ); | |
905 | if ( KERN_SUCCESS != kernResult ) { | |
906 | printf( "IOMasterPort returned %d\n", kernResult ); | |
907 | } | |
3b46e624 | 908 | |
5fafdf24 | 909 | classesToMatch = IOServiceMatching( kIOCDMediaClass ); |
19cb3738 FB |
910 | if ( classesToMatch == NULL ) { |
911 | printf( "IOServiceMatching returned a NULL dictionary.\n" ); | |
912 | } else { | |
913 | CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue ); | |
914 | } | |
915 | kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator ); | |
916 | if ( KERN_SUCCESS != kernResult ) | |
917 | { | |
918 | printf( "IOServiceGetMatchingServices returned %d\n", kernResult ); | |
919 | } | |
3b46e624 | 920 | |
19cb3738 FB |
921 | return kernResult; |
922 | } | |
923 | ||
924 | kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize ) | |
925 | { | |
926 | io_object_t nextMedia; | |
927 | kern_return_t kernResult = KERN_FAILURE; | |
928 | *bsdPath = '\0'; | |
929 | nextMedia = IOIteratorNext( mediaIterator ); | |
930 | if ( nextMedia ) | |
931 | { | |
932 | CFTypeRef bsdPathAsCFString; | |
933 | bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 ); | |
934 | if ( bsdPathAsCFString ) { | |
935 | size_t devPathLength; | |
936 | strcpy( bsdPath, _PATH_DEV ); | |
937 | strcat( bsdPath, "r" ); | |
938 | devPathLength = strlen( bsdPath ); | |
939 | if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) { | |
940 | kernResult = KERN_SUCCESS; | |
941 | } | |
942 | CFRelease( bsdPathAsCFString ); | |
943 | } | |
944 | IOObjectRelease( nextMedia ); | |
945 | } | |
3b46e624 | 946 | |
19cb3738 FB |
947 | return kernResult; |
948 | } | |
949 | ||
950 | #endif | |
951 | ||
952 | static int hdev_open(BlockDriverState *bs, const char *filename, int flags) | |
953 | { | |
954 | BDRVRawState *s = bs->opaque; | |
3c529d93 | 955 | int fd, open_flags, ret; |
19cb3738 | 956 | |
a76bab49 AL |
957 | posix_aio_init(); |
958 | ||
19cb3738 FB |
959 | #ifdef CONFIG_COCOA |
960 | if (strstart(filename, "/dev/cdrom", NULL)) { | |
961 | kern_return_t kernResult; | |
962 | io_iterator_t mediaIterator; | |
963 | char bsdPath[ MAXPATHLEN ]; | |
964 | int fd; | |
5fafdf24 | 965 | |
19cb3738 FB |
966 | kernResult = FindEjectableCDMedia( &mediaIterator ); |
967 | kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) ); | |
3b46e624 | 968 | |
19cb3738 FB |
969 | if ( bsdPath[ 0 ] != '\0' ) { |
970 | strcat(bsdPath,"s0"); | |
971 | /* some CDs don't have a partition 0 */ | |
972 | fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE); | |
973 | if (fd < 0) { | |
974 | bsdPath[strlen(bsdPath)-1] = '1'; | |
975 | } else { | |
976 | close(fd); | |
977 | } | |
978 | filename = bsdPath; | |
979 | } | |
3b46e624 | 980 | |
19cb3738 FB |
981 | if ( mediaIterator ) |
982 | IOObjectRelease( mediaIterator ); | |
983 | } | |
984 | #endif | |
985 | open_flags = O_BINARY; | |
986 | if ((flags & BDRV_O_ACCESS) == O_RDWR) { | |
987 | open_flags |= O_RDWR; | |
988 | } else { | |
989 | open_flags |= O_RDONLY; | |
990 | bs->read_only = 1; | |
991 | } | |
9f7965c7 AL |
992 | /* Use O_DSYNC for write-through caching, no flags for write-back caching, |
993 | * and O_DIRECT for no caching. */ | |
994 | if ((flags & BDRV_O_NOCACHE)) | |
33f00271 | 995 | open_flags |= O_DIRECT; |
9f7965c7 AL |
996 | else if (!(flags & BDRV_O_CACHE_WB)) |
997 | open_flags |= O_DSYNC; | |
19cb3738 FB |
998 | |
999 | s->type = FTYPE_FILE; | |
1000 | #if defined(__linux__) | |
1001 | if (strstart(filename, "/dev/cd", NULL)) { | |
1002 | /* open will not fail even if no CD is inserted */ | |
1003 | open_flags |= O_NONBLOCK; | |
1004 | s->type = FTYPE_CD; | |
1005 | } else if (strstart(filename, "/dev/fd", NULL)) { | |
1006 | s->type = FTYPE_FD; | |
6dd2db52 | 1007 | s->fd_open_flags = open_flags; |
19cb3738 FB |
1008 | /* open will not fail even if no floppy is inserted */ |
1009 | open_flags |= O_NONBLOCK; | |
8185d2c9 | 1010 | #ifdef CONFIG_AIO |
985a03b0 TS |
1011 | } else if (strstart(filename, "/dev/sg", NULL)) { |
1012 | bs->sg = 1; | |
8185d2c9 | 1013 | #endif |
19cb3738 FB |
1014 | } |
1015 | #endif | |
9f23011a BS |
1016 | #if defined(__FreeBSD__) |
1017 | if (strstart(filename, "/dev/cd", NULL) || | |
1018 | strstart(filename, "/dev/acd", NULL)) { | |
1019 | s->type = FTYPE_CD; | |
1020 | s->cd_open_flags = open_flags; | |
1021 | } | |
1022 | #endif | |
1023 | s->fd = -1; | |
19cb3738 FB |
1024 | fd = open(filename, open_flags, 0644); |
1025 | if (fd < 0) { | |
1026 | ret = -errno; | |
1027 | if (ret == -EROFS) | |
1028 | ret = -EACCES; | |
1029 | return ret; | |
1030 | } | |
1031 | s->fd = fd; | |
9f23011a BS |
1032 | #if defined(__FreeBSD__) |
1033 | /* make sure the door isnt locked at this time */ | |
1034 | if (s->type == FTYPE_CD) | |
1035 | ioctl (s->fd, CDIOCALLOW); | |
1036 | #endif | |
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; | |
1064 | #ifdef DEBUG_FLOPPY | |
1065 | printf("Floppy closed\n"); | |
1066 | #endif | |
1067 | } | |
1068 | if (s->fd < 0) { | |
5fafdf24 | 1069 | if (s->fd_got_error && |
19cb3738 FB |
1070 | (qemu_get_clock(rt_clock) - s->fd_error_time) < FD_OPEN_TIMEOUT) { |
1071 | #ifdef DEBUG_FLOPPY | |
1072 | printf("No floppy (open delayed)\n"); | |
1073 | #endif | |
1074 | return -EIO; | |
1075 | } | |
6dd2db52 | 1076 | s->fd = open(bs->filename, s->fd_open_flags); |
19cb3738 FB |
1077 | if (s->fd < 0) { |
1078 | s->fd_error_time = qemu_get_clock(rt_clock); | |
1079 | s->fd_got_error = 1; | |
1080 | if (last_media_present) | |
1081 | s->fd_media_changed = 1; | |
1082 | #ifdef DEBUG_FLOPPY | |
1083 | printf("No floppy\n"); | |
1084 | #endif | |
1085 | return -EIO; | |
1086 | } | |
1087 | #ifdef DEBUG_FLOPPY | |
1088 | printf("Floppy opened\n"); | |
1089 | #endif | |
1090 | } | |
1091 | if (!last_media_present) | |
1092 | s->fd_media_changed = 1; | |
1093 | s->fd_open_time = qemu_get_clock(rt_clock); | |
1094 | s->fd_got_error = 0; | |
1095 | return 0; | |
1096 | } | |
19cb3738 FB |
1097 | |
1098 | static int raw_is_inserted(BlockDriverState *bs) | |
1099 | { | |
1100 | BDRVRawState *s = bs->opaque; | |
1101 | int ret; | |
1102 | ||
1103 | switch(s->type) { | |
1104 | case FTYPE_CD: | |
1105 | ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT); | |
1106 | if (ret == CDS_DISC_OK) | |
1107 | return 1; | |
1108 | else | |
1109 | return 0; | |
1110 | break; | |
1111 | case FTYPE_FD: | |
1112 | ret = fd_open(bs); | |
1113 | return (ret >= 0); | |
1114 | default: | |
1115 | return 1; | |
1116 | } | |
1117 | } | |
1118 | ||
1119 | /* currently only used by fdc.c, but a CD version would be good too */ | |
1120 | static int raw_media_changed(BlockDriverState *bs) | |
1121 | { | |
1122 | BDRVRawState *s = bs->opaque; | |
1123 | ||
1124 | switch(s->type) { | |
1125 | case FTYPE_FD: | |
1126 | { | |
1127 | int ret; | |
1128 | /* XXX: we do not have a true media changed indication. It | |
1129 | does not work if the floppy is changed without trying | |
1130 | to read it */ | |
1131 | fd_open(bs); | |
1132 | ret = s->fd_media_changed; | |
1133 | s->fd_media_changed = 0; | |
1134 | #ifdef DEBUG_FLOPPY | |
1135 | printf("Floppy changed=%d\n", ret); | |
1136 | #endif | |
1137 | return ret; | |
1138 | } | |
1139 | default: | |
1140 | return -ENOTSUP; | |
1141 | } | |
1142 | } | |
1143 | ||
1144 | static int raw_eject(BlockDriverState *bs, int eject_flag) | |
1145 | { | |
1146 | BDRVRawState *s = bs->opaque; | |
1147 | ||
1148 | switch(s->type) { | |
1149 | case FTYPE_CD: | |
1150 | if (eject_flag) { | |
1151 | if (ioctl (s->fd, CDROMEJECT, NULL) < 0) | |
1152 | perror("CDROMEJECT"); | |
1153 | } else { | |
1154 | if (ioctl (s->fd, CDROMCLOSETRAY, NULL) < 0) | |
1155 | perror("CDROMEJECT"); | |
1156 | } | |
1157 | break; | |
1158 | case FTYPE_FD: | |
1159 | { | |
1160 | int fd; | |
1161 | if (s->fd >= 0) { | |
1162 | close(s->fd); | |
1163 | s->fd = -1; | |
1164 | } | |
6dd2db52 | 1165 | fd = open(bs->filename, s->fd_open_flags | O_NONBLOCK); |
19cb3738 FB |
1166 | if (fd >= 0) { |
1167 | if (ioctl(fd, FDEJECT, 0) < 0) | |
1168 | perror("FDEJECT"); | |
1169 | close(fd); | |
1170 | } | |
1171 | } | |
1172 | break; | |
1173 | default: | |
1174 | return -ENOTSUP; | |
1175 | } | |
1176 | return 0; | |
1177 | } | |
1178 | ||
1179 | static int raw_set_locked(BlockDriverState *bs, int locked) | |
1180 | { | |
1181 | BDRVRawState *s = bs->opaque; | |
1182 | ||
1183 | switch(s->type) { | |
1184 | case FTYPE_CD: | |
1185 | if (ioctl (s->fd, CDROM_LOCKDOOR, locked) < 0) { | |
1186 | /* Note: an error can happen if the distribution automatically | |
1187 | mounts the CD-ROM */ | |
1188 | // perror("CDROM_LOCKDOOR"); | |
1189 | } | |
1190 | break; | |
1191 | default: | |
1192 | return -ENOTSUP; | |
1193 | } | |
1194 | return 0; | |
1195 | } | |
1196 | ||
985a03b0 TS |
1197 | static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) |
1198 | { | |
1199 | BDRVRawState *s = bs->opaque; | |
1200 | ||
1201 | return ioctl(s->fd, req, buf); | |
1202 | } | |
221f715d | 1203 | |
8185d2c9 | 1204 | #ifdef CONFIG_AIO |
221f715d AL |
1205 | static BlockDriverAIOCB *raw_aio_ioctl(BlockDriverState *bs, |
1206 | unsigned long int req, void *buf, | |
1207 | BlockDriverCompletionFunc *cb, void *opaque) | |
1208 | { | |
f141eafe | 1209 | BDRVRawState *s = bs->opaque; |
221f715d AL |
1210 | RawAIOCB *acb; |
1211 | ||
f141eafe AL |
1212 | if (fd_open(bs) < 0) |
1213 | return NULL; | |
1214 | ||
c16b5a2c | 1215 | acb = qemu_aio_get(&raw_aio_pool, bs, cb, opaque); |
221f715d AL |
1216 | if (!acb) |
1217 | return NULL; | |
f141eafe AL |
1218 | acb->aiocb.aio_fildes = s->fd; |
1219 | acb->aiocb.ev_signo = SIGUSR2; | |
1220 | acb->aiocb.aio_offset = 0; | |
1221 | acb->aiocb.aio_flags = 0; | |
1222 | ||
1223 | acb->next = posix_aio_state->first_aio; | |
1224 | posix_aio_state->first_aio = acb; | |
221f715d | 1225 | |
f141eafe | 1226 | acb->aiocb.aio_ioctl_buf = buf; |
221f715d AL |
1227 | acb->aiocb.aio_ioctl_cmd = req; |
1228 | if (qemu_paio_ioctl(&acb->aiocb) < 0) { | |
1229 | raw_aio_remove(acb); | |
1230 | return NULL; | |
1231 | } | |
1232 | ||
1233 | return &acb->common; | |
1234 | } | |
8185d2c9 | 1235 | #endif |
221f715d | 1236 | |
9f23011a BS |
1237 | #elif defined(__FreeBSD__) |
1238 | ||
1239 | static int fd_open(BlockDriverState *bs) | |
1240 | { | |
1241 | BDRVRawState *s = bs->opaque; | |
1242 | ||
1243 | /* this is just to ensure s->fd is sane (its called by io ops) */ | |
1244 | if (s->fd >= 0) | |
1245 | return 0; | |
1246 | return -EIO; | |
1247 | } | |
1248 | ||
1249 | static int cd_open(BlockDriverState *bs) | |
1250 | { | |
1251 | #if defined(__FreeBSD__) | |
1252 | BDRVRawState *s = bs->opaque; | |
1253 | int fd; | |
1254 | ||
1255 | switch(s->type) { | |
1256 | case FTYPE_CD: | |
1257 | /* XXX force reread of possibly changed/newly loaded disc, | |
1258 | * FreeBSD seems to not notice sometimes... */ | |
1259 | if (s->fd >= 0) | |
1260 | close (s->fd); | |
1261 | fd = open(bs->filename, s->cd_open_flags, 0644); | |
1262 | if (fd < 0) { | |
1263 | s->fd = -1; | |
1264 | return -EIO; | |
1265 | } | |
1266 | s->fd = fd; | |
1267 | /* make sure the door isnt locked at this time */ | |
1268 | ioctl (s->fd, CDIOCALLOW); | |
1269 | } | |
1270 | #endif | |
1271 | return 0; | |
1272 | } | |
1273 | ||
1274 | static int raw_is_inserted(BlockDriverState *bs) | |
1275 | { | |
1276 | BDRVRawState *s = bs->opaque; | |
1277 | ||
1278 | switch(s->type) { | |
1279 | case FTYPE_CD: | |
1280 | return (raw_getlength(bs) > 0); | |
1281 | case FTYPE_FD: | |
1282 | /* XXX handle this */ | |
1283 | /* FALLTHRU */ | |
1284 | default: | |
1285 | return 1; | |
1286 | } | |
1287 | } | |
1288 | ||
1289 | static int raw_media_changed(BlockDriverState *bs) | |
1290 | { | |
1291 | return -ENOTSUP; | |
1292 | } | |
1293 | ||
1294 | static int raw_eject(BlockDriverState *bs, int eject_flag) | |
1295 | { | |
1296 | BDRVRawState *s = bs->opaque; | |
1297 | ||
1298 | switch(s->type) { | |
1299 | case FTYPE_CD: | |
1300 | if (s->fd < 0) | |
1301 | return -ENOTSUP; | |
1302 | (void) ioctl (s->fd, CDIOCALLOW); | |
1303 | if (eject_flag) { | |
1304 | if (ioctl (s->fd, CDIOCEJECT) < 0) | |
1305 | perror("CDIOCEJECT"); | |
1306 | } else { | |
1307 | if (ioctl (s->fd, CDIOCCLOSE) < 0) | |
1308 | perror("CDIOCCLOSE"); | |
1309 | } | |
1310 | if (cd_open(bs) < 0) | |
1311 | return -ENOTSUP; | |
1312 | break; | |
1313 | case FTYPE_FD: | |
1314 | /* XXX handle this */ | |
1315 | /* FALLTHRU */ | |
1316 | default: | |
1317 | return -ENOTSUP; | |
1318 | } | |
1319 | return 0; | |
1320 | } | |
1321 | ||
1322 | static int raw_set_locked(BlockDriverState *bs, int locked) | |
1323 | { | |
1324 | BDRVRawState *s = bs->opaque; | |
1325 | ||
1326 | switch(s->type) { | |
1327 | case FTYPE_CD: | |
1328 | if (s->fd < 0) | |
1329 | return -ENOTSUP; | |
1330 | if (ioctl (s->fd, (locked ? CDIOCPREVENT : CDIOCALLOW)) < 0) { | |
1331 | /* Note: an error can happen if the distribution automatically | |
1332 | mounts the CD-ROM */ | |
1333 | // perror("CDROM_LOCKDOOR"); | |
1334 | } | |
1335 | break; | |
1336 | default: | |
1337 | return -ENOTSUP; | |
1338 | } | |
1339 | return 0; | |
1340 | } | |
1341 | ||
1342 | static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) | |
1343 | { | |
1344 | return -ENOTSUP; | |
1345 | } | |
1346 | #else /* !linux && !FreeBSD */ | |
19cb3738 | 1347 | |
08af02e2 AL |
1348 | static int fd_open(BlockDriverState *bs) |
1349 | { | |
1350 | return 0; | |
1351 | } | |
1352 | ||
19cb3738 FB |
1353 | static int raw_is_inserted(BlockDriverState *bs) |
1354 | { | |
1355 | return 1; | |
1356 | } | |
1357 | ||
1358 | static int raw_media_changed(BlockDriverState *bs) | |
1359 | { | |
1360 | return -ENOTSUP; | |
1361 | } | |
1362 | ||
1363 | static int raw_eject(BlockDriverState *bs, int eject_flag) | |
1364 | { | |
1365 | return -ENOTSUP; | |
1366 | } | |
1367 | ||
1368 | static int raw_set_locked(BlockDriverState *bs, int locked) | |
1369 | { | |
1370 | return -ENOTSUP; | |
1371 | } | |
1372 | ||
985a03b0 TS |
1373 | static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) |
1374 | { | |
1375 | return -ENOTSUP; | |
1376 | } | |
04eeb8b6 | 1377 | |
221f715d AL |
1378 | static BlockDriverAIOCB *raw_aio_ioctl(BlockDriverState *bs, |
1379 | unsigned long int req, void *buf, | |
1380 | BlockDriverCompletionFunc *cb, void *opaque) | |
04eeb8b6 | 1381 | { |
bbeea539 | 1382 | return NULL; |
04eeb8b6 | 1383 | } |
221f715d | 1384 | #endif /* !linux && !FreeBSD */ |
04eeb8b6 | 1385 | |
0e7e1989 | 1386 | static int hdev_create(const char *filename, QEMUOptionParameter *options) |
93c65b47 AL |
1387 | { |
1388 | int fd; | |
1389 | int ret = 0; | |
1390 | struct stat stat_buf; | |
0e7e1989 | 1391 | int64_t total_size = 0; |
93c65b47 | 1392 | |
0e7e1989 KW |
1393 | /* Read out options */ |
1394 | while (options && options->name) { | |
1395 | if (!strcmp(options->name, "size")) { | |
1396 | total_size = options->value.n / 512; | |
1397 | } | |
1398 | options++; | |
1399 | } | |
93c65b47 AL |
1400 | |
1401 | fd = open(filename, O_WRONLY | O_BINARY); | |
1402 | if (fd < 0) | |
1403 | return -EIO; | |
1404 | ||
1405 | if (fstat(fd, &stat_buf) < 0) | |
1406 | ret = -EIO; | |
4099df58 | 1407 | else if (!S_ISBLK(stat_buf.st_mode) && !S_ISCHR(stat_buf.st_mode)) |
93c65b47 AL |
1408 | ret = -EIO; |
1409 | else if (lseek(fd, 0, SEEK_END) < total_size * 512) | |
1410 | ret = -ENOSPC; | |
1411 | ||
1412 | close(fd); | |
1413 | return ret; | |
1414 | } | |
1415 | ||
5efa9d5a | 1416 | static BlockDriver bdrv_host_device = { |
e60f469c AJ |
1417 | .format_name = "host_device", |
1418 | .instance_size = sizeof(BDRVRawState), | |
1419 | .bdrv_open = hdev_open, | |
1420 | .bdrv_close = raw_close, | |
93c65b47 | 1421 | .bdrv_create = hdev_create, |
e60f469c | 1422 | .bdrv_flush = raw_flush, |
3b46e624 | 1423 | |
414f0dab | 1424 | #ifdef CONFIG_AIO |
f141eafe AL |
1425 | .bdrv_aio_readv = raw_aio_readv, |
1426 | .bdrv_aio_writev = raw_aio_writev, | |
414f0dab | 1427 | #endif |
3c529d93 | 1428 | |
eda578e5 AL |
1429 | .bdrv_read = raw_read, |
1430 | .bdrv_write = raw_write, | |
e60f469c | 1431 | .bdrv_getlength = raw_getlength, |
19cb3738 FB |
1432 | |
1433 | /* removable device support */ | |
e60f469c AJ |
1434 | .bdrv_is_inserted = raw_is_inserted, |
1435 | .bdrv_media_changed = raw_media_changed, | |
1436 | .bdrv_eject = raw_eject, | |
1437 | .bdrv_set_locked = raw_set_locked, | |
985a03b0 | 1438 | /* generic scsi device */ |
e60f469c | 1439 | .bdrv_ioctl = raw_ioctl, |
8185d2c9 | 1440 | #ifdef CONFIG_AIO |
221f715d | 1441 | .bdrv_aio_ioctl = raw_aio_ioctl, |
8185d2c9 | 1442 | #endif |
19cb3738 | 1443 | }; |
5efa9d5a AL |
1444 | |
1445 | static void bdrv_raw_init(void) | |
1446 | { | |
1447 | bdrv_register(&bdrv_raw); | |
1448 | bdrv_register(&bdrv_host_device); | |
1449 | } | |
1450 | ||
1451 | block_init(bdrv_raw_init); |