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