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