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