]> Git Repo - qemu.git/blame - block/file-posix.c
block: Add .bdrv_truncate() error messages
[qemu.git] / block / file-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 */
80c71a24 24#include "qemu/osdep.h"
da34e65c 25#include "qapi/error.h"
f348b6d1 26#include "qemu/cutils.h"
d49b6836 27#include "qemu/error-report.h"
737e150e 28#include "block/block_int.h"
1de7afc9 29#include "qemu/module.h"
de81a169 30#include "trace.h"
737e150e 31#include "block/thread-pool.h"
1de7afc9 32#include "qemu/iov.h"
0187f5c9 33#include "block/raw-aio.h"
06247428 34#include "qapi/util.h"
d49b6836 35#include "qapi/qmp/qstring.h"
83f64091 36
83affaa6 37#if defined(__APPLE__) && (__MACH__)
83f64091
FB
38#include <paths.h>
39#include <sys/param.h>
40#include <IOKit/IOKitLib.h>
41#include <IOKit/IOBSD.h>
42#include <IOKit/storage/IOMediaBSDClient.h>
43#include <IOKit/storage/IOMedia.h>
44#include <IOKit/storage/IOCDMedia.h>
45//#include <IOKit/storage/IOCDTypes.h>
d0855f12 46#include <IOKit/storage/IODVDMedia.h>
83f64091
FB
47#include <CoreFoundation/CoreFoundation.h>
48#endif
49
50#ifdef __sun__
2e9671da 51#define _POSIX_PTHREAD_SEMANTICS 1
83f64091
FB
52#include <sys/dkio.h>
53#endif
19cb3738
FB
54#ifdef __linux__
55#include <sys/ioctl.h>
05acda4d 56#include <sys/param.h>
19cb3738
FB
57#include <linux/cdrom.h>
58#include <linux/fd.h>
5500316d 59#include <linux/fs.h>
1a9335e4 60#include <linux/hdreg.h>
3307ed7b 61#include <scsi/sg.h>
1a9335e4
ET
62#ifdef __s390__
63#include <asm/dasd.h>
64#endif
4ab15590
CL
65#ifndef FS_NOCOW_FL
66#define FS_NOCOW_FL 0x00800000 /* Do not cow file */
67#endif
5500316d 68#endif
b953f075 69#if defined(CONFIG_FALLOCATE_PUNCH_HOLE) || defined(CONFIG_FALLOCATE_ZERO_RANGE)
3d4fa43e
KK
70#include <linux/falloc.h>
71#endif
a167ba50 72#if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
1cb6c3fd 73#include <sys/disk.h>
9f23011a 74#include <sys/cdio.h>
1cb6c3fd 75#endif
83f64091 76
128ab2ff
BS
77#ifdef __OpenBSD__
78#include <sys/ioctl.h>
79#include <sys/disklabel.h>
80#include <sys/dkio.h>
81#endif
82
d1f6fd8d
CE
83#ifdef __NetBSD__
84#include <sys/ioctl.h>
85#include <sys/disklabel.h>
86#include <sys/dkio.h>
87#include <sys/disk.h>
88#endif
89
c5e97233
BS
90#ifdef __DragonFly__
91#include <sys/ioctl.h>
92#include <sys/diskslice.h>
93#endif
94
dce512de
CH
95#ifdef CONFIG_XFS
96#include <xfs/xfs.h>
97#endif
98
faf07963 99//#define DEBUG_BLOCK
bcb22555
DA
100
101#ifdef DEBUG_BLOCK
102# define DEBUG_BLOCK_PRINT 1
8c05dbf9 103#else
bcb22555 104# define DEBUG_BLOCK_PRINT 0
8c05dbf9 105#endif
bcb22555
DA
106#define DPRINTF(fmt, ...) \
107do { \
108 if (DEBUG_BLOCK_PRINT) { \
109 printf(fmt, ## __VA_ARGS__); \
110 } \
111} while (0)
8c05dbf9 112
f6465578
AL
113/* OS X does not have O_DSYNC */
114#ifndef O_DSYNC
1c27a8b3 115#ifdef O_SYNC
7ab064d2 116#define O_DSYNC O_SYNC
1c27a8b3
JA
117#elif defined(O_FSYNC)
118#define O_DSYNC O_FSYNC
119#endif
f6465578
AL
120#endif
121
9f7965c7
AL
122/* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */
123#ifndef O_DIRECT
124#define O_DIRECT O_DSYNC
125#endif
126
19cb3738
FB
127#define FTYPE_FILE 0
128#define FTYPE_CD 1
83f64091 129
581b9e29
CH
130#define MAX_BLOCKSIZE 4096
131
19cb3738
FB
132typedef struct BDRVRawState {
133 int fd;
134 int type;
0e1d8f4c 135 int open_flags;
c25f53b0
PB
136 size_t buf_align;
137
dce512de 138#ifdef CONFIG_XFS
260a82e5 139 bool is_xfs:1;
dce512de 140#endif
260a82e5 141 bool has_discard:1;
97a2ae34 142 bool has_write_zeroes:1;
260a82e5 143 bool discard_zeroes:1;
0a4279d9 144 bool use_linux_aio:1;
e5bcf967 145 bool page_cache_inconsistent:1;
d50d8222 146 bool has_fallocate;
3cad8307 147 bool needs_alignment;
19cb3738
FB
148} BDRVRawState;
149
eeb6b45d
JC
150typedef struct BDRVRawReopenState {
151 int fd;
152 int open_flags;
eeb6b45d
JC
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
8a4ed0d1
ET
214/*
215 * Get logical block size via ioctl. On success store it in @sector_size_p.
216 */
217static int probe_logical_blocksize(int fd, unsigned int *sector_size_p)
c25f53b0 218{
c25f53b0 219 unsigned int sector_size;
8a4ed0d1 220 bool success = false;
700f9ce0 221 int i;
c25f53b0 222
8a4ed0d1 223 errno = ENOTSUP;
700f9ce0 224 static const unsigned long ioctl_list[] = {
c25f53b0 225#ifdef BLKSSZGET
700f9ce0 226 BLKSSZGET,
c25f53b0
PB
227#endif
228#ifdef DKIOCGETBLOCKSIZE
700f9ce0 229 DKIOCGETBLOCKSIZE,
c25f53b0
PB
230#endif
231#ifdef DIOCGSECTORSIZE
700f9ce0 232 DIOCGSECTORSIZE,
c25f53b0 233#endif
700f9ce0
PM
234 };
235
236 /* Try a few ioctls to get the right size */
237 for (i = 0; i < (int)ARRAY_SIZE(ioctl_list); i++) {
238 if (ioctl(fd, ioctl_list[i], &sector_size) >= 0) {
239 *sector_size_p = sector_size;
240 success = true;
241 }
242 }
8a4ed0d1
ET
243
244 return success ? 0 : -errno;
245}
246
1a9335e4
ET
247/**
248 * Get physical block size of @fd.
249 * On success, store it in @blk_size and return 0.
250 * On failure, return -errno.
251 */
252static int probe_physical_blocksize(int fd, unsigned int *blk_size)
253{
254#ifdef BLKPBSZGET
255 if (ioctl(fd, BLKPBSZGET, blk_size) < 0) {
256 return -errno;
257 }
258 return 0;
259#else
260 return -ENOTSUP;
261#endif
262}
263
22d182e8
SH
264/* Check if read is allowed with given memory buffer and length.
265 *
266 * This function is used to check O_DIRECT memory buffer and request alignment.
267 */
268static bool raw_is_io_aligned(int fd, void *buf, size_t len)
269{
270 ssize_t ret = pread(fd, buf, len, 0);
271
272 if (ret >= 0) {
273 return true;
274 }
275
276#ifdef __linux__
277 /* The Linux kernel returns EINVAL for misaligned O_DIRECT reads. Ignore
278 * other errors (e.g. real I/O error), which could happen on a failed
279 * drive, since we only care about probing alignment.
280 */
281 if (errno != EINVAL) {
282 return true;
283 }
284#endif
285
286 return false;
287}
288
8a4ed0d1
ET
289static void raw_probe_alignment(BlockDriverState *bs, int fd, Error **errp)
290{
291 BDRVRawState *s = bs->opaque;
292 char *buf;
459b4e66 293 size_t max_align = MAX(MAX_BLOCKSIZE, getpagesize());
8a4ed0d1 294
b192af8a 295 /* For SCSI generic devices the alignment is not really used.
8a4ed0d1 296 With buffered I/O, we don't have any restrictions. */
b192af8a 297 if (bdrv_is_sg(bs) || !s->needs_alignment) {
a5b8dd2c 298 bs->bl.request_alignment = 1;
8a4ed0d1
ET
299 s->buf_align = 1;
300 return;
301 }
302
a5b8dd2c 303 bs->bl.request_alignment = 0;
8a4ed0d1
ET
304 s->buf_align = 0;
305 /* Let's try to use the logical blocksize for the alignment. */
a5b8dd2c
EB
306 if (probe_logical_blocksize(fd, &bs->bl.request_alignment) < 0) {
307 bs->bl.request_alignment = 0;
8a4ed0d1 308 }
c25f53b0
PB
309#ifdef CONFIG_XFS
310 if (s->is_xfs) {
311 struct dioattr da;
df26a350 312 if (xfsctl(NULL, fd, XFS_IOC_DIOINFO, &da) >= 0) {
a5b8dd2c 313 bs->bl.request_alignment = da.d_miniosz;
c25f53b0
PB
314 /* The kernel returns wrong information for d_mem */
315 /* s->buf_align = da.d_mem; */
316 }
317 }
318#endif
319
320 /* If we could not get the sizes so far, we can only guess them */
321 if (!s->buf_align) {
322 size_t align;
459b4e66
DL
323 buf = qemu_memalign(max_align, 2 * max_align);
324 for (align = 512; align <= max_align; align <<= 1) {
325 if (raw_is_io_aligned(fd, buf + align, max_align)) {
c25f53b0
PB
326 s->buf_align = align;
327 break;
328 }
329 }
330 qemu_vfree(buf);
331 }
332
a5b8dd2c 333 if (!bs->bl.request_alignment) {
c25f53b0 334 size_t align;
459b4e66
DL
335 buf = qemu_memalign(s->buf_align, max_align);
336 for (align = 512; align <= max_align; align <<= 1) {
22d182e8 337 if (raw_is_io_aligned(fd, buf, align)) {
a5b8dd2c 338 bs->bl.request_alignment = align;
c25f53b0
PB
339 break;
340 }
341 }
342 qemu_vfree(buf);
343 }
df26a350 344
a5b8dd2c 345 if (!s->buf_align || !bs->bl.request_alignment) {
8cc9c6e9
EB
346 error_setg(errp, "Could not find working O_DIRECT alignment");
347 error_append_hint(errp, "Try cache.direct=off\n");
df26a350 348 }
c25f53b0
PB
349}
350
6a8dc042
JC
351static void raw_parse_flags(int bdrv_flags, int *open_flags)
352{
353 assert(open_flags != NULL);
354
355 *open_flags |= O_BINARY;
356 *open_flags &= ~O_ACCMODE;
357 if (bdrv_flags & BDRV_O_RDWR) {
358 *open_flags |= O_RDWR;
359 } else {
360 *open_flags |= O_RDONLY;
361 }
362
363 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
364 * and O_DIRECT for no caching. */
365 if ((bdrv_flags & BDRV_O_NOCACHE)) {
366 *open_flags |= O_DIRECT;
367 }
6a8dc042
JC
368}
369
078896a9
HR
370static void raw_parse_filename(const char *filename, QDict *options,
371 Error **errp)
372{
373 /* The filename does not have to be prefixed by the protocol name, since
374 * "file" is the default protocol; therefore, the return value of this
375 * function call can be ignored. */
376 strstart(filename, "file:", &filename);
377
378 qdict_put_obj(options, "filename", QOBJECT(qstring_from_str(filename)));
379}
380
c66a6157
KW
381static QemuOptsList raw_runtime_opts = {
382 .name = "raw",
383 .head = QTAILQ_HEAD_INITIALIZER(raw_runtime_opts.head),
384 .desc = {
385 {
386 .name = "filename",
387 .type = QEMU_OPT_STRING,
388 .help = "File name of the image",
389 },
0a4279d9
KW
390 {
391 .name = "aio",
392 .type = QEMU_OPT_STRING,
393 .help = "host AIO implementation (threads, native)",
394 },
c66a6157
KW
395 { /* end of list */ }
396 },
397};
398
399static int raw_open_common(BlockDriverState *bs, QDict *options,
e428e439 400 int bdrv_flags, int open_flags, Error **errp)
83f64091
FB
401{
402 BDRVRawState *s = bs->opaque;
c66a6157
KW
403 QemuOpts *opts;
404 Error *local_err = NULL;
8bfea15d 405 const char *filename = NULL;
0a4279d9 406 BlockdevAioOptions aio, aio_default;
0e1d8f4c 407 int fd, ret;
260a82e5 408 struct stat st;
83f64091 409
87ea75d5 410 opts = qemu_opts_create(&raw_runtime_opts, NULL, 0, &error_abort);
c66a6157 411 qemu_opts_absorb_qdict(opts, options, &local_err);
84d18f06 412 if (local_err) {
e428e439 413 error_propagate(errp, local_err);
c66a6157
KW
414 ret = -EINVAL;
415 goto fail;
416 }
417
418 filename = qemu_opt_get(opts, "filename");
419
1de1ae0a
CE
420 ret = raw_normalize_devicepath(&filename);
421 if (ret != 0) {
e428e439 422 error_setg_errno(errp, -ret, "Could not normalize device path");
c66a6157 423 goto fail;
1de1ae0a
CE
424 }
425
0a4279d9
KW
426 aio_default = (bdrv_flags & BDRV_O_NATIVE_AIO)
427 ? BLOCKDEV_AIO_OPTIONS_NATIVE
428 : BLOCKDEV_AIO_OPTIONS_THREADS;
429 aio = qapi_enum_parse(BlockdevAioOptions_lookup, qemu_opt_get(opts, "aio"),
430 BLOCKDEV_AIO_OPTIONS__MAX, aio_default, &local_err);
431 if (local_err) {
432 error_propagate(errp, local_err);
433 ret = -EINVAL;
434 goto fail;
435 }
436 s->use_linux_aio = (aio == BLOCKDEV_AIO_OPTIONS_NATIVE);
437
6a8dc042
JC
438 s->open_flags = open_flags;
439 raw_parse_flags(bdrv_flags, &s->open_flags);
83f64091 440
90babde0 441 s->fd = -1;
40ff6d7e 442 fd = qemu_open(filename, s->open_flags, 0644);
19cb3738
FB
443 if (fd < 0) {
444 ret = -errno;
09237757 445 error_setg_errno(errp, errno, "Could not open '%s'", filename);
c66a6157 446 if (ret == -EROFS) {
19cb3738 447 ret = -EACCES;
c66a6157
KW
448 }
449 goto fail;
19cb3738 450 }
83f64091 451 s->fd = fd;
9ef91a67 452
5c6c3a6c 453#ifdef CONFIG_LINUX_AIO
0a4279d9
KW
454 /* Currently Linux does AIO only for files opened with O_DIRECT */
455 if (s->use_linux_aio && !(s->open_flags & O_DIRECT)) {
d657c0c2
KW
456 error_setg(errp, "aio=native was specified, but it requires "
457 "cache.direct=on, which was not specified.");
458 ret = -EINVAL;
459 goto fail;
96518254 460 }
1501ecc1 461#else
0a4279d9 462 if (s->use_linux_aio) {
d657c0c2
KW
463 error_setg(errp, "aio=native was specified, but is not supported "
464 "in this build.");
465 ret = -EINVAL;
466 goto fail;
1501ecc1
SH
467 }
468#endif /* !defined(CONFIG_LINUX_AIO) */
9ef91a67 469
7ce21016 470 s->has_discard = true;
97a2ae34 471 s->has_write_zeroes = true;
465fe887 472 bs->supported_zero_flags = BDRV_REQ_MAY_UNMAP;
3cad8307
RPM
473 if ((bs->open_flags & BDRV_O_NOCACHE) != 0) {
474 s->needs_alignment = true;
475 }
260a82e5
PB
476
477 if (fstat(s->fd, &st) < 0) {
01212d4e 478 ret = -errno;
260a82e5
PB
479 error_setg_errno(errp, errno, "Could not stat file");
480 goto fail;
481 }
482 if (S_ISREG(st.st_mode)) {
483 s->discard_zeroes = true;
d50d8222 484 s->has_fallocate = true;
260a82e5 485 }
d0b4503e
PB
486 if (S_ISBLK(st.st_mode)) {
487#ifdef BLKDISCARDZEROES
488 unsigned int arg;
489 if (ioctl(s->fd, BLKDISCARDZEROES, &arg) == 0 && arg) {
490 s->discard_zeroes = true;
491 }
492#endif
493#ifdef __linux__
494 /* On Linux 3.10, BLKDISCARD leaves stale data in the page cache. Do
495 * not rely on the contents of discarded blocks unless using O_DIRECT.
97a2ae34 496 * Same for BLKZEROOUT.
d0b4503e
PB
497 */
498 if (!(bs->open_flags & BDRV_O_NOCACHE)) {
499 s->discard_zeroes = false;
97a2ae34 500 s->has_write_zeroes = false;
d0b4503e
PB
501 }
502#endif
503 }
3cad8307
RPM
504#ifdef __FreeBSD__
505 if (S_ISCHR(st.st_mode)) {
506 /*
507 * The file is a char device (disk), which on FreeBSD isn't behind
508 * a pager, so force all requests to be aligned. This is needed
509 * so QEMU makes sure all IO operations on the device are aligned
510 * to sector size, or else FreeBSD will reject them with EINVAL.
511 */
512 s->needs_alignment = true;
513 }
514#endif
260a82e5 515
dce512de
CH
516#ifdef CONFIG_XFS
517 if (platform_test_xfs_fd(s->fd)) {
7ce21016 518 s->is_xfs = true;
dce512de
CH
519 }
520#endif
521
c66a6157
KW
522 ret = 0;
523fail:
8bfea15d
KW
524 if (filename && (bdrv_flags & BDRV_O_TEMPORARY)) {
525 unlink(filename);
526 }
c66a6157
KW
527 qemu_opts_del(opts);
528 return ret;
83f64091
FB
529}
530
015a1036
HR
531static int raw_open(BlockDriverState *bs, QDict *options, int flags,
532 Error **errp)
90babde0
CH
533{
534 BDRVRawState *s = bs->opaque;
535
536 s->type = FTYPE_FILE;
9be38598 537 return raw_open_common(bs, options, flags, 0, errp);
90babde0
CH
538}
539
eeb6b45d
JC
540static int raw_reopen_prepare(BDRVReopenState *state,
541 BlockReopenQueue *queue, Error **errp)
542{
543 BDRVRawState *s;
4e6d13c9 544 BDRVRawReopenState *rs;
eeb6b45d 545 int ret = 0;
df26a350 546 Error *local_err = NULL;
eeb6b45d
JC
547
548 assert(state != NULL);
549 assert(state->bs != NULL);
550
551 s = state->bs->opaque;
552
5839e53b 553 state->opaque = g_new0(BDRVRawReopenState, 1);
4e6d13c9 554 rs = state->opaque;
eeb6b45d 555
f709623b 556 if (s->type == FTYPE_CD) {
4e6d13c9 557 rs->open_flags |= O_NONBLOCK;
1bc6b705
JC
558 }
559
4e6d13c9 560 raw_parse_flags(state->flags, &rs->open_flags);
eeb6b45d 561
4e6d13c9 562 rs->fd = -1;
eeb6b45d 563
fdf263f6 564 int fcntl_flags = O_APPEND | O_NONBLOCK;
eeb6b45d
JC
565#ifdef O_NOATIME
566 fcntl_flags |= O_NOATIME;
567#endif
568
fdf263f6
AF
569#ifdef O_ASYNC
570 /* Not all operating systems have O_ASYNC, and those that don't
4e6d13c9 571 * will not let us track the state into rs->open_flags (typically
fdf263f6
AF
572 * you achieve the same effect with an ioctl, for example I_SETSIG
573 * on Solaris). But we do not use O_ASYNC, so that's fine.
574 */
575 assert((s->open_flags & O_ASYNC) == 0);
576#endif
577
4e6d13c9 578 if ((rs->open_flags & ~fcntl_flags) == (s->open_flags & ~fcntl_flags)) {
eeb6b45d 579 /* dup the original fd */
4e6d13c9
FZ
580 rs->fd = qemu_dup(s->fd);
581 if (rs->fd >= 0) {
582 ret = fcntl_setfl(rs->fd, rs->open_flags);
eeb6b45d 583 if (ret) {
4e6d13c9
FZ
584 qemu_close(rs->fd);
585 rs->fd = -1;
eeb6b45d
JC
586 }
587 }
588 }
589
590 /* If we cannot use fcntl, or fcntl failed, fall back to qemu_open() */
4e6d13c9 591 if (rs->fd == -1) {
bdd03cdf
HR
592 const char *normalized_filename = state->bs->filename;
593 ret = raw_normalize_devicepath(&normalized_filename);
594 if (ret < 0) {
595 error_setg_errno(errp, -ret, "Could not normalize device path");
596 } else {
4e6d13c9
FZ
597 assert(!(rs->open_flags & O_CREAT));
598 rs->fd = qemu_open(normalized_filename, rs->open_flags);
599 if (rs->fd == -1) {
bdd03cdf
HR
600 error_setg_errno(errp, errno, "Could not reopen file");
601 ret = -1;
602 }
eeb6b45d
JC
603 }
604 }
df26a350
KW
605
606 /* Fail already reopen_prepare() if we can't get a working O_DIRECT
607 * alignment with the new fd. */
4e6d13c9
FZ
608 if (rs->fd != -1) {
609 raw_probe_alignment(state->bs, rs->fd, &local_err);
df26a350 610 if (local_err) {
4e6d13c9
FZ
611 qemu_close(rs->fd);
612 rs->fd = -1;
df26a350
KW
613 error_propagate(errp, local_err);
614 ret = -EINVAL;
615 }
616 }
617
eeb6b45d
JC
618 return ret;
619}
620
eeb6b45d
JC
621static void raw_reopen_commit(BDRVReopenState *state)
622{
4e6d13c9 623 BDRVRawReopenState *rs = state->opaque;
eeb6b45d
JC
624 BDRVRawState *s = state->bs->opaque;
625
4e6d13c9 626 s->open_flags = rs->open_flags;
eeb6b45d
JC
627
628 qemu_close(s->fd);
4e6d13c9 629 s->fd = rs->fd;
eeb6b45d
JC
630
631 g_free(state->opaque);
632 state->opaque = NULL;
633}
634
635
636static void raw_reopen_abort(BDRVReopenState *state)
637{
4e6d13c9 638 BDRVRawReopenState *rs = state->opaque;
eeb6b45d
JC
639
640 /* nothing to do if NULL, we didn't get far enough */
4e6d13c9 641 if (rs == NULL) {
eeb6b45d
JC
642 return;
643 }
644
4e6d13c9
FZ
645 if (rs->fd >= 0) {
646 qemu_close(rs->fd);
647 rs->fd = -1;
eeb6b45d
JC
648 }
649 g_free(state->opaque);
650 state->opaque = NULL;
651}
652
48265250 653static int hdev_get_max_transfer_length(BlockDriverState *bs, int fd)
6f607174
FZ
654{
655#ifdef BLKSECTGET
48265250
EF
656 int max_bytes = 0;
657 short max_sectors = 0;
658 if (bs->sg && ioctl(fd, BLKSECTGET, &max_bytes) == 0) {
659 return max_bytes;
660 } else if (!bs->sg && ioctl(fd, BLKSECTGET, &max_sectors) == 0) {
661 return max_sectors << BDRV_SECTOR_BITS;
6f607174
FZ
662 } else {
663 return -errno;
664 }
665#else
666 return -ENOSYS;
667#endif
668}
669
9103f1ce
FZ
670static int hdev_get_max_segments(const struct stat *st)
671{
672#ifdef CONFIG_LINUX
673 char buf[32];
674 const char *end;
675 char *sysfspath;
676 int ret;
677 int fd = -1;
678 long max_segments;
679
680 sysfspath = g_strdup_printf("/sys/dev/block/%u:%u/queue/max_segments",
681 major(st->st_rdev), minor(st->st_rdev));
682 fd = open(sysfspath, O_RDONLY);
683 if (fd == -1) {
684 ret = -errno;
685 goto out;
686 }
687 do {
69583490 688 ret = read(fd, buf, sizeof(buf) - 1);
9103f1ce
FZ
689 } while (ret == -1 && errno == EINTR);
690 if (ret < 0) {
691 ret = -errno;
692 goto out;
693 } else if (ret == 0) {
694 ret = -EIO;
695 goto out;
696 }
697 buf[ret] = 0;
698 /* The file is ended with '\n', pass 'end' to accept that. */
699 ret = qemu_strtol(buf, &end, 10, &max_segments);
700 if (ret == 0 && end && *end == '\n') {
701 ret = max_segments;
702 }
703
704out:
fed414df
FZ
705 if (fd != -1) {
706 close(fd);
707 }
9103f1ce
FZ
708 g_free(sysfspath);
709 return ret;
710#else
711 return -ENOTSUP;
712#endif
713}
714
3baca891 715static void raw_refresh_limits(BlockDriverState *bs, Error **errp)
c25f53b0
PB
716{
717 BDRVRawState *s = bs->opaque;
6f607174
FZ
718 struct stat st;
719
720 if (!fstat(s->fd, &st)) {
c4c41a0a 721 if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode)) {
48265250
EF
722 int ret = hdev_get_max_transfer_length(bs, s->fd);
723 if (ret > 0 && ret <= BDRV_REQUEST_MAX_BYTES) {
724 bs->bl.max_transfer = pow2floor(ret);
6f607174 725 }
9103f1ce
FZ
726 ret = hdev_get_max_segments(&st);
727 if (ret > 0) {
728 bs->bl.max_transfer = MIN(bs->bl.max_transfer,
729 ret * getpagesize());
730 }
6f607174
FZ
731 }
732 }
eeb6b45d 733
df26a350 734 raw_probe_alignment(bs, s->fd, errp);
4196d2f0 735 bs->bl.min_mem_alignment = s->buf_align;
459b4e66 736 bs->bl.opt_mem_alignment = MAX(s->buf_align, getpagesize());
c25f53b0 737}
83f64091 738
1a9335e4
ET
739static int check_for_dasd(int fd)
740{
741#ifdef BIODASDINFO2
742 struct dasd_information2_t info = {0};
743
744 return ioctl(fd, BIODASDINFO2, &info);
745#else
746 return -1;
747#endif
748}
749
750/**
751 * Try to get @bs's logical and physical block size.
752 * On success, store them in @bsz and return zero.
753 * On failure, return negative errno.
754 */
755static int hdev_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz)
756{
757 BDRVRawState *s = bs->opaque;
758 int ret;
759
760 /* If DASD, get blocksizes */
761 if (check_for_dasd(s->fd) < 0) {
762 return -ENOTSUP;
763 }
764 ret = probe_logical_blocksize(s->fd, &bsz->log);
765 if (ret < 0) {
766 return ret;
767 }
768 return probe_physical_blocksize(s->fd, &bsz->phys);
769}
770
771/**
772 * Try to get @bs's geometry: cyls, heads, sectors.
773 * On success, store them in @geo and return 0.
774 * On failure return -errno.
775 * (Allows block driver to assign default geometry values that guest sees)
776 */
777#ifdef __linux__
778static int hdev_probe_geometry(BlockDriverState *bs, HDGeometry *geo)
779{
780 BDRVRawState *s = bs->opaque;
781 struct hd_geometry ioctl_geo = {0};
1a9335e4
ET
782
783 /* If DASD, get its geometry */
784 if (check_for_dasd(s->fd) < 0) {
785 return -ENOTSUP;
786 }
787 if (ioctl(s->fd, HDIO_GETGEO, &ioctl_geo) < 0) {
788 return -errno;
789 }
790 /* HDIO_GETGEO may return success even though geo contains zeros
791 (e.g. certain multipath setups) */
792 if (!ioctl_geo.heads || !ioctl_geo.sectors || !ioctl_geo.cylinders) {
793 return -ENOTSUP;
794 }
795 /* Do not return a geometry for partition */
796 if (ioctl_geo.start != 0) {
797 return -ENOTSUP;
798 }
799 geo->heads = ioctl_geo.heads;
800 geo->sectors = ioctl_geo.sectors;
1a9335e4
ET
801 geo->cylinders = ioctl_geo.cylinders;
802
803 return 0;
804}
805#else /* __linux__ */
806static int hdev_probe_geometry(BlockDriverState *bs, HDGeometry *geo)
807{
808 return -ENOTSUP;
809}
810#endif
811
de81a169
PB
812static ssize_t handle_aiocb_ioctl(RawPosixAIOData *aiocb)
813{
814 int ret;
815
816 ret = ioctl(aiocb->aio_fildes, aiocb->aio_ioctl_cmd, aiocb->aio_ioctl_buf);
817 if (ret == -1) {
818 return -errno;
819 }
820
b608c8dc 821 return 0;
de81a169
PB
822}
823
824static ssize_t handle_aiocb_flush(RawPosixAIOData *aiocb)
825{
e5bcf967 826 BDRVRawState *s = aiocb->bs->opaque;
de81a169
PB
827 int ret;
828
e5bcf967
KW
829 if (s->page_cache_inconsistent) {
830 return -EIO;
831 }
832
de81a169
PB
833 ret = qemu_fdatasync(aiocb->aio_fildes);
834 if (ret == -1) {
e5bcf967
KW
835 /* There is no clear definition of the semantics of a failing fsync(),
836 * so we may have to assume the worst. The sad truth is that this
837 * assumption is correct for Linux. Some pages are now probably marked
838 * clean in the page cache even though they are inconsistent with the
839 * on-disk contents. The next fdatasync() call would succeed, but no
840 * further writeback attempt will be made. We can't get back to a state
841 * in which we know what is on disk (we would have to rewrite
842 * everything that was touched since the last fdatasync() at least), so
843 * make bdrv_flush() fail permanently. Given that the behaviour isn't
844 * really defined, I have little hope that other OSes are doing better.
845 *
846 * Obviously, this doesn't affect O_DIRECT, which bypasses the page
847 * cache. */
848 if ((s->open_flags & O_DIRECT) == 0) {
849 s->page_cache_inconsistent = true;
850 }
de81a169
PB
851 return -errno;
852 }
853 return 0;
854}
855
856#ifdef CONFIG_PREADV
857
858static bool preadv_present = true;
859
860static ssize_t
861qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset)
862{
863 return preadv(fd, iov, nr_iov, offset);
864}
865
866static ssize_t
867qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset)
868{
869 return pwritev(fd, iov, nr_iov, offset);
870}
871
872#else
873
874static bool preadv_present = false;
875
876static ssize_t
877qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset)
878{
879 return -ENOSYS;
880}
881
882static ssize_t
883qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset)
884{
885 return -ENOSYS;
886}
887
888#endif
889
890static ssize_t handle_aiocb_rw_vector(RawPosixAIOData *aiocb)
891{
892 ssize_t len;
893
894 do {
895 if (aiocb->aio_type & QEMU_AIO_WRITE)
896 len = qemu_pwritev(aiocb->aio_fildes,
897 aiocb->aio_iov,
898 aiocb->aio_niov,
899 aiocb->aio_offset);
900 else
901 len = qemu_preadv(aiocb->aio_fildes,
902 aiocb->aio_iov,
903 aiocb->aio_niov,
904 aiocb->aio_offset);
905 } while (len == -1 && errno == EINTR);
906
907 if (len == -1) {
908 return -errno;
909 }
910 return len;
911}
912
913/*
914 * Read/writes the data to/from a given linear buffer.
915 *
916 * Returns the number of bytes handles or -errno in case of an error. Short
917 * reads are only returned if the end of the file is reached.
918 */
919static ssize_t handle_aiocb_rw_linear(RawPosixAIOData *aiocb, char *buf)
920{
921 ssize_t offset = 0;
922 ssize_t len;
923
924 while (offset < aiocb->aio_nbytes) {
925 if (aiocb->aio_type & QEMU_AIO_WRITE) {
926 len = pwrite(aiocb->aio_fildes,
927 (const char *)buf + offset,
928 aiocb->aio_nbytes - offset,
929 aiocb->aio_offset + offset);
930 } else {
931 len = pread(aiocb->aio_fildes,
932 buf + offset,
933 aiocb->aio_nbytes - offset,
934 aiocb->aio_offset + offset);
935 }
936 if (len == -1 && errno == EINTR) {
937 continue;
61ed73cf
SH
938 } else if (len == -1 && errno == EINVAL &&
939 (aiocb->bs->open_flags & BDRV_O_NOCACHE) &&
940 !(aiocb->aio_type & QEMU_AIO_WRITE) &&
941 offset > 0) {
942 /* O_DIRECT pread() may fail with EINVAL when offset is unaligned
943 * after a short read. Assume that O_DIRECT short reads only occur
944 * at EOF. Therefore this is a short read, not an I/O error.
945 */
946 break;
de81a169
PB
947 } else if (len == -1) {
948 offset = -errno;
949 break;
950 } else if (len == 0) {
951 break;
952 }
953 offset += len;
954 }
955
956 return offset;
957}
958
959static ssize_t handle_aiocb_rw(RawPosixAIOData *aiocb)
960{
961 ssize_t nbytes;
962 char *buf;
963
964 if (!(aiocb->aio_type & QEMU_AIO_MISALIGNED)) {
965 /*
966 * If there is just a single buffer, and it is properly aligned
967 * we can just use plain pread/pwrite without any problems.
968 */
969 if (aiocb->aio_niov == 1) {
970 return handle_aiocb_rw_linear(aiocb, aiocb->aio_iov->iov_base);
971 }
972 /*
973 * We have more than one iovec, and all are properly aligned.
974 *
975 * Try preadv/pwritev first and fall back to linearizing the
976 * buffer if it's not supported.
977 */
978 if (preadv_present) {
979 nbytes = handle_aiocb_rw_vector(aiocb);
980 if (nbytes == aiocb->aio_nbytes ||
981 (nbytes < 0 && nbytes != -ENOSYS)) {
982 return nbytes;
983 }
984 preadv_present = false;
985 }
986
987 /*
988 * XXX(hch): short read/write. no easy way to handle the reminder
989 * using these interfaces. For now retry using plain
990 * pread/pwrite?
991 */
992 }
993
994 /*
995 * Ok, we have to do it the hard way, copy all segments into
996 * a single aligned buffer.
997 */
50d4a858
KW
998 buf = qemu_try_blockalign(aiocb->bs, aiocb->aio_nbytes);
999 if (buf == NULL) {
1000 return -ENOMEM;
1001 }
1002
de81a169
PB
1003 if (aiocb->aio_type & QEMU_AIO_WRITE) {
1004 char *p = buf;
1005 int i;
1006
1007 for (i = 0; i < aiocb->aio_niov; ++i) {
1008 memcpy(p, aiocb->aio_iov[i].iov_base, aiocb->aio_iov[i].iov_len);
1009 p += aiocb->aio_iov[i].iov_len;
1010 }
8eb029c2 1011 assert(p - buf == aiocb->aio_nbytes);
de81a169
PB
1012 }
1013
1014 nbytes = handle_aiocb_rw_linear(aiocb, buf);
1015 if (!(aiocb->aio_type & QEMU_AIO_WRITE)) {
1016 char *p = buf;
1017 size_t count = aiocb->aio_nbytes, copy;
1018 int i;
1019
1020 for (i = 0; i < aiocb->aio_niov && count; ++i) {
1021 copy = count;
1022 if (copy > aiocb->aio_iov[i].iov_len) {
1023 copy = aiocb->aio_iov[i].iov_len;
1024 }
1025 memcpy(aiocb->aio_iov[i].iov_base, p, copy);
8eb029c2 1026 assert(count >= copy);
de81a169
PB
1027 p += copy;
1028 count -= copy;
1029 }
8eb029c2 1030 assert(count == 0);
de81a169
PB
1031 }
1032 qemu_vfree(buf);
1033
1034 return nbytes;
1035}
1036
8238010b 1037#ifdef CONFIG_XFS
97a2ae34
PB
1038static int xfs_write_zeroes(BDRVRawState *s, int64_t offset, uint64_t bytes)
1039{
1040 struct xfs_flock64 fl;
bcb22555 1041 int err;
97a2ae34
PB
1042
1043 memset(&fl, 0, sizeof(fl));
1044 fl.l_whence = SEEK_SET;
1045 fl.l_start = offset;
1046 fl.l_len = bytes;
1047
1048 if (xfsctl(NULL, s->fd, XFS_IOC_ZERO_RANGE, &fl) < 0) {
bcb22555
DA
1049 err = errno;
1050 DPRINTF("cannot write zero range (%s)\n", strerror(errno));
1051 return -err;
97a2ae34
PB
1052 }
1053
1054 return 0;
1055}
1056
8238010b
PB
1057static int xfs_discard(BDRVRawState *s, int64_t offset, uint64_t bytes)
1058{
1059 struct xfs_flock64 fl;
bcb22555 1060 int err;
8238010b
PB
1061
1062 memset(&fl, 0, sizeof(fl));
1063 fl.l_whence = SEEK_SET;
1064 fl.l_start = offset;
1065 fl.l_len = bytes;
1066
1067 if (xfsctl(NULL, s->fd, XFS_IOC_UNRESVSP64, &fl) < 0) {
bcb22555
DA
1068 err = errno;
1069 DPRINTF("cannot punch hole (%s)\n", strerror(errno));
1070 return -err;
8238010b
PB
1071 }
1072
1073 return 0;
1074}
1075#endif
1076
1486df0e
DL
1077static int translate_err(int err)
1078{
1079 if (err == -ENODEV || err == -ENOSYS || err == -EOPNOTSUPP ||
1080 err == -ENOTTY) {
1081 err = -ENOTSUP;
1082 }
1083 return err;
1084}
1085
d50d8222 1086#ifdef CONFIG_FALLOCATE
0b991712
DL
1087static int do_fallocate(int fd, int mode, off_t offset, off_t len)
1088{
1089 do {
1090 if (fallocate(fd, mode, offset, len) == 0) {
1091 return 0;
1092 }
1093 } while (errno == EINTR);
1094 return translate_err(-errno);
1095}
1096#endif
1097
37cc9f7f 1098static ssize_t handle_aiocb_write_zeroes_block(RawPosixAIOData *aiocb)
97a2ae34 1099{
37cc9f7f 1100 int ret = -ENOTSUP;
97a2ae34
PB
1101 BDRVRawState *s = aiocb->bs->opaque;
1102
37cc9f7f 1103 if (!s->has_write_zeroes) {
97a2ae34
PB
1104 return -ENOTSUP;
1105 }
1106
97a2ae34 1107#ifdef BLKZEROOUT
37cc9f7f
DL
1108 do {
1109 uint64_t range[2] = { aiocb->aio_offset, aiocb->aio_nbytes };
1110 if (ioctl(aiocb->aio_fildes, BLKZEROOUT, range) == 0) {
1111 return 0;
97a2ae34 1112 }
37cc9f7f
DL
1113 } while (errno == EINTR);
1114
1115 ret = translate_err(-errno);
97a2ae34 1116#endif
97a2ae34 1117
1486df0e 1118 if (ret == -ENOTSUP) {
97a2ae34 1119 s->has_write_zeroes = false;
97a2ae34
PB
1120 }
1121 return ret;
1122}
1123
37cc9f7f
DL
1124static ssize_t handle_aiocb_write_zeroes(RawPosixAIOData *aiocb)
1125{
a6dcf097 1126#if defined(CONFIG_FALLOCATE) || defined(CONFIG_XFS)
37cc9f7f 1127 BDRVRawState *s = aiocb->bs->opaque;
a6dcf097 1128#endif
37cc9f7f
DL
1129
1130 if (aiocb->aio_type & QEMU_AIO_BLKDEV) {
1131 return handle_aiocb_write_zeroes_block(aiocb);
1132 }
1133
1134#ifdef CONFIG_XFS
1135 if (s->is_xfs) {
1136 return xfs_write_zeroes(s, aiocb->aio_offset, aiocb->aio_nbytes);
1137 }
1138#endif
1139
b953f075
DL
1140#ifdef CONFIG_FALLOCATE_ZERO_RANGE
1141 if (s->has_write_zeroes) {
1142 int ret = do_fallocate(s->fd, FALLOC_FL_ZERO_RANGE,
1143 aiocb->aio_offset, aiocb->aio_nbytes);
1144 if (ret == 0 || ret != -ENOTSUP) {
1145 return ret;
1146 }
1147 s->has_write_zeroes = false;
1148 }
1149#endif
1150
1cdc3239
DL
1151#ifdef CONFIG_FALLOCATE_PUNCH_HOLE
1152 if (s->has_discard && s->has_fallocate) {
1153 int ret = do_fallocate(s->fd,
1154 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
1155 aiocb->aio_offset, aiocb->aio_nbytes);
1156 if (ret == 0) {
1157 ret = do_fallocate(s->fd, 0, aiocb->aio_offset, aiocb->aio_nbytes);
1158 if (ret == 0 || ret != -ENOTSUP) {
1159 return ret;
1160 }
1161 s->has_fallocate = false;
1162 } else if (ret != -ENOTSUP) {
1163 return ret;
1164 } else {
1165 s->has_discard = false;
1166 }
1167 }
1168#endif
1169
d50d8222
DL
1170#ifdef CONFIG_FALLOCATE
1171 if (s->has_fallocate && aiocb->aio_offset >= bdrv_getlength(aiocb->bs)) {
1172 int ret = do_fallocate(s->fd, 0, aiocb->aio_offset, aiocb->aio_nbytes);
1173 if (ret == 0 || ret != -ENOTSUP) {
1174 return ret;
1175 }
1176 s->has_fallocate = false;
1177 }
1178#endif
1179
37cc9f7f
DL
1180 return -ENOTSUP;
1181}
1182
8238010b
PB
1183static ssize_t handle_aiocb_discard(RawPosixAIOData *aiocb)
1184{
1185 int ret = -EOPNOTSUPP;
1186 BDRVRawState *s = aiocb->bs->opaque;
1187
7ce21016
PB
1188 if (!s->has_discard) {
1189 return -ENOTSUP;
8238010b
PB
1190 }
1191
1192 if (aiocb->aio_type & QEMU_AIO_BLKDEV) {
1193#ifdef BLKDISCARD
1194 do {
1195 uint64_t range[2] = { aiocb->aio_offset, aiocb->aio_nbytes };
1196 if (ioctl(aiocb->aio_fildes, BLKDISCARD, range) == 0) {
1197 return 0;
1198 }
1199 } while (errno == EINTR);
1200
1201 ret = -errno;
1202#endif
1203 } else {
1204#ifdef CONFIG_XFS
1205 if (s->is_xfs) {
1206 return xfs_discard(s, aiocb->aio_offset, aiocb->aio_nbytes);
1207 }
1208#endif
1209
1210#ifdef CONFIG_FALLOCATE_PUNCH_HOLE
0b991712
DL
1211 ret = do_fallocate(s->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
1212 aiocb->aio_offset, aiocb->aio_nbytes);
8238010b
PB
1213#endif
1214 }
1215
1486df0e
DL
1216 ret = translate_err(ret);
1217 if (ret == -ENOTSUP) {
7ce21016 1218 s->has_discard = false;
8238010b
PB
1219 }
1220 return ret;
1221}
1222
de81a169
PB
1223static int aio_worker(void *arg)
1224{
1225 RawPosixAIOData *aiocb = arg;
1226 ssize_t ret = 0;
1227
1228 switch (aiocb->aio_type & QEMU_AIO_TYPE_MASK) {
1229 case QEMU_AIO_READ:
1230 ret = handle_aiocb_rw(aiocb);
c0191e76 1231 if (ret >= 0 && ret < aiocb->aio_nbytes) {
de81a169
PB
1232 iov_memset(aiocb->aio_iov, aiocb->aio_niov, ret,
1233 0, aiocb->aio_nbytes - ret);
1234
1235 ret = aiocb->aio_nbytes;
1236 }
1237 if (ret == aiocb->aio_nbytes) {
1238 ret = 0;
1239 } else if (ret >= 0 && ret < aiocb->aio_nbytes) {
1240 ret = -EINVAL;
1241 }
1242 break;
1243 case QEMU_AIO_WRITE:
1244 ret = handle_aiocb_rw(aiocb);
1245 if (ret == aiocb->aio_nbytes) {
1246 ret = 0;
1247 } else if (ret >= 0 && ret < aiocb->aio_nbytes) {
1248 ret = -EINVAL;
1249 }
1250 break;
1251 case QEMU_AIO_FLUSH:
1252 ret = handle_aiocb_flush(aiocb);
1253 break;
1254 case QEMU_AIO_IOCTL:
1255 ret = handle_aiocb_ioctl(aiocb);
1256 break;
8238010b
PB
1257 case QEMU_AIO_DISCARD:
1258 ret = handle_aiocb_discard(aiocb);
1259 break;
97a2ae34
PB
1260 case QEMU_AIO_WRITE_ZEROES:
1261 ret = handle_aiocb_write_zeroes(aiocb);
1262 break;
de81a169
PB
1263 default:
1264 fprintf(stderr, "invalid aio request (0x%x)\n", aiocb->aio_type);
1265 ret = -EINVAL;
1266 break;
1267 }
1268
c84b3192 1269 g_free(aiocb);
de81a169
PB
1270 return ret;
1271}
1272
260a82e5 1273static int paio_submit_co(BlockDriverState *bs, int fd,
2ffa76c2
EB
1274 int64_t offset, QEMUIOVector *qiov,
1275 int count, int type)
260a82e5 1276{
c84b3192 1277 RawPosixAIOData *acb = g_new(RawPosixAIOData, 1);
260a82e5
PB
1278 ThreadPool *pool;
1279
1280 acb->bs = bs;
1281 acb->aio_type = type;
1282 acb->aio_fildes = fd;
1283
2ffa76c2
EB
1284 acb->aio_nbytes = count;
1285 acb->aio_offset = offset;
8eb029c2 1286
260a82e5
PB
1287 if (qiov) {
1288 acb->aio_iov = qiov->iov;
1289 acb->aio_niov = qiov->niov;
2ffa76c2 1290 assert(qiov->size == count);
260a82e5 1291 }
260a82e5 1292
2ffa76c2 1293 trace_paio_submit_co(offset, count, type);
260a82e5
PB
1294 pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
1295 return thread_pool_submit_co(pool, aio_worker, acb);
1296}
1297
7c84b1b8 1298static BlockAIOCB *paio_submit(BlockDriverState *bs, int fd,
36e3b2e7 1299 int64_t offset, QEMUIOVector *qiov, int count,
097310b5 1300 BlockCompletionFunc *cb, void *opaque, int type)
de81a169 1301{
c84b3192 1302 RawPosixAIOData *acb = g_new(RawPosixAIOData, 1);
c4d9d196 1303 ThreadPool *pool;
de81a169
PB
1304
1305 acb->bs = bs;
1306 acb->aio_type = type;
1307 acb->aio_fildes = fd;
1308
36e3b2e7
EB
1309 acb->aio_nbytes = count;
1310 acb->aio_offset = offset;
8eb029c2 1311
de81a169
PB
1312 if (qiov) {
1313 acb->aio_iov = qiov->iov;
1314 acb->aio_niov = qiov->niov;
8eb029c2 1315 assert(qiov->size == acb->aio_nbytes);
de81a169 1316 }
de81a169 1317
36e3b2e7 1318 trace_paio_submit(acb, opaque, offset, count, type);
c4d9d196
SH
1319 pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
1320 return thread_pool_submit_aio(pool, aio_worker, acb, cb, opaque);
de81a169
PB
1321}
1322
9d52aa3c
KW
1323static int coroutine_fn raw_co_prw(BlockDriverState *bs, uint64_t offset,
1324 uint64_t bytes, QEMUIOVector *qiov, int type)
83f64091 1325{
ce1a14dc 1326 BDRVRawState *s = bs->opaque;
ce1a14dc 1327
19cb3738 1328 if (fd_open(bs) < 0)
2174f12b 1329 return -EIO;
19cb3738 1330
f141eafe 1331 /*
3cad8307
RPM
1332 * Check if the underlying device requires requests to be aligned,
1333 * and if the request we are trying to submit is aligned or not.
1334 * If this is the case tell the low-level driver that it needs
1335 * to copy the buffer.
f141eafe 1336 */
3cad8307 1337 if (s->needs_alignment) {
c53b1c51 1338 if (!bdrv_qiov_is_aligned(bs, qiov)) {
5c6c3a6c 1339 type |= QEMU_AIO_MISALIGNED;
e44bd6fc 1340#ifdef CONFIG_LINUX_AIO
0a4279d9 1341 } else if (s->use_linux_aio) {
0187f5c9 1342 LinuxAioState *aio = aio_get_linux_aio(bdrv_get_aio_context(bs));
9d52aa3c 1343 assert(qiov->size == bytes);
0187f5c9 1344 return laio_co_submit(bs, aio, s->fd, offset, qiov, type);
e44bd6fc 1345#endif
5c6c3a6c 1346 }
9ef91a67 1347 }
f141eafe 1348
9d52aa3c 1349 return paio_submit_co(bs, s->fd, offset, qiov, bytes, type);
2174f12b
KW
1350}
1351
9d52aa3c
KW
1352static int coroutine_fn raw_co_preadv(BlockDriverState *bs, uint64_t offset,
1353 uint64_t bytes, QEMUIOVector *qiov,
1354 int flags)
2174f12b 1355{
9d52aa3c 1356 return raw_co_prw(bs, offset, bytes, qiov, QEMU_AIO_READ);
2174f12b
KW
1357}
1358
9d52aa3c
KW
1359static int coroutine_fn raw_co_pwritev(BlockDriverState *bs, uint64_t offset,
1360 uint64_t bytes, QEMUIOVector *qiov,
1361 int flags)
2174f12b 1362{
9d52aa3c
KW
1363 assert(flags == 0);
1364 return raw_co_prw(bs, offset, bytes, qiov, QEMU_AIO_WRITE);
83f64091
FB
1365}
1366
1b3abdcc
ML
1367static void raw_aio_plug(BlockDriverState *bs)
1368{
1369#ifdef CONFIG_LINUX_AIO
0a4279d9
KW
1370 BDRVRawState *s = bs->opaque;
1371 if (s->use_linux_aio) {
0187f5c9
PB
1372 LinuxAioState *aio = aio_get_linux_aio(bdrv_get_aio_context(bs));
1373 laio_io_plug(bs, aio);
1b3abdcc
ML
1374 }
1375#endif
1376}
1377
1378static void raw_aio_unplug(BlockDriverState *bs)
1379{
1380#ifdef CONFIG_LINUX_AIO
0a4279d9
KW
1381 BDRVRawState *s = bs->opaque;
1382 if (s->use_linux_aio) {
0187f5c9
PB
1383 LinuxAioState *aio = aio_get_linux_aio(bdrv_get_aio_context(bs));
1384 laio_io_unplug(bs, aio);
1b3abdcc
ML
1385 }
1386#endif
1387}
1388
7c84b1b8 1389static BlockAIOCB *raw_aio_flush(BlockDriverState *bs,
097310b5 1390 BlockCompletionFunc *cb, void *opaque)
b2e12bc6
CH
1391{
1392 BDRVRawState *s = bs->opaque;
1393
1394 if (fd_open(bs) < 0)
1395 return NULL;
1396
1e5b9d2f 1397 return paio_submit(bs, s->fd, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH);
b2e12bc6
CH
1398}
1399
83f64091
FB
1400static void raw_close(BlockDriverState *bs)
1401{
1402 BDRVRawState *s = bs->opaque;
c2f3426c 1403
19cb3738 1404 if (s->fd >= 0) {
2e1e79da 1405 qemu_close(s->fd);
19cb3738
FB
1406 s->fd = -1;
1407 }
83f64091
FB
1408}
1409
4bff28b8 1410static int raw_truncate(BlockDriverState *bs, int64_t offset, Error **errp)
83f64091
FB
1411{
1412 BDRVRawState *s = bs->opaque;
55b949c8 1413 struct stat st;
f59adb32 1414 int ret;
55b949c8
CH
1415
1416 if (fstat(s->fd, &st)) {
f59adb32
HR
1417 ret = -errno;
1418 error_setg_errno(errp, -ret, "Failed to fstat() the file");
1419 return ret;
55b949c8
CH
1420 }
1421
1422 if (S_ISREG(st.st_mode)) {
1423 if (ftruncate(s->fd, offset) < 0) {
f59adb32
HR
1424 ret = -errno;
1425 error_setg_errno(errp, -ret, "Failed to resize the file");
1426 return ret;
55b949c8
CH
1427 }
1428 } else if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
f59adb32
HR
1429 if (offset > raw_getlength(bs)) {
1430 error_setg(errp, "Cannot grow device files");
1431 return -EINVAL;
1432 }
55b949c8 1433 } else {
f59adb32 1434 error_setg(errp, "Resizing this file is not supported");
55b949c8
CH
1435 return -ENOTSUP;
1436 }
1437
83f64091
FB
1438 return 0;
1439}
1440
128ab2ff
BS
1441#ifdef __OpenBSD__
1442static int64_t raw_getlength(BlockDriverState *bs)
1443{
1444 BDRVRawState *s = bs->opaque;
1445 int fd = s->fd;
1446 struct stat st;
1447
1448 if (fstat(fd, &st))
aa729704 1449 return -errno;
128ab2ff
BS
1450 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
1451 struct disklabel dl;
1452
1453 if (ioctl(fd, DIOCGDINFO, &dl))
aa729704 1454 return -errno;
128ab2ff
BS
1455 return (uint64_t)dl.d_secsize *
1456 dl.d_partitions[DISKPART(st.st_rdev)].p_size;
1457 } else
1458 return st.st_size;
1459}
d1f6fd8d
CE
1460#elif defined(__NetBSD__)
1461static int64_t raw_getlength(BlockDriverState *bs)
1462{
1463 BDRVRawState *s = bs->opaque;
1464 int fd = s->fd;
1465 struct stat st;
1466
1467 if (fstat(fd, &st))
aa729704 1468 return -errno;
d1f6fd8d
CE
1469 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
1470 struct dkwedge_info dkw;
1471
1472 if (ioctl(fd, DIOCGWEDGEINFO, &dkw) != -1) {
1473 return dkw.dkw_size * 512;
1474 } else {
1475 struct disklabel dl;
1476
1477 if (ioctl(fd, DIOCGDINFO, &dl))
aa729704 1478 return -errno;
d1f6fd8d
CE
1479 return (uint64_t)dl.d_secsize *
1480 dl.d_partitions[DISKPART(st.st_rdev)].p_size;
1481 }
1482 } else
1483 return st.st_size;
1484}
50779cc2
CH
1485#elif defined(__sun__)
1486static int64_t raw_getlength(BlockDriverState *bs)
1487{
1488 BDRVRawState *s = bs->opaque;
1489 struct dk_minfo minfo;
1490 int ret;
aa729704 1491 int64_t size;
50779cc2
CH
1492
1493 ret = fd_open(bs);
1494 if (ret < 0) {
1495 return ret;
1496 }
1497
1498 /*
1499 * Use the DKIOCGMEDIAINFO ioctl to read the size.
1500 */
1501 ret = ioctl(s->fd, DKIOCGMEDIAINFO, &minfo);
1502 if (ret != -1) {
1503 return minfo.dki_lbsize * minfo.dki_capacity;
1504 }
1505
1506 /*
1507 * There are reports that lseek on some devices fails, but
1508 * irc discussion said that contingency on contingency was overkill.
1509 */
aa729704
MA
1510 size = lseek(s->fd, 0, SEEK_END);
1511 if (size < 0) {
1512 return -errno;
1513 }
1514 return size;
50779cc2
CH
1515}
1516#elif defined(CONFIG_BSD)
1517static int64_t raw_getlength(BlockDriverState *bs)
83f64091
FB
1518{
1519 BDRVRawState *s = bs->opaque;
1520 int fd = s->fd;
1521 int64_t size;
83f64091 1522 struct stat sb;
a167ba50 1523#if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
9f23011a 1524 int reopened = 0;
83f64091 1525#endif
19cb3738
FB
1526 int ret;
1527
1528 ret = fd_open(bs);
1529 if (ret < 0)
1530 return ret;
83f64091 1531
a167ba50 1532#if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
9f23011a
BS
1533again:
1534#endif
83f64091
FB
1535 if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
1536#ifdef DIOCGMEDIASIZE
1537 if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
c5e97233
BS
1538#elif defined(DIOCGPART)
1539 {
1540 struct partinfo pi;
1541 if (ioctl(fd, DIOCGPART, &pi) == 0)
1542 size = pi.media_size;
1543 else
1544 size = 0;
1545 }
1546 if (size == 0)
83f64091 1547#endif
83affaa6 1548#if defined(__APPLE__) && defined(__MACH__)
728dacbd
JA
1549 {
1550 uint64_t sectors = 0;
1551 uint32_t sector_size = 0;
1552
1553 if (ioctl(fd, DKIOCGETBLOCKCOUNT, &sectors) == 0
1554 && ioctl(fd, DKIOCGETBLOCKSIZE, &sector_size) == 0) {
1555 size = sectors * sector_size;
1556 } else {
1557 size = lseek(fd, 0LL, SEEK_END);
1558 if (size < 0) {
1559 return -errno;
1560 }
1561 }
1562 }
83f64091
FB
1563#else
1564 size = lseek(fd, 0LL, SEEK_END);
aa729704
MA
1565 if (size < 0) {
1566 return -errno;
1567 }
9f23011a 1568#endif
a167ba50 1569#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
9f23011a
BS
1570 switch(s->type) {
1571 case FTYPE_CD:
1572 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
1573 if (size == 2048LL * (unsigned)-1)
1574 size = 0;
1575 /* XXX no disc? maybe we need to reopen... */
f3a5d3f8 1576 if (size <= 0 && !reopened && cdrom_reopen(bs) >= 0) {
9f23011a
BS
1577 reopened = 1;
1578 goto again;
1579 }
1580 }
83f64091 1581#endif
50779cc2 1582 } else {
83f64091 1583 size = lseek(fd, 0, SEEK_END);
aa729704
MA
1584 if (size < 0) {
1585 return -errno;
1586 }
83f64091 1587 }
83f64091
FB
1588 return size;
1589}
50779cc2
CH
1590#else
1591static int64_t raw_getlength(BlockDriverState *bs)
1592{
1593 BDRVRawState *s = bs->opaque;
1594 int ret;
aa729704 1595 int64_t size;
50779cc2
CH
1596
1597 ret = fd_open(bs);
1598 if (ret < 0) {
1599 return ret;
1600 }
1601
aa729704
MA
1602 size = lseek(s->fd, 0, SEEK_END);
1603 if (size < 0) {
1604 return -errno;
1605 }
1606 return size;
50779cc2 1607}
128ab2ff 1608#endif
83f64091 1609
4a1d5e1f
FZ
1610static int64_t raw_get_allocated_file_size(BlockDriverState *bs)
1611{
1612 struct stat st;
1613 BDRVRawState *s = bs->opaque;
1614
1615 if (fstat(s->fd, &st) < 0) {
1616 return -errno;
1617 }
1618 return (int64_t)st.st_blocks * 512;
1619}
1620
6f482f74 1621static int raw_create(const char *filename, QemuOpts *opts, Error **errp)
83f64091
FB
1622{
1623 int fd;
1e37d059 1624 int result = 0;
0e7e1989 1625 int64_t total_size = 0;
4ab15590 1626 bool nocow = false;
06247428
HT
1627 PreallocMode prealloc;
1628 char *buf = NULL;
1629 Error *local_err = NULL;
83f64091 1630
464d9f64
HR
1631 strstart(filename, "file:", &filename);
1632
0e7e1989 1633 /* Read out options */
180e9526
HT
1634 total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
1635 BDRV_SECTOR_SIZE);
4ab15590 1636 nocow = qemu_opt_get_bool(opts, BLOCK_OPT_NOCOW, false);
06247428
HT
1637 buf = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC);
1638 prealloc = qapi_enum_parse(PreallocMode_lookup, buf,
7fb1cf16 1639 PREALLOC_MODE__MAX, PREALLOC_MODE_OFF,
06247428
HT
1640 &local_err);
1641 g_free(buf);
1642 if (local_err) {
1643 error_propagate(errp, local_err);
1644 result = -EINVAL;
1645 goto out;
1646 }
83f64091 1647
73ba05d9 1648 fd = qemu_open(filename, O_RDWR | O_CREAT | O_TRUNC | O_BINARY,
6165f4d8 1649 0644);
1e37d059
SW
1650 if (fd < 0) {
1651 result = -errno;
e428e439 1652 error_setg_errno(errp, -result, "Could not create file");
06247428
HT
1653 goto out;
1654 }
1655
1656 if (nocow) {
4ab15590 1657#ifdef __linux__
06247428
HT
1658 /* Set NOCOW flag to solve performance issue on fs like btrfs.
1659 * This is an optimisation. The FS_IOC_SETFLAGS ioctl return value
1660 * will be ignored since any failure of this operation should not
1661 * block the left work.
1662 */
1663 int attr;
1664 if (ioctl(fd, FS_IOC_GETFLAGS, &attr) == 0) {
1665 attr |= FS_NOCOW_FL;
1666 ioctl(fd, FS_IOC_SETFLAGS, &attr);
4ab15590 1667 }
06247428
HT
1668#endif
1669 }
1670
ed911435
KW
1671 switch (prealloc) {
1672#ifdef CONFIG_POSIX_FALLOCATE
1673 case PREALLOC_MODE_FALLOC:
c6ccc2c5
NS
1674 /*
1675 * Truncating before posix_fallocate() makes it about twice slower on
1676 * file systems that do not support fallocate(), trying to check if a
1677 * block is allocated before allocating it, so don't do that here.
1678 */
06247428
HT
1679 result = -posix_fallocate(fd, 0, total_size);
1680 if (result != 0) {
c6ccc2c5 1681 /* posix_fallocate() doesn't set errno. */
06247428
HT
1682 error_setg_errno(errp, -result,
1683 "Could not preallocate data for the new file");
1e37d059 1684 }
ed911435
KW
1685 break;
1686#endif
1687 case PREALLOC_MODE_FULL:
1688 {
5a1dad9d
NS
1689 /*
1690 * Knowing the final size from the beginning could allow the file
1691 * system driver to do less allocations and possibly avoid
1692 * fragmentation of the file.
1693 */
1694 if (ftruncate(fd, total_size) != 0) {
1695 result = -errno;
1696 error_setg_errno(errp, -result, "Could not resize file");
1697 goto out_close;
1698 }
1699
06247428 1700 int64_t num = 0, left = total_size;
ed911435 1701 buf = g_malloc0(65536);
06247428
HT
1702
1703 while (left > 0) {
1704 num = MIN(left, 65536);
1705 result = write(fd, buf, num);
1706 if (result < 0) {
1707 result = -errno;
1708 error_setg_errno(errp, -result,
1709 "Could not write to the new file");
1710 break;
1711 }
39411cf3 1712 left -= result;
1e37d059 1713 }
731de380 1714 if (result >= 0) {
098ffa66
HR
1715 result = fsync(fd);
1716 if (result < 0) {
1717 result = -errno;
1718 error_setg_errno(errp, -result,
1719 "Could not flush new file to disk");
1720 }
731de380 1721 }
06247428 1722 g_free(buf);
ed911435
KW
1723 break;
1724 }
1725 case PREALLOC_MODE_OFF:
f6a72404
NS
1726 if (ftruncate(fd, total_size) != 0) {
1727 result = -errno;
1728 error_setg_errno(errp, -result, "Could not resize file");
1729 }
ed911435
KW
1730 break;
1731 default:
06247428
HT
1732 result = -EINVAL;
1733 error_setg(errp, "Unsupported preallocation mode: %s",
1734 PreallocMode_lookup[prealloc]);
ed911435 1735 break;
1e37d059 1736 }
06247428 1737
5a1dad9d 1738out_close:
06247428
HT
1739 if (qemu_close(fd) != 0 && result == 0) {
1740 result = -errno;
1741 error_setg_errno(errp, -result, "Could not close the new file");
1742 }
1743out:
1e37d059 1744 return result;
83f64091
FB
1745}
1746
d1f06fe6
MA
1747/*
1748 * Find allocation range in @bs around offset @start.
1749 * May change underlying file descriptor's file offset.
1750 * If @start is not in a hole, store @start in @data, and the
1751 * beginning of the next hole in @hole, and return 0.
1752 * If @start is in a non-trailing hole, store @start in @hole and the
1753 * beginning of the next non-hole in @data, and return 0.
1754 * If @start is in a trailing hole or beyond EOF, return -ENXIO.
1755 * If we can't find out, return a negative errno other than -ENXIO.
1756 */
1757static int find_allocation(BlockDriverState *bs, off_t start,
1758 off_t *data, off_t *hole)
4f11aa8a
HR
1759{
1760#if defined SEEK_HOLE && defined SEEK_DATA
94282e71 1761 BDRVRawState *s = bs->opaque;
d1f06fe6 1762 off_t offs;
94282e71 1763
d1f06fe6
MA
1764 /*
1765 * SEEK_DATA cases:
1766 * D1. offs == start: start is in data
1767 * D2. offs > start: start is in a hole, next data at offs
1768 * D3. offs < 0, errno = ENXIO: either start is in a trailing hole
1769 * or start is beyond EOF
1770 * If the latter happens, the file has been truncated behind
1771 * our back since we opened it. All bets are off then.
1772 * Treating like a trailing hole is simplest.
1773 * D4. offs < 0, errno != ENXIO: we learned nothing
1774 */
1775 offs = lseek(s->fd, start, SEEK_DATA);
1776 if (offs < 0) {
1777 return -errno; /* D3 or D4 */
1778 }
1779 assert(offs >= start);
1780
1781 if (offs > start) {
1782 /* D2: in hole, next data at offs */
1783 *hole = start;
1784 *data = offs;
1785 return 0;
5500316d
PB
1786 }
1787
d1f06fe6
MA
1788 /* D1: in data, end not yet known */
1789
1790 /*
1791 * SEEK_HOLE cases:
1792 * H1. offs == start: start is in a hole
1793 * If this happens here, a hole has been dug behind our back
1794 * since the previous lseek().
1795 * H2. offs > start: either start is in data, next hole at offs,
1796 * or start is in trailing hole, EOF at offs
1797 * Linux treats trailing holes like any other hole: offs ==
1798 * start. Solaris seeks to EOF instead: offs > start (blech).
1799 * If that happens here, a hole has been dug behind our back
1800 * since the previous lseek().
1801 * H3. offs < 0, errno = ENXIO: start is beyond EOF
1802 * If this happens, the file has been truncated behind our
1803 * back since we opened it. Treat it like a trailing hole.
1804 * H4. offs < 0, errno != ENXIO: we learned nothing
1805 * Pretend we know nothing at all, i.e. "forget" about D1.
1806 */
1807 offs = lseek(s->fd, start, SEEK_HOLE);
1808 if (offs < 0) {
1809 return -errno; /* D1 and (H3 or H4) */
1810 }
1811 assert(offs >= start);
1812
1813 if (offs > start) {
1814 /*
1815 * D1 and H2: either in data, next hole at offs, or it was in
1816 * data but is now in a trailing hole. In the latter case,
1817 * all bets are off. Treating it as if it there was data all
1818 * the way to EOF is safe, so simply do that.
1819 */
4f11aa8a 1820 *data = start;
d1f06fe6
MA
1821 *hole = offs;
1822 return 0;
5500316d 1823 }
4f11aa8a 1824
d1f06fe6
MA
1825 /* D1 and H1 */
1826 return -EBUSY;
5500316d 1827#else
4f11aa8a 1828 return -ENOTSUP;
5500316d 1829#endif
4f11aa8a
HR
1830}
1831
1832/*
be2ebc6d 1833 * Returns the allocation status of the specified sectors.
4f11aa8a
HR
1834 *
1835 * If 'sector_num' is beyond the end of the disk image the return value is 0
1836 * and 'pnum' is set to 0.
1837 *
1838 * 'pnum' is set to the number of sectors (including and immediately following
1839 * the specified sector) that are known to be in the same
1840 * allocated/unallocated state.
1841 *
1842 * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes
1843 * beyond the end of the disk image it will be clamped.
1844 */
1845static int64_t coroutine_fn raw_co_get_block_status(BlockDriverState *bs,
1846 int64_t sector_num,
67a0fd2a
FZ
1847 int nb_sectors, int *pnum,
1848 BlockDriverState **file)
4f11aa8a
HR
1849{
1850 off_t start, data = 0, hole = 0;
e6d7ec32 1851 int64_t total_size;
d7f62751 1852 int ret;
4f11aa8a
HR
1853
1854 ret = fd_open(bs);
1855 if (ret < 0) {
1856 return ret;
1857 }
1858
1859 start = sector_num * BDRV_SECTOR_SIZE;
e6d7ec32
HR
1860 total_size = bdrv_getlength(bs);
1861 if (total_size < 0) {
1862 return total_size;
1863 } else if (start >= total_size) {
1864 *pnum = 0;
1865 return 0;
1866 } else if (start + nb_sectors * BDRV_SECTOR_SIZE > total_size) {
1867 nb_sectors = DIV_ROUND_UP(total_size - start, BDRV_SECTOR_SIZE);
1868 }
4f11aa8a 1869
d1f06fe6
MA
1870 ret = find_allocation(bs, start, &data, &hole);
1871 if (ret == -ENXIO) {
1872 /* Trailing hole */
1873 *pnum = nb_sectors;
1874 ret = BDRV_BLOCK_ZERO;
1875 } else if (ret < 0) {
1876 /* No info available, so pretend there are no holes */
1877 *pnum = nb_sectors;
1878 ret = BDRV_BLOCK_DATA;
1879 } else if (data == start) {
f4a769ab
KW
1880 /* On a data extent, compute sectors to the end of the extent,
1881 * possibly including a partial sector at EOF. */
1882 *pnum = MIN(nb_sectors, DIV_ROUND_UP(hole - start, BDRV_SECTOR_SIZE));
d1f06fe6 1883 ret = BDRV_BLOCK_DATA;
5500316d
PB
1884 } else {
1885 /* On a hole, compute sectors to the beginning of the next extent. */
d1f06fe6 1886 assert(hole == start);
5500316d 1887 *pnum = MIN(nb_sectors, (data - start) / BDRV_SECTOR_SIZE);
d1f06fe6 1888 ret = BDRV_BLOCK_ZERO;
5500316d 1889 }
02650acb 1890 *file = bs;
d1f06fe6 1891 return ret | BDRV_BLOCK_OFFSET_VALID | start;
5500316d
PB
1892}
1893
4da444a0
EB
1894static coroutine_fn BlockAIOCB *raw_aio_pdiscard(BlockDriverState *bs,
1895 int64_t offset, int count,
097310b5 1896 BlockCompletionFunc *cb, void *opaque)
dce512de 1897{
dce512de
CH
1898 BDRVRawState *s = bs->opaque;
1899
4da444a0 1900 return paio_submit(bs, s->fd, offset, NULL, count,
8238010b 1901 cb, opaque, QEMU_AIO_DISCARD);
dce512de 1902}
0e7e1989 1903
2ffa76c2
EB
1904static int coroutine_fn raw_co_pwrite_zeroes(
1905 BlockDriverState *bs, int64_t offset,
1906 int count, BdrvRequestFlags flags)
260a82e5
PB
1907{
1908 BDRVRawState *s = bs->opaque;
1909
1910 if (!(flags & BDRV_REQ_MAY_UNMAP)) {
2ffa76c2 1911 return paio_submit_co(bs, s->fd, offset, NULL, count,
97a2ae34
PB
1912 QEMU_AIO_WRITE_ZEROES);
1913 } else if (s->discard_zeroes) {
2ffa76c2 1914 return paio_submit_co(bs, s->fd, offset, NULL, count,
97a2ae34 1915 QEMU_AIO_DISCARD);
260a82e5 1916 }
97a2ae34 1917 return -ENOTSUP;
260a82e5
PB
1918}
1919
1920static int raw_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1921{
1922 BDRVRawState *s = bs->opaque;
1923
1924 bdi->unallocated_blocks_are_zero = s->discard_zeroes;
1925 bdi->can_write_zeroes_with_unmap = s->discard_zeroes;
1926 return 0;
1927}
1928
6f482f74
CL
1929static QemuOptsList raw_create_opts = {
1930 .name = "raw-create-opts",
1931 .head = QTAILQ_HEAD_INITIALIZER(raw_create_opts.head),
1932 .desc = {
1933 {
1934 .name = BLOCK_OPT_SIZE,
1935 .type = QEMU_OPT_SIZE,
1936 .help = "Virtual disk size"
1937 },
4ab15590
CL
1938 {
1939 .name = BLOCK_OPT_NOCOW,
1940 .type = QEMU_OPT_BOOL,
1941 .help = "Turn off copy-on-write (valid only on btrfs)"
1942 },
06247428
HT
1943 {
1944 .name = BLOCK_OPT_PREALLOC,
1945 .type = QEMU_OPT_STRING,
1946 .help = "Preallocation mode (allowed values: off, falloc, full)"
1947 },
6f482f74
CL
1948 { /* end of list */ }
1949 }
0e7e1989
KW
1950};
1951
5f535a94 1952BlockDriver bdrv_file = {
84a12e66
CH
1953 .format_name = "file",
1954 .protocol_name = "file",
856ae5c3 1955 .instance_size = sizeof(BDRVRawState),
030be321 1956 .bdrv_needs_filename = true,
856ae5c3 1957 .bdrv_probe = NULL, /* no probe for protocols */
078896a9 1958 .bdrv_parse_filename = raw_parse_filename,
66f82cee 1959 .bdrv_file_open = raw_open,
eeb6b45d
JC
1960 .bdrv_reopen_prepare = raw_reopen_prepare,
1961 .bdrv_reopen_commit = raw_reopen_commit,
1962 .bdrv_reopen_abort = raw_reopen_abort,
856ae5c3 1963 .bdrv_close = raw_close,
c282e1fd 1964 .bdrv_create = raw_create,
3ac21627 1965 .bdrv_has_zero_init = bdrv_has_zero_init_1,
b6b8a333 1966 .bdrv_co_get_block_status = raw_co_get_block_status,
2ffa76c2 1967 .bdrv_co_pwrite_zeroes = raw_co_pwrite_zeroes,
3b46e624 1968
9d52aa3c
KW
1969 .bdrv_co_preadv = raw_co_preadv,
1970 .bdrv_co_pwritev = raw_co_pwritev,
b2e12bc6 1971 .bdrv_aio_flush = raw_aio_flush,
4da444a0 1972 .bdrv_aio_pdiscard = raw_aio_pdiscard,
c25f53b0 1973 .bdrv_refresh_limits = raw_refresh_limits,
1b3abdcc
ML
1974 .bdrv_io_plug = raw_aio_plug,
1975 .bdrv_io_unplug = raw_aio_unplug,
3c529d93 1976
83f64091
FB
1977 .bdrv_truncate = raw_truncate,
1978 .bdrv_getlength = raw_getlength,
260a82e5 1979 .bdrv_get_info = raw_get_info,
4a1d5e1f
FZ
1980 .bdrv_get_allocated_file_size
1981 = raw_get_allocated_file_size,
0e7e1989 1982
6f482f74 1983 .create_opts = &raw_create_opts,
83f64091
FB
1984};
1985
19cb3738
FB
1986/***********************************************/
1987/* host device */
1988
83affaa6 1989#if defined(__APPLE__) && defined(__MACH__)
98caa5bc
JA
1990static kern_return_t GetBSDPath(io_iterator_t mediaIterator, char *bsdPath,
1991 CFIndex maxPathSize, int flags);
d0855f12 1992static char *FindEjectableOpticalMedia(io_iterator_t *mediaIterator)
19cb3738 1993{
d0855f12 1994 kern_return_t kernResult = KERN_FAILURE;
19cb3738
FB
1995 mach_port_t masterPort;
1996 CFMutableDictionaryRef classesToMatch;
d0855f12
JA
1997 const char *matching_array[] = {kIODVDMediaClass, kIOCDMediaClass};
1998 char *mediaType = NULL;
19cb3738
FB
1999
2000 kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
2001 if ( KERN_SUCCESS != kernResult ) {
2002 printf( "IOMasterPort returned %d\n", kernResult );
2003 }
3b46e624 2004
d0855f12
JA
2005 int index;
2006 for (index = 0; index < ARRAY_SIZE(matching_array); index++) {
2007 classesToMatch = IOServiceMatching(matching_array[index]);
2008 if (classesToMatch == NULL) {
2009 error_report("IOServiceMatching returned NULL for %s",
2010 matching_array[index]);
2011 continue;
2012 }
2013 CFDictionarySetValue(classesToMatch, CFSTR(kIOMediaEjectableKey),
2014 kCFBooleanTrue);
2015 kernResult = IOServiceGetMatchingServices(masterPort, classesToMatch,
2016 mediaIterator);
2017 if (kernResult != KERN_SUCCESS) {
2018 error_report("Note: IOServiceGetMatchingServices returned %d",
2019 kernResult);
2020 continue;
2021 }
3b46e624 2022
d0855f12
JA
2023 /* If a match was found, leave the loop */
2024 if (*mediaIterator != 0) {
2025 DPRINTF("Matching using %s\n", matching_array[index]);
2026 mediaType = g_strdup(matching_array[index]);
2027 break;
2028 }
2029 }
2030 return mediaType;
19cb3738
FB
2031}
2032
98caa5bc
JA
2033kern_return_t GetBSDPath(io_iterator_t mediaIterator, char *bsdPath,
2034 CFIndex maxPathSize, int flags)
19cb3738
FB
2035{
2036 io_object_t nextMedia;
2037 kern_return_t kernResult = KERN_FAILURE;
2038 *bsdPath = '\0';
2039 nextMedia = IOIteratorNext( mediaIterator );
2040 if ( nextMedia )
2041 {
2042 CFTypeRef bsdPathAsCFString;
2043 bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
2044 if ( bsdPathAsCFString ) {
2045 size_t devPathLength;
2046 strcpy( bsdPath, _PATH_DEV );
98caa5bc
JA
2047 if (flags & BDRV_O_NOCACHE) {
2048 strcat(bsdPath, "r");
2049 }
19cb3738
FB
2050 devPathLength = strlen( bsdPath );
2051 if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
2052 kernResult = KERN_SUCCESS;
2053 }
2054 CFRelease( bsdPathAsCFString );
2055 }
2056 IOObjectRelease( nextMedia );
2057 }
3b46e624 2058
19cb3738
FB
2059 return kernResult;
2060}
2061
d0855f12
JA
2062/* Sets up a real cdrom for use in QEMU */
2063static bool setup_cdrom(char *bsd_path, Error **errp)
2064{
2065 int index, num_of_test_partitions = 2, fd;
2066 char test_partition[MAXPATHLEN];
2067 bool partition_found = false;
2068
2069 /* look for a working partition */
2070 for (index = 0; index < num_of_test_partitions; index++) {
2071 snprintf(test_partition, sizeof(test_partition), "%ss%d", bsd_path,
2072 index);
2073 fd = qemu_open(test_partition, O_RDONLY | O_BINARY | O_LARGEFILE);
2074 if (fd >= 0) {
2075 partition_found = true;
2076 qemu_close(fd);
2077 break;
2078 }
2079 }
2080
2081 /* if a working partition on the device was not found */
2082 if (partition_found == false) {
2083 error_setg(errp, "Failed to find a working partition on disc");
2084 } else {
2085 DPRINTF("Using %s as optical disc\n", test_partition);
2086 pstrcpy(bsd_path, MAXPATHLEN, test_partition);
2087 }
2088 return partition_found;
2089}
2090
2091/* Prints directions on mounting and unmounting a device */
2092static void print_unmounting_directions(const char *file_name)
2093{
2094 error_report("If device %s is mounted on the desktop, unmount"
2095 " it first before using it in QEMU", file_name);
2096 error_report("Command to unmount device: diskutil unmountDisk %s",
2097 file_name);
2098 error_report("Command to mount device: diskutil mountDisk %s", file_name);
2099}
2100
2101#endif /* defined(__APPLE__) && defined(__MACH__) */
19cb3738 2102
508c7cb3
CH
2103static int hdev_probe_device(const char *filename)
2104{
2105 struct stat st;
2106
2107 /* allow a dedicated CD-ROM driver to match with a higher priority */
2108 if (strstart(filename, "/dev/cdrom", NULL))
2109 return 50;
2110
2111 if (stat(filename, &st) >= 0 &&
2112 (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
2113 return 100;
2114 }
2115
2116 return 0;
2117}
2118
da888d37
SH
2119static int check_hdev_writable(BDRVRawState *s)
2120{
2121#if defined(BLKROGET)
2122 /* Linux block devices can be configured "read-only" using blockdev(8).
2123 * This is independent of device node permissions and therefore open(2)
2124 * with O_RDWR succeeds. Actual writes fail with EPERM.
2125 *
2126 * bdrv_open() is supposed to fail if the disk is read-only. Explicitly
2127 * check for read-only block devices so that Linux block devices behave
2128 * properly.
2129 */
2130 struct stat st;
2131 int readonly = 0;
2132
2133 if (fstat(s->fd, &st)) {
2134 return -errno;
2135 }
2136
2137 if (!S_ISBLK(st.st_mode)) {
2138 return 0;
2139 }
2140
2141 if (ioctl(s->fd, BLKROGET, &readonly) < 0) {
2142 return -errno;
2143 }
2144
2145 if (readonly) {
2146 return -EACCES;
2147 }
2148#endif /* defined(BLKROGET) */
2149 return 0;
2150}
2151
7af803d4
HR
2152static void hdev_parse_filename(const char *filename, QDict *options,
2153 Error **errp)
2154{
2155 /* The prefix is optional, just as for "file". */
2156 strstart(filename, "host_device:", &filename);
2157
2158 qdict_put_obj(options, "filename", QOBJECT(qstring_from_str(filename)));
2159}
2160
3307ed7b
DA
2161static bool hdev_is_sg(BlockDriverState *bs)
2162{
2163
2164#if defined(__linux__)
2165
0d4377b3 2166 BDRVRawState *s = bs->opaque;
3307ed7b
DA
2167 struct stat st;
2168 struct sg_scsi_id scsiid;
2169 int sg_version;
0d4377b3
KW
2170 int ret;
2171
2172 if (stat(bs->filename, &st) < 0 || !S_ISCHR(st.st_mode)) {
2173 return false;
2174 }
3307ed7b 2175
0d4377b3
KW
2176 ret = ioctl(s->fd, SG_GET_VERSION_NUM, &sg_version);
2177 if (ret < 0) {
2178 return false;
2179 }
2180
2181 ret = ioctl(s->fd, SG_GET_SCSI_ID, &scsiid);
2182 if (ret >= 0) {
3307ed7b
DA
2183 DPRINTF("SG device found: type=%d, version=%d\n",
2184 scsiid.scsi_type, sg_version);
2185 return true;
2186 }
2187
2188#endif
2189
2190 return false;
2191}
2192
015a1036
HR
2193static int hdev_open(BlockDriverState *bs, QDict *options, int flags,
2194 Error **errp)
19cb3738
FB
2195{
2196 BDRVRawState *s = bs->opaque;
e428e439 2197 Error *local_err = NULL;
da888d37 2198 int ret;
a76bab49 2199
83affaa6 2200#if defined(__APPLE__) && defined(__MACH__)
129c7d1c
MA
2201 /*
2202 * Caution: while qdict_get_str() is fine, getting non-string types
2203 * would require more care. When @options come from -blockdev or
2204 * blockdev_add, its members are typed according to the QAPI
2205 * schema, but when they come from -drive, they're all QString.
2206 */
3307ed7b 2207 const char *filename = qdict_get_str(options, "filename");
d0855f12
JA
2208 char bsd_path[MAXPATHLEN] = "";
2209 bool error_occurred = false;
2210
2211 /* If using a real cdrom */
2212 if (strcmp(filename, "/dev/cdrom") == 0) {
2213 char *mediaType = NULL;
2214 kern_return_t ret_val;
2215 io_iterator_t mediaIterator = 0;
2216
2217 mediaType = FindEjectableOpticalMedia(&mediaIterator);
2218 if (mediaType == NULL) {
2219 error_setg(errp, "Please make sure your CD/DVD is in the optical"
2220 " drive");
2221 error_occurred = true;
2222 goto hdev_open_Mac_error;
2223 }
3307ed7b 2224
d0855f12
JA
2225 ret_val = GetBSDPath(mediaIterator, bsd_path, sizeof(bsd_path), flags);
2226 if (ret_val != KERN_SUCCESS) {
2227 error_setg(errp, "Could not get BSD path for optical drive");
2228 error_occurred = true;
2229 goto hdev_open_Mac_error;
2230 }
2231
2232 /* If a real optical drive was not found */
2233 if (bsd_path[0] == '\0') {
2234 error_setg(errp, "Failed to obtain bsd path for optical drive");
2235 error_occurred = true;
2236 goto hdev_open_Mac_error;
2237 }
2238
2239 /* If using a cdrom disc and finding a partition on the disc failed */
2240 if (strncmp(mediaType, kIOCDMediaClass, 9) == 0 &&
2241 setup_cdrom(bsd_path, errp) == false) {
2242 print_unmounting_directions(bsd_path);
2243 error_occurred = true;
2244 goto hdev_open_Mac_error;
19cb3738 2245 }
3b46e624 2246
d0855f12
JA
2247 qdict_put(options, "filename", qstring_from_str(bsd_path));
2248
2249hdev_open_Mac_error:
2250 g_free(mediaType);
2251 if (mediaIterator) {
2252 IOObjectRelease(mediaIterator);
2253 }
2254 if (error_occurred) {
2255 return -ENOENT;
2256 }
19cb3738 2257 }
d0855f12 2258#endif /* defined(__APPLE__) && defined(__MACH__) */
19cb3738
FB
2259
2260 s->type = FTYPE_FILE;
90babde0 2261
e428e439 2262 ret = raw_open_common(bs, options, flags, 0, &local_err);
da888d37 2263 if (ret < 0) {
621ff94d 2264 error_propagate(errp, local_err);
d0855f12
JA
2265#if defined(__APPLE__) && defined(__MACH__)
2266 if (*bsd_path) {
2267 filename = bsd_path;
2268 }
2269 /* if a physical device experienced an error while being opened */
2270 if (strncmp(filename, "/dev/", 5) == 0) {
2271 print_unmounting_directions(filename);
2272 }
2273#endif /* defined(__APPLE__) && defined(__MACH__) */
da888d37
SH
2274 return ret;
2275 }
2276
3307ed7b
DA
2277 /* Since this does ioctl the device must be already opened */
2278 bs->sg = hdev_is_sg(bs);
2279
da888d37
SH
2280 if (flags & BDRV_O_RDWR) {
2281 ret = check_hdev_writable(s);
2282 if (ret < 0) {
2283 raw_close(bs);
e428e439 2284 error_setg_errno(errp, -ret, "The device is not writable");
da888d37
SH
2285 return ret;
2286 }
2287 }
2288
2289 return ret;
19cb3738
FB
2290}
2291
03ff3ca3 2292#if defined(__linux__)
221f715d 2293
7c84b1b8 2294static BlockAIOCB *hdev_aio_ioctl(BlockDriverState *bs,
221f715d 2295 unsigned long int req, void *buf,
097310b5 2296 BlockCompletionFunc *cb, void *opaque)
221f715d 2297{
f141eafe 2298 BDRVRawState *s = bs->opaque;
c208e8c2 2299 RawPosixAIOData *acb;
c4d9d196 2300 ThreadPool *pool;
221f715d 2301
f141eafe
AL
2302 if (fd_open(bs) < 0)
2303 return NULL;
c208e8c2 2304
c84b3192 2305 acb = g_new(RawPosixAIOData, 1);
c208e8c2
PB
2306 acb->bs = bs;
2307 acb->aio_type = QEMU_AIO_IOCTL;
2308 acb->aio_fildes = s->fd;
2309 acb->aio_offset = 0;
2310 acb->aio_ioctl_buf = buf;
2311 acb->aio_ioctl_cmd = req;
c4d9d196
SH
2312 pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
2313 return thread_pool_submit_aio(pool, aio_worker, acb, cb, opaque);
221f715d 2314}
f709623b 2315#endif /* linux */
221f715d 2316
9f23011a
BS
2317static int fd_open(BlockDriverState *bs)
2318{
2319 BDRVRawState *s = bs->opaque;
2320
2321 /* this is just to ensure s->fd is sane (its called by io ops) */
2322 if (s->fd >= 0)
2323 return 0;
2324 return -EIO;
2325}
04eeb8b6 2326
4da444a0
EB
2327static coroutine_fn BlockAIOCB *hdev_aio_pdiscard(BlockDriverState *bs,
2328 int64_t offset, int count,
097310b5 2329 BlockCompletionFunc *cb, void *opaque)
c36dd8a0
AF
2330{
2331 BDRVRawState *s = bs->opaque;
2332
2333 if (fd_open(bs) < 0) {
2334 return NULL;
2335 }
4da444a0 2336 return paio_submit(bs, s->fd, offset, NULL, count,
c36dd8a0
AF
2337 cb, opaque, QEMU_AIO_DISCARD|QEMU_AIO_BLKDEV);
2338}
2339
2ffa76c2
EB
2340static coroutine_fn int hdev_co_pwrite_zeroes(BlockDriverState *bs,
2341 int64_t offset, int count, BdrvRequestFlags flags)
d0b4503e
PB
2342{
2343 BDRVRawState *s = bs->opaque;
2344 int rc;
2345
2346 rc = fd_open(bs);
2347 if (rc < 0) {
2348 return rc;
2349 }
2350 if (!(flags & BDRV_REQ_MAY_UNMAP)) {
2ffa76c2 2351 return paio_submit_co(bs, s->fd, offset, NULL, count,
97a2ae34
PB
2352 QEMU_AIO_WRITE_ZEROES|QEMU_AIO_BLKDEV);
2353 } else if (s->discard_zeroes) {
2ffa76c2 2354 return paio_submit_co(bs, s->fd, offset, NULL, count,
97a2ae34 2355 QEMU_AIO_DISCARD|QEMU_AIO_BLKDEV);
d0b4503e 2356 }
97a2ae34 2357 return -ENOTSUP;
d0b4503e
PB
2358}
2359
6f482f74 2360static int hdev_create(const char *filename, QemuOpts *opts,
d5124c00 2361 Error **errp)
93c65b47
AL
2362{
2363 int fd;
2364 int ret = 0;
2365 struct stat stat_buf;
0e7e1989 2366 int64_t total_size = 0;
cc28c6aa
HR
2367 bool has_prefix;
2368
f709623b
HR
2369 /* This function is used by both protocol block drivers and therefore either
2370 * of these prefixes may be given.
cc28c6aa
HR
2371 * The return value has to be stored somewhere, otherwise this is an error
2372 * due to -Werror=unused-value. */
2373 has_prefix =
2374 strstart(filename, "host_device:", &filename) ||
f709623b 2375 strstart(filename, "host_cdrom:" , &filename);
cc28c6aa
HR
2376
2377 (void)has_prefix;
93c65b47 2378
bdd03cdf
HR
2379 ret = raw_normalize_devicepath(&filename);
2380 if (ret < 0) {
2381 error_setg_errno(errp, -ret, "Could not normalize device path");
2382 return ret;
2383 }
2384
0e7e1989 2385 /* Read out options */
180e9526
HT
2386 total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
2387 BDRV_SECTOR_SIZE);
93c65b47 2388
6165f4d8 2389 fd = qemu_open(filename, O_WRONLY | O_BINARY);
e428e439
HR
2390 if (fd < 0) {
2391 ret = -errno;
2392 error_setg_errno(errp, -ret, "Could not open device");
2393 return ret;
2394 }
93c65b47 2395
e428e439 2396 if (fstat(fd, &stat_buf) < 0) {
57e69b7d 2397 ret = -errno;
e428e439
HR
2398 error_setg_errno(errp, -ret, "Could not stat device");
2399 } else if (!S_ISBLK(stat_buf.st_mode) && !S_ISCHR(stat_buf.st_mode)) {
2400 error_setg(errp,
2401 "The given file is neither a block nor a character device");
57e69b7d 2402 ret = -ENODEV;
180e9526 2403 } else if (lseek(fd, 0, SEEK_END) < total_size) {
e428e439 2404 error_setg(errp, "Device is too small");
93c65b47 2405 ret = -ENOSPC;
e428e439 2406 }
93c65b47 2407
2e1e79da 2408 qemu_close(fd);
93c65b47
AL
2409 return ret;
2410}
2411
5efa9d5a 2412static BlockDriver bdrv_host_device = {
0b4ce02e 2413 .format_name = "host_device",
84a12e66 2414 .protocol_name = "host_device",
0b4ce02e 2415 .instance_size = sizeof(BDRVRawState),
030be321 2416 .bdrv_needs_filename = true,
0b4ce02e 2417 .bdrv_probe_device = hdev_probe_device,
7af803d4 2418 .bdrv_parse_filename = hdev_parse_filename,
66f82cee 2419 .bdrv_file_open = hdev_open,
0b4ce02e 2420 .bdrv_close = raw_close,
1bc6b705
JC
2421 .bdrv_reopen_prepare = raw_reopen_prepare,
2422 .bdrv_reopen_commit = raw_reopen_commit,
2423 .bdrv_reopen_abort = raw_reopen_abort,
c282e1fd 2424 .bdrv_create = hdev_create,
6f482f74 2425 .create_opts = &raw_create_opts,
2ffa76c2 2426 .bdrv_co_pwrite_zeroes = hdev_co_pwrite_zeroes,
3b46e624 2427
9d52aa3c
KW
2428 .bdrv_co_preadv = raw_co_preadv,
2429 .bdrv_co_pwritev = raw_co_pwritev,
b2e12bc6 2430 .bdrv_aio_flush = raw_aio_flush,
4da444a0 2431 .bdrv_aio_pdiscard = hdev_aio_pdiscard,
c25f53b0 2432 .bdrv_refresh_limits = raw_refresh_limits,
1b3abdcc
ML
2433 .bdrv_io_plug = raw_aio_plug,
2434 .bdrv_io_unplug = raw_aio_unplug,
3c529d93 2435
55b949c8 2436 .bdrv_truncate = raw_truncate,
e60f469c 2437 .bdrv_getlength = raw_getlength,
260a82e5 2438 .bdrv_get_info = raw_get_info,
4a1d5e1f
FZ
2439 .bdrv_get_allocated_file_size
2440 = raw_get_allocated_file_size,
1a9335e4
ET
2441 .bdrv_probe_blocksizes = hdev_probe_blocksizes,
2442 .bdrv_probe_geometry = hdev_probe_geometry,
19cb3738 2443
f3a5d3f8 2444 /* generic scsi device */
63ec93db 2445#ifdef __linux__
63ec93db
CH
2446 .bdrv_aio_ioctl = hdev_aio_ioctl,
2447#endif
f3a5d3f8
CH
2448};
2449
18fa1c42
HR
2450#if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
2451static void cdrom_parse_filename(const char *filename, QDict *options,
2452 Error **errp)
2453{
2454 /* The prefix is optional, just as for "file". */
2455 strstart(filename, "host_cdrom:", &filename);
2456
2457 qdict_put_obj(options, "filename", QOBJECT(qstring_from_str(filename)));
2458}
2459#endif
2460
2461#ifdef __linux__
015a1036
HR
2462static int cdrom_open(BlockDriverState *bs, QDict *options, int flags,
2463 Error **errp)
f3a5d3f8
CH
2464{
2465 BDRVRawState *s = bs->opaque;
2466
f3a5d3f8
CH
2467 s->type = FTYPE_CD;
2468
19a3da7f 2469 /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
9be38598 2470 return raw_open_common(bs, options, flags, O_NONBLOCK, errp);
f3a5d3f8
CH
2471}
2472
508c7cb3
CH
2473static int cdrom_probe_device(const char *filename)
2474{
3baf720e
CR
2475 int fd, ret;
2476 int prio = 0;
343f8568 2477 struct stat st;
3baf720e 2478
6165f4d8 2479 fd = qemu_open(filename, O_RDONLY | O_NONBLOCK);
3baf720e
CR
2480 if (fd < 0) {
2481 goto out;
2482 }
343f8568
JS
2483 ret = fstat(fd, &st);
2484 if (ret == -1 || !S_ISBLK(st.st_mode)) {
2485 goto outc;
2486 }
3baf720e
CR
2487
2488 /* Attempt to detect via a CDROM specific ioctl */
2489 ret = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
2490 if (ret >= 0)
2491 prio = 100;
2492
343f8568 2493outc:
2e1e79da 2494 qemu_close(fd);
3baf720e
CR
2495out:
2496 return prio;
508c7cb3
CH
2497}
2498
e031f750 2499static bool cdrom_is_inserted(BlockDriverState *bs)
f3a5d3f8
CH
2500{
2501 BDRVRawState *s = bs->opaque;
2502 int ret;
2503
2504 ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
e031f750 2505 return ret == CDS_DISC_OK;
f3a5d3f8
CH
2506}
2507
f36f3949 2508static void cdrom_eject(BlockDriverState *bs, bool eject_flag)
f3a5d3f8
CH
2509{
2510 BDRVRawState *s = bs->opaque;
2511
2512 if (eject_flag) {
2513 if (ioctl(s->fd, CDROMEJECT, NULL) < 0)
2514 perror("CDROMEJECT");
2515 } else {
2516 if (ioctl(s->fd, CDROMCLOSETRAY, NULL) < 0)
2517 perror("CDROMEJECT");
2518 }
f3a5d3f8
CH
2519}
2520
025e849a 2521static void cdrom_lock_medium(BlockDriverState *bs, bool locked)
f3a5d3f8
CH
2522{
2523 BDRVRawState *s = bs->opaque;
2524
2525 if (ioctl(s->fd, CDROM_LOCKDOOR, locked) < 0) {
2526 /*
2527 * Note: an error can happen if the distribution automatically
2528 * mounts the CD-ROM
2529 */
2530 /* perror("CDROM_LOCKDOOR"); */
2531 }
f3a5d3f8
CH
2532}
2533
2534static BlockDriver bdrv_host_cdrom = {
2535 .format_name = "host_cdrom",
84a12e66 2536 .protocol_name = "host_cdrom",
f3a5d3f8 2537 .instance_size = sizeof(BDRVRawState),
030be321 2538 .bdrv_needs_filename = true,
508c7cb3 2539 .bdrv_probe_device = cdrom_probe_device,
18fa1c42 2540 .bdrv_parse_filename = cdrom_parse_filename,
66f82cee 2541 .bdrv_file_open = cdrom_open,
f3a5d3f8 2542 .bdrv_close = raw_close,
1bc6b705
JC
2543 .bdrv_reopen_prepare = raw_reopen_prepare,
2544 .bdrv_reopen_commit = raw_reopen_commit,
2545 .bdrv_reopen_abort = raw_reopen_abort,
c282e1fd 2546 .bdrv_create = hdev_create,
6f482f74 2547 .create_opts = &raw_create_opts,
f3a5d3f8 2548
9d52aa3c
KW
2549
2550 .bdrv_co_preadv = raw_co_preadv,
2551 .bdrv_co_pwritev = raw_co_pwritev,
b2e12bc6 2552 .bdrv_aio_flush = raw_aio_flush,
c25f53b0 2553 .bdrv_refresh_limits = raw_refresh_limits,
1b3abdcc
ML
2554 .bdrv_io_plug = raw_aio_plug,
2555 .bdrv_io_unplug = raw_aio_unplug,
f3a5d3f8 2556
55b949c8 2557 .bdrv_truncate = raw_truncate,
b94a2610
KW
2558 .bdrv_getlength = raw_getlength,
2559 .has_variable_length = true,
4a1d5e1f
FZ
2560 .bdrv_get_allocated_file_size
2561 = raw_get_allocated_file_size,
f3a5d3f8
CH
2562
2563 /* removable device support */
2564 .bdrv_is_inserted = cdrom_is_inserted,
2565 .bdrv_eject = cdrom_eject,
025e849a 2566 .bdrv_lock_medium = cdrom_lock_medium,
f3a5d3f8
CH
2567
2568 /* generic scsi device */
63ec93db 2569 .bdrv_aio_ioctl = hdev_aio_ioctl,
f3a5d3f8
CH
2570};
2571#endif /* __linux__ */
2572
a167ba50 2573#if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
511018e4
AT
2574static int cdrom_open(BlockDriverState *bs, QDict *options, int flags,
2575 Error **errp)
f3a5d3f8
CH
2576{
2577 BDRVRawState *s = bs->opaque;
e428e439 2578 Error *local_err = NULL;
f3a5d3f8
CH
2579 int ret;
2580
2581 s->type = FTYPE_CD;
2582
e428e439
HR
2583 ret = raw_open_common(bs, options, flags, 0, &local_err);
2584 if (ret) {
621ff94d 2585 error_propagate(errp, local_err);
f3a5d3f8 2586 return ret;
e428e439 2587 }
f3a5d3f8 2588
9b2260cb 2589 /* make sure the door isn't locked at this time */
f3a5d3f8
CH
2590 ioctl(s->fd, CDIOCALLOW);
2591 return 0;
2592}
2593
508c7cb3
CH
2594static int cdrom_probe_device(const char *filename)
2595{
2596 if (strstart(filename, "/dev/cd", NULL) ||
2597 strstart(filename, "/dev/acd", NULL))
2598 return 100;
2599 return 0;
2600}
2601
f3a5d3f8
CH
2602static int cdrom_reopen(BlockDriverState *bs)
2603{
2604 BDRVRawState *s = bs->opaque;
2605 int fd;
2606
2607 /*
2608 * Force reread of possibly changed/newly loaded disc,
2609 * FreeBSD seems to not notice sometimes...
2610 */
2611 if (s->fd >= 0)
2e1e79da 2612 qemu_close(s->fd);
6165f4d8 2613 fd = qemu_open(bs->filename, s->open_flags, 0644);
f3a5d3f8
CH
2614 if (fd < 0) {
2615 s->fd = -1;
2616 return -EIO;
2617 }
2618 s->fd = fd;
2619
9b2260cb 2620 /* make sure the door isn't locked at this time */
f3a5d3f8
CH
2621 ioctl(s->fd, CDIOCALLOW);
2622 return 0;
2623}
2624
e031f750 2625static bool cdrom_is_inserted(BlockDriverState *bs)
f3a5d3f8
CH
2626{
2627 return raw_getlength(bs) > 0;
2628}
2629
f36f3949 2630static void cdrom_eject(BlockDriverState *bs, bool eject_flag)
f3a5d3f8
CH
2631{
2632 BDRVRawState *s = bs->opaque;
2633
2634 if (s->fd < 0)
822e1cd1 2635 return;
f3a5d3f8
CH
2636
2637 (void) ioctl(s->fd, CDIOCALLOW);
2638
2639 if (eject_flag) {
2640 if (ioctl(s->fd, CDIOCEJECT) < 0)
2641 perror("CDIOCEJECT");
2642 } else {
2643 if (ioctl(s->fd, CDIOCCLOSE) < 0)
2644 perror("CDIOCCLOSE");
2645 }
2646
822e1cd1 2647 cdrom_reopen(bs);
f3a5d3f8
CH
2648}
2649
025e849a 2650static void cdrom_lock_medium(BlockDriverState *bs, bool locked)
f3a5d3f8
CH
2651{
2652 BDRVRawState *s = bs->opaque;
2653
2654 if (s->fd < 0)
7bf37fed 2655 return;
f3a5d3f8
CH
2656 if (ioctl(s->fd, (locked ? CDIOCPREVENT : CDIOCALLOW)) < 0) {
2657 /*
2658 * Note: an error can happen if the distribution automatically
2659 * mounts the CD-ROM
2660 */
2661 /* perror("CDROM_LOCKDOOR"); */
2662 }
f3a5d3f8
CH
2663}
2664
2665static BlockDriver bdrv_host_cdrom = {
2666 .format_name = "host_cdrom",
84a12e66 2667 .protocol_name = "host_cdrom",
f3a5d3f8 2668 .instance_size = sizeof(BDRVRawState),
030be321 2669 .bdrv_needs_filename = true,
508c7cb3 2670 .bdrv_probe_device = cdrom_probe_device,
18fa1c42 2671 .bdrv_parse_filename = cdrom_parse_filename,
66f82cee 2672 .bdrv_file_open = cdrom_open,
f3a5d3f8 2673 .bdrv_close = raw_close,
1bc6b705
JC
2674 .bdrv_reopen_prepare = raw_reopen_prepare,
2675 .bdrv_reopen_commit = raw_reopen_commit,
2676 .bdrv_reopen_abort = raw_reopen_abort,
c282e1fd 2677 .bdrv_create = hdev_create,
6f482f74 2678 .create_opts = &raw_create_opts,
f3a5d3f8 2679
9d52aa3c
KW
2680 .bdrv_co_preadv = raw_co_preadv,
2681 .bdrv_co_pwritev = raw_co_pwritev,
b2e12bc6 2682 .bdrv_aio_flush = raw_aio_flush,
c25f53b0 2683 .bdrv_refresh_limits = raw_refresh_limits,
1b3abdcc
ML
2684 .bdrv_io_plug = raw_aio_plug,
2685 .bdrv_io_unplug = raw_aio_unplug,
f3a5d3f8 2686
55b949c8 2687 .bdrv_truncate = raw_truncate,
b94a2610
KW
2688 .bdrv_getlength = raw_getlength,
2689 .has_variable_length = true,
4a1d5e1f
FZ
2690 .bdrv_get_allocated_file_size
2691 = raw_get_allocated_file_size,
f3a5d3f8 2692
19cb3738 2693 /* removable device support */
f3a5d3f8
CH
2694 .bdrv_is_inserted = cdrom_is_inserted,
2695 .bdrv_eject = cdrom_eject,
025e849a 2696 .bdrv_lock_medium = cdrom_lock_medium,
19cb3738 2697};
f3a5d3f8 2698#endif /* __FreeBSD__ */
5efa9d5a 2699
84a12e66 2700static void bdrv_file_init(void)
5efa9d5a 2701{
508c7cb3
CH
2702 /*
2703 * Register all the drivers. Note that order is important, the driver
2704 * registered last will get probed first.
2705 */
84a12e66 2706 bdrv_register(&bdrv_file);
5efa9d5a 2707 bdrv_register(&bdrv_host_device);
f3a5d3f8 2708#ifdef __linux__
f3a5d3f8
CH
2709 bdrv_register(&bdrv_host_cdrom);
2710#endif
a167ba50 2711#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
f3a5d3f8
CH
2712 bdrv_register(&bdrv_host_cdrom);
2713#endif
5efa9d5a
AL
2714}
2715
84a12e66 2716block_init(bdrv_file_init);
This page took 1.080879 seconds and 4 git commands to generate.