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