]> git.proxmox.com Git - qemu.git/blame - block/raw-posix.c
block: introduce BlockDriver.bdrv_needs_filename to enable some drivers.
[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"
1de7afc9
PB
25#include "qemu/timer.h"
26#include "qemu/log.h"
737e150e 27#include "block/block_int.h"
1de7afc9 28#include "qemu/module.h"
de81a169 29#include "trace.h"
737e150e 30#include "block/thread-pool.h"
1de7afc9 31#include "qemu/iov.h"
9f8540ec 32#include "raw-aio.h"
83f64091 33
83affaa6 34#if defined(__APPLE__) && (__MACH__)
83f64091
FB
35#include <paths.h>
36#include <sys/param.h>
37#include <IOKit/IOKitLib.h>
38#include <IOKit/IOBSD.h>
39#include <IOKit/storage/IOMediaBSDClient.h>
40#include <IOKit/storage/IOMedia.h>
41#include <IOKit/storage/IOCDMedia.h>
42//#include <IOKit/storage/IOCDTypes.h>
43#include <CoreFoundation/CoreFoundation.h>
44#endif
45
46#ifdef __sun__
2e9671da 47#define _POSIX_PTHREAD_SEMANTICS 1
83f64091
FB
48#include <sys/dkio.h>
49#endif
19cb3738 50#ifdef __linux__
343f8568
JS
51#include <sys/types.h>
52#include <sys/stat.h>
19cb3738 53#include <sys/ioctl.h>
05acda4d 54#include <sys/param.h>
19cb3738
FB
55#include <linux/cdrom.h>
56#include <linux/fd.h>
5500316d
PB
57#include <linux/fs.h>
58#endif
59#ifdef CONFIG_FIEMAP
60#include <linux/fiemap.h>
19cb3738 61#endif
3d4fa43e
KK
62#ifdef CONFIG_FALLOCATE_PUNCH_HOLE
63#include <linux/falloc.h>
64#endif
a167ba50 65#if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
1cb6c3fd 66#include <sys/disk.h>
9f23011a 67#include <sys/cdio.h>
1cb6c3fd 68#endif
83f64091 69
128ab2ff
BS
70#ifdef __OpenBSD__
71#include <sys/ioctl.h>
72#include <sys/disklabel.h>
73#include <sys/dkio.h>
74#endif
75
d1f6fd8d
CE
76#ifdef __NetBSD__
77#include <sys/ioctl.h>
78#include <sys/disklabel.h>
79#include <sys/dkio.h>
80#include <sys/disk.h>
81#endif
82
c5e97233
BS
83#ifdef __DragonFly__
84#include <sys/ioctl.h>
85#include <sys/diskslice.h>
86#endif
87
dce512de
CH
88#ifdef CONFIG_XFS
89#include <xfs/xfs.h>
90#endif
91
19cb3738 92//#define DEBUG_FLOPPY
83f64091 93
faf07963 94//#define DEBUG_BLOCK
03ff3ca3 95#if defined(DEBUG_BLOCK)
001faf32
BS
96#define DEBUG_BLOCK_PRINT(formatCstr, ...) do { if (qemu_log_enabled()) \
97 { qemu_log(formatCstr, ## __VA_ARGS__); qemu_log_flush(); } } while (0)
8c05dbf9 98#else
001faf32 99#define DEBUG_BLOCK_PRINT(formatCstr, ...)
8c05dbf9
TS
100#endif
101
f6465578
AL
102/* OS X does not have O_DSYNC */
103#ifndef O_DSYNC
1c27a8b3 104#ifdef O_SYNC
7ab064d2 105#define O_DSYNC O_SYNC
1c27a8b3 106#elif defined(O_FSYNC)
107#define O_DSYNC O_FSYNC
108#endif
f6465578
AL
109#endif
110
9f7965c7
AL
111/* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */
112#ifndef O_DIRECT
113#define O_DIRECT O_DSYNC
114#endif
115
19cb3738
FB
116#define FTYPE_FILE 0
117#define FTYPE_CD 1
118#define FTYPE_FD 2
83f64091 119
c57c846a 120/* if the FD is not accessed during that time (in ns), we try to
19cb3738 121 reopen it to see if the disk has been changed */
c57c846a 122#define FD_OPEN_TIMEOUT (1000000000)
83f64091 123
581b9e29
CH
124#define MAX_BLOCKSIZE 4096
125
19cb3738
FB
126typedef struct BDRVRawState {
127 int fd;
128 int type;
0e1d8f4c 129 int open_flags;
19cb3738
FB
130#if defined(__linux__)
131 /* linux floppy specific */
19cb3738
FB
132 int64_t fd_open_time;
133 int64_t fd_error_time;
134 int fd_got_error;
135 int fd_media_changed;
83f64091 136#endif
e44bd6fc 137#ifdef CONFIG_LINUX_AIO
5c6c3a6c 138 int use_aio;
1e5b9d2f 139 void *aio_ctx;
e44bd6fc 140#endif
dce512de
CH
141#ifdef CONFIG_XFS
142 bool is_xfs : 1;
143#endif
c85191e5 144 bool has_discard : 1;
19cb3738
FB
145} BDRVRawState;
146
eeb6b45d
JC
147typedef struct BDRVRawReopenState {
148 int fd;
149 int open_flags;
150#ifdef CONFIG_LINUX_AIO
151 int use_aio;
152#endif
153} BDRVRawReopenState;
154
19cb3738 155static int fd_open(BlockDriverState *bs);
22afa7b5 156static int64_t raw_getlength(BlockDriverState *bs);
83f64091 157
de81a169
PB
158typedef struct RawPosixAIOData {
159 BlockDriverState *bs;
160 int aio_fildes;
161 union {
162 struct iovec *aio_iov;
163 void *aio_ioctl_buf;
164 };
165 int aio_niov;
8238010b 166 uint64_t aio_nbytes;
de81a169
PB
167#define aio_ioctl_cmd aio_nbytes /* for QEMU_AIO_IOCTL */
168 off_t aio_offset;
169 int aio_type;
170} RawPosixAIOData;
171
a167ba50 172#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
f3a5d3f8 173static int cdrom_reopen(BlockDriverState *bs);
9f23011a
BS
174#endif
175
1de1ae0a
CE
176#if defined(__NetBSD__)
177static int raw_normalize_devicepath(const char **filename)
178{
179 static char namebuf[PATH_MAX];
180 const char *dp, *fname;
181 struct stat sb;
182
183 fname = *filename;
184 dp = strrchr(fname, '/');
185 if (lstat(fname, &sb) < 0) {
186 fprintf(stderr, "%s: stat failed: %s\n",
187 fname, strerror(errno));
188 return -errno;
189 }
190
191 if (!S_ISBLK(sb.st_mode)) {
192 return 0;
193 }
194
195 if (dp == NULL) {
196 snprintf(namebuf, PATH_MAX, "r%s", fname);
197 } else {
198 snprintf(namebuf, PATH_MAX, "%.*s/r%s",
199 (int)(dp - fname), fname, dp + 1);
200 }
201 fprintf(stderr, "%s is a block device", fname);
202 *filename = namebuf;
203 fprintf(stderr, ", using %s\n", *filename);
204
205 return 0;
206}
207#else
208static int raw_normalize_devicepath(const char **filename)
209{
210 return 0;
211}
212#endif
213
6a8dc042
JC
214static void raw_parse_flags(int bdrv_flags, int *open_flags)
215{
216 assert(open_flags != NULL);
217
218 *open_flags |= O_BINARY;
219 *open_flags &= ~O_ACCMODE;
220 if (bdrv_flags & BDRV_O_RDWR) {
221 *open_flags |= O_RDWR;
222 } else {
223 *open_flags |= O_RDONLY;
224 }
225
226 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
227 * and O_DIRECT for no caching. */
228 if ((bdrv_flags & BDRV_O_NOCACHE)) {
229 *open_flags |= O_DIRECT;
230 }
6a8dc042
JC
231}
232
fc32a72d
JC
233#ifdef CONFIG_LINUX_AIO
234static int raw_set_aio(void **aio_ctx, int *use_aio, int bdrv_flags)
235{
236 int ret = -1;
237 assert(aio_ctx != NULL);
238 assert(use_aio != NULL);
239 /*
240 * Currently Linux do AIO only for files opened with O_DIRECT
241 * specified so check NOCACHE flag too
242 */
243 if ((bdrv_flags & (BDRV_O_NOCACHE|BDRV_O_NATIVE_AIO)) ==
244 (BDRV_O_NOCACHE|BDRV_O_NATIVE_AIO)) {
245
246 /* if non-NULL, laio_init() has already been run */
247 if (*aio_ctx == NULL) {
248 *aio_ctx = laio_init();
249 if (!*aio_ctx) {
250 goto error;
251 }
252 }
253 *use_aio = 1;
254 } else {
255 *use_aio = 0;
256 }
257
258 ret = 0;
259
260error:
261 return ret;
262}
263#endif
264
c66a6157
KW
265static QemuOptsList raw_runtime_opts = {
266 .name = "raw",
267 .head = QTAILQ_HEAD_INITIALIZER(raw_runtime_opts.head),
268 .desc = {
269 {
270 .name = "filename",
271 .type = QEMU_OPT_STRING,
272 .help = "File name of the image",
273 },
274 { /* end of list */ }
275 },
276};
277
278static int raw_open_common(BlockDriverState *bs, QDict *options,
19a3da7f 279 int bdrv_flags, int open_flags)
83f64091
FB
280{
281 BDRVRawState *s = bs->opaque;
c66a6157
KW
282 QemuOpts *opts;
283 Error *local_err = NULL;
284 const char *filename;
0e1d8f4c 285 int fd, ret;
83f64091 286
c66a6157
KW
287 opts = qemu_opts_create_nofail(&raw_runtime_opts);
288 qemu_opts_absorb_qdict(opts, options, &local_err);
289 if (error_is_set(&local_err)) {
290 qerror_report_err(local_err);
291 error_free(local_err);
292 ret = -EINVAL;
293 goto fail;
294 }
295
296 filename = qemu_opt_get(opts, "filename");
297
1de1ae0a
CE
298 ret = raw_normalize_devicepath(&filename);
299 if (ret != 0) {
c66a6157 300 goto fail;
1de1ae0a
CE
301 }
302
6a8dc042
JC
303 s->open_flags = open_flags;
304 raw_parse_flags(bdrv_flags, &s->open_flags);
83f64091 305
90babde0 306 s->fd = -1;
40ff6d7e 307 fd = qemu_open(filename, s->open_flags, 0644);
19cb3738
FB
308 if (fd < 0) {
309 ret = -errno;
c66a6157 310 if (ret == -EROFS) {
19cb3738 311 ret = -EACCES;
c66a6157
KW
312 }
313 goto fail;
19cb3738 314 }
83f64091 315 s->fd = fd;
9ef91a67 316
5c6c3a6c 317#ifdef CONFIG_LINUX_AIO
fc32a72d 318 if (raw_set_aio(&s->aio_ctx, &s->use_aio, bdrv_flags)) {
47e6b251 319 qemu_close(fd);
c66a6157
KW
320 ret = -errno;
321 goto fail;
9ef91a67 322 }
fc32a72d 323#endif
9ef91a67 324
c85191e5 325 s->has_discard = 1;
dce512de
CH
326#ifdef CONFIG_XFS
327 if (platform_test_xfs_fd(s->fd)) {
328 s->is_xfs = 1;
329 }
330#endif
331
c66a6157
KW
332 ret = 0;
333fail:
334 qemu_opts_del(opts);
335 return ret;
83f64091
FB
336}
337
015a1036
MR
338static int raw_open(BlockDriverState *bs, QDict *options, int flags,
339 Error **errp)
90babde0
CH
340{
341 BDRVRawState *s = bs->opaque;
342
343 s->type = FTYPE_FILE;
c66a6157 344 return raw_open_common(bs, options, flags, 0);
90babde0
CH
345}
346
eeb6b45d
JC
347static int raw_reopen_prepare(BDRVReopenState *state,
348 BlockReopenQueue *queue, Error **errp)
349{
350 BDRVRawState *s;
351 BDRVRawReopenState *raw_s;
352 int ret = 0;
353
354 assert(state != NULL);
355 assert(state->bs != NULL);
356
357 s = state->bs->opaque;
358
359 state->opaque = g_malloc0(sizeof(BDRVRawReopenState));
360 raw_s = state->opaque;
361
362#ifdef CONFIG_LINUX_AIO
363 raw_s->use_aio = s->use_aio;
364
365 /* we can use s->aio_ctx instead of a copy, because the use_aio flag is
366 * valid in the 'false' condition even if aio_ctx is set, and raw_set_aio()
367 * won't override aio_ctx if aio_ctx is non-NULL */
368 if (raw_set_aio(&s->aio_ctx, &raw_s->use_aio, state->flags)) {
369 return -1;
370 }
371#endif
372
1bc6b705
JC
373 if (s->type == FTYPE_FD || s->type == FTYPE_CD) {
374 raw_s->open_flags |= O_NONBLOCK;
375 }
376
eeb6b45d
JC
377 raw_parse_flags(state->flags, &raw_s->open_flags);
378
379 raw_s->fd = -1;
380
fdf263f6 381 int fcntl_flags = O_APPEND | O_NONBLOCK;
eeb6b45d
JC
382#ifdef O_NOATIME
383 fcntl_flags |= O_NOATIME;
384#endif
385
fdf263f6
AF
386#ifdef O_ASYNC
387 /* Not all operating systems have O_ASYNC, and those that don't
388 * will not let us track the state into raw_s->open_flags (typically
389 * you achieve the same effect with an ioctl, for example I_SETSIG
390 * on Solaris). But we do not use O_ASYNC, so that's fine.
391 */
392 assert((s->open_flags & O_ASYNC) == 0);
393#endif
394
eeb6b45d
JC
395 if ((raw_s->open_flags & ~fcntl_flags) == (s->open_flags & ~fcntl_flags)) {
396 /* dup the original fd */
397 /* TODO: use qemu fcntl wrapper */
398#ifdef F_DUPFD_CLOEXEC
399 raw_s->fd = fcntl(s->fd, F_DUPFD_CLOEXEC, 0);
400#else
401 raw_s->fd = dup(s->fd);
402 if (raw_s->fd != -1) {
403 qemu_set_cloexec(raw_s->fd);
404 }
405#endif
406 if (raw_s->fd >= 0) {
407 ret = fcntl_setfl(raw_s->fd, raw_s->open_flags);
408 if (ret) {
409 qemu_close(raw_s->fd);
410 raw_s->fd = -1;
411 }
412 }
413 }
414
415 /* If we cannot use fcntl, or fcntl failed, fall back to qemu_open() */
416 if (raw_s->fd == -1) {
417 assert(!(raw_s->open_flags & O_CREAT));
418 raw_s->fd = qemu_open(state->bs->filename, raw_s->open_flags);
419 if (raw_s->fd == -1) {
420 ret = -1;
421 }
422 }
423 return ret;
424}
425
426
427static void raw_reopen_commit(BDRVReopenState *state)
428{
429 BDRVRawReopenState *raw_s = state->opaque;
430 BDRVRawState *s = state->bs->opaque;
431
432 s->open_flags = raw_s->open_flags;
433
434 qemu_close(s->fd);
435 s->fd = raw_s->fd;
436#ifdef CONFIG_LINUX_AIO
437 s->use_aio = raw_s->use_aio;
438#endif
439
440 g_free(state->opaque);
441 state->opaque = NULL;
442}
443
444
445static void raw_reopen_abort(BDRVReopenState *state)
446{
447 BDRVRawReopenState *raw_s = state->opaque;
448
449 /* nothing to do if NULL, we didn't get far enough */
450 if (raw_s == NULL) {
451 return;
452 }
453
454 if (raw_s->fd >= 0) {
455 qemu_close(raw_s->fd);
456 raw_s->fd = -1;
457 }
458 g_free(state->opaque);
459 state->opaque = NULL;
460}
461
462
83f64091
FB
463/* XXX: use host sector size if necessary with:
464#ifdef DIOCGSECTORSIZE
465 {
466 unsigned int sectorsize = 512;
467 if (!ioctl(fd, DIOCGSECTORSIZE, &sectorsize) &&
468 sectorsize > bufsize)
469 bufsize = sectorsize;
470 }
471#endif
472#ifdef CONFIG_COCOA
2ee9fb48 473 uint32_t blockSize = 512;
83f64091
FB
474 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
475 bufsize = blockSize;
476 }
477#endif
478*/
479
de81a169
PB
480static ssize_t handle_aiocb_ioctl(RawPosixAIOData *aiocb)
481{
482 int ret;
483
484 ret = ioctl(aiocb->aio_fildes, aiocb->aio_ioctl_cmd, aiocb->aio_ioctl_buf);
485 if (ret == -1) {
486 return -errno;
487 }
488
b608c8dc 489 return 0;
de81a169
PB
490}
491
492static ssize_t handle_aiocb_flush(RawPosixAIOData *aiocb)
493{
494 int ret;
495
496 ret = qemu_fdatasync(aiocb->aio_fildes);
497 if (ret == -1) {
498 return -errno;
499 }
500 return 0;
501}
502
503#ifdef CONFIG_PREADV
504
505static bool preadv_present = true;
506
507static ssize_t
508qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset)
509{
510 return preadv(fd, iov, nr_iov, offset);
511}
512
513static ssize_t
514qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset)
515{
516 return pwritev(fd, iov, nr_iov, offset);
517}
518
519#else
520
521static bool preadv_present = false;
522
523static ssize_t
524qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset)
525{
526 return -ENOSYS;
527}
528
529static ssize_t
530qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset)
531{
532 return -ENOSYS;
533}
534
535#endif
536
537static ssize_t handle_aiocb_rw_vector(RawPosixAIOData *aiocb)
538{
539 ssize_t len;
540
541 do {
542 if (aiocb->aio_type & QEMU_AIO_WRITE)
543 len = qemu_pwritev(aiocb->aio_fildes,
544 aiocb->aio_iov,
545 aiocb->aio_niov,
546 aiocb->aio_offset);
547 else
548 len = qemu_preadv(aiocb->aio_fildes,
549 aiocb->aio_iov,
550 aiocb->aio_niov,
551 aiocb->aio_offset);
552 } while (len == -1 && errno == EINTR);
553
554 if (len == -1) {
555 return -errno;
556 }
557 return len;
558}
559
560/*
561 * Read/writes the data to/from a given linear buffer.
562 *
563 * Returns the number of bytes handles or -errno in case of an error. Short
564 * reads are only returned if the end of the file is reached.
565 */
566static ssize_t handle_aiocb_rw_linear(RawPosixAIOData *aiocb, char *buf)
567{
568 ssize_t offset = 0;
569 ssize_t len;
570
571 while (offset < aiocb->aio_nbytes) {
572 if (aiocb->aio_type & QEMU_AIO_WRITE) {
573 len = pwrite(aiocb->aio_fildes,
574 (const char *)buf + offset,
575 aiocb->aio_nbytes - offset,
576 aiocb->aio_offset + offset);
577 } else {
578 len = pread(aiocb->aio_fildes,
579 buf + offset,
580 aiocb->aio_nbytes - offset,
581 aiocb->aio_offset + offset);
582 }
583 if (len == -1 && errno == EINTR) {
584 continue;
585 } else if (len == -1) {
586 offset = -errno;
587 break;
588 } else if (len == 0) {
589 break;
590 }
591 offset += len;
592 }
593
594 return offset;
595}
596
597static ssize_t handle_aiocb_rw(RawPosixAIOData *aiocb)
598{
599 ssize_t nbytes;
600 char *buf;
601
602 if (!(aiocb->aio_type & QEMU_AIO_MISALIGNED)) {
603 /*
604 * If there is just a single buffer, and it is properly aligned
605 * we can just use plain pread/pwrite without any problems.
606 */
607 if (aiocb->aio_niov == 1) {
608 return handle_aiocb_rw_linear(aiocb, aiocb->aio_iov->iov_base);
609 }
610 /*
611 * We have more than one iovec, and all are properly aligned.
612 *
613 * Try preadv/pwritev first and fall back to linearizing the
614 * buffer if it's not supported.
615 */
616 if (preadv_present) {
617 nbytes = handle_aiocb_rw_vector(aiocb);
618 if (nbytes == aiocb->aio_nbytes ||
619 (nbytes < 0 && nbytes != -ENOSYS)) {
620 return nbytes;
621 }
622 preadv_present = false;
623 }
624
625 /*
626 * XXX(hch): short read/write. no easy way to handle the reminder
627 * using these interfaces. For now retry using plain
628 * pread/pwrite?
629 */
630 }
631
632 /*
633 * Ok, we have to do it the hard way, copy all segments into
634 * a single aligned buffer.
635 */
636 buf = qemu_blockalign(aiocb->bs, aiocb->aio_nbytes);
637 if (aiocb->aio_type & QEMU_AIO_WRITE) {
638 char *p = buf;
639 int i;
640
641 for (i = 0; i < aiocb->aio_niov; ++i) {
642 memcpy(p, aiocb->aio_iov[i].iov_base, aiocb->aio_iov[i].iov_len);
643 p += aiocb->aio_iov[i].iov_len;
644 }
645 }
646
647 nbytes = handle_aiocb_rw_linear(aiocb, buf);
648 if (!(aiocb->aio_type & QEMU_AIO_WRITE)) {
649 char *p = buf;
650 size_t count = aiocb->aio_nbytes, copy;
651 int i;
652
653 for (i = 0; i < aiocb->aio_niov && count; ++i) {
654 copy = count;
655 if (copy > aiocb->aio_iov[i].iov_len) {
656 copy = aiocb->aio_iov[i].iov_len;
657 }
658 memcpy(aiocb->aio_iov[i].iov_base, p, copy);
659 p += copy;
660 count -= copy;
661 }
662 }
663 qemu_vfree(buf);
664
665 return nbytes;
666}
667
8238010b
PB
668#ifdef CONFIG_XFS
669static int xfs_discard(BDRVRawState *s, int64_t offset, uint64_t bytes)
670{
671 struct xfs_flock64 fl;
672
673 memset(&fl, 0, sizeof(fl));
674 fl.l_whence = SEEK_SET;
675 fl.l_start = offset;
676 fl.l_len = bytes;
677
678 if (xfsctl(NULL, s->fd, XFS_IOC_UNRESVSP64, &fl) < 0) {
679 DEBUG_BLOCK_PRINT("cannot punch hole (%s)\n", strerror(errno));
680 return -errno;
681 }
682
683 return 0;
684}
685#endif
686
687static ssize_t handle_aiocb_discard(RawPosixAIOData *aiocb)
688{
689 int ret = -EOPNOTSUPP;
690 BDRVRawState *s = aiocb->bs->opaque;
691
692 if (s->has_discard == 0) {
693 return 0;
694 }
695
696 if (aiocb->aio_type & QEMU_AIO_BLKDEV) {
697#ifdef BLKDISCARD
698 do {
699 uint64_t range[2] = { aiocb->aio_offset, aiocb->aio_nbytes };
700 if (ioctl(aiocb->aio_fildes, BLKDISCARD, range) == 0) {
701 return 0;
702 }
703 } while (errno == EINTR);
704
705 ret = -errno;
706#endif
707 } else {
708#ifdef CONFIG_XFS
709 if (s->is_xfs) {
710 return xfs_discard(s, aiocb->aio_offset, aiocb->aio_nbytes);
711 }
712#endif
713
714#ifdef CONFIG_FALLOCATE_PUNCH_HOLE
715 do {
716 if (fallocate(s->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
717 aiocb->aio_offset, aiocb->aio_nbytes) == 0) {
718 return 0;
719 }
720 } while (errno == EINTR);
721
722 ret = -errno;
723#endif
724 }
725
726 if (ret == -ENODEV || ret == -ENOSYS || ret == -EOPNOTSUPP ||
727 ret == -ENOTTY) {
728 s->has_discard = 0;
729 ret = 0;
730 }
731 return ret;
732}
733
de81a169
PB
734static int aio_worker(void *arg)
735{
736 RawPosixAIOData *aiocb = arg;
737 ssize_t ret = 0;
738
739 switch (aiocb->aio_type & QEMU_AIO_TYPE_MASK) {
740 case QEMU_AIO_READ:
741 ret = handle_aiocb_rw(aiocb);
742 if (ret >= 0 && ret < aiocb->aio_nbytes && aiocb->bs->growable) {
743 iov_memset(aiocb->aio_iov, aiocb->aio_niov, ret,
744 0, aiocb->aio_nbytes - ret);
745
746 ret = aiocb->aio_nbytes;
747 }
748 if (ret == aiocb->aio_nbytes) {
749 ret = 0;
750 } else if (ret >= 0 && ret < aiocb->aio_nbytes) {
751 ret = -EINVAL;
752 }
753 break;
754 case QEMU_AIO_WRITE:
755 ret = handle_aiocb_rw(aiocb);
756 if (ret == aiocb->aio_nbytes) {
757 ret = 0;
758 } else if (ret >= 0 && ret < aiocb->aio_nbytes) {
759 ret = -EINVAL;
760 }
761 break;
762 case QEMU_AIO_FLUSH:
763 ret = handle_aiocb_flush(aiocb);
764 break;
765 case QEMU_AIO_IOCTL:
766 ret = handle_aiocb_ioctl(aiocb);
767 break;
8238010b
PB
768 case QEMU_AIO_DISCARD:
769 ret = handle_aiocb_discard(aiocb);
770 break;
de81a169
PB
771 default:
772 fprintf(stderr, "invalid aio request (0x%x)\n", aiocb->aio_type);
773 ret = -EINVAL;
774 break;
775 }
776
777 g_slice_free(RawPosixAIOData, aiocb);
778 return ret;
779}
780
781static BlockDriverAIOCB *paio_submit(BlockDriverState *bs, int fd,
782 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
783 BlockDriverCompletionFunc *cb, void *opaque, int type)
784{
785 RawPosixAIOData *acb = g_slice_new(RawPosixAIOData);
c4d9d196 786 ThreadPool *pool;
de81a169
PB
787
788 acb->bs = bs;
789 acb->aio_type = type;
790 acb->aio_fildes = fd;
791
792 if (qiov) {
793 acb->aio_iov = qiov->iov;
794 acb->aio_niov = qiov->niov;
795 }
796 acb->aio_nbytes = nb_sectors * 512;
797 acb->aio_offset = sector_num * 512;
798
799 trace_paio_submit(acb, opaque, sector_num, nb_sectors, type);
c4d9d196
SH
800 pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
801 return thread_pool_submit_aio(pool, aio_worker, acb, cb, opaque);
de81a169
PB
802}
803
9ef91a67
CH
804static BlockDriverAIOCB *raw_aio_submit(BlockDriverState *bs,
805 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
806 BlockDriverCompletionFunc *cb, void *opaque, int type)
83f64091 807{
ce1a14dc 808 BDRVRawState *s = bs->opaque;
ce1a14dc 809
19cb3738
FB
810 if (fd_open(bs) < 0)
811 return NULL;
812
f141eafe
AL
813 /*
814 * If O_DIRECT is used the buffer needs to be aligned on a sector
c1ee7d56 815 * boundary. Check if this is the case or tell the low-level
9ef91a67 816 * driver that it needs to copy the buffer.
f141eafe 817 */
9acc5a06 818 if ((bs->open_flags & BDRV_O_NOCACHE)) {
c53b1c51 819 if (!bdrv_qiov_is_aligned(bs, qiov)) {
5c6c3a6c 820 type |= QEMU_AIO_MISALIGNED;
e44bd6fc 821#ifdef CONFIG_LINUX_AIO
5c6c3a6c
CH
822 } else if (s->use_aio) {
823 return laio_submit(bs, s->aio_ctx, s->fd, sector_num, qiov,
e44bd6fc
SW
824 nb_sectors, cb, opaque, type);
825#endif
5c6c3a6c 826 }
9ef91a67 827 }
f141eafe 828
1e5b9d2f 829 return paio_submit(bs, s->fd, sector_num, qiov, nb_sectors,
9ef91a67 830 cb, opaque, type);
83f64091
FB
831}
832
f141eafe
AL
833static BlockDriverAIOCB *raw_aio_readv(BlockDriverState *bs,
834 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
ce1a14dc 835 BlockDriverCompletionFunc *cb, void *opaque)
83f64091 836{
9ef91a67
CH
837 return raw_aio_submit(bs, sector_num, qiov, nb_sectors,
838 cb, opaque, QEMU_AIO_READ);
83f64091
FB
839}
840
f141eafe
AL
841static BlockDriverAIOCB *raw_aio_writev(BlockDriverState *bs,
842 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
ce1a14dc 843 BlockDriverCompletionFunc *cb, void *opaque)
83f64091 844{
9ef91a67
CH
845 return raw_aio_submit(bs, sector_num, qiov, nb_sectors,
846 cb, opaque, QEMU_AIO_WRITE);
83f64091 847}
53538725 848
b2e12bc6
CH
849static BlockDriverAIOCB *raw_aio_flush(BlockDriverState *bs,
850 BlockDriverCompletionFunc *cb, void *opaque)
851{
852 BDRVRawState *s = bs->opaque;
853
854 if (fd_open(bs) < 0)
855 return NULL;
856
1e5b9d2f 857 return paio_submit(bs, s->fd, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH);
b2e12bc6
CH
858}
859
83f64091
FB
860static void raw_close(BlockDriverState *bs)
861{
862 BDRVRawState *s = bs->opaque;
19cb3738 863 if (s->fd >= 0) {
2e1e79da 864 qemu_close(s->fd);
19cb3738
FB
865 s->fd = -1;
866 }
83f64091
FB
867}
868
869static int raw_truncate(BlockDriverState *bs, int64_t offset)
870{
871 BDRVRawState *s = bs->opaque;
55b949c8
CH
872 struct stat st;
873
874 if (fstat(s->fd, &st)) {
83f64091 875 return -errno;
55b949c8
CH
876 }
877
878 if (S_ISREG(st.st_mode)) {
879 if (ftruncate(s->fd, offset) < 0) {
880 return -errno;
881 }
882 } else if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
883 if (offset > raw_getlength(bs)) {
884 return -EINVAL;
885 }
886 } else {
887 return -ENOTSUP;
888 }
889
83f64091
FB
890 return 0;
891}
892
128ab2ff
BS
893#ifdef __OpenBSD__
894static int64_t raw_getlength(BlockDriverState *bs)
895{
896 BDRVRawState *s = bs->opaque;
897 int fd = s->fd;
898 struct stat st;
899
900 if (fstat(fd, &st))
901 return -1;
902 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
903 struct disklabel dl;
904
905 if (ioctl(fd, DIOCGDINFO, &dl))
906 return -1;
907 return (uint64_t)dl.d_secsize *
908 dl.d_partitions[DISKPART(st.st_rdev)].p_size;
909 } else
910 return st.st_size;
911}
d1f6fd8d
CE
912#elif defined(__NetBSD__)
913static int64_t raw_getlength(BlockDriverState *bs)
914{
915 BDRVRawState *s = bs->opaque;
916 int fd = s->fd;
917 struct stat st;
918
919 if (fstat(fd, &st))
920 return -1;
921 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
922 struct dkwedge_info dkw;
923
924 if (ioctl(fd, DIOCGWEDGEINFO, &dkw) != -1) {
925 return dkw.dkw_size * 512;
926 } else {
927 struct disklabel dl;
928
929 if (ioctl(fd, DIOCGDINFO, &dl))
930 return -1;
931 return (uint64_t)dl.d_secsize *
932 dl.d_partitions[DISKPART(st.st_rdev)].p_size;
933 }
934 } else
935 return st.st_size;
936}
50779cc2
CH
937#elif defined(__sun__)
938static int64_t raw_getlength(BlockDriverState *bs)
939{
940 BDRVRawState *s = bs->opaque;
941 struct dk_minfo minfo;
942 int ret;
943
944 ret = fd_open(bs);
945 if (ret < 0) {
946 return ret;
947 }
948
949 /*
950 * Use the DKIOCGMEDIAINFO ioctl to read the size.
951 */
952 ret = ioctl(s->fd, DKIOCGMEDIAINFO, &minfo);
953 if (ret != -1) {
954 return minfo.dki_lbsize * minfo.dki_capacity;
955 }
956
957 /*
958 * There are reports that lseek on some devices fails, but
959 * irc discussion said that contingency on contingency was overkill.
960 */
961 return lseek(s->fd, 0, SEEK_END);
962}
963#elif defined(CONFIG_BSD)
964static int64_t raw_getlength(BlockDriverState *bs)
83f64091
FB
965{
966 BDRVRawState *s = bs->opaque;
967 int fd = s->fd;
968 int64_t size;
83f64091 969 struct stat sb;
a167ba50 970#if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
9f23011a 971 int reopened = 0;
83f64091 972#endif
19cb3738
FB
973 int ret;
974
975 ret = fd_open(bs);
976 if (ret < 0)
977 return ret;
83f64091 978
a167ba50 979#if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
9f23011a
BS
980again:
981#endif
83f64091
FB
982 if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
983#ifdef DIOCGMEDIASIZE
984 if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
c5e97233
BS
985#elif defined(DIOCGPART)
986 {
987 struct partinfo pi;
988 if (ioctl(fd, DIOCGPART, &pi) == 0)
989 size = pi.media_size;
990 else
991 size = 0;
992 }
993 if (size == 0)
83f64091 994#endif
83affaa6 995#if defined(__APPLE__) && defined(__MACH__)
83f64091
FB
996 size = LONG_LONG_MAX;
997#else
998 size = lseek(fd, 0LL, SEEK_END);
9f23011a 999#endif
a167ba50 1000#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
9f23011a
BS
1001 switch(s->type) {
1002 case FTYPE_CD:
1003 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
1004 if (size == 2048LL * (unsigned)-1)
1005 size = 0;
1006 /* XXX no disc? maybe we need to reopen... */
f3a5d3f8 1007 if (size <= 0 && !reopened && cdrom_reopen(bs) >= 0) {
9f23011a
BS
1008 reopened = 1;
1009 goto again;
1010 }
1011 }
83f64091 1012#endif
50779cc2 1013 } else {
83f64091
FB
1014 size = lseek(fd, 0, SEEK_END);
1015 }
83f64091
FB
1016 return size;
1017}
50779cc2
CH
1018#else
1019static int64_t raw_getlength(BlockDriverState *bs)
1020{
1021 BDRVRawState *s = bs->opaque;
1022 int ret;
1023
1024 ret = fd_open(bs);
1025 if (ret < 0) {
1026 return ret;
1027 }
1028
1029 return lseek(s->fd, 0, SEEK_END);
1030}
128ab2ff 1031#endif
83f64091 1032
4a1d5e1f
FZ
1033static int64_t raw_get_allocated_file_size(BlockDriverState *bs)
1034{
1035 struct stat st;
1036 BDRVRawState *s = bs->opaque;
1037
1038 if (fstat(s->fd, &st) < 0) {
1039 return -errno;
1040 }
1041 return (int64_t)st.st_blocks * 512;
1042}
1043
d5124c00
MR
1044static int raw_create(const char *filename, QEMUOptionParameter *options,
1045 Error **errp)
83f64091
FB
1046{
1047 int fd;
1e37d059 1048 int result = 0;
0e7e1989 1049 int64_t total_size = 0;
83f64091 1050
0e7e1989
KW
1051 /* Read out options */
1052 while (options && options->name) {
1053 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
9040385d 1054 total_size = options->value.n / BDRV_SECTOR_SIZE;
0e7e1989
KW
1055 }
1056 options++;
1057 }
83f64091 1058
6165f4d8
CB
1059 fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
1060 0644);
1e37d059
SW
1061 if (fd < 0) {
1062 result = -errno;
1063 } else {
9040385d 1064 if (ftruncate(fd, total_size * BDRV_SECTOR_SIZE) != 0) {
1e37d059
SW
1065 result = -errno;
1066 }
2e1e79da 1067 if (qemu_close(fd) != 0) {
1e37d059
SW
1068 result = -errno;
1069 }
1070 }
1071 return result;
83f64091
FB
1072}
1073
5500316d
PB
1074/*
1075 * Returns true iff the specified sector is present in the disk image. Drivers
1076 * not implementing the functionality are assumed to not support backing files,
1077 * hence all their sectors are reported as allocated.
1078 *
1079 * If 'sector_num' is beyond the end of the disk image the return value is 0
1080 * and 'pnum' is set to 0.
1081 *
1082 * 'pnum' is set to the number of sectors (including and immediately following
1083 * the specified sector) that are known to be in the same
1084 * allocated/unallocated state.
1085 *
1086 * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes
1087 * beyond the end of the disk image it will be clamped.
1088 */
b6b8a333 1089static int64_t coroutine_fn raw_co_get_block_status(BlockDriverState *bs,
5500316d
PB
1090 int64_t sector_num,
1091 int nb_sectors, int *pnum)
1092{
5500316d 1093 off_t start, data, hole;
63390a8d 1094 int64_t ret;
5500316d
PB
1095
1096 ret = fd_open(bs);
1097 if (ret < 0) {
1098 return ret;
1099 }
1100
1101 start = sector_num * BDRV_SECTOR_SIZE;
63390a8d 1102 ret = BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | start;
94282e71 1103
5500316d 1104#ifdef CONFIG_FIEMAP
94282e71
KW
1105
1106 BDRVRawState *s = bs->opaque;
5500316d
PB
1107 struct {
1108 struct fiemap fm;
1109 struct fiemap_extent fe;
1110 } f;
94282e71 1111
5500316d
PB
1112 f.fm.fm_start = start;
1113 f.fm.fm_length = (int64_t)nb_sectors * BDRV_SECTOR_SIZE;
1114 f.fm.fm_flags = 0;
1115 f.fm.fm_extent_count = 1;
1116 f.fm.fm_reserved = 0;
1117 if (ioctl(s->fd, FS_IOC_FIEMAP, &f) == -1) {
1118 /* Assume everything is allocated. */
1119 *pnum = nb_sectors;
63390a8d 1120 return ret;
5500316d
PB
1121 }
1122
1123 if (f.fm.fm_mapped_extents == 0) {
1124 /* No extents found, data is beyond f.fm.fm_start + f.fm.fm_length.
1125 * f.fm.fm_start + f.fm.fm_length must be clamped to the file size!
1126 */
1127 off_t length = lseek(s->fd, 0, SEEK_END);
1128 hole = f.fm.fm_start;
1129 data = MIN(f.fm.fm_start + f.fm.fm_length, length);
1130 } else {
1131 data = f.fe.fe_logical;
1132 hole = f.fe.fe_logical + f.fe.fe_length;
f5f7abcf
PB
1133 if (f.fe.fe_flags & FIEMAP_EXTENT_UNWRITTEN) {
1134 ret |= BDRV_BLOCK_ZERO;
1135 }
5500316d 1136 }
94282e71 1137
5500316d 1138#elif defined SEEK_HOLE && defined SEEK_DATA
94282e71
KW
1139
1140 BDRVRawState *s = bs->opaque;
1141
5500316d
PB
1142 hole = lseek(s->fd, start, SEEK_HOLE);
1143 if (hole == -1) {
1144 /* -ENXIO indicates that sector_num was past the end of the file.
1145 * There is a virtual hole there. */
1146 assert(errno != -ENXIO);
1147
1148 /* Most likely EINVAL. Assume everything is allocated. */
1149 *pnum = nb_sectors;
63390a8d 1150 return ret;
5500316d
PB
1151 }
1152
1153 if (hole > start) {
1154 data = start;
1155 } else {
1156 /* On a hole. We need another syscall to find its end. */
1157 data = lseek(s->fd, start, SEEK_DATA);
1158 if (data == -1) {
1159 data = lseek(s->fd, 0, SEEK_END);
1160 }
1161 }
1162#else
63390a8d
PB
1163 data = 0;
1164 hole = start + nb_sectors * BDRV_SECTOR_SIZE;
5500316d
PB
1165#endif
1166
1167 if (data <= start) {
1168 /* On a data extent, compute sectors to the end of the extent. */
1169 *pnum = MIN(nb_sectors, (hole - start) / BDRV_SECTOR_SIZE);
5500316d
PB
1170 } else {
1171 /* On a hole, compute sectors to the beginning of the next extent. */
1172 *pnum = MIN(nb_sectors, (data - start) / BDRV_SECTOR_SIZE);
63390a8d
PB
1173 ret &= ~BDRV_BLOCK_DATA;
1174 ret |= BDRV_BLOCK_ZERO;
5500316d 1175 }
63390a8d
PB
1176
1177 return ret;
5500316d
PB
1178}
1179
8238010b
PB
1180static coroutine_fn BlockDriverAIOCB *raw_aio_discard(BlockDriverState *bs,
1181 int64_t sector_num, int nb_sectors,
1182 BlockDriverCompletionFunc *cb, void *opaque)
dce512de 1183{
dce512de
CH
1184 BDRVRawState *s = bs->opaque;
1185
8238010b
PB
1186 return paio_submit(bs, s->fd, sector_num, NULL, nb_sectors,
1187 cb, opaque, QEMU_AIO_DISCARD);
dce512de 1188}
0e7e1989
KW
1189
1190static QEMUOptionParameter raw_create_options[] = {
db08adf5
KW
1191 {
1192 .name = BLOCK_OPT_SIZE,
1193 .type = OPT_SIZE,
1194 .help = "Virtual disk size"
1195 },
0e7e1989
KW
1196 { NULL }
1197};
1198
84a12e66
CH
1199static BlockDriver bdrv_file = {
1200 .format_name = "file",
1201 .protocol_name = "file",
856ae5c3 1202 .instance_size = sizeof(BDRVRawState),
030be321 1203 .bdrv_needs_filename = true,
856ae5c3 1204 .bdrv_probe = NULL, /* no probe for protocols */
66f82cee 1205 .bdrv_file_open = raw_open,
eeb6b45d
JC
1206 .bdrv_reopen_prepare = raw_reopen_prepare,
1207 .bdrv_reopen_commit = raw_reopen_commit,
1208 .bdrv_reopen_abort = raw_reopen_abort,
856ae5c3
BS
1209 .bdrv_close = raw_close,
1210 .bdrv_create = raw_create,
3ac21627 1211 .bdrv_has_zero_init = bdrv_has_zero_init_1,
b6b8a333 1212 .bdrv_co_get_block_status = raw_co_get_block_status,
3b46e624 1213
f141eafe
AL
1214 .bdrv_aio_readv = raw_aio_readv,
1215 .bdrv_aio_writev = raw_aio_writev,
b2e12bc6 1216 .bdrv_aio_flush = raw_aio_flush,
8238010b 1217 .bdrv_aio_discard = raw_aio_discard,
3c529d93 1218
83f64091
FB
1219 .bdrv_truncate = raw_truncate,
1220 .bdrv_getlength = raw_getlength,
4a1d5e1f
FZ
1221 .bdrv_get_allocated_file_size
1222 = raw_get_allocated_file_size,
0e7e1989
KW
1223
1224 .create_options = raw_create_options,
83f64091
FB
1225};
1226
19cb3738
FB
1227/***********************************************/
1228/* host device */
1229
83affaa6 1230#if defined(__APPLE__) && defined(__MACH__)
19cb3738
FB
1231static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
1232static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
1233
1234kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
1235{
5fafdf24 1236 kern_return_t kernResult;
19cb3738
FB
1237 mach_port_t masterPort;
1238 CFMutableDictionaryRef classesToMatch;
1239
1240 kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
1241 if ( KERN_SUCCESS != kernResult ) {
1242 printf( "IOMasterPort returned %d\n", kernResult );
1243 }
3b46e624 1244
5fafdf24 1245 classesToMatch = IOServiceMatching( kIOCDMediaClass );
19cb3738
FB
1246 if ( classesToMatch == NULL ) {
1247 printf( "IOServiceMatching returned a NULL dictionary.\n" );
1248 } else {
1249 CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
1250 }
1251 kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
1252 if ( KERN_SUCCESS != kernResult )
1253 {
1254 printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
1255 }
3b46e624 1256
19cb3738
FB
1257 return kernResult;
1258}
1259
1260kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
1261{
1262 io_object_t nextMedia;
1263 kern_return_t kernResult = KERN_FAILURE;
1264 *bsdPath = '\0';
1265 nextMedia = IOIteratorNext( mediaIterator );
1266 if ( nextMedia )
1267 {
1268 CFTypeRef bsdPathAsCFString;
1269 bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
1270 if ( bsdPathAsCFString ) {
1271 size_t devPathLength;
1272 strcpy( bsdPath, _PATH_DEV );
1273 strcat( bsdPath, "r" );
1274 devPathLength = strlen( bsdPath );
1275 if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
1276 kernResult = KERN_SUCCESS;
1277 }
1278 CFRelease( bsdPathAsCFString );
1279 }
1280 IOObjectRelease( nextMedia );
1281 }
3b46e624 1282
19cb3738
FB
1283 return kernResult;
1284}
1285
1286#endif
1287
508c7cb3
CH
1288static int hdev_probe_device(const char *filename)
1289{
1290 struct stat st;
1291
1292 /* allow a dedicated CD-ROM driver to match with a higher priority */
1293 if (strstart(filename, "/dev/cdrom", NULL))
1294 return 50;
1295
1296 if (stat(filename, &st) >= 0 &&
1297 (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
1298 return 100;
1299 }
1300
1301 return 0;
1302}
1303
da888d37
SH
1304static int check_hdev_writable(BDRVRawState *s)
1305{
1306#if defined(BLKROGET)
1307 /* Linux block devices can be configured "read-only" using blockdev(8).
1308 * This is independent of device node permissions and therefore open(2)
1309 * with O_RDWR succeeds. Actual writes fail with EPERM.
1310 *
1311 * bdrv_open() is supposed to fail if the disk is read-only. Explicitly
1312 * check for read-only block devices so that Linux block devices behave
1313 * properly.
1314 */
1315 struct stat st;
1316 int readonly = 0;
1317
1318 if (fstat(s->fd, &st)) {
1319 return -errno;
1320 }
1321
1322 if (!S_ISBLK(st.st_mode)) {
1323 return 0;
1324 }
1325
1326 if (ioctl(s->fd, BLKROGET, &readonly) < 0) {
1327 return -errno;
1328 }
1329
1330 if (readonly) {
1331 return -EACCES;
1332 }
1333#endif /* defined(BLKROGET) */
1334 return 0;
1335}
1336
015a1036
MR
1337static int hdev_open(BlockDriverState *bs, QDict *options, int flags,
1338 Error **errp)
19cb3738
FB
1339{
1340 BDRVRawState *s = bs->opaque;
da888d37 1341 int ret;
c66a6157 1342 const char *filename = qdict_get_str(options, "filename");
a76bab49 1343
83affaa6 1344#if defined(__APPLE__) && defined(__MACH__)
19cb3738
FB
1345 if (strstart(filename, "/dev/cdrom", NULL)) {
1346 kern_return_t kernResult;
1347 io_iterator_t mediaIterator;
1348 char bsdPath[ MAXPATHLEN ];
1349 int fd;
5fafdf24 1350
19cb3738
FB
1351 kernResult = FindEjectableCDMedia( &mediaIterator );
1352 kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
3b46e624 1353
19cb3738
FB
1354 if ( bsdPath[ 0 ] != '\0' ) {
1355 strcat(bsdPath,"s0");
1356 /* some CDs don't have a partition 0 */
6165f4d8 1357 fd = qemu_open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
19cb3738
FB
1358 if (fd < 0) {
1359 bsdPath[strlen(bsdPath)-1] = '1';
1360 } else {
2e1e79da 1361 qemu_close(fd);
19cb3738
FB
1362 }
1363 filename = bsdPath;
a5c5ea3f 1364 qdict_put(options, "filename", qstring_from_str(filename));
19cb3738 1365 }
3b46e624 1366
19cb3738
FB
1367 if ( mediaIterator )
1368 IOObjectRelease( mediaIterator );
1369 }
1370#endif
19cb3738
FB
1371
1372 s->type = FTYPE_FILE;
4dd75c70 1373#if defined(__linux__)
05acda4d
BK
1374 {
1375 char resolved_path[ MAXPATHLEN ], *temp;
1376
1377 temp = realpath(filename, resolved_path);
1378 if (temp && strstart(temp, "/dev/sg", NULL)) {
1379 bs->sg = 1;
1380 }
19cb3738
FB
1381 }
1382#endif
90babde0 1383
c66a6157 1384 ret = raw_open_common(bs, options, flags, 0);
da888d37
SH
1385 if (ret < 0) {
1386 return ret;
1387 }
1388
1389 if (flags & BDRV_O_RDWR) {
1390 ret = check_hdev_writable(s);
1391 if (ret < 0) {
1392 raw_close(bs);
1393 return ret;
1394 }
1395 }
1396
1397 return ret;
19cb3738
FB
1398}
1399
03ff3ca3 1400#if defined(__linux__)
19cb3738
FB
1401/* Note: we do not have a reliable method to detect if the floppy is
1402 present. The current method is to try to open the floppy at every
1403 I/O and to keep it opened during a few hundreds of ms. */
1404static int fd_open(BlockDriverState *bs)
1405{
1406 BDRVRawState *s = bs->opaque;
1407 int last_media_present;
1408
1409 if (s->type != FTYPE_FD)
1410 return 0;
1411 last_media_present = (s->fd >= 0);
5fafdf24 1412 if (s->fd >= 0 &&
c57c846a 1413 (get_clock() - s->fd_open_time) >= FD_OPEN_TIMEOUT) {
2e1e79da 1414 qemu_close(s->fd);
19cb3738
FB
1415 s->fd = -1;
1416#ifdef DEBUG_FLOPPY
1417 printf("Floppy closed\n");
1418#endif
1419 }
1420 if (s->fd < 0) {
5fafdf24 1421 if (s->fd_got_error &&
c57c846a 1422 (get_clock() - s->fd_error_time) < FD_OPEN_TIMEOUT) {
19cb3738
FB
1423#ifdef DEBUG_FLOPPY
1424 printf("No floppy (open delayed)\n");
1425#endif
1426 return -EIO;
1427 }
6165f4d8 1428 s->fd = qemu_open(bs->filename, s->open_flags & ~O_NONBLOCK);
19cb3738 1429 if (s->fd < 0) {
c57c846a 1430 s->fd_error_time = get_clock();
19cb3738
FB
1431 s->fd_got_error = 1;
1432 if (last_media_present)
1433 s->fd_media_changed = 1;
1434#ifdef DEBUG_FLOPPY
1435 printf("No floppy\n");
1436#endif
1437 return -EIO;
1438 }
1439#ifdef DEBUG_FLOPPY
1440 printf("Floppy opened\n");
1441#endif
1442 }
1443 if (!last_media_present)
1444 s->fd_media_changed = 1;
c57c846a 1445 s->fd_open_time = get_clock();
19cb3738
FB
1446 s->fd_got_error = 0;
1447 return 0;
1448}
19cb3738 1449
63ec93db 1450static int hdev_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
985a03b0
TS
1451{
1452 BDRVRawState *s = bs->opaque;
1453
1454 return ioctl(s->fd, req, buf);
1455}
221f715d 1456
63ec93db 1457static BlockDriverAIOCB *hdev_aio_ioctl(BlockDriverState *bs,
221f715d
AL
1458 unsigned long int req, void *buf,
1459 BlockDriverCompletionFunc *cb, void *opaque)
1460{
f141eafe 1461 BDRVRawState *s = bs->opaque;
c208e8c2 1462 RawPosixAIOData *acb;
c4d9d196 1463 ThreadPool *pool;
221f715d 1464
f141eafe
AL
1465 if (fd_open(bs) < 0)
1466 return NULL;
c208e8c2
PB
1467
1468 acb = g_slice_new(RawPosixAIOData);
1469 acb->bs = bs;
1470 acb->aio_type = QEMU_AIO_IOCTL;
1471 acb->aio_fildes = s->fd;
1472 acb->aio_offset = 0;
1473 acb->aio_ioctl_buf = buf;
1474 acb->aio_ioctl_cmd = req;
c4d9d196
SH
1475 pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
1476 return thread_pool_submit_aio(pool, aio_worker, acb, cb, opaque);
221f715d
AL
1477}
1478
a167ba50 1479#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
9f23011a
BS
1480static int fd_open(BlockDriverState *bs)
1481{
1482 BDRVRawState *s = bs->opaque;
1483
1484 /* this is just to ensure s->fd is sane (its called by io ops) */
1485 if (s->fd >= 0)
1486 return 0;
1487 return -EIO;
1488}
9f23011a 1489#else /* !linux && !FreeBSD */
19cb3738 1490
08af02e2
AL
1491static int fd_open(BlockDriverState *bs)
1492{
1493 return 0;
1494}
1495
221f715d 1496#endif /* !linux && !FreeBSD */
04eeb8b6 1497
c36dd8a0
AF
1498static coroutine_fn BlockDriverAIOCB *hdev_aio_discard(BlockDriverState *bs,
1499 int64_t sector_num, int nb_sectors,
1500 BlockDriverCompletionFunc *cb, void *opaque)
1501{
1502 BDRVRawState *s = bs->opaque;
1503
1504 if (fd_open(bs) < 0) {
1505 return NULL;
1506 }
1507 return paio_submit(bs, s->fd, sector_num, NULL, nb_sectors,
1508 cb, opaque, QEMU_AIO_DISCARD|QEMU_AIO_BLKDEV);
1509}
1510
d5124c00
MR
1511static int hdev_create(const char *filename, QEMUOptionParameter *options,
1512 Error **errp)
93c65b47
AL
1513{
1514 int fd;
1515 int ret = 0;
1516 struct stat stat_buf;
0e7e1989 1517 int64_t total_size = 0;
93c65b47 1518
0e7e1989
KW
1519 /* Read out options */
1520 while (options && options->name) {
1521 if (!strcmp(options->name, "size")) {
9040385d 1522 total_size = options->value.n / BDRV_SECTOR_SIZE;
0e7e1989
KW
1523 }
1524 options++;
1525 }
93c65b47 1526
6165f4d8 1527 fd = qemu_open(filename, O_WRONLY | O_BINARY);
93c65b47 1528 if (fd < 0)
57e69b7d 1529 return -errno;
93c65b47
AL
1530
1531 if (fstat(fd, &stat_buf) < 0)
57e69b7d 1532 ret = -errno;
4099df58 1533 else if (!S_ISBLK(stat_buf.st_mode) && !S_ISCHR(stat_buf.st_mode))
57e69b7d 1534 ret = -ENODEV;
9040385d 1535 else if (lseek(fd, 0, SEEK_END) < total_size * BDRV_SECTOR_SIZE)
93c65b47
AL
1536 ret = -ENOSPC;
1537
2e1e79da 1538 qemu_close(fd);
93c65b47
AL
1539 return ret;
1540}
1541
5efa9d5a 1542static BlockDriver bdrv_host_device = {
0b4ce02e 1543 .format_name = "host_device",
84a12e66 1544 .protocol_name = "host_device",
0b4ce02e 1545 .instance_size = sizeof(BDRVRawState),
030be321 1546 .bdrv_needs_filename = true,
0b4ce02e 1547 .bdrv_probe_device = hdev_probe_device,
66f82cee 1548 .bdrv_file_open = hdev_open,
0b4ce02e 1549 .bdrv_close = raw_close,
1bc6b705
JC
1550 .bdrv_reopen_prepare = raw_reopen_prepare,
1551 .bdrv_reopen_commit = raw_reopen_commit,
1552 .bdrv_reopen_abort = raw_reopen_abort,
93c65b47 1553 .bdrv_create = hdev_create,
0b4ce02e 1554 .create_options = raw_create_options,
3b46e624 1555
f141eafe
AL
1556 .bdrv_aio_readv = raw_aio_readv,
1557 .bdrv_aio_writev = raw_aio_writev,
b2e12bc6 1558 .bdrv_aio_flush = raw_aio_flush,
8238010b 1559 .bdrv_aio_discard = hdev_aio_discard,
3c529d93 1560
55b949c8 1561 .bdrv_truncate = raw_truncate,
e60f469c 1562 .bdrv_getlength = raw_getlength,
4a1d5e1f
FZ
1563 .bdrv_get_allocated_file_size
1564 = raw_get_allocated_file_size,
19cb3738 1565
f3a5d3f8 1566 /* generic scsi device */
63ec93db
CH
1567#ifdef __linux__
1568 .bdrv_ioctl = hdev_ioctl,
63ec93db
CH
1569 .bdrv_aio_ioctl = hdev_aio_ioctl,
1570#endif
f3a5d3f8
CH
1571};
1572
1573#ifdef __linux__
015a1036
MR
1574static int floppy_open(BlockDriverState *bs, QDict *options, int flags,
1575 Error **errp)
f3a5d3f8
CH
1576{
1577 BDRVRawState *s = bs->opaque;
1578 int ret;
1579
f3a5d3f8 1580 s->type = FTYPE_FD;
f3a5d3f8 1581
19a3da7f 1582 /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
c66a6157 1583 ret = raw_open_common(bs, options, flags, O_NONBLOCK);
f3a5d3f8
CH
1584 if (ret)
1585 return ret;
1586
1587 /* close fd so that we can reopen it as needed */
2e1e79da 1588 qemu_close(s->fd);
f3a5d3f8
CH
1589 s->fd = -1;
1590 s->fd_media_changed = 1;
1591
1592 return 0;
1593}
1594
508c7cb3
CH
1595static int floppy_probe_device(const char *filename)
1596{
2ebf7c4b
CR
1597 int fd, ret;
1598 int prio = 0;
1599 struct floppy_struct fdparam;
343f8568 1600 struct stat st;
2ebf7c4b 1601
e1740828
CB
1602 if (strstart(filename, "/dev/fd", NULL) &&
1603 !strstart(filename, "/dev/fdset/", NULL)) {
2ebf7c4b 1604 prio = 50;
e1740828 1605 }
2ebf7c4b 1606
6165f4d8 1607 fd = qemu_open(filename, O_RDONLY | O_NONBLOCK);
2ebf7c4b
CR
1608 if (fd < 0) {
1609 goto out;
1610 }
343f8568
JS
1611 ret = fstat(fd, &st);
1612 if (ret == -1 || !S_ISBLK(st.st_mode)) {
1613 goto outc;
1614 }
2ebf7c4b
CR
1615
1616 /* Attempt to detect via a floppy specific ioctl */
1617 ret = ioctl(fd, FDGETPRM, &fdparam);
1618 if (ret >= 0)
1619 prio = 100;
1620
343f8568 1621outc:
2e1e79da 1622 qemu_close(fd);
2ebf7c4b
CR
1623out:
1624 return prio;
508c7cb3
CH
1625}
1626
1627
f3a5d3f8
CH
1628static int floppy_is_inserted(BlockDriverState *bs)
1629{
1630 return fd_open(bs) >= 0;
1631}
1632
1633static int floppy_media_changed(BlockDriverState *bs)
1634{
1635 BDRVRawState *s = bs->opaque;
1636 int ret;
1637
1638 /*
1639 * XXX: we do not have a true media changed indication.
1640 * It does not work if the floppy is changed without trying to read it.
1641 */
1642 fd_open(bs);
1643 ret = s->fd_media_changed;
1644 s->fd_media_changed = 0;
1645#ifdef DEBUG_FLOPPY
1646 printf("Floppy changed=%d\n", ret);
1647#endif
1648 return ret;
1649}
1650
f36f3949 1651static void floppy_eject(BlockDriverState *bs, bool eject_flag)
f3a5d3f8
CH
1652{
1653 BDRVRawState *s = bs->opaque;
1654 int fd;
1655
1656 if (s->fd >= 0) {
2e1e79da 1657 qemu_close(s->fd);
f3a5d3f8
CH
1658 s->fd = -1;
1659 }
6165f4d8 1660 fd = qemu_open(bs->filename, s->open_flags | O_NONBLOCK);
f3a5d3f8
CH
1661 if (fd >= 0) {
1662 if (ioctl(fd, FDEJECT, 0) < 0)
1663 perror("FDEJECT");
2e1e79da 1664 qemu_close(fd);
f3a5d3f8 1665 }
f3a5d3f8
CH
1666}
1667
1668static BlockDriver bdrv_host_floppy = {
1669 .format_name = "host_floppy",
84a12e66 1670 .protocol_name = "host_floppy",
f3a5d3f8 1671 .instance_size = sizeof(BDRVRawState),
030be321 1672 .bdrv_needs_filename = true,
508c7cb3 1673 .bdrv_probe_device = floppy_probe_device,
66f82cee 1674 .bdrv_file_open = floppy_open,
f3a5d3f8 1675 .bdrv_close = raw_close,
1bc6b705
JC
1676 .bdrv_reopen_prepare = raw_reopen_prepare,
1677 .bdrv_reopen_commit = raw_reopen_commit,
1678 .bdrv_reopen_abort = raw_reopen_abort,
f3a5d3f8 1679 .bdrv_create = hdev_create,
0b4ce02e 1680 .create_options = raw_create_options,
f3a5d3f8 1681
f3a5d3f8
CH
1682 .bdrv_aio_readv = raw_aio_readv,
1683 .bdrv_aio_writev = raw_aio_writev,
b2e12bc6 1684 .bdrv_aio_flush = raw_aio_flush,
f3a5d3f8 1685
55b949c8 1686 .bdrv_truncate = raw_truncate,
f3a5d3f8 1687 .bdrv_getlength = raw_getlength,
4a1d5e1f
FZ
1688 .bdrv_get_allocated_file_size
1689 = raw_get_allocated_file_size,
f3a5d3f8
CH
1690
1691 /* removable device support */
1692 .bdrv_is_inserted = floppy_is_inserted,
1693 .bdrv_media_changed = floppy_media_changed,
1694 .bdrv_eject = floppy_eject,
f3a5d3f8
CH
1695};
1696
015a1036
MR
1697static int cdrom_open(BlockDriverState *bs, QDict *options, int flags,
1698 Error **errp)
f3a5d3f8
CH
1699{
1700 BDRVRawState *s = bs->opaque;
1701
f3a5d3f8
CH
1702 s->type = FTYPE_CD;
1703
19a3da7f 1704 /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
c66a6157 1705 return raw_open_common(bs, options, flags, O_NONBLOCK);
f3a5d3f8
CH
1706}
1707
508c7cb3
CH
1708static int cdrom_probe_device(const char *filename)
1709{
3baf720e
CR
1710 int fd, ret;
1711 int prio = 0;
343f8568 1712 struct stat st;
3baf720e 1713
6165f4d8 1714 fd = qemu_open(filename, O_RDONLY | O_NONBLOCK);
3baf720e
CR
1715 if (fd < 0) {
1716 goto out;
1717 }
343f8568
JS
1718 ret = fstat(fd, &st);
1719 if (ret == -1 || !S_ISBLK(st.st_mode)) {
1720 goto outc;
1721 }
3baf720e
CR
1722
1723 /* Attempt to detect via a CDROM specific ioctl */
1724 ret = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
1725 if (ret >= 0)
1726 prio = 100;
1727
343f8568 1728outc:
2e1e79da 1729 qemu_close(fd);
3baf720e
CR
1730out:
1731 return prio;
508c7cb3
CH
1732}
1733
f3a5d3f8
CH
1734static int cdrom_is_inserted(BlockDriverState *bs)
1735{
1736 BDRVRawState *s = bs->opaque;
1737 int ret;
1738
1739 ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
1740 if (ret == CDS_DISC_OK)
1741 return 1;
1742 return 0;
1743}
1744
f36f3949 1745static void cdrom_eject(BlockDriverState *bs, bool eject_flag)
f3a5d3f8
CH
1746{
1747 BDRVRawState *s = bs->opaque;
1748
1749 if (eject_flag) {
1750 if (ioctl(s->fd, CDROMEJECT, NULL) < 0)
1751 perror("CDROMEJECT");
1752 } else {
1753 if (ioctl(s->fd, CDROMCLOSETRAY, NULL) < 0)
1754 perror("CDROMEJECT");
1755 }
f3a5d3f8
CH
1756}
1757
025e849a 1758static void cdrom_lock_medium(BlockDriverState *bs, bool locked)
f3a5d3f8
CH
1759{
1760 BDRVRawState *s = bs->opaque;
1761
1762 if (ioctl(s->fd, CDROM_LOCKDOOR, locked) < 0) {
1763 /*
1764 * Note: an error can happen if the distribution automatically
1765 * mounts the CD-ROM
1766 */
1767 /* perror("CDROM_LOCKDOOR"); */
1768 }
f3a5d3f8
CH
1769}
1770
1771static BlockDriver bdrv_host_cdrom = {
1772 .format_name = "host_cdrom",
84a12e66 1773 .protocol_name = "host_cdrom",
f3a5d3f8 1774 .instance_size = sizeof(BDRVRawState),
030be321 1775 .bdrv_needs_filename = true,
508c7cb3 1776 .bdrv_probe_device = cdrom_probe_device,
66f82cee 1777 .bdrv_file_open = cdrom_open,
f3a5d3f8 1778 .bdrv_close = raw_close,
1bc6b705
JC
1779 .bdrv_reopen_prepare = raw_reopen_prepare,
1780 .bdrv_reopen_commit = raw_reopen_commit,
1781 .bdrv_reopen_abort = raw_reopen_abort,
f3a5d3f8 1782 .bdrv_create = hdev_create,
0b4ce02e 1783 .create_options = raw_create_options,
f3a5d3f8 1784
f3a5d3f8
CH
1785 .bdrv_aio_readv = raw_aio_readv,
1786 .bdrv_aio_writev = raw_aio_writev,
b2e12bc6 1787 .bdrv_aio_flush = raw_aio_flush,
f3a5d3f8 1788
55b949c8 1789 .bdrv_truncate = raw_truncate,
f3a5d3f8 1790 .bdrv_getlength = raw_getlength,
4a1d5e1f
FZ
1791 .bdrv_get_allocated_file_size
1792 = raw_get_allocated_file_size,
f3a5d3f8
CH
1793
1794 /* removable device support */
1795 .bdrv_is_inserted = cdrom_is_inserted,
1796 .bdrv_eject = cdrom_eject,
025e849a 1797 .bdrv_lock_medium = cdrom_lock_medium,
f3a5d3f8
CH
1798
1799 /* generic scsi device */
63ec93db 1800 .bdrv_ioctl = hdev_ioctl,
63ec93db 1801 .bdrv_aio_ioctl = hdev_aio_ioctl,
f3a5d3f8
CH
1802};
1803#endif /* __linux__ */
1804
a167ba50 1805#if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
56d1b4d2 1806static int cdrom_open(BlockDriverState *bs, QDict *options, int flags)
f3a5d3f8
CH
1807{
1808 BDRVRawState *s = bs->opaque;
1809 int ret;
1810
1811 s->type = FTYPE_CD;
1812
c66a6157 1813 ret = raw_open_common(bs, options, flags, 0);
f3a5d3f8
CH
1814 if (ret)
1815 return ret;
1816
9b2260cb 1817 /* make sure the door isn't locked at this time */
f3a5d3f8
CH
1818 ioctl(s->fd, CDIOCALLOW);
1819 return 0;
1820}
1821
508c7cb3
CH
1822static int cdrom_probe_device(const char *filename)
1823{
1824 if (strstart(filename, "/dev/cd", NULL) ||
1825 strstart(filename, "/dev/acd", NULL))
1826 return 100;
1827 return 0;
1828}
1829
f3a5d3f8
CH
1830static int cdrom_reopen(BlockDriverState *bs)
1831{
1832 BDRVRawState *s = bs->opaque;
1833 int fd;
1834
1835 /*
1836 * Force reread of possibly changed/newly loaded disc,
1837 * FreeBSD seems to not notice sometimes...
1838 */
1839 if (s->fd >= 0)
2e1e79da 1840 qemu_close(s->fd);
6165f4d8 1841 fd = qemu_open(bs->filename, s->open_flags, 0644);
f3a5d3f8
CH
1842 if (fd < 0) {
1843 s->fd = -1;
1844 return -EIO;
1845 }
1846 s->fd = fd;
1847
9b2260cb 1848 /* make sure the door isn't locked at this time */
f3a5d3f8
CH
1849 ioctl(s->fd, CDIOCALLOW);
1850 return 0;
1851}
1852
1853static int cdrom_is_inserted(BlockDriverState *bs)
1854{
1855 return raw_getlength(bs) > 0;
1856}
1857
f36f3949 1858static void cdrom_eject(BlockDriverState *bs, bool eject_flag)
f3a5d3f8
CH
1859{
1860 BDRVRawState *s = bs->opaque;
1861
1862 if (s->fd < 0)
822e1cd1 1863 return;
f3a5d3f8
CH
1864
1865 (void) ioctl(s->fd, CDIOCALLOW);
1866
1867 if (eject_flag) {
1868 if (ioctl(s->fd, CDIOCEJECT) < 0)
1869 perror("CDIOCEJECT");
1870 } else {
1871 if (ioctl(s->fd, CDIOCCLOSE) < 0)
1872 perror("CDIOCCLOSE");
1873 }
1874
822e1cd1 1875 cdrom_reopen(bs);
f3a5d3f8
CH
1876}
1877
025e849a 1878static void cdrom_lock_medium(BlockDriverState *bs, bool locked)
f3a5d3f8
CH
1879{
1880 BDRVRawState *s = bs->opaque;
1881
1882 if (s->fd < 0)
7bf37fed 1883 return;
f3a5d3f8
CH
1884 if (ioctl(s->fd, (locked ? CDIOCPREVENT : CDIOCALLOW)) < 0) {
1885 /*
1886 * Note: an error can happen if the distribution automatically
1887 * mounts the CD-ROM
1888 */
1889 /* perror("CDROM_LOCKDOOR"); */
1890 }
f3a5d3f8
CH
1891}
1892
1893static BlockDriver bdrv_host_cdrom = {
1894 .format_name = "host_cdrom",
84a12e66 1895 .protocol_name = "host_cdrom",
f3a5d3f8 1896 .instance_size = sizeof(BDRVRawState),
030be321 1897 .bdrv_needs_filename = true,
508c7cb3 1898 .bdrv_probe_device = cdrom_probe_device,
66f82cee 1899 .bdrv_file_open = cdrom_open,
f3a5d3f8 1900 .bdrv_close = raw_close,
1bc6b705
JC
1901 .bdrv_reopen_prepare = raw_reopen_prepare,
1902 .bdrv_reopen_commit = raw_reopen_commit,
1903 .bdrv_reopen_abort = raw_reopen_abort,
f3a5d3f8 1904 .bdrv_create = hdev_create,
0b4ce02e 1905 .create_options = raw_create_options,
f3a5d3f8 1906
f3a5d3f8
CH
1907 .bdrv_aio_readv = raw_aio_readv,
1908 .bdrv_aio_writev = raw_aio_writev,
b2e12bc6 1909 .bdrv_aio_flush = raw_aio_flush,
f3a5d3f8 1910
55b949c8 1911 .bdrv_truncate = raw_truncate,
f3a5d3f8 1912 .bdrv_getlength = raw_getlength,
4a1d5e1f
FZ
1913 .bdrv_get_allocated_file_size
1914 = raw_get_allocated_file_size,
f3a5d3f8 1915
19cb3738 1916 /* removable device support */
f3a5d3f8
CH
1917 .bdrv_is_inserted = cdrom_is_inserted,
1918 .bdrv_eject = cdrom_eject,
025e849a 1919 .bdrv_lock_medium = cdrom_lock_medium,
19cb3738 1920};
f3a5d3f8 1921#endif /* __FreeBSD__ */
5efa9d5a 1922
4065742a
SH
1923#ifdef CONFIG_LINUX_AIO
1924/**
1925 * Return the file descriptor for Linux AIO
1926 *
1927 * This function is a layering violation and should be removed when it becomes
1928 * possible to call the block layer outside the global mutex. It allows the
1929 * caller to hijack the file descriptor so I/O can be performed outside the
1930 * block layer.
1931 */
1932int raw_get_aio_fd(BlockDriverState *bs)
1933{
1934 BDRVRawState *s;
1935
1936 if (!bs->drv) {
1937 return -ENOMEDIUM;
1938 }
1939
1940 if (bs->drv == bdrv_find_format("raw")) {
1941 bs = bs->file;
1942 }
1943
1944 /* raw-posix has several protocols so just check for raw_aio_readv */
1945 if (bs->drv->bdrv_aio_readv != raw_aio_readv) {
1946 return -ENOTSUP;
1947 }
1948
1949 s = bs->opaque;
1950 if (!s->use_aio) {
1951 return -ENOTSUP;
1952 }
1953 return s->fd;
1954}
1955#endif /* CONFIG_LINUX_AIO */
1956
84a12e66 1957static void bdrv_file_init(void)
5efa9d5a 1958{
508c7cb3
CH
1959 /*
1960 * Register all the drivers. Note that order is important, the driver
1961 * registered last will get probed first.
1962 */
84a12e66 1963 bdrv_register(&bdrv_file);
5efa9d5a 1964 bdrv_register(&bdrv_host_device);
f3a5d3f8
CH
1965#ifdef __linux__
1966 bdrv_register(&bdrv_host_floppy);
1967 bdrv_register(&bdrv_host_cdrom);
1968#endif
a167ba50 1969#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
f3a5d3f8
CH
1970 bdrv_register(&bdrv_host_cdrom);
1971#endif
5efa9d5a
AL
1972}
1973
84a12e66 1974block_init(bdrv_file_init);