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