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