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