]> git.proxmox.com Git - mirror_qemu.git/blame - block/raw-posix.c
bochs: Handle failure for potentially large allocations
[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"
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 57#include <linux/fs.h>
4ab15590
CL
58#ifndef FS_NOCOW_FL
59#define FS_NOCOW_FL 0x00800000 /* Do not cow file */
60#endif
5500316d
PB
61#endif
62#ifdef CONFIG_FIEMAP
63#include <linux/fiemap.h>
19cb3738 64#endif
3d4fa43e
KK
65#ifdef CONFIG_FALLOCATE_PUNCH_HOLE
66#include <linux/falloc.h>
67#endif
a167ba50 68#if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
1cb6c3fd 69#include <sys/disk.h>
9f23011a 70#include <sys/cdio.h>
1cb6c3fd 71#endif
83f64091 72
128ab2ff
BS
73#ifdef __OpenBSD__
74#include <sys/ioctl.h>
75#include <sys/disklabel.h>
76#include <sys/dkio.h>
77#endif
78
d1f6fd8d
CE
79#ifdef __NetBSD__
80#include <sys/ioctl.h>
81#include <sys/disklabel.h>
82#include <sys/dkio.h>
83#include <sys/disk.h>
84#endif
85
c5e97233
BS
86#ifdef __DragonFly__
87#include <sys/ioctl.h>
88#include <sys/diskslice.h>
89#endif
90
dce512de
CH
91#ifdef CONFIG_XFS
92#include <xfs/xfs.h>
93#endif
94
19cb3738 95//#define DEBUG_FLOPPY
83f64091 96
faf07963 97//#define DEBUG_BLOCK
03ff3ca3 98#if defined(DEBUG_BLOCK)
001faf32
BS
99#define DEBUG_BLOCK_PRINT(formatCstr, ...) do { if (qemu_log_enabled()) \
100 { qemu_log(formatCstr, ## __VA_ARGS__); qemu_log_flush(); } } while (0)
8c05dbf9 101#else
001faf32 102#define DEBUG_BLOCK_PRINT(formatCstr, ...)
8c05dbf9
TS
103#endif
104
f6465578
AL
105/* OS X does not have O_DSYNC */
106#ifndef O_DSYNC
1c27a8b3 107#ifdef O_SYNC
7ab064d2 108#define O_DSYNC O_SYNC
1c27a8b3
JA
109#elif defined(O_FSYNC)
110#define O_DSYNC O_FSYNC
111#endif
f6465578
AL
112#endif
113
9f7965c7
AL
114/* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */
115#ifndef O_DIRECT
116#define O_DIRECT O_DSYNC
117#endif
118
19cb3738
FB
119#define FTYPE_FILE 0
120#define FTYPE_CD 1
121#define FTYPE_FD 2
83f64091 122
c57c846a 123/* if the FD is not accessed during that time (in ns), we try to
19cb3738 124 reopen it to see if the disk has been changed */
c57c846a 125#define FD_OPEN_TIMEOUT (1000000000)
83f64091 126
581b9e29
CH
127#define MAX_BLOCKSIZE 4096
128
19cb3738
FB
129typedef struct BDRVRawState {
130 int fd;
131 int type;
0e1d8f4c 132 int open_flags;
c25f53b0
PB
133 size_t buf_align;
134
19cb3738
FB
135#if defined(__linux__)
136 /* linux floppy specific */
19cb3738
FB
137 int64_t fd_open_time;
138 int64_t fd_error_time;
139 int fd_got_error;
140 int fd_media_changed;
83f64091 141#endif
e44bd6fc 142#ifdef CONFIG_LINUX_AIO
5c6c3a6c 143 int use_aio;
1e5b9d2f 144 void *aio_ctx;
e44bd6fc 145#endif
dce512de 146#ifdef CONFIG_XFS
260a82e5 147 bool is_xfs:1;
dce512de 148#endif
260a82e5 149 bool has_discard:1;
97a2ae34 150 bool has_write_zeroes:1;
260a82e5 151 bool discard_zeroes:1;
4f11aa8a
HR
152#ifdef CONFIG_FIEMAP
153 bool skip_fiemap;
154#endif
19cb3738
FB
155} BDRVRawState;
156
eeb6b45d
JC
157typedef struct BDRVRawReopenState {
158 int fd;
159 int open_flags;
160#ifdef CONFIG_LINUX_AIO
161 int use_aio;
162#endif
163} BDRVRawReopenState;
164
19cb3738 165static int fd_open(BlockDriverState *bs);
22afa7b5 166static int64_t raw_getlength(BlockDriverState *bs);
83f64091 167
de81a169
PB
168typedef struct RawPosixAIOData {
169 BlockDriverState *bs;
170 int aio_fildes;
171 union {
172 struct iovec *aio_iov;
173 void *aio_ioctl_buf;
174 };
175 int aio_niov;
8238010b 176 uint64_t aio_nbytes;
de81a169
PB
177#define aio_ioctl_cmd aio_nbytes /* for QEMU_AIO_IOCTL */
178 off_t aio_offset;
179 int aio_type;
180} RawPosixAIOData;
181
a167ba50 182#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
f3a5d3f8 183static int cdrom_reopen(BlockDriverState *bs);
9f23011a
BS
184#endif
185
1de1ae0a
CE
186#if defined(__NetBSD__)
187static int raw_normalize_devicepath(const char **filename)
188{
189 static char namebuf[PATH_MAX];
190 const char *dp, *fname;
191 struct stat sb;
192
193 fname = *filename;
194 dp = strrchr(fname, '/');
195 if (lstat(fname, &sb) < 0) {
196 fprintf(stderr, "%s: stat failed: %s\n",
197 fname, strerror(errno));
198 return -errno;
199 }
200
201 if (!S_ISBLK(sb.st_mode)) {
202 return 0;
203 }
204
205 if (dp == NULL) {
206 snprintf(namebuf, PATH_MAX, "r%s", fname);
207 } else {
208 snprintf(namebuf, PATH_MAX, "%.*s/r%s",
209 (int)(dp - fname), fname, dp + 1);
210 }
211 fprintf(stderr, "%s is a block device", fname);
212 *filename = namebuf;
213 fprintf(stderr, ", using %s\n", *filename);
214
215 return 0;
216}
217#else
218static int raw_normalize_devicepath(const char **filename)
219{
220 return 0;
221}
222#endif
223
df26a350 224static void raw_probe_alignment(BlockDriverState *bs, int fd, Error **errp)
c25f53b0
PB
225{
226 BDRVRawState *s = bs->opaque;
227 char *buf;
228 unsigned int sector_size;
229
230 /* For /dev/sg devices the alignment is not really used.
231 With buffered I/O, we don't have any restrictions. */
232 if (bs->sg || !(s->open_flags & O_DIRECT)) {
233 bs->request_alignment = 1;
234 s->buf_align = 1;
235 return;
236 }
237
238 /* Try a few ioctls to get the right size */
239 bs->request_alignment = 0;
240 s->buf_align = 0;
241
242#ifdef BLKSSZGET
df26a350 243 if (ioctl(fd, BLKSSZGET, &sector_size) >= 0) {
c25f53b0
PB
244 bs->request_alignment = sector_size;
245 }
246#endif
247#ifdef DKIOCGETBLOCKSIZE
df26a350 248 if (ioctl(fd, DKIOCGETBLOCKSIZE, &sector_size) >= 0) {
c25f53b0
PB
249 bs->request_alignment = sector_size;
250 }
251#endif
252#ifdef DIOCGSECTORSIZE
df26a350 253 if (ioctl(fd, DIOCGSECTORSIZE, &sector_size) >= 0) {
c25f53b0
PB
254 bs->request_alignment = sector_size;
255 }
256#endif
257#ifdef CONFIG_XFS
258 if (s->is_xfs) {
259 struct dioattr da;
df26a350 260 if (xfsctl(NULL, fd, XFS_IOC_DIOINFO, &da) >= 0) {
c25f53b0
PB
261 bs->request_alignment = da.d_miniosz;
262 /* The kernel returns wrong information for d_mem */
263 /* s->buf_align = da.d_mem; */
264 }
265 }
266#endif
267
268 /* If we could not get the sizes so far, we can only guess them */
269 if (!s->buf_align) {
270 size_t align;
271 buf = qemu_memalign(MAX_BLOCKSIZE, 2 * MAX_BLOCKSIZE);
272 for (align = 512; align <= MAX_BLOCKSIZE; align <<= 1) {
df26a350 273 if (pread(fd, buf + align, MAX_BLOCKSIZE, 0) >= 0) {
c25f53b0
PB
274 s->buf_align = align;
275 break;
276 }
277 }
278 qemu_vfree(buf);
279 }
280
281 if (!bs->request_alignment) {
282 size_t align;
283 buf = qemu_memalign(s->buf_align, MAX_BLOCKSIZE);
284 for (align = 512; align <= MAX_BLOCKSIZE; align <<= 1) {
df26a350 285 if (pread(fd, buf, align, 0) >= 0) {
c25f53b0
PB
286 bs->request_alignment = align;
287 break;
288 }
289 }
290 qemu_vfree(buf);
291 }
df26a350
KW
292
293 if (!s->buf_align || !bs->request_alignment) {
294 error_setg(errp, "Could not find working O_DIRECT alignment. "
295 "Try cache.direct=off.");
296 }
c25f53b0
PB
297}
298
6a8dc042
JC
299static void raw_parse_flags(int bdrv_flags, int *open_flags)
300{
301 assert(open_flags != NULL);
302
303 *open_flags |= O_BINARY;
304 *open_flags &= ~O_ACCMODE;
305 if (bdrv_flags & BDRV_O_RDWR) {
306 *open_flags |= O_RDWR;
307 } else {
308 *open_flags |= O_RDONLY;
309 }
310
311 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
312 * and O_DIRECT for no caching. */
313 if ((bdrv_flags & BDRV_O_NOCACHE)) {
314 *open_flags |= O_DIRECT;
315 }
6a8dc042
JC
316}
317
c2f3426c
SH
318static void raw_detach_aio_context(BlockDriverState *bs)
319{
320#ifdef CONFIG_LINUX_AIO
321 BDRVRawState *s = bs->opaque;
322
323 if (s->use_aio) {
324 laio_detach_aio_context(s->aio_ctx, bdrv_get_aio_context(bs));
325 }
326#endif
327}
328
329static void raw_attach_aio_context(BlockDriverState *bs,
330 AioContext *new_context)
331{
332#ifdef CONFIG_LINUX_AIO
333 BDRVRawState *s = bs->opaque;
334
335 if (s->use_aio) {
336 laio_attach_aio_context(s->aio_ctx, new_context);
337 }
338#endif
339}
340
fc32a72d
JC
341#ifdef CONFIG_LINUX_AIO
342static int raw_set_aio(void **aio_ctx, int *use_aio, int bdrv_flags)
343{
344 int ret = -1;
345 assert(aio_ctx != NULL);
346 assert(use_aio != NULL);
347 /*
348 * Currently Linux do AIO only for files opened with O_DIRECT
349 * specified so check NOCACHE flag too
350 */
351 if ((bdrv_flags & (BDRV_O_NOCACHE|BDRV_O_NATIVE_AIO)) ==
352 (BDRV_O_NOCACHE|BDRV_O_NATIVE_AIO)) {
353
354 /* if non-NULL, laio_init() has already been run */
355 if (*aio_ctx == NULL) {
356 *aio_ctx = laio_init();
357 if (!*aio_ctx) {
358 goto error;
359 }
360 }
361 *use_aio = 1;
362 } else {
363 *use_aio = 0;
364 }
365
366 ret = 0;
367
368error:
369 return ret;
370}
371#endif
372
078896a9
HR
373static void raw_parse_filename(const char *filename, QDict *options,
374 Error **errp)
375{
376 /* The filename does not have to be prefixed by the protocol name, since
377 * "file" is the default protocol; therefore, the return value of this
378 * function call can be ignored. */
379 strstart(filename, "file:", &filename);
380
381 qdict_put_obj(options, "filename", QOBJECT(qstring_from_str(filename)));
382}
383
c66a6157
KW
384static QemuOptsList raw_runtime_opts = {
385 .name = "raw",
386 .head = QTAILQ_HEAD_INITIALIZER(raw_runtime_opts.head),
387 .desc = {
388 {
389 .name = "filename",
390 .type = QEMU_OPT_STRING,
391 .help = "File name of the image",
392 },
393 { /* end of list */ }
394 },
395};
396
397static int raw_open_common(BlockDriverState *bs, QDict *options,
e428e439 398 int bdrv_flags, int open_flags, Error **errp)
83f64091
FB
399{
400 BDRVRawState *s = bs->opaque;
c66a6157
KW
401 QemuOpts *opts;
402 Error *local_err = NULL;
8bfea15d 403 const char *filename = NULL;
0e1d8f4c 404 int fd, ret;
260a82e5 405 struct stat st;
83f64091 406
87ea75d5 407 opts = qemu_opts_create(&raw_runtime_opts, NULL, 0, &error_abort);
c66a6157 408 qemu_opts_absorb_qdict(opts, options, &local_err);
84d18f06 409 if (local_err) {
e428e439 410 error_propagate(errp, local_err);
c66a6157
KW
411 ret = -EINVAL;
412 goto fail;
413 }
414
415 filename = qemu_opt_get(opts, "filename");
416
1de1ae0a
CE
417 ret = raw_normalize_devicepath(&filename);
418 if (ret != 0) {
e428e439 419 error_setg_errno(errp, -ret, "Could not normalize device path");
c66a6157 420 goto fail;
1de1ae0a
CE
421 }
422
6a8dc042
JC
423 s->open_flags = open_flags;
424 raw_parse_flags(bdrv_flags, &s->open_flags);
83f64091 425
90babde0 426 s->fd = -1;
40ff6d7e 427 fd = qemu_open(filename, s->open_flags, 0644);
19cb3738
FB
428 if (fd < 0) {
429 ret = -errno;
c66a6157 430 if (ret == -EROFS) {
19cb3738 431 ret = -EACCES;
c66a6157
KW
432 }
433 goto fail;
19cb3738 434 }
83f64091 435 s->fd = fd;
9ef91a67 436
5c6c3a6c 437#ifdef CONFIG_LINUX_AIO
fc32a72d 438 if (raw_set_aio(&s->aio_ctx, &s->use_aio, bdrv_flags)) {
47e6b251 439 qemu_close(fd);
c66a6157 440 ret = -errno;
e428e439 441 error_setg_errno(errp, -ret, "Could not set AIO state");
c66a6157 442 goto fail;
9ef91a67 443 }
fc32a72d 444#endif
9ef91a67 445
7ce21016 446 s->has_discard = true;
97a2ae34 447 s->has_write_zeroes = true;
260a82e5
PB
448
449 if (fstat(s->fd, &st) < 0) {
450 error_setg_errno(errp, errno, "Could not stat file");
451 goto fail;
452 }
453 if (S_ISREG(st.st_mode)) {
454 s->discard_zeroes = true;
455 }
d0b4503e
PB
456 if (S_ISBLK(st.st_mode)) {
457#ifdef BLKDISCARDZEROES
458 unsigned int arg;
459 if (ioctl(s->fd, BLKDISCARDZEROES, &arg) == 0 && arg) {
460 s->discard_zeroes = true;
461 }
462#endif
463#ifdef __linux__
464 /* On Linux 3.10, BLKDISCARD leaves stale data in the page cache. Do
465 * not rely on the contents of discarded blocks unless using O_DIRECT.
97a2ae34 466 * Same for BLKZEROOUT.
d0b4503e
PB
467 */
468 if (!(bs->open_flags & BDRV_O_NOCACHE)) {
469 s->discard_zeroes = false;
97a2ae34 470 s->has_write_zeroes = false;
d0b4503e
PB
471 }
472#endif
473 }
260a82e5 474
dce512de
CH
475#ifdef CONFIG_XFS
476 if (platform_test_xfs_fd(s->fd)) {
7ce21016 477 s->is_xfs = true;
dce512de
CH
478 }
479#endif
480
c2f3426c
SH
481 raw_attach_aio_context(bs, bdrv_get_aio_context(bs));
482
c66a6157
KW
483 ret = 0;
484fail:
8bfea15d
KW
485 if (filename && (bdrv_flags & BDRV_O_TEMPORARY)) {
486 unlink(filename);
487 }
c66a6157
KW
488 qemu_opts_del(opts);
489 return ret;
83f64091
FB
490}
491
015a1036
HR
492static int raw_open(BlockDriverState *bs, QDict *options, int flags,
493 Error **errp)
90babde0
CH
494{
495 BDRVRawState *s = bs->opaque;
e428e439
HR
496 Error *local_err = NULL;
497 int ret;
90babde0
CH
498
499 s->type = FTYPE_FILE;
e428e439 500 ret = raw_open_common(bs, options, flags, 0, &local_err);
84d18f06 501 if (local_err) {
e428e439
HR
502 error_propagate(errp, local_err);
503 }
504 return ret;
90babde0
CH
505}
506
eeb6b45d
JC
507static int raw_reopen_prepare(BDRVReopenState *state,
508 BlockReopenQueue *queue, Error **errp)
509{
510 BDRVRawState *s;
511 BDRVRawReopenState *raw_s;
512 int ret = 0;
df26a350 513 Error *local_err = NULL;
eeb6b45d
JC
514
515 assert(state != NULL);
516 assert(state->bs != NULL);
517
518 s = state->bs->opaque;
519
520 state->opaque = g_malloc0(sizeof(BDRVRawReopenState));
521 raw_s = state->opaque;
522
523#ifdef CONFIG_LINUX_AIO
524 raw_s->use_aio = s->use_aio;
525
526 /* we can use s->aio_ctx instead of a copy, because the use_aio flag is
527 * valid in the 'false' condition even if aio_ctx is set, and raw_set_aio()
528 * won't override aio_ctx if aio_ctx is non-NULL */
529 if (raw_set_aio(&s->aio_ctx, &raw_s->use_aio, state->flags)) {
e428e439 530 error_setg(errp, "Could not set AIO state");
eeb6b45d
JC
531 return -1;
532 }
533#endif
534
1bc6b705
JC
535 if (s->type == FTYPE_FD || s->type == FTYPE_CD) {
536 raw_s->open_flags |= O_NONBLOCK;
537 }
538
eeb6b45d
JC
539 raw_parse_flags(state->flags, &raw_s->open_flags);
540
541 raw_s->fd = -1;
542
fdf263f6 543 int fcntl_flags = O_APPEND | O_NONBLOCK;
eeb6b45d
JC
544#ifdef O_NOATIME
545 fcntl_flags |= O_NOATIME;
546#endif
547
fdf263f6
AF
548#ifdef O_ASYNC
549 /* Not all operating systems have O_ASYNC, and those that don't
550 * will not let us track the state into raw_s->open_flags (typically
551 * you achieve the same effect with an ioctl, for example I_SETSIG
552 * on Solaris). But we do not use O_ASYNC, so that's fine.
553 */
554 assert((s->open_flags & O_ASYNC) == 0);
555#endif
556
eeb6b45d
JC
557 if ((raw_s->open_flags & ~fcntl_flags) == (s->open_flags & ~fcntl_flags)) {
558 /* dup the original fd */
559 /* TODO: use qemu fcntl wrapper */
560#ifdef F_DUPFD_CLOEXEC
561 raw_s->fd = fcntl(s->fd, F_DUPFD_CLOEXEC, 0);
562#else
563 raw_s->fd = dup(s->fd);
564 if (raw_s->fd != -1) {
565 qemu_set_cloexec(raw_s->fd);
566 }
567#endif
568 if (raw_s->fd >= 0) {
569 ret = fcntl_setfl(raw_s->fd, raw_s->open_flags);
570 if (ret) {
571 qemu_close(raw_s->fd);
572 raw_s->fd = -1;
573 }
574 }
575 }
576
577 /* If we cannot use fcntl, or fcntl failed, fall back to qemu_open() */
578 if (raw_s->fd == -1) {
579 assert(!(raw_s->open_flags & O_CREAT));
580 raw_s->fd = qemu_open(state->bs->filename, raw_s->open_flags);
581 if (raw_s->fd == -1) {
e428e439 582 error_setg_errno(errp, errno, "Could not reopen file");
eeb6b45d
JC
583 ret = -1;
584 }
585 }
df26a350
KW
586
587 /* Fail already reopen_prepare() if we can't get a working O_DIRECT
588 * alignment with the new fd. */
589 if (raw_s->fd != -1) {
590 raw_probe_alignment(state->bs, raw_s->fd, &local_err);
591 if (local_err) {
592 qemu_close(raw_s->fd);
593 raw_s->fd = -1;
594 error_propagate(errp, local_err);
595 ret = -EINVAL;
596 }
597 }
598
eeb6b45d
JC
599 return ret;
600}
601
eeb6b45d
JC
602static void raw_reopen_commit(BDRVReopenState *state)
603{
604 BDRVRawReopenState *raw_s = state->opaque;
605 BDRVRawState *s = state->bs->opaque;
606
607 s->open_flags = raw_s->open_flags;
608
609 qemu_close(s->fd);
610 s->fd = raw_s->fd;
611#ifdef CONFIG_LINUX_AIO
612 s->use_aio = raw_s->use_aio;
613#endif
614
615 g_free(state->opaque);
616 state->opaque = NULL;
617}
618
619
620static void raw_reopen_abort(BDRVReopenState *state)
621{
622 BDRVRawReopenState *raw_s = state->opaque;
623
624 /* nothing to do if NULL, we didn't get far enough */
625 if (raw_s == NULL) {
626 return;
627 }
628
629 if (raw_s->fd >= 0) {
630 qemu_close(raw_s->fd);
631 raw_s->fd = -1;
632 }
633 g_free(state->opaque);
634 state->opaque = NULL;
635}
636
3baca891 637static void raw_refresh_limits(BlockDriverState *bs, Error **errp)
c25f53b0
PB
638{
639 BDRVRawState *s = bs->opaque;
eeb6b45d 640
df26a350 641 raw_probe_alignment(bs, s->fd, errp);
c25f53b0 642 bs->bl.opt_mem_alignment = s->buf_align;
c25f53b0 643}
83f64091 644
de81a169
PB
645static ssize_t handle_aiocb_ioctl(RawPosixAIOData *aiocb)
646{
647 int ret;
648
649 ret = ioctl(aiocb->aio_fildes, aiocb->aio_ioctl_cmd, aiocb->aio_ioctl_buf);
650 if (ret == -1) {
651 return -errno;
652 }
653
b608c8dc 654 return 0;
de81a169
PB
655}
656
657static ssize_t handle_aiocb_flush(RawPosixAIOData *aiocb)
658{
659 int ret;
660
661 ret = qemu_fdatasync(aiocb->aio_fildes);
662 if (ret == -1) {
663 return -errno;
664 }
665 return 0;
666}
667
668#ifdef CONFIG_PREADV
669
670static bool preadv_present = true;
671
672static ssize_t
673qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset)
674{
675 return preadv(fd, iov, nr_iov, offset);
676}
677
678static ssize_t
679qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset)
680{
681 return pwritev(fd, iov, nr_iov, offset);
682}
683
684#else
685
686static bool preadv_present = false;
687
688static ssize_t
689qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset)
690{
691 return -ENOSYS;
692}
693
694static ssize_t
695qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset)
696{
697 return -ENOSYS;
698}
699
700#endif
701
702static ssize_t handle_aiocb_rw_vector(RawPosixAIOData *aiocb)
703{
704 ssize_t len;
705
706 do {
707 if (aiocb->aio_type & QEMU_AIO_WRITE)
708 len = qemu_pwritev(aiocb->aio_fildes,
709 aiocb->aio_iov,
710 aiocb->aio_niov,
711 aiocb->aio_offset);
712 else
713 len = qemu_preadv(aiocb->aio_fildes,
714 aiocb->aio_iov,
715 aiocb->aio_niov,
716 aiocb->aio_offset);
717 } while (len == -1 && errno == EINTR);
718
719 if (len == -1) {
720 return -errno;
721 }
722 return len;
723}
724
725/*
726 * Read/writes the data to/from a given linear buffer.
727 *
728 * Returns the number of bytes handles or -errno in case of an error. Short
729 * reads are only returned if the end of the file is reached.
730 */
731static ssize_t handle_aiocb_rw_linear(RawPosixAIOData *aiocb, char *buf)
732{
733 ssize_t offset = 0;
734 ssize_t len;
735
736 while (offset < aiocb->aio_nbytes) {
737 if (aiocb->aio_type & QEMU_AIO_WRITE) {
738 len = pwrite(aiocb->aio_fildes,
739 (const char *)buf + offset,
740 aiocb->aio_nbytes - offset,
741 aiocb->aio_offset + offset);
742 } else {
743 len = pread(aiocb->aio_fildes,
744 buf + offset,
745 aiocb->aio_nbytes - offset,
746 aiocb->aio_offset + offset);
747 }
748 if (len == -1 && errno == EINTR) {
749 continue;
750 } else if (len == -1) {
751 offset = -errno;
752 break;
753 } else if (len == 0) {
754 break;
755 }
756 offset += len;
757 }
758
759 return offset;
760}
761
762static ssize_t handle_aiocb_rw(RawPosixAIOData *aiocb)
763{
764 ssize_t nbytes;
765 char *buf;
766
767 if (!(aiocb->aio_type & QEMU_AIO_MISALIGNED)) {
768 /*
769 * If there is just a single buffer, and it is properly aligned
770 * we can just use plain pread/pwrite without any problems.
771 */
772 if (aiocb->aio_niov == 1) {
773 return handle_aiocb_rw_linear(aiocb, aiocb->aio_iov->iov_base);
774 }
775 /*
776 * We have more than one iovec, and all are properly aligned.
777 *
778 * Try preadv/pwritev first and fall back to linearizing the
779 * buffer if it's not supported.
780 */
781 if (preadv_present) {
782 nbytes = handle_aiocb_rw_vector(aiocb);
783 if (nbytes == aiocb->aio_nbytes ||
784 (nbytes < 0 && nbytes != -ENOSYS)) {
785 return nbytes;
786 }
787 preadv_present = false;
788 }
789
790 /*
791 * XXX(hch): short read/write. no easy way to handle the reminder
792 * using these interfaces. For now retry using plain
793 * pread/pwrite?
794 */
795 }
796
797 /*
798 * Ok, we have to do it the hard way, copy all segments into
799 * a single aligned buffer.
800 */
801 buf = qemu_blockalign(aiocb->bs, aiocb->aio_nbytes);
802 if (aiocb->aio_type & QEMU_AIO_WRITE) {
803 char *p = buf;
804 int i;
805
806 for (i = 0; i < aiocb->aio_niov; ++i) {
807 memcpy(p, aiocb->aio_iov[i].iov_base, aiocb->aio_iov[i].iov_len);
808 p += aiocb->aio_iov[i].iov_len;
809 }
8eb029c2 810 assert(p - buf == aiocb->aio_nbytes);
de81a169
PB
811 }
812
813 nbytes = handle_aiocb_rw_linear(aiocb, buf);
814 if (!(aiocb->aio_type & QEMU_AIO_WRITE)) {
815 char *p = buf;
816 size_t count = aiocb->aio_nbytes, copy;
817 int i;
818
819 for (i = 0; i < aiocb->aio_niov && count; ++i) {
820 copy = count;
821 if (copy > aiocb->aio_iov[i].iov_len) {
822 copy = aiocb->aio_iov[i].iov_len;
823 }
824 memcpy(aiocb->aio_iov[i].iov_base, p, copy);
8eb029c2 825 assert(count >= copy);
de81a169
PB
826 p += copy;
827 count -= copy;
828 }
8eb029c2 829 assert(count == 0);
de81a169
PB
830 }
831 qemu_vfree(buf);
832
833 return nbytes;
834}
835
8238010b 836#ifdef CONFIG_XFS
97a2ae34
PB
837static int xfs_write_zeroes(BDRVRawState *s, int64_t offset, uint64_t bytes)
838{
839 struct xfs_flock64 fl;
840
841 memset(&fl, 0, sizeof(fl));
842 fl.l_whence = SEEK_SET;
843 fl.l_start = offset;
844 fl.l_len = bytes;
845
846 if (xfsctl(NULL, s->fd, XFS_IOC_ZERO_RANGE, &fl) < 0) {
847 DEBUG_BLOCK_PRINT("cannot write zero range (%s)\n", strerror(errno));
848 return -errno;
849 }
850
851 return 0;
852}
853
8238010b
PB
854static int xfs_discard(BDRVRawState *s, int64_t offset, uint64_t bytes)
855{
856 struct xfs_flock64 fl;
857
858 memset(&fl, 0, sizeof(fl));
859 fl.l_whence = SEEK_SET;
860 fl.l_start = offset;
861 fl.l_len = bytes;
862
863 if (xfsctl(NULL, s->fd, XFS_IOC_UNRESVSP64, &fl) < 0) {
864 DEBUG_BLOCK_PRINT("cannot punch hole (%s)\n", strerror(errno));
865 return -errno;
866 }
867
868 return 0;
869}
870#endif
871
97a2ae34
PB
872static ssize_t handle_aiocb_write_zeroes(RawPosixAIOData *aiocb)
873{
874 int ret = -EOPNOTSUPP;
875 BDRVRawState *s = aiocb->bs->opaque;
876
877 if (s->has_write_zeroes == 0) {
878 return -ENOTSUP;
879 }
880
881 if (aiocb->aio_type & QEMU_AIO_BLKDEV) {
882#ifdef BLKZEROOUT
883 do {
884 uint64_t range[2] = { aiocb->aio_offset, aiocb->aio_nbytes };
885 if (ioctl(aiocb->aio_fildes, BLKZEROOUT, range) == 0) {
886 return 0;
887 }
888 } while (errno == EINTR);
889
890 ret = -errno;
891#endif
892 } else {
893#ifdef CONFIG_XFS
894 if (s->is_xfs) {
895 return xfs_write_zeroes(s, aiocb->aio_offset, aiocb->aio_nbytes);
896 }
897#endif
898 }
899
900 if (ret == -ENODEV || ret == -ENOSYS || ret == -EOPNOTSUPP ||
901 ret == -ENOTTY) {
902 s->has_write_zeroes = false;
903 ret = -ENOTSUP;
904 }
905 return ret;
906}
907
8238010b
PB
908static ssize_t handle_aiocb_discard(RawPosixAIOData *aiocb)
909{
910 int ret = -EOPNOTSUPP;
911 BDRVRawState *s = aiocb->bs->opaque;
912
7ce21016
PB
913 if (!s->has_discard) {
914 return -ENOTSUP;
8238010b
PB
915 }
916
917 if (aiocb->aio_type & QEMU_AIO_BLKDEV) {
918#ifdef BLKDISCARD
919 do {
920 uint64_t range[2] = { aiocb->aio_offset, aiocb->aio_nbytes };
921 if (ioctl(aiocb->aio_fildes, BLKDISCARD, range) == 0) {
922 return 0;
923 }
924 } while (errno == EINTR);
925
926 ret = -errno;
927#endif
928 } else {
929#ifdef CONFIG_XFS
930 if (s->is_xfs) {
931 return xfs_discard(s, aiocb->aio_offset, aiocb->aio_nbytes);
932 }
933#endif
934
935#ifdef CONFIG_FALLOCATE_PUNCH_HOLE
936 do {
937 if (fallocate(s->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
938 aiocb->aio_offset, aiocb->aio_nbytes) == 0) {
939 return 0;
940 }
941 } while (errno == EINTR);
942
943 ret = -errno;
944#endif
945 }
946
947 if (ret == -ENODEV || ret == -ENOSYS || ret == -EOPNOTSUPP ||
948 ret == -ENOTTY) {
7ce21016
PB
949 s->has_discard = false;
950 ret = -ENOTSUP;
8238010b
PB
951 }
952 return ret;
953}
954
de81a169
PB
955static int aio_worker(void *arg)
956{
957 RawPosixAIOData *aiocb = arg;
958 ssize_t ret = 0;
959
960 switch (aiocb->aio_type & QEMU_AIO_TYPE_MASK) {
961 case QEMU_AIO_READ:
962 ret = handle_aiocb_rw(aiocb);
963 if (ret >= 0 && ret < aiocb->aio_nbytes && aiocb->bs->growable) {
964 iov_memset(aiocb->aio_iov, aiocb->aio_niov, ret,
965 0, aiocb->aio_nbytes - ret);
966
967 ret = aiocb->aio_nbytes;
968 }
969 if (ret == aiocb->aio_nbytes) {
970 ret = 0;
971 } else if (ret >= 0 && ret < aiocb->aio_nbytes) {
972 ret = -EINVAL;
973 }
974 break;
975 case QEMU_AIO_WRITE:
976 ret = handle_aiocb_rw(aiocb);
977 if (ret == aiocb->aio_nbytes) {
978 ret = 0;
979 } else if (ret >= 0 && ret < aiocb->aio_nbytes) {
980 ret = -EINVAL;
981 }
982 break;
983 case QEMU_AIO_FLUSH:
984 ret = handle_aiocb_flush(aiocb);
985 break;
986 case QEMU_AIO_IOCTL:
987 ret = handle_aiocb_ioctl(aiocb);
988 break;
8238010b
PB
989 case QEMU_AIO_DISCARD:
990 ret = handle_aiocb_discard(aiocb);
991 break;
97a2ae34
PB
992 case QEMU_AIO_WRITE_ZEROES:
993 ret = handle_aiocb_write_zeroes(aiocb);
994 break;
de81a169
PB
995 default:
996 fprintf(stderr, "invalid aio request (0x%x)\n", aiocb->aio_type);
997 ret = -EINVAL;
998 break;
999 }
1000
1001 g_slice_free(RawPosixAIOData, aiocb);
1002 return ret;
1003}
1004
260a82e5
PB
1005static int paio_submit_co(BlockDriverState *bs, int fd,
1006 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
1007 int type)
1008{
1009 RawPosixAIOData *acb = g_slice_new(RawPosixAIOData);
1010 ThreadPool *pool;
1011
1012 acb->bs = bs;
1013 acb->aio_type = type;
1014 acb->aio_fildes = fd;
1015
8eb029c2
KW
1016 acb->aio_nbytes = nb_sectors * BDRV_SECTOR_SIZE;
1017 acb->aio_offset = sector_num * BDRV_SECTOR_SIZE;
1018
260a82e5
PB
1019 if (qiov) {
1020 acb->aio_iov = qiov->iov;
1021 acb->aio_niov = qiov->niov;
8eb029c2 1022 assert(qiov->size == acb->aio_nbytes);
260a82e5 1023 }
260a82e5
PB
1024
1025 trace_paio_submit_co(sector_num, nb_sectors, type);
1026 pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
1027 return thread_pool_submit_co(pool, aio_worker, acb);
1028}
1029
de81a169
PB
1030static BlockDriverAIOCB *paio_submit(BlockDriverState *bs, int fd,
1031 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
1032 BlockDriverCompletionFunc *cb, void *opaque, int type)
1033{
1034 RawPosixAIOData *acb = g_slice_new(RawPosixAIOData);
c4d9d196 1035 ThreadPool *pool;
de81a169
PB
1036
1037 acb->bs = bs;
1038 acb->aio_type = type;
1039 acb->aio_fildes = fd;
1040
8eb029c2
KW
1041 acb->aio_nbytes = nb_sectors * BDRV_SECTOR_SIZE;
1042 acb->aio_offset = sector_num * BDRV_SECTOR_SIZE;
1043
de81a169
PB
1044 if (qiov) {
1045 acb->aio_iov = qiov->iov;
1046 acb->aio_niov = qiov->niov;
8eb029c2 1047 assert(qiov->size == acb->aio_nbytes);
de81a169 1048 }
de81a169
PB
1049
1050 trace_paio_submit(acb, opaque, sector_num, nb_sectors, type);
c4d9d196
SH
1051 pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
1052 return thread_pool_submit_aio(pool, aio_worker, acb, cb, opaque);
de81a169
PB
1053}
1054
9ef91a67
CH
1055static BlockDriverAIOCB *raw_aio_submit(BlockDriverState *bs,
1056 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
1057 BlockDriverCompletionFunc *cb, void *opaque, int type)
83f64091 1058{
ce1a14dc 1059 BDRVRawState *s = bs->opaque;
ce1a14dc 1060
19cb3738
FB
1061 if (fd_open(bs) < 0)
1062 return NULL;
1063
f141eafe
AL
1064 /*
1065 * If O_DIRECT is used the buffer needs to be aligned on a sector
c1ee7d56 1066 * boundary. Check if this is the case or tell the low-level
9ef91a67 1067 * driver that it needs to copy the buffer.
f141eafe 1068 */
9acc5a06 1069 if ((bs->open_flags & BDRV_O_NOCACHE)) {
c53b1c51 1070 if (!bdrv_qiov_is_aligned(bs, qiov)) {
5c6c3a6c 1071 type |= QEMU_AIO_MISALIGNED;
e44bd6fc 1072#ifdef CONFIG_LINUX_AIO
5c6c3a6c
CH
1073 } else if (s->use_aio) {
1074 return laio_submit(bs, s->aio_ctx, s->fd, sector_num, qiov,
e44bd6fc
SW
1075 nb_sectors, cb, opaque, type);
1076#endif
5c6c3a6c 1077 }
9ef91a67 1078 }
f141eafe 1079
1e5b9d2f 1080 return paio_submit(bs, s->fd, sector_num, qiov, nb_sectors,
9ef91a67 1081 cb, opaque, type);
83f64091
FB
1082}
1083
1b3abdcc
ML
1084static void raw_aio_plug(BlockDriverState *bs)
1085{
1086#ifdef CONFIG_LINUX_AIO
1087 BDRVRawState *s = bs->opaque;
1088 if (s->use_aio) {
1089 laio_io_plug(bs, s->aio_ctx);
1090 }
1091#endif
1092}
1093
1094static void raw_aio_unplug(BlockDriverState *bs)
1095{
1096#ifdef CONFIG_LINUX_AIO
1097 BDRVRawState *s = bs->opaque;
1098 if (s->use_aio) {
1099 laio_io_unplug(bs, s->aio_ctx, true);
1100 }
1101#endif
1102}
1103
1104static void raw_aio_flush_io_queue(BlockDriverState *bs)
1105{
1106#ifdef CONFIG_LINUX_AIO
1107 BDRVRawState *s = bs->opaque;
1108 if (s->use_aio) {
1109 laio_io_unplug(bs, s->aio_ctx, false);
1110 }
1111#endif
1112}
1113
f141eafe
AL
1114static BlockDriverAIOCB *raw_aio_readv(BlockDriverState *bs,
1115 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
ce1a14dc 1116 BlockDriverCompletionFunc *cb, void *opaque)
83f64091 1117{
9ef91a67
CH
1118 return raw_aio_submit(bs, sector_num, qiov, nb_sectors,
1119 cb, opaque, QEMU_AIO_READ);
83f64091
FB
1120}
1121
f141eafe
AL
1122static BlockDriverAIOCB *raw_aio_writev(BlockDriverState *bs,
1123 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
ce1a14dc 1124 BlockDriverCompletionFunc *cb, void *opaque)
83f64091 1125{
9ef91a67
CH
1126 return raw_aio_submit(bs, sector_num, qiov, nb_sectors,
1127 cb, opaque, QEMU_AIO_WRITE);
83f64091 1128}
53538725 1129
b2e12bc6
CH
1130static BlockDriverAIOCB *raw_aio_flush(BlockDriverState *bs,
1131 BlockDriverCompletionFunc *cb, void *opaque)
1132{
1133 BDRVRawState *s = bs->opaque;
1134
1135 if (fd_open(bs) < 0)
1136 return NULL;
1137
1e5b9d2f 1138 return paio_submit(bs, s->fd, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH);
b2e12bc6
CH
1139}
1140
83f64091
FB
1141static void raw_close(BlockDriverState *bs)
1142{
1143 BDRVRawState *s = bs->opaque;
c2f3426c
SH
1144
1145 raw_detach_aio_context(bs);
1146
abd269b7
SH
1147#ifdef CONFIG_LINUX_AIO
1148 if (s->use_aio) {
1149 laio_cleanup(s->aio_ctx);
1150 }
1151#endif
19cb3738 1152 if (s->fd >= 0) {
2e1e79da 1153 qemu_close(s->fd);
19cb3738
FB
1154 s->fd = -1;
1155 }
83f64091
FB
1156}
1157
1158static int raw_truncate(BlockDriverState *bs, int64_t offset)
1159{
1160 BDRVRawState *s = bs->opaque;
55b949c8
CH
1161 struct stat st;
1162
1163 if (fstat(s->fd, &st)) {
83f64091 1164 return -errno;
55b949c8
CH
1165 }
1166
1167 if (S_ISREG(st.st_mode)) {
1168 if (ftruncate(s->fd, offset) < 0) {
1169 return -errno;
1170 }
1171 } else if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
1172 if (offset > raw_getlength(bs)) {
1173 return -EINVAL;
1174 }
1175 } else {
1176 return -ENOTSUP;
1177 }
1178
83f64091
FB
1179 return 0;
1180}
1181
128ab2ff
BS
1182#ifdef __OpenBSD__
1183static int64_t raw_getlength(BlockDriverState *bs)
1184{
1185 BDRVRawState *s = bs->opaque;
1186 int fd = s->fd;
1187 struct stat st;
1188
1189 if (fstat(fd, &st))
aa729704 1190 return -errno;
128ab2ff
BS
1191 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
1192 struct disklabel dl;
1193
1194 if (ioctl(fd, DIOCGDINFO, &dl))
aa729704 1195 return -errno;
128ab2ff
BS
1196 return (uint64_t)dl.d_secsize *
1197 dl.d_partitions[DISKPART(st.st_rdev)].p_size;
1198 } else
1199 return st.st_size;
1200}
d1f6fd8d
CE
1201#elif defined(__NetBSD__)
1202static int64_t raw_getlength(BlockDriverState *bs)
1203{
1204 BDRVRawState *s = bs->opaque;
1205 int fd = s->fd;
1206 struct stat st;
1207
1208 if (fstat(fd, &st))
aa729704 1209 return -errno;
d1f6fd8d
CE
1210 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
1211 struct dkwedge_info dkw;
1212
1213 if (ioctl(fd, DIOCGWEDGEINFO, &dkw) != -1) {
1214 return dkw.dkw_size * 512;
1215 } else {
1216 struct disklabel dl;
1217
1218 if (ioctl(fd, DIOCGDINFO, &dl))
aa729704 1219 return -errno;
d1f6fd8d
CE
1220 return (uint64_t)dl.d_secsize *
1221 dl.d_partitions[DISKPART(st.st_rdev)].p_size;
1222 }
1223 } else
1224 return st.st_size;
1225}
50779cc2
CH
1226#elif defined(__sun__)
1227static int64_t raw_getlength(BlockDriverState *bs)
1228{
1229 BDRVRawState *s = bs->opaque;
1230 struct dk_minfo minfo;
1231 int ret;
aa729704 1232 int64_t size;
50779cc2
CH
1233
1234 ret = fd_open(bs);
1235 if (ret < 0) {
1236 return ret;
1237 }
1238
1239 /*
1240 * Use the DKIOCGMEDIAINFO ioctl to read the size.
1241 */
1242 ret = ioctl(s->fd, DKIOCGMEDIAINFO, &minfo);
1243 if (ret != -1) {
1244 return minfo.dki_lbsize * minfo.dki_capacity;
1245 }
1246
1247 /*
1248 * There are reports that lseek on some devices fails, but
1249 * irc discussion said that contingency on contingency was overkill.
1250 */
aa729704
MA
1251 size = lseek(s->fd, 0, SEEK_END);
1252 if (size < 0) {
1253 return -errno;
1254 }
1255 return size;
50779cc2
CH
1256}
1257#elif defined(CONFIG_BSD)
1258static int64_t raw_getlength(BlockDriverState *bs)
83f64091
FB
1259{
1260 BDRVRawState *s = bs->opaque;
1261 int fd = s->fd;
1262 int64_t size;
83f64091 1263 struct stat sb;
a167ba50 1264#if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
9f23011a 1265 int reopened = 0;
83f64091 1266#endif
19cb3738
FB
1267 int ret;
1268
1269 ret = fd_open(bs);
1270 if (ret < 0)
1271 return ret;
83f64091 1272
a167ba50 1273#if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
9f23011a
BS
1274again:
1275#endif
83f64091
FB
1276 if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
1277#ifdef DIOCGMEDIASIZE
1278 if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
c5e97233
BS
1279#elif defined(DIOCGPART)
1280 {
1281 struct partinfo pi;
1282 if (ioctl(fd, DIOCGPART, &pi) == 0)
1283 size = pi.media_size;
1284 else
1285 size = 0;
1286 }
1287 if (size == 0)
83f64091 1288#endif
83affaa6 1289#if defined(__APPLE__) && defined(__MACH__)
675036e4 1290 size = LLONG_MAX;
83f64091
FB
1291#else
1292 size = lseek(fd, 0LL, SEEK_END);
aa729704
MA
1293 if (size < 0) {
1294 return -errno;
1295 }
9f23011a 1296#endif
a167ba50 1297#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
9f23011a
BS
1298 switch(s->type) {
1299 case FTYPE_CD:
1300 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
1301 if (size == 2048LL * (unsigned)-1)
1302 size = 0;
1303 /* XXX no disc? maybe we need to reopen... */
f3a5d3f8 1304 if (size <= 0 && !reopened && cdrom_reopen(bs) >= 0) {
9f23011a
BS
1305 reopened = 1;
1306 goto again;
1307 }
1308 }
83f64091 1309#endif
50779cc2 1310 } else {
83f64091 1311 size = lseek(fd, 0, SEEK_END);
aa729704
MA
1312 if (size < 0) {
1313 return -errno;
1314 }
83f64091 1315 }
83f64091
FB
1316 return size;
1317}
50779cc2
CH
1318#else
1319static int64_t raw_getlength(BlockDriverState *bs)
1320{
1321 BDRVRawState *s = bs->opaque;
1322 int ret;
aa729704 1323 int64_t size;
50779cc2
CH
1324
1325 ret = fd_open(bs);
1326 if (ret < 0) {
1327 return ret;
1328 }
1329
aa729704
MA
1330 size = lseek(s->fd, 0, SEEK_END);
1331 if (size < 0) {
1332 return -errno;
1333 }
1334 return size;
50779cc2 1335}
128ab2ff 1336#endif
83f64091 1337
4a1d5e1f
FZ
1338static int64_t raw_get_allocated_file_size(BlockDriverState *bs)
1339{
1340 struct stat st;
1341 BDRVRawState *s = bs->opaque;
1342
1343 if (fstat(s->fd, &st) < 0) {
1344 return -errno;
1345 }
1346 return (int64_t)st.st_blocks * 512;
1347}
1348
6f482f74 1349static int raw_create(const char *filename, QemuOpts *opts, Error **errp)
83f64091
FB
1350{
1351 int fd;
1e37d059 1352 int result = 0;
0e7e1989 1353 int64_t total_size = 0;
4ab15590 1354 bool nocow = false;
83f64091 1355
464d9f64
HR
1356 strstart(filename, "file:", &filename);
1357
0e7e1989 1358 /* Read out options */
6f482f74
CL
1359 total_size =
1360 qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0) / BDRV_SECTOR_SIZE;
4ab15590 1361 nocow = qemu_opt_get_bool(opts, BLOCK_OPT_NOCOW, false);
83f64091 1362
6165f4d8
CB
1363 fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
1364 0644);
1e37d059
SW
1365 if (fd < 0) {
1366 result = -errno;
e428e439 1367 error_setg_errno(errp, -result, "Could not create file");
1e37d059 1368 } else {
4ab15590
CL
1369 if (nocow) {
1370#ifdef __linux__
1371 /* Set NOCOW flag to solve performance issue on fs like btrfs.
1372 * This is an optimisation. The FS_IOC_SETFLAGS ioctl return value
1373 * will be ignored since any failure of this operation should not
1374 * block the left work.
1375 */
1376 int attr;
1377 if (ioctl(fd, FS_IOC_GETFLAGS, &attr) == 0) {
1378 attr |= FS_NOCOW_FL;
1379 ioctl(fd, FS_IOC_SETFLAGS, &attr);
1380 }
1381#endif
1382 }
1383
9040385d 1384 if (ftruncate(fd, total_size * BDRV_SECTOR_SIZE) != 0) {
1e37d059 1385 result = -errno;
e428e439 1386 error_setg_errno(errp, -result, "Could not resize file");
1e37d059 1387 }
2e1e79da 1388 if (qemu_close(fd) != 0) {
1e37d059 1389 result = -errno;
e428e439 1390 error_setg_errno(errp, -result, "Could not close the new file");
1e37d059
SW
1391 }
1392 }
1393 return result;
83f64091
FB
1394}
1395
4f11aa8a
HR
1396static int64_t try_fiemap(BlockDriverState *bs, off_t start, off_t *data,
1397 off_t *hole, int nb_sectors, int *pnum)
5500316d 1398{
5500316d 1399#ifdef CONFIG_FIEMAP
94282e71 1400 BDRVRawState *s = bs->opaque;
4f11aa8a 1401 int64_t ret = BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | start;
5500316d
PB
1402 struct {
1403 struct fiemap fm;
1404 struct fiemap_extent fe;
1405 } f;
94282e71 1406
4f11aa8a
HR
1407 if (s->skip_fiemap) {
1408 return -ENOTSUP;
1409 }
1410
5500316d
PB
1411 f.fm.fm_start = start;
1412 f.fm.fm_length = (int64_t)nb_sectors * BDRV_SECTOR_SIZE;
1413 f.fm.fm_flags = 0;
1414 f.fm.fm_extent_count = 1;
1415 f.fm.fm_reserved = 0;
1416 if (ioctl(s->fd, FS_IOC_FIEMAP, &f) == -1) {
4f11aa8a
HR
1417 s->skip_fiemap = true;
1418 return -errno;
5500316d
PB
1419 }
1420
1421 if (f.fm.fm_mapped_extents == 0) {
1422 /* No extents found, data is beyond f.fm.fm_start + f.fm.fm_length.
1423 * f.fm.fm_start + f.fm.fm_length must be clamped to the file size!
1424 */
1425 off_t length = lseek(s->fd, 0, SEEK_END);
4f11aa8a
HR
1426 *hole = f.fm.fm_start;
1427 *data = MIN(f.fm.fm_start + f.fm.fm_length, length);
5500316d 1428 } else {
4f11aa8a
HR
1429 *data = f.fe.fe_logical;
1430 *hole = f.fe.fe_logical + f.fe.fe_length;
f5f7abcf
PB
1431 if (f.fe.fe_flags & FIEMAP_EXTENT_UNWRITTEN) {
1432 ret |= BDRV_BLOCK_ZERO;
1433 }
5500316d 1434 }
94282e71 1435
4f11aa8a
HR
1436 return ret;
1437#else
1438 return -ENOTSUP;
1439#endif
1440}
94282e71 1441
4f11aa8a
HR
1442static int64_t try_seek_hole(BlockDriverState *bs, off_t start, off_t *data,
1443 off_t *hole, int *pnum)
1444{
1445#if defined SEEK_HOLE && defined SEEK_DATA
94282e71
KW
1446 BDRVRawState *s = bs->opaque;
1447
4f11aa8a
HR
1448 *hole = lseek(s->fd, start, SEEK_HOLE);
1449 if (*hole == -1) {
5500316d
PB
1450 /* -ENXIO indicates that sector_num was past the end of the file.
1451 * There is a virtual hole there. */
1452 assert(errno != -ENXIO);
1453
4f11aa8a 1454 return -errno;
5500316d
PB
1455 }
1456
4f11aa8a
HR
1457 if (*hole > start) {
1458 *data = start;
5500316d
PB
1459 } else {
1460 /* On a hole. We need another syscall to find its end. */
4f11aa8a
HR
1461 *data = lseek(s->fd, start, SEEK_DATA);
1462 if (*data == -1) {
1463 *data = lseek(s->fd, 0, SEEK_END);
5500316d
PB
1464 }
1465 }
4f11aa8a
HR
1466
1467 return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | start;
5500316d 1468#else
4f11aa8a 1469 return -ENOTSUP;
5500316d 1470#endif
4f11aa8a
HR
1471}
1472
1473/*
1474 * Returns true iff the specified sector is present in the disk image. Drivers
1475 * not implementing the functionality are assumed to not support backing files,
1476 * hence all their sectors are reported as allocated.
1477 *
1478 * If 'sector_num' is beyond the end of the disk image the return value is 0
1479 * and 'pnum' is set to 0.
1480 *
1481 * 'pnum' is set to the number of sectors (including and immediately following
1482 * the specified sector) that are known to be in the same
1483 * allocated/unallocated state.
1484 *
1485 * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes
1486 * beyond the end of the disk image it will be clamped.
1487 */
1488static int64_t coroutine_fn raw_co_get_block_status(BlockDriverState *bs,
1489 int64_t sector_num,
1490 int nb_sectors, int *pnum)
1491{
1492 off_t start, data = 0, hole = 0;
1493 int64_t ret;
1494
1495 ret = fd_open(bs);
1496 if (ret < 0) {
1497 return ret;
1498 }
1499
1500 start = sector_num * BDRV_SECTOR_SIZE;
1501
1502 ret = try_fiemap(bs, start, &data, &hole, nb_sectors, pnum);
1503 if (ret < 0) {
1504 ret = try_seek_hole(bs, start, &data, &hole, pnum);
1505 if (ret < 0) {
1506 /* Assume everything is allocated. */
1507 data = 0;
1508 hole = start + nb_sectors * BDRV_SECTOR_SIZE;
1509 ret = BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | start;
1510 }
1511 }
5500316d
PB
1512
1513 if (data <= start) {
1514 /* On a data extent, compute sectors to the end of the extent. */
1515 *pnum = MIN(nb_sectors, (hole - start) / BDRV_SECTOR_SIZE);
5500316d
PB
1516 } else {
1517 /* On a hole, compute sectors to the beginning of the next extent. */
1518 *pnum = MIN(nb_sectors, (data - start) / BDRV_SECTOR_SIZE);
63390a8d
PB
1519 ret &= ~BDRV_BLOCK_DATA;
1520 ret |= BDRV_BLOCK_ZERO;
5500316d 1521 }
63390a8d
PB
1522
1523 return ret;
5500316d
PB
1524}
1525
8238010b
PB
1526static coroutine_fn BlockDriverAIOCB *raw_aio_discard(BlockDriverState *bs,
1527 int64_t sector_num, int nb_sectors,
1528 BlockDriverCompletionFunc *cb, void *opaque)
dce512de 1529{
dce512de
CH
1530 BDRVRawState *s = bs->opaque;
1531
8238010b
PB
1532 return paio_submit(bs, s->fd, sector_num, NULL, nb_sectors,
1533 cb, opaque, QEMU_AIO_DISCARD);
dce512de 1534}
0e7e1989 1535
260a82e5
PB
1536static int coroutine_fn raw_co_write_zeroes(
1537 BlockDriverState *bs, int64_t sector_num,
1538 int nb_sectors, BdrvRequestFlags flags)
1539{
1540 BDRVRawState *s = bs->opaque;
1541
1542 if (!(flags & BDRV_REQ_MAY_UNMAP)) {
97a2ae34
PB
1543 return paio_submit_co(bs, s->fd, sector_num, NULL, nb_sectors,
1544 QEMU_AIO_WRITE_ZEROES);
1545 } else if (s->discard_zeroes) {
1546 return paio_submit_co(bs, s->fd, sector_num, NULL, nb_sectors,
1547 QEMU_AIO_DISCARD);
260a82e5 1548 }
97a2ae34 1549 return -ENOTSUP;
260a82e5
PB
1550}
1551
1552static int raw_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1553{
1554 BDRVRawState *s = bs->opaque;
1555
1556 bdi->unallocated_blocks_are_zero = s->discard_zeroes;
1557 bdi->can_write_zeroes_with_unmap = s->discard_zeroes;
1558 return 0;
1559}
1560
6f482f74
CL
1561static QemuOptsList raw_create_opts = {
1562 .name = "raw-create-opts",
1563 .head = QTAILQ_HEAD_INITIALIZER(raw_create_opts.head),
1564 .desc = {
1565 {
1566 .name = BLOCK_OPT_SIZE,
1567 .type = QEMU_OPT_SIZE,
1568 .help = "Virtual disk size"
1569 },
4ab15590
CL
1570 {
1571 .name = BLOCK_OPT_NOCOW,
1572 .type = QEMU_OPT_BOOL,
1573 .help = "Turn off copy-on-write (valid only on btrfs)"
1574 },
6f482f74
CL
1575 { /* end of list */ }
1576 }
0e7e1989
KW
1577};
1578
84a12e66
CH
1579static BlockDriver bdrv_file = {
1580 .format_name = "file",
1581 .protocol_name = "file",
856ae5c3 1582 .instance_size = sizeof(BDRVRawState),
030be321 1583 .bdrv_needs_filename = true,
856ae5c3 1584 .bdrv_probe = NULL, /* no probe for protocols */
078896a9 1585 .bdrv_parse_filename = raw_parse_filename,
66f82cee 1586 .bdrv_file_open = raw_open,
eeb6b45d
JC
1587 .bdrv_reopen_prepare = raw_reopen_prepare,
1588 .bdrv_reopen_commit = raw_reopen_commit,
1589 .bdrv_reopen_abort = raw_reopen_abort,
856ae5c3 1590 .bdrv_close = raw_close,
c282e1fd 1591 .bdrv_create = raw_create,
3ac21627 1592 .bdrv_has_zero_init = bdrv_has_zero_init_1,
b6b8a333 1593 .bdrv_co_get_block_status = raw_co_get_block_status,
260a82e5 1594 .bdrv_co_write_zeroes = raw_co_write_zeroes,
3b46e624 1595
f141eafe
AL
1596 .bdrv_aio_readv = raw_aio_readv,
1597 .bdrv_aio_writev = raw_aio_writev,
b2e12bc6 1598 .bdrv_aio_flush = raw_aio_flush,
8238010b 1599 .bdrv_aio_discard = raw_aio_discard,
c25f53b0 1600 .bdrv_refresh_limits = raw_refresh_limits,
1b3abdcc
ML
1601 .bdrv_io_plug = raw_aio_plug,
1602 .bdrv_io_unplug = raw_aio_unplug,
1603 .bdrv_flush_io_queue = raw_aio_flush_io_queue,
3c529d93 1604
83f64091
FB
1605 .bdrv_truncate = raw_truncate,
1606 .bdrv_getlength = raw_getlength,
260a82e5 1607 .bdrv_get_info = raw_get_info,
4a1d5e1f
FZ
1608 .bdrv_get_allocated_file_size
1609 = raw_get_allocated_file_size,
0e7e1989 1610
c2f3426c
SH
1611 .bdrv_detach_aio_context = raw_detach_aio_context,
1612 .bdrv_attach_aio_context = raw_attach_aio_context,
1613
6f482f74 1614 .create_opts = &raw_create_opts,
83f64091
FB
1615};
1616
19cb3738
FB
1617/***********************************************/
1618/* host device */
1619
83affaa6 1620#if defined(__APPLE__) && defined(__MACH__)
19cb3738
FB
1621static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
1622static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
1623
1624kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
1625{
5fafdf24 1626 kern_return_t kernResult;
19cb3738
FB
1627 mach_port_t masterPort;
1628 CFMutableDictionaryRef classesToMatch;
1629
1630 kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
1631 if ( KERN_SUCCESS != kernResult ) {
1632 printf( "IOMasterPort returned %d\n", kernResult );
1633 }
3b46e624 1634
5fafdf24 1635 classesToMatch = IOServiceMatching( kIOCDMediaClass );
19cb3738
FB
1636 if ( classesToMatch == NULL ) {
1637 printf( "IOServiceMatching returned a NULL dictionary.\n" );
1638 } else {
1639 CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
1640 }
1641 kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
1642 if ( KERN_SUCCESS != kernResult )
1643 {
1644 printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
1645 }
3b46e624 1646
19cb3738
FB
1647 return kernResult;
1648}
1649
1650kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
1651{
1652 io_object_t nextMedia;
1653 kern_return_t kernResult = KERN_FAILURE;
1654 *bsdPath = '\0';
1655 nextMedia = IOIteratorNext( mediaIterator );
1656 if ( nextMedia )
1657 {
1658 CFTypeRef bsdPathAsCFString;
1659 bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
1660 if ( bsdPathAsCFString ) {
1661 size_t devPathLength;
1662 strcpy( bsdPath, _PATH_DEV );
1663 strcat( bsdPath, "r" );
1664 devPathLength = strlen( bsdPath );
1665 if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
1666 kernResult = KERN_SUCCESS;
1667 }
1668 CFRelease( bsdPathAsCFString );
1669 }
1670 IOObjectRelease( nextMedia );
1671 }
3b46e624 1672
19cb3738
FB
1673 return kernResult;
1674}
1675
1676#endif
1677
508c7cb3
CH
1678static int hdev_probe_device(const char *filename)
1679{
1680 struct stat st;
1681
1682 /* allow a dedicated CD-ROM driver to match with a higher priority */
1683 if (strstart(filename, "/dev/cdrom", NULL))
1684 return 50;
1685
1686 if (stat(filename, &st) >= 0 &&
1687 (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
1688 return 100;
1689 }
1690
1691 return 0;
1692}
1693
da888d37
SH
1694static int check_hdev_writable(BDRVRawState *s)
1695{
1696#if defined(BLKROGET)
1697 /* Linux block devices can be configured "read-only" using blockdev(8).
1698 * This is independent of device node permissions and therefore open(2)
1699 * with O_RDWR succeeds. Actual writes fail with EPERM.
1700 *
1701 * bdrv_open() is supposed to fail if the disk is read-only. Explicitly
1702 * check for read-only block devices so that Linux block devices behave
1703 * properly.
1704 */
1705 struct stat st;
1706 int readonly = 0;
1707
1708 if (fstat(s->fd, &st)) {
1709 return -errno;
1710 }
1711
1712 if (!S_ISBLK(st.st_mode)) {
1713 return 0;
1714 }
1715
1716 if (ioctl(s->fd, BLKROGET, &readonly) < 0) {
1717 return -errno;
1718 }
1719
1720 if (readonly) {
1721 return -EACCES;
1722 }
1723#endif /* defined(BLKROGET) */
1724 return 0;
1725}
1726
7af803d4
HR
1727static void hdev_parse_filename(const char *filename, QDict *options,
1728 Error **errp)
1729{
1730 /* The prefix is optional, just as for "file". */
1731 strstart(filename, "host_device:", &filename);
1732
1733 qdict_put_obj(options, "filename", QOBJECT(qstring_from_str(filename)));
1734}
1735
015a1036
HR
1736static int hdev_open(BlockDriverState *bs, QDict *options, int flags,
1737 Error **errp)
19cb3738
FB
1738{
1739 BDRVRawState *s = bs->opaque;
e428e439 1740 Error *local_err = NULL;
da888d37 1741 int ret;
c66a6157 1742 const char *filename = qdict_get_str(options, "filename");
a76bab49 1743
83affaa6 1744#if defined(__APPLE__) && defined(__MACH__)
19cb3738
FB
1745 if (strstart(filename, "/dev/cdrom", NULL)) {
1746 kern_return_t kernResult;
1747 io_iterator_t mediaIterator;
1748 char bsdPath[ MAXPATHLEN ];
1749 int fd;
5fafdf24 1750
19cb3738
FB
1751 kernResult = FindEjectableCDMedia( &mediaIterator );
1752 kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
3b46e624 1753
19cb3738
FB
1754 if ( bsdPath[ 0 ] != '\0' ) {
1755 strcat(bsdPath,"s0");
1756 /* some CDs don't have a partition 0 */
6165f4d8 1757 fd = qemu_open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
19cb3738
FB
1758 if (fd < 0) {
1759 bsdPath[strlen(bsdPath)-1] = '1';
1760 } else {
2e1e79da 1761 qemu_close(fd);
19cb3738
FB
1762 }
1763 filename = bsdPath;
a5c5ea3f 1764 qdict_put(options, "filename", qstring_from_str(filename));
19cb3738 1765 }
3b46e624 1766
19cb3738
FB
1767 if ( mediaIterator )
1768 IOObjectRelease( mediaIterator );
1769 }
1770#endif
19cb3738
FB
1771
1772 s->type = FTYPE_FILE;
4dd75c70 1773#if defined(__linux__)
05acda4d
BK
1774 {
1775 char resolved_path[ MAXPATHLEN ], *temp;
1776
1777 temp = realpath(filename, resolved_path);
1778 if (temp && strstart(temp, "/dev/sg", NULL)) {
1779 bs->sg = 1;
1780 }
19cb3738
FB
1781 }
1782#endif
90babde0 1783
e428e439 1784 ret = raw_open_common(bs, options, flags, 0, &local_err);
da888d37 1785 if (ret < 0) {
84d18f06 1786 if (local_err) {
e428e439
HR
1787 error_propagate(errp, local_err);
1788 }
da888d37
SH
1789 return ret;
1790 }
1791
1792 if (flags & BDRV_O_RDWR) {
1793 ret = check_hdev_writable(s);
1794 if (ret < 0) {
1795 raw_close(bs);
e428e439 1796 error_setg_errno(errp, -ret, "The device is not writable");
da888d37
SH
1797 return ret;
1798 }
1799 }
1800
1801 return ret;
19cb3738
FB
1802}
1803
03ff3ca3 1804#if defined(__linux__)
19cb3738
FB
1805/* Note: we do not have a reliable method to detect if the floppy is
1806 present. The current method is to try to open the floppy at every
1807 I/O and to keep it opened during a few hundreds of ms. */
1808static int fd_open(BlockDriverState *bs)
1809{
1810 BDRVRawState *s = bs->opaque;
1811 int last_media_present;
1812
1813 if (s->type != FTYPE_FD)
1814 return 0;
1815 last_media_present = (s->fd >= 0);
5fafdf24 1816 if (s->fd >= 0 &&
c57c846a 1817 (get_clock() - s->fd_open_time) >= FD_OPEN_TIMEOUT) {
2e1e79da 1818 qemu_close(s->fd);
19cb3738
FB
1819 s->fd = -1;
1820#ifdef DEBUG_FLOPPY
1821 printf("Floppy closed\n");
1822#endif
1823 }
1824 if (s->fd < 0) {
5fafdf24 1825 if (s->fd_got_error &&
c57c846a 1826 (get_clock() - s->fd_error_time) < FD_OPEN_TIMEOUT) {
19cb3738
FB
1827#ifdef DEBUG_FLOPPY
1828 printf("No floppy (open delayed)\n");
1829#endif
1830 return -EIO;
1831 }
6165f4d8 1832 s->fd = qemu_open(bs->filename, s->open_flags & ~O_NONBLOCK);
19cb3738 1833 if (s->fd < 0) {
c57c846a 1834 s->fd_error_time = get_clock();
19cb3738
FB
1835 s->fd_got_error = 1;
1836 if (last_media_present)
1837 s->fd_media_changed = 1;
1838#ifdef DEBUG_FLOPPY
1839 printf("No floppy\n");
1840#endif
1841 return -EIO;
1842 }
1843#ifdef DEBUG_FLOPPY
1844 printf("Floppy opened\n");
1845#endif
1846 }
1847 if (!last_media_present)
1848 s->fd_media_changed = 1;
c57c846a 1849 s->fd_open_time = get_clock();
19cb3738
FB
1850 s->fd_got_error = 0;
1851 return 0;
1852}
19cb3738 1853
63ec93db 1854static int hdev_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
985a03b0
TS
1855{
1856 BDRVRawState *s = bs->opaque;
1857
1858 return ioctl(s->fd, req, buf);
1859}
221f715d 1860
63ec93db 1861static BlockDriverAIOCB *hdev_aio_ioctl(BlockDriverState *bs,
221f715d
AL
1862 unsigned long int req, void *buf,
1863 BlockDriverCompletionFunc *cb, void *opaque)
1864{
f141eafe 1865 BDRVRawState *s = bs->opaque;
c208e8c2 1866 RawPosixAIOData *acb;
c4d9d196 1867 ThreadPool *pool;
221f715d 1868
f141eafe
AL
1869 if (fd_open(bs) < 0)
1870 return NULL;
c208e8c2
PB
1871
1872 acb = g_slice_new(RawPosixAIOData);
1873 acb->bs = bs;
1874 acb->aio_type = QEMU_AIO_IOCTL;
1875 acb->aio_fildes = s->fd;
1876 acb->aio_offset = 0;
1877 acb->aio_ioctl_buf = buf;
1878 acb->aio_ioctl_cmd = req;
c4d9d196
SH
1879 pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
1880 return thread_pool_submit_aio(pool, aio_worker, acb, cb, opaque);
221f715d
AL
1881}
1882
a167ba50 1883#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
9f23011a
BS
1884static int fd_open(BlockDriverState *bs)
1885{
1886 BDRVRawState *s = bs->opaque;
1887
1888 /* this is just to ensure s->fd is sane (its called by io ops) */
1889 if (s->fd >= 0)
1890 return 0;
1891 return -EIO;
1892}
9f23011a 1893#else /* !linux && !FreeBSD */
19cb3738 1894
08af02e2
AL
1895static int fd_open(BlockDriverState *bs)
1896{
1897 return 0;
1898}
1899
221f715d 1900#endif /* !linux && !FreeBSD */
04eeb8b6 1901
c36dd8a0
AF
1902static coroutine_fn BlockDriverAIOCB *hdev_aio_discard(BlockDriverState *bs,
1903 int64_t sector_num, int nb_sectors,
1904 BlockDriverCompletionFunc *cb, void *opaque)
1905{
1906 BDRVRawState *s = bs->opaque;
1907
1908 if (fd_open(bs) < 0) {
1909 return NULL;
1910 }
1911 return paio_submit(bs, s->fd, sector_num, NULL, nb_sectors,
1912 cb, opaque, QEMU_AIO_DISCARD|QEMU_AIO_BLKDEV);
1913}
1914
d0b4503e
PB
1915static coroutine_fn int hdev_co_write_zeroes(BlockDriverState *bs,
1916 int64_t sector_num, int nb_sectors, BdrvRequestFlags flags)
1917{
1918 BDRVRawState *s = bs->opaque;
1919 int rc;
1920
1921 rc = fd_open(bs);
1922 if (rc < 0) {
1923 return rc;
1924 }
1925 if (!(flags & BDRV_REQ_MAY_UNMAP)) {
97a2ae34
PB
1926 return paio_submit_co(bs, s->fd, sector_num, NULL, nb_sectors,
1927 QEMU_AIO_WRITE_ZEROES|QEMU_AIO_BLKDEV);
1928 } else if (s->discard_zeroes) {
1929 return paio_submit_co(bs, s->fd, sector_num, NULL, nb_sectors,
1930 QEMU_AIO_DISCARD|QEMU_AIO_BLKDEV);
d0b4503e 1931 }
97a2ae34 1932 return -ENOTSUP;
d0b4503e
PB
1933}
1934
6f482f74 1935static int hdev_create(const char *filename, QemuOpts *opts,
d5124c00 1936 Error **errp)
93c65b47
AL
1937{
1938 int fd;
1939 int ret = 0;
1940 struct stat stat_buf;
0e7e1989 1941 int64_t total_size = 0;
cc28c6aa
HR
1942 bool has_prefix;
1943
1944 /* This function is used by all three protocol block drivers and therefore
1945 * any of these three prefixes may be given.
1946 * The return value has to be stored somewhere, otherwise this is an error
1947 * due to -Werror=unused-value. */
1948 has_prefix =
1949 strstart(filename, "host_device:", &filename) ||
1950 strstart(filename, "host_cdrom:" , &filename) ||
1951 strstart(filename, "host_floppy:", &filename);
1952
1953 (void)has_prefix;
93c65b47 1954
0e7e1989 1955 /* Read out options */
6f482f74
CL
1956 total_size =
1957 qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0) / BDRV_SECTOR_SIZE;
93c65b47 1958
6165f4d8 1959 fd = qemu_open(filename, O_WRONLY | O_BINARY);
e428e439
HR
1960 if (fd < 0) {
1961 ret = -errno;
1962 error_setg_errno(errp, -ret, "Could not open device");
1963 return ret;
1964 }
93c65b47 1965
e428e439 1966 if (fstat(fd, &stat_buf) < 0) {
57e69b7d 1967 ret = -errno;
e428e439
HR
1968 error_setg_errno(errp, -ret, "Could not stat device");
1969 } else if (!S_ISBLK(stat_buf.st_mode) && !S_ISCHR(stat_buf.st_mode)) {
1970 error_setg(errp,
1971 "The given file is neither a block nor a character device");
57e69b7d 1972 ret = -ENODEV;
e428e439
HR
1973 } else if (lseek(fd, 0, SEEK_END) < total_size * BDRV_SECTOR_SIZE) {
1974 error_setg(errp, "Device is too small");
93c65b47 1975 ret = -ENOSPC;
e428e439 1976 }
93c65b47 1977
2e1e79da 1978 qemu_close(fd);
93c65b47
AL
1979 return ret;
1980}
1981
5efa9d5a 1982static BlockDriver bdrv_host_device = {
0b4ce02e 1983 .format_name = "host_device",
84a12e66 1984 .protocol_name = "host_device",
0b4ce02e 1985 .instance_size = sizeof(BDRVRawState),
030be321 1986 .bdrv_needs_filename = true,
0b4ce02e 1987 .bdrv_probe_device = hdev_probe_device,
7af803d4 1988 .bdrv_parse_filename = hdev_parse_filename,
66f82cee 1989 .bdrv_file_open = hdev_open,
0b4ce02e 1990 .bdrv_close = raw_close,
1bc6b705
JC
1991 .bdrv_reopen_prepare = raw_reopen_prepare,
1992 .bdrv_reopen_commit = raw_reopen_commit,
1993 .bdrv_reopen_abort = raw_reopen_abort,
c282e1fd 1994 .bdrv_create = hdev_create,
6f482f74 1995 .create_opts = &raw_create_opts,
d0b4503e 1996 .bdrv_co_write_zeroes = hdev_co_write_zeroes,
3b46e624 1997
f141eafe
AL
1998 .bdrv_aio_readv = raw_aio_readv,
1999 .bdrv_aio_writev = raw_aio_writev,
b2e12bc6 2000 .bdrv_aio_flush = raw_aio_flush,
8238010b 2001 .bdrv_aio_discard = hdev_aio_discard,
c25f53b0 2002 .bdrv_refresh_limits = raw_refresh_limits,
1b3abdcc
ML
2003 .bdrv_io_plug = raw_aio_plug,
2004 .bdrv_io_unplug = raw_aio_unplug,
2005 .bdrv_flush_io_queue = raw_aio_flush_io_queue,
3c529d93 2006
55b949c8 2007 .bdrv_truncate = raw_truncate,
e60f469c 2008 .bdrv_getlength = raw_getlength,
260a82e5 2009 .bdrv_get_info = raw_get_info,
4a1d5e1f
FZ
2010 .bdrv_get_allocated_file_size
2011 = raw_get_allocated_file_size,
19cb3738 2012
c2f3426c
SH
2013 .bdrv_detach_aio_context = raw_detach_aio_context,
2014 .bdrv_attach_aio_context = raw_attach_aio_context,
2015
f3a5d3f8 2016 /* generic scsi device */
63ec93db
CH
2017#ifdef __linux__
2018 .bdrv_ioctl = hdev_ioctl,
63ec93db
CH
2019 .bdrv_aio_ioctl = hdev_aio_ioctl,
2020#endif
f3a5d3f8
CH
2021};
2022
2023#ifdef __linux__
d3f49845
HR
2024static void floppy_parse_filename(const char *filename, QDict *options,
2025 Error **errp)
2026{
2027 /* The prefix is optional, just as for "file". */
2028 strstart(filename, "host_floppy:", &filename);
2029
2030 qdict_put_obj(options, "filename", QOBJECT(qstring_from_str(filename)));
2031}
2032
015a1036
HR
2033static int floppy_open(BlockDriverState *bs, QDict *options, int flags,
2034 Error **errp)
f3a5d3f8
CH
2035{
2036 BDRVRawState *s = bs->opaque;
e428e439 2037 Error *local_err = NULL;
f3a5d3f8
CH
2038 int ret;
2039
f3a5d3f8 2040 s->type = FTYPE_FD;
f3a5d3f8 2041
19a3da7f 2042 /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
e428e439
HR
2043 ret = raw_open_common(bs, options, flags, O_NONBLOCK, &local_err);
2044 if (ret) {
84d18f06 2045 if (local_err) {
e428e439
HR
2046 error_propagate(errp, local_err);
2047 }
f3a5d3f8 2048 return ret;
e428e439 2049 }
f3a5d3f8
CH
2050
2051 /* close fd so that we can reopen it as needed */
2e1e79da 2052 qemu_close(s->fd);
f3a5d3f8
CH
2053 s->fd = -1;
2054 s->fd_media_changed = 1;
2055
2056 return 0;
2057}
2058
508c7cb3
CH
2059static int floppy_probe_device(const char *filename)
2060{
2ebf7c4b
CR
2061 int fd, ret;
2062 int prio = 0;
2063 struct floppy_struct fdparam;
343f8568 2064 struct stat st;
2ebf7c4b 2065
e1740828
CB
2066 if (strstart(filename, "/dev/fd", NULL) &&
2067 !strstart(filename, "/dev/fdset/", NULL)) {
2ebf7c4b 2068 prio = 50;
e1740828 2069 }
2ebf7c4b 2070
6165f4d8 2071 fd = qemu_open(filename, O_RDONLY | O_NONBLOCK);
2ebf7c4b
CR
2072 if (fd < 0) {
2073 goto out;
2074 }
343f8568
JS
2075 ret = fstat(fd, &st);
2076 if (ret == -1 || !S_ISBLK(st.st_mode)) {
2077 goto outc;
2078 }
2ebf7c4b
CR
2079
2080 /* Attempt to detect via a floppy specific ioctl */
2081 ret = ioctl(fd, FDGETPRM, &fdparam);
2082 if (ret >= 0)
2083 prio = 100;
2084
343f8568 2085outc:
2e1e79da 2086 qemu_close(fd);
2ebf7c4b
CR
2087out:
2088 return prio;
508c7cb3
CH
2089}
2090
2091
f3a5d3f8
CH
2092static int floppy_is_inserted(BlockDriverState *bs)
2093{
2094 return fd_open(bs) >= 0;
2095}
2096
2097static int floppy_media_changed(BlockDriverState *bs)
2098{
2099 BDRVRawState *s = bs->opaque;
2100 int ret;
2101
2102 /*
2103 * XXX: we do not have a true media changed indication.
2104 * It does not work if the floppy is changed without trying to read it.
2105 */
2106 fd_open(bs);
2107 ret = s->fd_media_changed;
2108 s->fd_media_changed = 0;
2109#ifdef DEBUG_FLOPPY
2110 printf("Floppy changed=%d\n", ret);
2111#endif
2112 return ret;
2113}
2114
f36f3949 2115static void floppy_eject(BlockDriverState *bs, bool eject_flag)
f3a5d3f8
CH
2116{
2117 BDRVRawState *s = bs->opaque;
2118 int fd;
2119
2120 if (s->fd >= 0) {
2e1e79da 2121 qemu_close(s->fd);
f3a5d3f8
CH
2122 s->fd = -1;
2123 }
6165f4d8 2124 fd = qemu_open(bs->filename, s->open_flags | O_NONBLOCK);
f3a5d3f8
CH
2125 if (fd >= 0) {
2126 if (ioctl(fd, FDEJECT, 0) < 0)
2127 perror("FDEJECT");
2e1e79da 2128 qemu_close(fd);
f3a5d3f8 2129 }
f3a5d3f8
CH
2130}
2131
2132static BlockDriver bdrv_host_floppy = {
2133 .format_name = "host_floppy",
84a12e66 2134 .protocol_name = "host_floppy",
f3a5d3f8 2135 .instance_size = sizeof(BDRVRawState),
030be321 2136 .bdrv_needs_filename = true,
508c7cb3 2137 .bdrv_probe_device = floppy_probe_device,
d3f49845 2138 .bdrv_parse_filename = floppy_parse_filename,
66f82cee 2139 .bdrv_file_open = floppy_open,
f3a5d3f8 2140 .bdrv_close = raw_close,
1bc6b705
JC
2141 .bdrv_reopen_prepare = raw_reopen_prepare,
2142 .bdrv_reopen_commit = raw_reopen_commit,
2143 .bdrv_reopen_abort = raw_reopen_abort,
c282e1fd 2144 .bdrv_create = hdev_create,
6f482f74 2145 .create_opts = &raw_create_opts,
f3a5d3f8 2146
f3a5d3f8
CH
2147 .bdrv_aio_readv = raw_aio_readv,
2148 .bdrv_aio_writev = raw_aio_writev,
b2e12bc6 2149 .bdrv_aio_flush = raw_aio_flush,
c25f53b0 2150 .bdrv_refresh_limits = raw_refresh_limits,
1b3abdcc
ML
2151 .bdrv_io_plug = raw_aio_plug,
2152 .bdrv_io_unplug = raw_aio_unplug,
2153 .bdrv_flush_io_queue = raw_aio_flush_io_queue,
f3a5d3f8 2154
55b949c8 2155 .bdrv_truncate = raw_truncate,
b94a2610
KW
2156 .bdrv_getlength = raw_getlength,
2157 .has_variable_length = true,
4a1d5e1f
FZ
2158 .bdrv_get_allocated_file_size
2159 = raw_get_allocated_file_size,
f3a5d3f8 2160
c2f3426c
SH
2161 .bdrv_detach_aio_context = raw_detach_aio_context,
2162 .bdrv_attach_aio_context = raw_attach_aio_context,
2163
f3a5d3f8
CH
2164 /* removable device support */
2165 .bdrv_is_inserted = floppy_is_inserted,
2166 .bdrv_media_changed = floppy_media_changed,
2167 .bdrv_eject = floppy_eject,
f3a5d3f8 2168};
18fa1c42 2169#endif
f3a5d3f8 2170
18fa1c42
HR
2171#if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
2172static void cdrom_parse_filename(const char *filename, QDict *options,
2173 Error **errp)
2174{
2175 /* The prefix is optional, just as for "file". */
2176 strstart(filename, "host_cdrom:", &filename);
2177
2178 qdict_put_obj(options, "filename", QOBJECT(qstring_from_str(filename)));
2179}
2180#endif
2181
2182#ifdef __linux__
015a1036
HR
2183static int cdrom_open(BlockDriverState *bs, QDict *options, int flags,
2184 Error **errp)
f3a5d3f8
CH
2185{
2186 BDRVRawState *s = bs->opaque;
e428e439
HR
2187 Error *local_err = NULL;
2188 int ret;
f3a5d3f8 2189
f3a5d3f8
CH
2190 s->type = FTYPE_CD;
2191
19a3da7f 2192 /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
e428e439 2193 ret = raw_open_common(bs, options, flags, O_NONBLOCK, &local_err);
84d18f06 2194 if (local_err) {
e428e439
HR
2195 error_propagate(errp, local_err);
2196 }
2197 return ret;
f3a5d3f8
CH
2198}
2199
508c7cb3
CH
2200static int cdrom_probe_device(const char *filename)
2201{
3baf720e
CR
2202 int fd, ret;
2203 int prio = 0;
343f8568 2204 struct stat st;
3baf720e 2205
6165f4d8 2206 fd = qemu_open(filename, O_RDONLY | O_NONBLOCK);
3baf720e
CR
2207 if (fd < 0) {
2208 goto out;
2209 }
343f8568
JS
2210 ret = fstat(fd, &st);
2211 if (ret == -1 || !S_ISBLK(st.st_mode)) {
2212 goto outc;
2213 }
3baf720e
CR
2214
2215 /* Attempt to detect via a CDROM specific ioctl */
2216 ret = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
2217 if (ret >= 0)
2218 prio = 100;
2219
343f8568 2220outc:
2e1e79da 2221 qemu_close(fd);
3baf720e
CR
2222out:
2223 return prio;
508c7cb3
CH
2224}
2225
f3a5d3f8
CH
2226static int cdrom_is_inserted(BlockDriverState *bs)
2227{
2228 BDRVRawState *s = bs->opaque;
2229 int ret;
2230
2231 ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
2232 if (ret == CDS_DISC_OK)
2233 return 1;
2234 return 0;
2235}
2236
f36f3949 2237static void cdrom_eject(BlockDriverState *bs, bool eject_flag)
f3a5d3f8
CH
2238{
2239 BDRVRawState *s = bs->opaque;
2240
2241 if (eject_flag) {
2242 if (ioctl(s->fd, CDROMEJECT, NULL) < 0)
2243 perror("CDROMEJECT");
2244 } else {
2245 if (ioctl(s->fd, CDROMCLOSETRAY, NULL) < 0)
2246 perror("CDROMEJECT");
2247 }
f3a5d3f8
CH
2248}
2249
025e849a 2250static void cdrom_lock_medium(BlockDriverState *bs, bool locked)
f3a5d3f8
CH
2251{
2252 BDRVRawState *s = bs->opaque;
2253
2254 if (ioctl(s->fd, CDROM_LOCKDOOR, locked) < 0) {
2255 /*
2256 * Note: an error can happen if the distribution automatically
2257 * mounts the CD-ROM
2258 */
2259 /* perror("CDROM_LOCKDOOR"); */
2260 }
f3a5d3f8
CH
2261}
2262
2263static BlockDriver bdrv_host_cdrom = {
2264 .format_name = "host_cdrom",
84a12e66 2265 .protocol_name = "host_cdrom",
f3a5d3f8 2266 .instance_size = sizeof(BDRVRawState),
030be321 2267 .bdrv_needs_filename = true,
508c7cb3 2268 .bdrv_probe_device = cdrom_probe_device,
18fa1c42 2269 .bdrv_parse_filename = cdrom_parse_filename,
66f82cee 2270 .bdrv_file_open = cdrom_open,
f3a5d3f8 2271 .bdrv_close = raw_close,
1bc6b705
JC
2272 .bdrv_reopen_prepare = raw_reopen_prepare,
2273 .bdrv_reopen_commit = raw_reopen_commit,
2274 .bdrv_reopen_abort = raw_reopen_abort,
c282e1fd 2275 .bdrv_create = hdev_create,
6f482f74 2276 .create_opts = &raw_create_opts,
f3a5d3f8 2277
f3a5d3f8
CH
2278 .bdrv_aio_readv = raw_aio_readv,
2279 .bdrv_aio_writev = raw_aio_writev,
b2e12bc6 2280 .bdrv_aio_flush = raw_aio_flush,
c25f53b0 2281 .bdrv_refresh_limits = raw_refresh_limits,
1b3abdcc
ML
2282 .bdrv_io_plug = raw_aio_plug,
2283 .bdrv_io_unplug = raw_aio_unplug,
2284 .bdrv_flush_io_queue = raw_aio_flush_io_queue,
f3a5d3f8 2285
55b949c8 2286 .bdrv_truncate = raw_truncate,
b94a2610
KW
2287 .bdrv_getlength = raw_getlength,
2288 .has_variable_length = true,
4a1d5e1f
FZ
2289 .bdrv_get_allocated_file_size
2290 = raw_get_allocated_file_size,
f3a5d3f8 2291
c2f3426c
SH
2292 .bdrv_detach_aio_context = raw_detach_aio_context,
2293 .bdrv_attach_aio_context = raw_attach_aio_context,
2294
f3a5d3f8
CH
2295 /* removable device support */
2296 .bdrv_is_inserted = cdrom_is_inserted,
2297 .bdrv_eject = cdrom_eject,
025e849a 2298 .bdrv_lock_medium = cdrom_lock_medium,
f3a5d3f8
CH
2299
2300 /* generic scsi device */
63ec93db 2301 .bdrv_ioctl = hdev_ioctl,
63ec93db 2302 .bdrv_aio_ioctl = hdev_aio_ioctl,
f3a5d3f8
CH
2303};
2304#endif /* __linux__ */
2305
a167ba50 2306#if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
511018e4
AT
2307static int cdrom_open(BlockDriverState *bs, QDict *options, int flags,
2308 Error **errp)
f3a5d3f8
CH
2309{
2310 BDRVRawState *s = bs->opaque;
e428e439 2311 Error *local_err = NULL;
f3a5d3f8
CH
2312 int ret;
2313
2314 s->type = FTYPE_CD;
2315
e428e439
HR
2316 ret = raw_open_common(bs, options, flags, 0, &local_err);
2317 if (ret) {
84d18f06 2318 if (local_err) {
e428e439
HR
2319 error_propagate(errp, local_err);
2320 }
f3a5d3f8 2321 return ret;
e428e439 2322 }
f3a5d3f8 2323
9b2260cb 2324 /* make sure the door isn't locked at this time */
f3a5d3f8
CH
2325 ioctl(s->fd, CDIOCALLOW);
2326 return 0;
2327}
2328
508c7cb3
CH
2329static int cdrom_probe_device(const char *filename)
2330{
2331 if (strstart(filename, "/dev/cd", NULL) ||
2332 strstart(filename, "/dev/acd", NULL))
2333 return 100;
2334 return 0;
2335}
2336
f3a5d3f8
CH
2337static int cdrom_reopen(BlockDriverState *bs)
2338{
2339 BDRVRawState *s = bs->opaque;
2340 int fd;
2341
2342 /*
2343 * Force reread of possibly changed/newly loaded disc,
2344 * FreeBSD seems to not notice sometimes...
2345 */
2346 if (s->fd >= 0)
2e1e79da 2347 qemu_close(s->fd);
6165f4d8 2348 fd = qemu_open(bs->filename, s->open_flags, 0644);
f3a5d3f8
CH
2349 if (fd < 0) {
2350 s->fd = -1;
2351 return -EIO;
2352 }
2353 s->fd = fd;
2354
9b2260cb 2355 /* make sure the door isn't locked at this time */
f3a5d3f8
CH
2356 ioctl(s->fd, CDIOCALLOW);
2357 return 0;
2358}
2359
2360static int cdrom_is_inserted(BlockDriverState *bs)
2361{
2362 return raw_getlength(bs) > 0;
2363}
2364
f36f3949 2365static void cdrom_eject(BlockDriverState *bs, bool eject_flag)
f3a5d3f8
CH
2366{
2367 BDRVRawState *s = bs->opaque;
2368
2369 if (s->fd < 0)
822e1cd1 2370 return;
f3a5d3f8
CH
2371
2372 (void) ioctl(s->fd, CDIOCALLOW);
2373
2374 if (eject_flag) {
2375 if (ioctl(s->fd, CDIOCEJECT) < 0)
2376 perror("CDIOCEJECT");
2377 } else {
2378 if (ioctl(s->fd, CDIOCCLOSE) < 0)
2379 perror("CDIOCCLOSE");
2380 }
2381
822e1cd1 2382 cdrom_reopen(bs);
f3a5d3f8
CH
2383}
2384
025e849a 2385static void cdrom_lock_medium(BlockDriverState *bs, bool locked)
f3a5d3f8
CH
2386{
2387 BDRVRawState *s = bs->opaque;
2388
2389 if (s->fd < 0)
7bf37fed 2390 return;
f3a5d3f8
CH
2391 if (ioctl(s->fd, (locked ? CDIOCPREVENT : CDIOCALLOW)) < 0) {
2392 /*
2393 * Note: an error can happen if the distribution automatically
2394 * mounts the CD-ROM
2395 */
2396 /* perror("CDROM_LOCKDOOR"); */
2397 }
f3a5d3f8
CH
2398}
2399
2400static BlockDriver bdrv_host_cdrom = {
2401 .format_name = "host_cdrom",
84a12e66 2402 .protocol_name = "host_cdrom",
f3a5d3f8 2403 .instance_size = sizeof(BDRVRawState),
030be321 2404 .bdrv_needs_filename = true,
508c7cb3 2405 .bdrv_probe_device = cdrom_probe_device,
18fa1c42 2406 .bdrv_parse_filename = cdrom_parse_filename,
66f82cee 2407 .bdrv_file_open = cdrom_open,
f3a5d3f8 2408 .bdrv_close = raw_close,
1bc6b705
JC
2409 .bdrv_reopen_prepare = raw_reopen_prepare,
2410 .bdrv_reopen_commit = raw_reopen_commit,
2411 .bdrv_reopen_abort = raw_reopen_abort,
c282e1fd 2412 .bdrv_create = hdev_create,
6f482f74 2413 .create_opts = &raw_create_opts,
f3a5d3f8 2414
f3a5d3f8
CH
2415 .bdrv_aio_readv = raw_aio_readv,
2416 .bdrv_aio_writev = raw_aio_writev,
b2e12bc6 2417 .bdrv_aio_flush = raw_aio_flush,
c25f53b0 2418 .bdrv_refresh_limits = raw_refresh_limits,
1b3abdcc
ML
2419 .bdrv_io_plug = raw_aio_plug,
2420 .bdrv_io_unplug = raw_aio_unplug,
2421 .bdrv_flush_io_queue = raw_aio_flush_io_queue,
f3a5d3f8 2422
55b949c8 2423 .bdrv_truncate = raw_truncate,
b94a2610
KW
2424 .bdrv_getlength = raw_getlength,
2425 .has_variable_length = true,
4a1d5e1f
FZ
2426 .bdrv_get_allocated_file_size
2427 = raw_get_allocated_file_size,
f3a5d3f8 2428
c2f3426c
SH
2429 .bdrv_detach_aio_context = raw_detach_aio_context,
2430 .bdrv_attach_aio_context = raw_attach_aio_context,
2431
19cb3738 2432 /* removable device support */
f3a5d3f8
CH
2433 .bdrv_is_inserted = cdrom_is_inserted,
2434 .bdrv_eject = cdrom_eject,
025e849a 2435 .bdrv_lock_medium = cdrom_lock_medium,
19cb3738 2436};
f3a5d3f8 2437#endif /* __FreeBSD__ */
5efa9d5a 2438
84a12e66 2439static void bdrv_file_init(void)
5efa9d5a 2440{
508c7cb3
CH
2441 /*
2442 * Register all the drivers. Note that order is important, the driver
2443 * registered last will get probed first.
2444 */
84a12e66 2445 bdrv_register(&bdrv_file);
5efa9d5a 2446 bdrv_register(&bdrv_host_device);
f3a5d3f8
CH
2447#ifdef __linux__
2448 bdrv_register(&bdrv_host_floppy);
2449 bdrv_register(&bdrv_host_cdrom);
2450#endif
a167ba50 2451#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
f3a5d3f8
CH
2452 bdrv_register(&bdrv_host_cdrom);
2453#endif
5efa9d5a
AL
2454}
2455
84a12e66 2456block_init(bdrv_file_init);