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