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