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