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