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