]>
Commit | Line | Data |
---|---|---|
83f64091 | 1 | /* |
223d4670 | 2 | * Block driver for RAW files (posix) |
5fafdf24 | 3 | * |
83f64091 | 4 | * Copyright (c) 2006 Fabrice Bellard |
5fafdf24 | 5 | * |
83f64091 FB |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
922a01a0 | 24 | |
80c71a24 | 25 | #include "qemu/osdep.h" |
a8d25326 | 26 | #include "qemu-common.h" |
da34e65c | 27 | #include "qapi/error.h" |
f348b6d1 | 28 | #include "qemu/cutils.h" |
d49b6836 | 29 | #include "qemu/error-report.h" |
737e150e | 30 | #include "block/block_int.h" |
1de7afc9 | 31 | #include "qemu/module.h" |
922a01a0 | 32 | #include "qemu/option.h" |
ffa244c8 | 33 | #include "qemu/units.h" |
de81a169 | 34 | #include "trace.h" |
737e150e | 35 | #include "block/thread-pool.h" |
1de7afc9 | 36 | #include "qemu/iov.h" |
0187f5c9 | 37 | #include "block/raw-aio.h" |
452fcdbc | 38 | #include "qapi/qmp/qdict.h" |
d49b6836 | 39 | #include "qapi/qmp/qstring.h" |
83f64091 | 40 | |
7c9e5276 PB |
41 | #include "scsi/pr-manager.h" |
42 | #include "scsi/constants.h" | |
43 | ||
83affaa6 | 44 | #if defined(__APPLE__) && (__MACH__) |
14176c8d JD |
45 | #include <sys/ioctl.h> |
46 | #if defined(HAVE_HOST_BLOCK_DEVICE) | |
83f64091 FB |
47 | #include <paths.h> |
48 | #include <sys/param.h> | |
49 | #include <IOKit/IOKitLib.h> | |
50 | #include <IOKit/IOBSD.h> | |
51 | #include <IOKit/storage/IOMediaBSDClient.h> | |
52 | #include <IOKit/storage/IOMedia.h> | |
53 | #include <IOKit/storage/IOCDMedia.h> | |
54 | //#include <IOKit/storage/IOCDTypes.h> | |
d0855f12 | 55 | #include <IOKit/storage/IODVDMedia.h> |
83f64091 | 56 | #include <CoreFoundation/CoreFoundation.h> |
14176c8d | 57 | #endif /* defined(HAVE_HOST_BLOCK_DEVICE) */ |
83f64091 FB |
58 | #endif |
59 | ||
60 | #ifdef __sun__ | |
2e9671da | 61 | #define _POSIX_PTHREAD_SEMANTICS 1 |
83f64091 FB |
62 | #include <sys/dkio.h> |
63 | #endif | |
19cb3738 FB |
64 | #ifdef __linux__ |
65 | #include <sys/ioctl.h> | |
05acda4d | 66 | #include <sys/param.h> |
1efad060 | 67 | #include <sys/syscall.h> |
5edc8557 | 68 | #include <sys/vfs.h> |
19cb3738 FB |
69 | #include <linux/cdrom.h> |
70 | #include <linux/fd.h> | |
5500316d | 71 | #include <linux/fs.h> |
1a9335e4 | 72 | #include <linux/hdreg.h> |
5edc8557 | 73 | #include <linux/magic.h> |
3307ed7b | 74 | #include <scsi/sg.h> |
1a9335e4 ET |
75 | #ifdef __s390__ |
76 | #include <asm/dasd.h> | |
77 | #endif | |
4ab15590 CL |
78 | #ifndef FS_NOCOW_FL |
79 | #define FS_NOCOW_FL 0x00800000 /* Do not cow file */ | |
80 | #endif | |
5500316d | 81 | #endif |
b953f075 | 82 | #if defined(CONFIG_FALLOCATE_PUNCH_HOLE) || defined(CONFIG_FALLOCATE_ZERO_RANGE) |
3d4fa43e KK |
83 | #include <linux/falloc.h> |
84 | #endif | |
a167ba50 | 85 | #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__) |
1cb6c3fd | 86 | #include <sys/disk.h> |
9f23011a | 87 | #include <sys/cdio.h> |
1cb6c3fd | 88 | #endif |
83f64091 | 89 | |
128ab2ff BS |
90 | #ifdef __OpenBSD__ |
91 | #include <sys/ioctl.h> | |
92 | #include <sys/disklabel.h> | |
93 | #include <sys/dkio.h> | |
94 | #endif | |
95 | ||
d1f6fd8d CE |
96 | #ifdef __NetBSD__ |
97 | #include <sys/ioctl.h> | |
98 | #include <sys/disklabel.h> | |
99 | #include <sys/dkio.h> | |
100 | #include <sys/disk.h> | |
101 | #endif | |
102 | ||
c5e97233 BS |
103 | #ifdef __DragonFly__ |
104 | #include <sys/ioctl.h> | |
105 | #include <sys/diskslice.h> | |
106 | #endif | |
107 | ||
dce512de CH |
108 | #ifdef CONFIG_XFS |
109 | #include <xfs/xfs.h> | |
110 | #endif | |
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 | 138 | bool use_lock; |
19cb3738 | 139 | int type; |
0e1d8f4c | 140 | int open_flags; |
c25f53b0 PB |
141 | size_t buf_align; |
142 | ||
244a5668 FZ |
143 | /* The current permissions. */ |
144 | uint64_t perm; | |
145 | uint64_t shared_perm; | |
146 | ||
2996ffad | 147 | /* The perms bits whose corresponding bytes are already locked in |
f2e3af29 | 148 | * s->fd. */ |
2996ffad FZ |
149 | uint64_t locked_perm; |
150 | uint64_t locked_shared_perm; | |
151 | ||
6ceabe6f | 152 | int perm_change_fd; |
094e3639 | 153 | int perm_change_flags; |
e0c9cf3a KW |
154 | BDRVReopenState *reopen_state; |
155 | ||
dce512de | 156 | #ifdef CONFIG_XFS |
260a82e5 | 157 | bool is_xfs:1; |
dce512de | 158 | #endif |
260a82e5 | 159 | bool has_discard:1; |
97a2ae34 | 160 | bool has_write_zeroes:1; |
260a82e5 | 161 | bool discard_zeroes:1; |
0a4279d9 | 162 | bool use_linux_aio:1; |
c6447510 | 163 | bool use_linux_io_uring:1; |
c7ddc882 | 164 | int page_cache_inconsistent; /* errno from fdatasync failure */ |
d50d8222 | 165 | bool has_fallocate; |
3cad8307 | 166 | bool needs_alignment; |
f357fcd8 | 167 | bool drop_cache; |
31be8a2a | 168 | bool check_cache_dropped; |
1c450366 AN |
169 | struct { |
170 | uint64_t discard_nb_ok; | |
171 | uint64_t discard_nb_failed; | |
172 | uint64_t discard_bytes_ok; | |
173 | } stats; | |
7c9e5276 PB |
174 | |
175 | PRManager *pr_mgr; | |
19cb3738 FB |
176 | } BDRVRawState; |
177 | ||
eeb6b45d | 178 | typedef struct BDRVRawReopenState { |
eeb6b45d | 179 | int open_flags; |
f357fcd8 | 180 | bool drop_cache; |
31be8a2a | 181 | bool check_cache_dropped; |
eeb6b45d JC |
182 | } BDRVRawReopenState; |
183 | ||
14176c8d JD |
184 | static int fd_open(BlockDriverState *bs) |
185 | { | |
186 | BDRVRawState *s = bs->opaque; | |
187 | ||
188 | /* this is just to ensure s->fd is sane (its called by io ops) */ | |
189 | if (s->fd >= 0) { | |
190 | return 0; | |
191 | } | |
192 | return -EIO; | |
193 | } | |
194 | ||
22afa7b5 | 195 | static int64_t raw_getlength(BlockDriverState *bs); |
83f64091 | 196 | |
de81a169 PB |
197 | typedef struct RawPosixAIOData { |
198 | BlockDriverState *bs; | |
d57c44d0 | 199 | int aio_type; |
de81a169 | 200 | int aio_fildes; |
d57c44d0 | 201 | |
de81a169 | 202 | off_t aio_offset; |
d57c44d0 KW |
203 | uint64_t aio_nbytes; |
204 | ||
93f4e2ff | 205 | union { |
d57c44d0 KW |
206 | struct { |
207 | struct iovec *iov; | |
208 | int niov; | |
209 | } io; | |
210 | struct { | |
211 | uint64_t cmd; | |
212 | void *buf; | |
213 | } ioctl; | |
93f4e2ff KW |
214 | struct { |
215 | int aio_fd2; | |
216 | off_t aio_offset2; | |
d57c44d0 | 217 | } copy_range; |
93f4e2ff KW |
218 | struct { |
219 | PreallocMode prealloc; | |
220 | Error **errp; | |
d57c44d0 | 221 | } truncate; |
93f4e2ff | 222 | }; |
de81a169 PB |
223 | } RawPosixAIOData; |
224 | ||
a167ba50 | 225 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
f3a5d3f8 | 226 | static int cdrom_reopen(BlockDriverState *bs); |
9f23011a BS |
227 | #endif |
228 | ||
797e3e38 DE |
229 | /* |
230 | * Elide EAGAIN and EACCES details when failing to lock, as this | |
231 | * indicates that the specified file region is already locked by | |
232 | * another process, which is considered a common scenario. | |
233 | */ | |
234 | #define raw_lock_error_setg_errno(errp, err, fmt, ...) \ | |
235 | do { \ | |
236 | if ((err) == EAGAIN || (err) == EACCES) { \ | |
237 | error_setg((errp), (fmt), ## __VA_ARGS__); \ | |
238 | } else { \ | |
239 | error_setg_errno((errp), (err), (fmt), ## __VA_ARGS__); \ | |
240 | } \ | |
241 | } while (0) | |
242 | ||
1de1ae0a | 243 | #if defined(__NetBSD__) |
db0754df | 244 | static int raw_normalize_devicepath(const char **filename, Error **errp) |
1de1ae0a CE |
245 | { |
246 | static char namebuf[PATH_MAX]; | |
247 | const char *dp, *fname; | |
248 | struct stat sb; | |
249 | ||
250 | fname = *filename; | |
251 | dp = strrchr(fname, '/'); | |
252 | if (lstat(fname, &sb) < 0) { | |
f6fc1e30 | 253 | error_setg_file_open(errp, errno, fname); |
1de1ae0a CE |
254 | return -errno; |
255 | } | |
256 | ||
257 | if (!S_ISBLK(sb.st_mode)) { | |
258 | return 0; | |
259 | } | |
260 | ||
261 | if (dp == NULL) { | |
262 | snprintf(namebuf, PATH_MAX, "r%s", fname); | |
263 | } else { | |
264 | snprintf(namebuf, PATH_MAX, "%.*s/r%s", | |
265 | (int)(dp - fname), fname, dp + 1); | |
266 | } | |
1de1ae0a | 267 | *filename = namebuf; |
db0754df | 268 | warn_report("%s is a block device, using %s", fname, *filename); |
1de1ae0a CE |
269 | |
270 | return 0; | |
271 | } | |
272 | #else | |
db0754df | 273 | static int raw_normalize_devicepath(const char **filename, Error **errp) |
1de1ae0a CE |
274 | { |
275 | return 0; | |
276 | } | |
277 | #endif | |
278 | ||
8a4ed0d1 ET |
279 | /* |
280 | * Get logical block size via ioctl. On success store it in @sector_size_p. | |
281 | */ | |
282 | static int probe_logical_blocksize(int fd, unsigned int *sector_size_p) | |
c25f53b0 | 283 | { |
c25f53b0 | 284 | unsigned int sector_size; |
8a4ed0d1 | 285 | bool success = false; |
700f9ce0 | 286 | int i; |
c25f53b0 | 287 | |
8a4ed0d1 | 288 | errno = ENOTSUP; |
700f9ce0 | 289 | static const unsigned long ioctl_list[] = { |
c25f53b0 | 290 | #ifdef BLKSSZGET |
700f9ce0 | 291 | BLKSSZGET, |
c25f53b0 PB |
292 | #endif |
293 | #ifdef DKIOCGETBLOCKSIZE | |
700f9ce0 | 294 | DKIOCGETBLOCKSIZE, |
c25f53b0 PB |
295 | #endif |
296 | #ifdef DIOCGSECTORSIZE | |
700f9ce0 | 297 | DIOCGSECTORSIZE, |
c25f53b0 | 298 | #endif |
700f9ce0 PM |
299 | }; |
300 | ||
301 | /* Try a few ioctls to get the right size */ | |
302 | for (i = 0; i < (int)ARRAY_SIZE(ioctl_list); i++) { | |
303 | if (ioctl(fd, ioctl_list[i], §or_size) >= 0) { | |
304 | *sector_size_p = sector_size; | |
305 | success = true; | |
306 | } | |
307 | } | |
8a4ed0d1 ET |
308 | |
309 | return success ? 0 : -errno; | |
310 | } | |
311 | ||
1a9335e4 ET |
312 | /** |
313 | * Get physical block size of @fd. | |
314 | * On success, store it in @blk_size and return 0. | |
315 | * On failure, return -errno. | |
316 | */ | |
317 | static int probe_physical_blocksize(int fd, unsigned int *blk_size) | |
318 | { | |
319 | #ifdef BLKPBSZGET | |
320 | if (ioctl(fd, BLKPBSZGET, blk_size) < 0) { | |
321 | return -errno; | |
322 | } | |
323 | return 0; | |
324 | #else | |
325 | return -ENOTSUP; | |
326 | #endif | |
327 | } | |
328 | ||
5edc8557 KW |
329 | /* |
330 | * Returns true if no alignment restrictions are necessary even for files | |
331 | * opened with O_DIRECT. | |
332 | * | |
333 | * raw_probe_alignment() probes the required alignment and assume that 1 means | |
334 | * the probing failed, so it falls back to a safe default of 4k. This can be | |
335 | * avoided if we know that byte alignment is okay for the file. | |
336 | */ | |
337 | static bool dio_byte_aligned(int fd) | |
338 | { | |
339 | #ifdef __linux__ | |
340 | struct statfs buf; | |
341 | int ret; | |
342 | ||
343 | ret = fstatfs(fd, &buf); | |
344 | if (ret == 0 && buf.f_type == NFS_SUPER_MAGIC) { | |
345 | return true; | |
346 | } | |
347 | #endif | |
348 | return false; | |
349 | } | |
350 | ||
22d182e8 SH |
351 | /* Check if read is allowed with given memory buffer and length. |
352 | * | |
353 | * This function is used to check O_DIRECT memory buffer and request alignment. | |
354 | */ | |
355 | static bool raw_is_io_aligned(int fd, void *buf, size_t len) | |
356 | { | |
357 | ssize_t ret = pread(fd, buf, len, 0); | |
358 | ||
359 | if (ret >= 0) { | |
360 | return true; | |
361 | } | |
362 | ||
363 | #ifdef __linux__ | |
364 | /* The Linux kernel returns EINVAL for misaligned O_DIRECT reads. Ignore | |
365 | * other errors (e.g. real I/O error), which could happen on a failed | |
366 | * drive, since we only care about probing alignment. | |
367 | */ | |
368 | if (errno != EINVAL) { | |
369 | return true; | |
370 | } | |
371 | #endif | |
372 | ||
373 | return false; | |
374 | } | |
375 | ||
8a4ed0d1 ET |
376 | static void raw_probe_alignment(BlockDriverState *bs, int fd, Error **errp) |
377 | { | |
378 | BDRVRawState *s = bs->opaque; | |
379 | char *buf; | |
038adc2f | 380 | size_t max_align = MAX(MAX_BLOCKSIZE, qemu_real_host_page_size); |
a6b257a0 | 381 | size_t alignments[] = {1, 512, 1024, 2048, 4096}; |
8a4ed0d1 | 382 | |
b192af8a | 383 | /* For SCSI generic devices the alignment is not really used. |
8a4ed0d1 | 384 | With buffered I/O, we don't have any restrictions. */ |
b192af8a | 385 | if (bdrv_is_sg(bs) || !s->needs_alignment) { |
a5b8dd2c | 386 | bs->bl.request_alignment = 1; |
8a4ed0d1 ET |
387 | s->buf_align = 1; |
388 | return; | |
389 | } | |
390 | ||
a5b8dd2c | 391 | bs->bl.request_alignment = 0; |
8a4ed0d1 ET |
392 | s->buf_align = 0; |
393 | /* Let's try to use the logical blocksize for the alignment. */ | |
a5b8dd2c EB |
394 | if (probe_logical_blocksize(fd, &bs->bl.request_alignment) < 0) { |
395 | bs->bl.request_alignment = 0; | |
8a4ed0d1 | 396 | } |
c25f53b0 PB |
397 | #ifdef CONFIG_XFS |
398 | if (s->is_xfs) { | |
399 | struct dioattr da; | |
df26a350 | 400 | if (xfsctl(NULL, fd, XFS_IOC_DIOINFO, &da) >= 0) { |
a5b8dd2c | 401 | bs->bl.request_alignment = da.d_miniosz; |
c25f53b0 PB |
402 | /* The kernel returns wrong information for d_mem */ |
403 | /* s->buf_align = da.d_mem; */ | |
404 | } | |
405 | } | |
406 | #endif | |
407 | ||
a6b257a0 NS |
408 | /* |
409 | * If we could not get the sizes so far, we can only guess them. First try | |
410 | * to detect request alignment, since it is more likely to succeed. Then | |
411 | * try to detect buf_align, which cannot be detected in some cases (e.g. | |
412 | * Gluster). If buf_align cannot be detected, we fallback to the value of | |
413 | * request_alignment. | |
414 | */ | |
415 | ||
416 | if (!bs->bl.request_alignment) { | |
417 | int i; | |
c25f53b0 | 418 | size_t align; |
a6b257a0 NS |
419 | buf = qemu_memalign(max_align, max_align); |
420 | for (i = 0; i < ARRAY_SIZE(alignments); i++) { | |
421 | align = alignments[i]; | |
422 | if (raw_is_io_aligned(fd, buf, align)) { | |
423 | /* Fallback to safe value. */ | |
424 | bs->bl.request_alignment = (align != 1) ? align : max_align; | |
c25f53b0 PB |
425 | break; |
426 | } | |
427 | } | |
428 | qemu_vfree(buf); | |
429 | } | |
430 | ||
a6b257a0 NS |
431 | if (!s->buf_align) { |
432 | int i; | |
c25f53b0 | 433 | size_t align; |
a6b257a0 NS |
434 | buf = qemu_memalign(max_align, 2 * max_align); |
435 | for (i = 0; i < ARRAY_SIZE(alignments); i++) { | |
436 | align = alignments[i]; | |
437 | if (raw_is_io_aligned(fd, buf + align, max_align)) { | |
236094c7 | 438 | /* Fallback to request_alignment. */ |
a6b257a0 | 439 | s->buf_align = (align != 1) ? align : bs->bl.request_alignment; |
c25f53b0 PB |
440 | break; |
441 | } | |
442 | } | |
443 | qemu_vfree(buf); | |
444 | } | |
df26a350 | 445 | |
a5b8dd2c | 446 | if (!s->buf_align || !bs->bl.request_alignment) { |
8cc9c6e9 EB |
447 | error_setg(errp, "Could not find working O_DIRECT alignment"); |
448 | error_append_hint(errp, "Try cache.direct=off\n"); | |
df26a350 | 449 | } |
c25f53b0 PB |
450 | } |
451 | ||
bca5283b | 452 | static int check_hdev_writable(int fd) |
20eaf1bf KW |
453 | { |
454 | #if defined(BLKROGET) | |
455 | /* Linux block devices can be configured "read-only" using blockdev(8). | |
456 | * This is independent of device node permissions and therefore open(2) | |
457 | * with O_RDWR succeeds. Actual writes fail with EPERM. | |
458 | * | |
459 | * bdrv_open() is supposed to fail if the disk is read-only. Explicitly | |
460 | * check for read-only block devices so that Linux block devices behave | |
461 | * properly. | |
462 | */ | |
463 | struct stat st; | |
464 | int readonly = 0; | |
465 | ||
bca5283b | 466 | if (fstat(fd, &st)) { |
20eaf1bf KW |
467 | return -errno; |
468 | } | |
469 | ||
470 | if (!S_ISBLK(st.st_mode)) { | |
471 | return 0; | |
472 | } | |
473 | ||
bca5283b | 474 | if (ioctl(fd, BLKROGET, &readonly) < 0) { |
20eaf1bf KW |
475 | return -errno; |
476 | } | |
477 | ||
478 | if (readonly) { | |
479 | return -EACCES; | |
480 | } | |
481 | #endif /* defined(BLKROGET) */ | |
482 | return 0; | |
483 | } | |
484 | ||
23dece19 | 485 | static void raw_parse_flags(int bdrv_flags, int *open_flags, bool has_writers) |
6a8dc042 | 486 | { |
23dece19 | 487 | bool read_write = false; |
6a8dc042 JC |
488 | assert(open_flags != NULL); |
489 | ||
490 | *open_flags |= O_BINARY; | |
491 | *open_flags &= ~O_ACCMODE; | |
23dece19 KW |
492 | |
493 | if (bdrv_flags & BDRV_O_AUTO_RDONLY) { | |
494 | read_write = has_writers; | |
495 | } else if (bdrv_flags & BDRV_O_RDWR) { | |
496 | read_write = true; | |
497 | } | |
498 | ||
499 | if (read_write) { | |
6a8dc042 JC |
500 | *open_flags |= O_RDWR; |
501 | } else { | |
502 | *open_flags |= O_RDONLY; | |
503 | } | |
504 | ||
505 | /* Use O_DSYNC for write-through caching, no flags for write-back caching, | |
506 | * and O_DIRECT for no caching. */ | |
507 | if ((bdrv_flags & BDRV_O_NOCACHE)) { | |
508 | *open_flags |= O_DIRECT; | |
509 | } | |
6a8dc042 JC |
510 | } |
511 | ||
078896a9 HR |
512 | static void raw_parse_filename(const char *filename, QDict *options, |
513 | Error **errp) | |
514 | { | |
03c320d8 | 515 | bdrv_parse_filename_strip_prefix(filename, "file:", options); |
078896a9 HR |
516 | } |
517 | ||
c66a6157 KW |
518 | static QemuOptsList raw_runtime_opts = { |
519 | .name = "raw", | |
520 | .head = QTAILQ_HEAD_INITIALIZER(raw_runtime_opts.head), | |
521 | .desc = { | |
522 | { | |
523 | .name = "filename", | |
524 | .type = QEMU_OPT_STRING, | |
525 | .help = "File name of the image", | |
526 | }, | |
0a4279d9 KW |
527 | { |
528 | .name = "aio", | |
529 | .type = QEMU_OPT_STRING, | |
c6447510 | 530 | .help = "host AIO implementation (threads, native, io_uring)", |
0a4279d9 | 531 | }, |
16b48d5d FZ |
532 | { |
533 | .name = "locking", | |
534 | .type = QEMU_OPT_STRING, | |
535 | .help = "file locking mode (on/off/auto, default: auto)", | |
536 | }, | |
7c9e5276 PB |
537 | { |
538 | .name = "pr-manager", | |
539 | .type = QEMU_OPT_STRING, | |
540 | .help = "id of persistent reservation manager object (default: none)", | |
541 | }, | |
f357fcd8 SH |
542 | #if defined(__linux__) |
543 | { | |
544 | .name = "drop-cache", | |
545 | .type = QEMU_OPT_BOOL, | |
546 | .help = "invalidate page cache during live migration (default: on)", | |
547 | }, | |
548 | #endif | |
31be8a2a SH |
549 | { |
550 | .name = "x-check-cache-dropped", | |
551 | .type = QEMU_OPT_BOOL, | |
552 | .help = "check that page cache was dropped on live migration (default: off)" | |
553 | }, | |
c66a6157 KW |
554 | { /* end of list */ } |
555 | }, | |
556 | }; | |
557 | ||
8a2ce0bc AG |
558 | static const char *const mutable_opts[] = { "x-check-cache-dropped", NULL }; |
559 | ||
c66a6157 | 560 | static int raw_open_common(BlockDriverState *bs, QDict *options, |
230ff739 JS |
561 | int bdrv_flags, int open_flags, |
562 | bool device, Error **errp) | |
83f64091 FB |
563 | { |
564 | BDRVRawState *s = bs->opaque; | |
c66a6157 KW |
565 | QemuOpts *opts; |
566 | Error *local_err = NULL; | |
8bfea15d | 567 | const char *filename = NULL; |
7c9e5276 | 568 | const char *str; |
0a4279d9 | 569 | BlockdevAioOptions aio, aio_default; |
0e1d8f4c | 570 | int fd, ret; |
260a82e5 | 571 | struct stat st; |
244a5668 | 572 | OnOffAuto locking; |
83f64091 | 573 | |
87ea75d5 | 574 | opts = qemu_opts_create(&raw_runtime_opts, NULL, 0, &error_abort); |
af175e85 | 575 | if (!qemu_opts_absorb_qdict(opts, options, errp)) { |
c66a6157 KW |
576 | ret = -EINVAL; |
577 | goto fail; | |
578 | } | |
579 | ||
580 | filename = qemu_opt_get(opts, "filename"); | |
581 | ||
db0754df | 582 | ret = raw_normalize_devicepath(&filename, errp); |
1de1ae0a | 583 | if (ret != 0) { |
c66a6157 | 584 | goto fail; |
1de1ae0a CE |
585 | } |
586 | ||
c6447510 AM |
587 | if (bdrv_flags & BDRV_O_NATIVE_AIO) { |
588 | aio_default = BLOCKDEV_AIO_OPTIONS_NATIVE; | |
589 | #ifdef CONFIG_LINUX_IO_URING | |
590 | } else if (bdrv_flags & BDRV_O_IO_URING) { | |
591 | aio_default = BLOCKDEV_AIO_OPTIONS_IO_URING; | |
592 | #endif | |
593 | } else { | |
594 | aio_default = BLOCKDEV_AIO_OPTIONS_THREADS; | |
595 | } | |
596 | ||
f7abe0ec MAL |
597 | aio = qapi_enum_parse(&BlockdevAioOptions_lookup, |
598 | qemu_opt_get(opts, "aio"), | |
06c60b6c | 599 | aio_default, &local_err); |
0a4279d9 KW |
600 | if (local_err) { |
601 | error_propagate(errp, local_err); | |
602 | ret = -EINVAL; | |
603 | goto fail; | |
604 | } | |
c6447510 | 605 | |
0a4279d9 | 606 | s->use_linux_aio = (aio == BLOCKDEV_AIO_OPTIONS_NATIVE); |
c6447510 AM |
607 | #ifdef CONFIG_LINUX_IO_URING |
608 | s->use_linux_io_uring = (aio == BLOCKDEV_AIO_OPTIONS_IO_URING); | |
609 | #endif | |
0a4279d9 | 610 | |
f7abe0ec MAL |
611 | locking = qapi_enum_parse(&OnOffAuto_lookup, |
612 | qemu_opt_get(opts, "locking"), | |
06c60b6c | 613 | ON_OFF_AUTO_AUTO, &local_err); |
244a5668 FZ |
614 | if (local_err) { |
615 | error_propagate(errp, local_err); | |
616 | ret = -EINVAL; | |
617 | goto fail; | |
618 | } | |
619 | switch (locking) { | |
620 | case ON_OFF_AUTO_ON: | |
621 | s->use_lock = true; | |
2b218f5d | 622 | if (!qemu_has_ofd_lock()) { |
db0754df FZ |
623 | warn_report("File lock requested but OFD locking syscall is " |
624 | "unavailable, falling back to POSIX file locks"); | |
625 | error_printf("Due to the implementation, locks can be lost " | |
626 | "unexpectedly.\n"); | |
2b218f5d | 627 | } |
244a5668 FZ |
628 | break; |
629 | case ON_OFF_AUTO_OFF: | |
630 | s->use_lock = false; | |
631 | break; | |
632 | case ON_OFF_AUTO_AUTO: | |
2b218f5d | 633 | s->use_lock = qemu_has_ofd_lock(); |
244a5668 FZ |
634 | break; |
635 | default: | |
636 | abort(); | |
637 | } | |
638 | ||
7c9e5276 PB |
639 | str = qemu_opt_get(opts, "pr-manager"); |
640 | if (str) { | |
641 | s->pr_mgr = pr_manager_lookup(str, &local_err); | |
642 | if (local_err) { | |
643 | error_propagate(errp, local_err); | |
644 | ret = -EINVAL; | |
645 | goto fail; | |
646 | } | |
647 | } | |
648 | ||
f357fcd8 | 649 | s->drop_cache = qemu_opt_get_bool(opts, "drop-cache", true); |
31be8a2a SH |
650 | s->check_cache_dropped = qemu_opt_get_bool(opts, "x-check-cache-dropped", |
651 | false); | |
652 | ||
6a8dc042 | 653 | s->open_flags = open_flags; |
23dece19 | 654 | raw_parse_flags(bdrv_flags, &s->open_flags, false); |
83f64091 | 655 | |
90babde0 | 656 | s->fd = -1; |
b18a24a9 | 657 | fd = qemu_open(filename, s->open_flags, errp); |
64107dc0 KW |
658 | ret = fd < 0 ? -errno : 0; |
659 | ||
64107dc0 | 660 | if (ret < 0) { |
c66a6157 | 661 | if (ret == -EROFS) { |
19cb3738 | 662 | ret = -EACCES; |
c66a6157 KW |
663 | } |
664 | goto fail; | |
19cb3738 | 665 | } |
83f64091 | 666 | s->fd = fd; |
9ef91a67 | 667 | |
bca5283b KW |
668 | /* Check s->open_flags rather than bdrv_flags due to auto-read-only */ |
669 | if (s->open_flags & O_RDWR) { | |
670 | ret = check_hdev_writable(s->fd); | |
671 | if (ret < 0) { | |
672 | error_setg_errno(errp, -ret, "The device is not writable"); | |
673 | goto fail; | |
674 | } | |
675 | } | |
676 | ||
244a5668 FZ |
677 | s->perm = 0; |
678 | s->shared_perm = BLK_PERM_ALL; | |
679 | ||
5c6c3a6c | 680 | #ifdef CONFIG_LINUX_AIO |
0a4279d9 | 681 | /* Currently Linux does AIO only for files opened with O_DIRECT */ |
ed6e2161 NA |
682 | if (s->use_linux_aio) { |
683 | if (!(s->open_flags & O_DIRECT)) { | |
684 | error_setg(errp, "aio=native was specified, but it requires " | |
685 | "cache.direct=on, which was not specified."); | |
686 | ret = -EINVAL; | |
687 | goto fail; | |
688 | } | |
689 | if (!aio_setup_linux_aio(bdrv_get_aio_context(bs), errp)) { | |
690 | error_prepend(errp, "Unable to use native AIO: "); | |
691 | goto fail; | |
692 | } | |
96518254 | 693 | } |
1501ecc1 | 694 | #else |
0a4279d9 | 695 | if (s->use_linux_aio) { |
d657c0c2 KW |
696 | error_setg(errp, "aio=native was specified, but is not supported " |
697 | "in this build."); | |
698 | ret = -EINVAL; | |
699 | goto fail; | |
1501ecc1 SH |
700 | } |
701 | #endif /* !defined(CONFIG_LINUX_AIO) */ | |
9ef91a67 | 702 | |
c6447510 AM |
703 | #ifdef CONFIG_LINUX_IO_URING |
704 | if (s->use_linux_io_uring) { | |
705 | if (!aio_setup_linux_io_uring(bdrv_get_aio_context(bs), errp)) { | |
706 | error_prepend(errp, "Unable to use io_uring: "); | |
707 | goto fail; | |
708 | } | |
709 | } | |
710 | #else | |
711 | if (s->use_linux_io_uring) { | |
712 | error_setg(errp, "aio=io_uring was specified, but is not supported " | |
713 | "in this build."); | |
714 | ret = -EINVAL; | |
715 | goto fail; | |
716 | } | |
717 | #endif /* !defined(CONFIG_LINUX_IO_URING) */ | |
718 | ||
7ce21016 | 719 | s->has_discard = true; |
97a2ae34 | 720 | s->has_write_zeroes = true; |
5edc8557 | 721 | if ((bs->open_flags & BDRV_O_NOCACHE) != 0 && !dio_byte_aligned(s->fd)) { |
3cad8307 RPM |
722 | s->needs_alignment = true; |
723 | } | |
260a82e5 PB |
724 | |
725 | if (fstat(s->fd, &st) < 0) { | |
01212d4e | 726 | ret = -errno; |
260a82e5 PB |
727 | error_setg_errno(errp, errno, "Could not stat file"); |
728 | goto fail; | |
729 | } | |
230ff739 JS |
730 | |
731 | if (!device) { | |
8d17adf3 DB |
732 | if (!S_ISREG(st.st_mode)) { |
733 | error_setg(errp, "'%s' driver requires '%s' to be a regular file", | |
734 | bs->drv->format_name, bs->filename); | |
230ff739 JS |
735 | ret = -EINVAL; |
736 | goto fail; | |
737 | } else { | |
738 | s->discard_zeroes = true; | |
739 | s->has_fallocate = true; | |
740 | } | |
741 | } else { | |
742 | if (!(S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) { | |
8d17adf3 DB |
743 | error_setg(errp, "'%s' driver requires '%s' to be either " |
744 | "a character or block device", | |
745 | bs->drv->format_name, bs->filename); | |
230ff739 JS |
746 | ret = -EINVAL; |
747 | goto fail; | |
748 | } | |
260a82e5 | 749 | } |
230ff739 | 750 | |
d0b4503e PB |
751 | if (S_ISBLK(st.st_mode)) { |
752 | #ifdef BLKDISCARDZEROES | |
753 | unsigned int arg; | |
754 | if (ioctl(s->fd, BLKDISCARDZEROES, &arg) == 0 && arg) { | |
755 | s->discard_zeroes = true; | |
756 | } | |
757 | #endif | |
758 | #ifdef __linux__ | |
759 | /* On Linux 3.10, BLKDISCARD leaves stale data in the page cache. Do | |
760 | * not rely on the contents of discarded blocks unless using O_DIRECT. | |
97a2ae34 | 761 | * Same for BLKZEROOUT. |
d0b4503e PB |
762 | */ |
763 | if (!(bs->open_flags & BDRV_O_NOCACHE)) { | |
764 | s->discard_zeroes = false; | |
97a2ae34 | 765 | s->has_write_zeroes = false; |
d0b4503e PB |
766 | } |
767 | #endif | |
768 | } | |
3cad8307 RPM |
769 | #ifdef __FreeBSD__ |
770 | if (S_ISCHR(st.st_mode)) { | |
771 | /* | |
772 | * The file is a char device (disk), which on FreeBSD isn't behind | |
773 | * a pager, so force all requests to be aligned. This is needed | |
774 | * so QEMU makes sure all IO operations on the device are aligned | |
775 | * to sector size, or else FreeBSD will reject them with EINVAL. | |
776 | */ | |
777 | s->needs_alignment = true; | |
778 | } | |
779 | #endif | |
260a82e5 | 780 | |
dce512de CH |
781 | #ifdef CONFIG_XFS |
782 | if (platform_test_xfs_fd(s->fd)) { | |
7ce21016 | 783 | s->is_xfs = true; |
dce512de CH |
784 | } |
785 | #endif | |
786 | ||
738301e1 | 787 | bs->supported_zero_flags = BDRV_REQ_MAY_UNMAP | BDRV_REQ_NO_FALLBACK; |
2f0c6e7a KW |
788 | if (S_ISREG(st.st_mode)) { |
789 | /* When extending regular files, we get zeros from the OS */ | |
790 | bs->supported_truncate_flags = BDRV_REQ_ZERO_WRITE; | |
791 | } | |
c66a6157 KW |
792 | ret = 0; |
793 | fail: | |
a8c5cf27 KW |
794 | if (ret < 0 && s->fd != -1) { |
795 | qemu_close(s->fd); | |
796 | } | |
8bfea15d KW |
797 | if (filename && (bdrv_flags & BDRV_O_TEMPORARY)) { |
798 | unlink(filename); | |
799 | } | |
c66a6157 KW |
800 | qemu_opts_del(opts); |
801 | return ret; | |
83f64091 FB |
802 | } |
803 | ||
015a1036 HR |
804 | static int raw_open(BlockDriverState *bs, QDict *options, int flags, |
805 | Error **errp) | |
90babde0 CH |
806 | { |
807 | BDRVRawState *s = bs->opaque; | |
808 | ||
809 | s->type = FTYPE_FILE; | |
230ff739 | 810 | return raw_open_common(bs, options, flags, 0, false, errp); |
90babde0 CH |
811 | } |
812 | ||
244a5668 FZ |
813 | typedef enum { |
814 | RAW_PL_PREPARE, | |
815 | RAW_PL_COMMIT, | |
816 | RAW_PL_ABORT, | |
817 | } RawPermLockOp; | |
818 | ||
819 | #define PERM_FOREACH(i) \ | |
820 | for ((i) = 0; (1ULL << (i)) <= BLK_PERM_ALL; i++) | |
821 | ||
822 | /* Lock bytes indicated by @perm_lock_bits and @shared_perm_lock_bits in the | |
823 | * file; if @unlock == true, also unlock the unneeded bytes. | |
824 | * @shared_perm_lock_bits is the mask of all permissions that are NOT shared. | |
825 | */ | |
2996ffad | 826 | static int raw_apply_lock_bytes(BDRVRawState *s, int fd, |
244a5668 FZ |
827 | uint64_t perm_lock_bits, |
828 | uint64_t shared_perm_lock_bits, | |
829 | bool unlock, Error **errp) | |
830 | { | |
831 | int ret; | |
832 | int i; | |
2996ffad FZ |
833 | uint64_t locked_perm, locked_shared_perm; |
834 | ||
835 | if (s) { | |
836 | locked_perm = s->locked_perm; | |
837 | locked_shared_perm = s->locked_shared_perm; | |
838 | } else { | |
839 | /* | |
840 | * We don't have the previous bits, just lock/unlock for each of the | |
841 | * requested bits. | |
842 | */ | |
843 | if (unlock) { | |
844 | locked_perm = BLK_PERM_ALL; | |
845 | locked_shared_perm = BLK_PERM_ALL; | |
846 | } else { | |
847 | locked_perm = 0; | |
848 | locked_shared_perm = 0; | |
849 | } | |
850 | } | |
244a5668 FZ |
851 | |
852 | PERM_FOREACH(i) { | |
853 | int off = RAW_LOCK_PERM_BASE + i; | |
2996ffad FZ |
854 | uint64_t bit = (1ULL << i); |
855 | if ((perm_lock_bits & bit) && !(locked_perm & bit)) { | |
d0a96155 | 856 | ret = qemu_lock_fd(fd, off, 1, false); |
244a5668 | 857 | if (ret) { |
797e3e38 DE |
858 | raw_lock_error_setg_errno(errp, -ret, "Failed to lock byte %d", |
859 | off); | |
244a5668 | 860 | return ret; |
2996ffad FZ |
861 | } else if (s) { |
862 | s->locked_perm |= bit; | |
244a5668 | 863 | } |
2996ffad | 864 | } else if (unlock && (locked_perm & bit) && !(perm_lock_bits & bit)) { |
d0a96155 | 865 | ret = qemu_unlock_fd(fd, off, 1); |
244a5668 | 866 | if (ret) { |
797e3e38 | 867 | error_setg_errno(errp, -ret, "Failed to unlock byte %d", off); |
244a5668 | 868 | return ret; |
2996ffad FZ |
869 | } else if (s) { |
870 | s->locked_perm &= ~bit; | |
244a5668 FZ |
871 | } |
872 | } | |
873 | } | |
874 | PERM_FOREACH(i) { | |
875 | int off = RAW_LOCK_SHARED_BASE + i; | |
2996ffad FZ |
876 | uint64_t bit = (1ULL << i); |
877 | if ((shared_perm_lock_bits & bit) && !(locked_shared_perm & bit)) { | |
d0a96155 | 878 | ret = qemu_lock_fd(fd, off, 1, false); |
244a5668 | 879 | if (ret) { |
797e3e38 DE |
880 | raw_lock_error_setg_errno(errp, -ret, "Failed to lock byte %d", |
881 | off); | |
244a5668 | 882 | return ret; |
2996ffad FZ |
883 | } else if (s) { |
884 | s->locked_shared_perm |= bit; | |
244a5668 | 885 | } |
2996ffad FZ |
886 | } else if (unlock && (locked_shared_perm & bit) && |
887 | !(shared_perm_lock_bits & bit)) { | |
d0a96155 | 888 | ret = qemu_unlock_fd(fd, off, 1); |
244a5668 | 889 | if (ret) { |
797e3e38 | 890 | error_setg_errno(errp, -ret, "Failed to unlock byte %d", off); |
244a5668 | 891 | return ret; |
2996ffad FZ |
892 | } else if (s) { |
893 | s->locked_shared_perm &= ~bit; | |
244a5668 FZ |
894 | } |
895 | } | |
896 | } | |
897 | return 0; | |
898 | } | |
899 | ||
900 | /* Check "unshared" bytes implied by @perm and ~@shared_perm in the file. */ | |
d0a96155 | 901 | static int raw_check_lock_bytes(int fd, uint64_t perm, uint64_t shared_perm, |
244a5668 FZ |
902 | Error **errp) |
903 | { | |
904 | int ret; | |
905 | int i; | |
906 | ||
907 | PERM_FOREACH(i) { | |
908 | int off = RAW_LOCK_SHARED_BASE + i; | |
909 | uint64_t p = 1ULL << i; | |
910 | if (perm & p) { | |
d0a96155 | 911 | ret = qemu_lock_fd_test(fd, off, 1, true); |
244a5668 FZ |
912 | if (ret) { |
913 | char *perm_name = bdrv_perm_names(p); | |
797e3e38 DE |
914 | |
915 | raw_lock_error_setg_errno(errp, -ret, | |
916 | "Failed to get \"%s\" lock", | |
917 | perm_name); | |
244a5668 | 918 | g_free(perm_name); |
244a5668 FZ |
919 | return ret; |
920 | } | |
921 | } | |
922 | } | |
923 | PERM_FOREACH(i) { | |
924 | int off = RAW_LOCK_PERM_BASE + i; | |
925 | uint64_t p = 1ULL << i; | |
926 | if (!(shared_perm & p)) { | |
d0a96155 | 927 | ret = qemu_lock_fd_test(fd, off, 1, true); |
244a5668 FZ |
928 | if (ret) { |
929 | char *perm_name = bdrv_perm_names(p); | |
797e3e38 DE |
930 | |
931 | raw_lock_error_setg_errno(errp, -ret, | |
932 | "Failed to get shared \"%s\" lock", | |
933 | perm_name); | |
244a5668 | 934 | g_free(perm_name); |
244a5668 FZ |
935 | return ret; |
936 | } | |
937 | } | |
938 | } | |
939 | return 0; | |
940 | } | |
941 | ||
942 | static int raw_handle_perm_lock(BlockDriverState *bs, | |
943 | RawPermLockOp op, | |
944 | uint64_t new_perm, uint64_t new_shared, | |
945 | Error **errp) | |
946 | { | |
947 | BDRVRawState *s = bs->opaque; | |
948 | int ret = 0; | |
949 | Error *local_err = NULL; | |
950 | ||
951 | if (!s->use_lock) { | |
952 | return 0; | |
953 | } | |
954 | ||
955 | if (bdrv_get_flags(bs) & BDRV_O_INACTIVE) { | |
956 | return 0; | |
957 | } | |
958 | ||
244a5668 FZ |
959 | switch (op) { |
960 | case RAW_PL_PREPARE: | |
696aaaed VSO |
961 | if ((s->perm | new_perm) == s->perm && |
962 | (s->shared_perm & new_shared) == s->shared_perm) | |
963 | { | |
964 | /* | |
965 | * We are going to unlock bytes, it should not fail. If it fail due | |
966 | * to some fs-dependent permission-unrelated reasons (which occurs | |
967 | * sometimes on NFS and leads to abort in bdrv_replace_child) we | |
968 | * can't prevent such errors by any check here. And we ignore them | |
969 | * anyway in ABORT and COMMIT. | |
970 | */ | |
971 | return 0; | |
972 | } | |
f2e3af29 | 973 | ret = raw_apply_lock_bytes(s, s->fd, s->perm | new_perm, |
244a5668 FZ |
974 | ~s->shared_perm | ~new_shared, |
975 | false, errp); | |
976 | if (!ret) { | |
f2e3af29 | 977 | ret = raw_check_lock_bytes(s->fd, new_perm, new_shared, errp); |
244a5668 FZ |
978 | if (!ret) { |
979 | return 0; | |
980 | } | |
b857431d FZ |
981 | error_append_hint(errp, |
982 | "Is another process using the image [%s]?\n", | |
983 | bs->filename); | |
244a5668 | 984 | } |
244a5668 FZ |
985 | /* fall through to unlock bytes. */ |
986 | case RAW_PL_ABORT: | |
f2e3af29 | 987 | raw_apply_lock_bytes(s, s->fd, s->perm, ~s->shared_perm, |
d0a96155 | 988 | true, &local_err); |
244a5668 FZ |
989 | if (local_err) { |
990 | /* Theoretically the above call only unlocks bytes and it cannot | |
991 | * fail. Something weird happened, report it. | |
992 | */ | |
db0754df | 993 | warn_report_err(local_err); |
244a5668 FZ |
994 | } |
995 | break; | |
996 | case RAW_PL_COMMIT: | |
f2e3af29 | 997 | raw_apply_lock_bytes(s, s->fd, new_perm, ~new_shared, |
d0a96155 | 998 | true, &local_err); |
244a5668 FZ |
999 | if (local_err) { |
1000 | /* Theoretically the above call only unlocks bytes and it cannot | |
1001 | * fail. Something weird happened, report it. | |
1002 | */ | |
db0754df | 1003 | warn_report_err(local_err); |
244a5668 FZ |
1004 | } |
1005 | break; | |
1006 | } | |
1007 | return ret; | |
1008 | } | |
1009 | ||
5cec2870 | 1010 | static int raw_reconfigure_getfd(BlockDriverState *bs, int flags, |
23dece19 | 1011 | int *open_flags, uint64_t perm, bool force_dup, |
6ceabe6f | 1012 | Error **errp) |
5cec2870 KW |
1013 | { |
1014 | BDRVRawState *s = bs->opaque; | |
1015 | int fd = -1; | |
1016 | int ret; | |
23dece19 KW |
1017 | bool has_writers = perm & |
1018 | (BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED | BLK_PERM_RESIZE); | |
5cec2870 KW |
1019 | int fcntl_flags = O_APPEND | O_NONBLOCK; |
1020 | #ifdef O_NOATIME | |
1021 | fcntl_flags |= O_NOATIME; | |
1022 | #endif | |
1023 | ||
1024 | *open_flags = 0; | |
1025 | if (s->type == FTYPE_CD) { | |
1026 | *open_flags |= O_NONBLOCK; | |
1027 | } | |
1028 | ||
23dece19 | 1029 | raw_parse_flags(flags, open_flags, has_writers); |
5cec2870 KW |
1030 | |
1031 | #ifdef O_ASYNC | |
1032 | /* Not all operating systems have O_ASYNC, and those that don't | |
1033 | * will not let us track the state into rs->open_flags (typically | |
1034 | * you achieve the same effect with an ioctl, for example I_SETSIG | |
1035 | * on Solaris). But we do not use O_ASYNC, so that's fine. | |
1036 | */ | |
1037 | assert((s->open_flags & O_ASYNC) == 0); | |
1038 | #endif | |
1039 | ||
6ceabe6f KW |
1040 | if (!force_dup && *open_flags == s->open_flags) { |
1041 | /* We're lucky, the existing fd is fine */ | |
1042 | return s->fd; | |
1043 | } | |
1044 | ||
5cec2870 KW |
1045 | if ((*open_flags & ~fcntl_flags) == (s->open_flags & ~fcntl_flags)) { |
1046 | /* dup the original fd */ | |
1047 | fd = qemu_dup(s->fd); | |
1048 | if (fd >= 0) { | |
1049 | ret = fcntl_setfl(fd, *open_flags); | |
1050 | if (ret) { | |
1051 | qemu_close(fd); | |
1052 | fd = -1; | |
1053 | } | |
1054 | } | |
1055 | } | |
1056 | ||
b18a24a9 | 1057 | /* If we cannot use fcntl, or fcntl failed, fall back to qemu_open() */ |
5cec2870 KW |
1058 | if (fd == -1) { |
1059 | const char *normalized_filename = bs->filename; | |
1060 | ret = raw_normalize_devicepath(&normalized_filename, errp); | |
1061 | if (ret >= 0) { | |
b18a24a9 | 1062 | fd = qemu_open(normalized_filename, *open_flags, errp); |
5cec2870 | 1063 | if (fd == -1) { |
5cec2870 KW |
1064 | return -1; |
1065 | } | |
1066 | } | |
1067 | } | |
1068 | ||
bca5283b KW |
1069 | if (fd != -1 && (*open_flags & O_RDWR)) { |
1070 | ret = check_hdev_writable(fd); | |
1071 | if (ret < 0) { | |
1072 | qemu_close(fd); | |
1073 | error_setg_errno(errp, -ret, "The device is not writable"); | |
1074 | return -1; | |
1075 | } | |
1076 | } | |
1077 | ||
5cec2870 KW |
1078 | return fd; |
1079 | } | |
1080 | ||
eeb6b45d JC |
1081 | static int raw_reopen_prepare(BDRVReopenState *state, |
1082 | BlockReopenQueue *queue, Error **errp) | |
1083 | { | |
1084 | BDRVRawState *s; | |
4e6d13c9 | 1085 | BDRVRawReopenState *rs; |
31be8a2a | 1086 | QemuOpts *opts; |
a6aeca0c | 1087 | int ret; |
eeb6b45d JC |
1088 | |
1089 | assert(state != NULL); | |
1090 | assert(state->bs != NULL); | |
1091 | ||
1092 | s = state->bs->opaque; | |
1093 | ||
5839e53b | 1094 | state->opaque = g_new0(BDRVRawReopenState, 1); |
4e6d13c9 | 1095 | rs = state->opaque; |
31be8a2a SH |
1096 | |
1097 | /* Handle options changes */ | |
1098 | opts = qemu_opts_create(&raw_runtime_opts, NULL, 0, &error_abort); | |
af175e85 | 1099 | if (!qemu_opts_absorb_qdict(opts, state->options, errp)) { |
31be8a2a SH |
1100 | ret = -EINVAL; |
1101 | goto out; | |
1102 | } | |
1103 | ||
f357fcd8 | 1104 | rs->drop_cache = qemu_opt_get_bool_del(opts, "drop-cache", true); |
8d324575 AG |
1105 | rs->check_cache_dropped = |
1106 | qemu_opt_get_bool_del(opts, "x-check-cache-dropped", false); | |
1107 | ||
1108 | /* This driver's reopen function doesn't currently allow changing | |
1109 | * other options, so let's put them back in the original QDict and | |
1110 | * bdrv_reopen_prepare() will detect changes and complain. */ | |
1111 | qemu_opts_to_qdict(opts, state->options); | |
eeb6b45d | 1112 | |
72373e40 VSO |
1113 | /* |
1114 | * As part of reopen prepare we also want to create new fd by | |
1115 | * raw_reconfigure_getfd(). But it wants updated "perm", when in | |
1116 | * bdrv_reopen_multiple() .bdrv_reopen_prepare() callback called prior to | |
1117 | * permission update. Happily, permission update is always a part (a seprate | |
1118 | * stage) of bdrv_reopen_multiple() so we can rely on this fact and | |
1119 | * reconfigure fd in raw_check_perm(). | |
1120 | */ | |
df26a350 | 1121 | |
e0c9cf3a | 1122 | s->reopen_state = state; |
a6aeca0c | 1123 | ret = 0; |
72373e40 | 1124 | |
31be8a2a SH |
1125 | out: |
1126 | qemu_opts_del(opts); | |
eeb6b45d JC |
1127 | return ret; |
1128 | } | |
1129 | ||
eeb6b45d JC |
1130 | static void raw_reopen_commit(BDRVReopenState *state) |
1131 | { | |
4e6d13c9 | 1132 | BDRVRawReopenState *rs = state->opaque; |
eeb6b45d JC |
1133 | BDRVRawState *s = state->bs->opaque; |
1134 | ||
f357fcd8 | 1135 | s->drop_cache = rs->drop_cache; |
31be8a2a | 1136 | s->check_cache_dropped = rs->check_cache_dropped; |
4e6d13c9 | 1137 | s->open_flags = rs->open_flags; |
eeb6b45d JC |
1138 | g_free(state->opaque); |
1139 | state->opaque = NULL; | |
e0c9cf3a KW |
1140 | |
1141 | assert(s->reopen_state == state); | |
1142 | s->reopen_state = NULL; | |
eeb6b45d JC |
1143 | } |
1144 | ||
1145 | ||
1146 | static void raw_reopen_abort(BDRVReopenState *state) | |
1147 | { | |
4e6d13c9 | 1148 | BDRVRawReopenState *rs = state->opaque; |
e0c9cf3a | 1149 | BDRVRawState *s = state->bs->opaque; |
eeb6b45d JC |
1150 | |
1151 | /* nothing to do if NULL, we didn't get far enough */ | |
4e6d13c9 | 1152 | if (rs == NULL) { |
eeb6b45d JC |
1153 | return; |
1154 | } | |
1155 | ||
eeb6b45d JC |
1156 | g_free(state->opaque); |
1157 | state->opaque = NULL; | |
e0c9cf3a KW |
1158 | |
1159 | assert(s->reopen_state == state); | |
1160 | s->reopen_state = NULL; | |
eeb6b45d JC |
1161 | } |
1162 | ||
18473467 | 1163 | static int hdev_get_max_hw_transfer(int fd, struct stat *st) |
6f607174 FZ |
1164 | { |
1165 | #ifdef BLKSECTGET | |
18473467 PB |
1166 | if (S_ISBLK(st->st_mode)) { |
1167 | unsigned short max_sectors = 0; | |
1168 | if (ioctl(fd, BLKSECTGET, &max_sectors) == 0) { | |
1169 | return max_sectors * 512; | |
1170 | } | |
6f607174 | 1171 | } else { |
18473467 PB |
1172 | int max_bytes = 0; |
1173 | if (ioctl(fd, BLKSECTGET, &max_bytes) == 0) { | |
1174 | return max_bytes; | |
1175 | } | |
6f607174 | 1176 | } |
18473467 | 1177 | return -errno; |
6f607174 FZ |
1178 | #else |
1179 | return -ENOSYS; | |
1180 | #endif | |
1181 | } | |
1182 | ||
18473467 | 1183 | static int hdev_get_max_segments(int fd, struct stat *st) |
9103f1ce FZ |
1184 | { |
1185 | #ifdef CONFIG_LINUX | |
1186 | char buf[32]; | |
1187 | const char *end; | |
867eccfe | 1188 | char *sysfspath = NULL; |
9103f1ce | 1189 | int ret; |
867eccfe | 1190 | int sysfd = -1; |
9103f1ce | 1191 | long max_segments; |
867eccfe | 1192 | |
18473467 | 1193 | if (S_ISCHR(st->st_mode)) { |
8ad5ab61 PB |
1194 | if (ioctl(fd, SG_GET_SG_TABLESIZE, &ret) == 0) { |
1195 | return ret; | |
1196 | } | |
1197 | return -ENOTSUP; | |
1198 | } | |
1199 | ||
18473467 | 1200 | if (!S_ISBLK(st->st_mode)) { |
8ad5ab61 PB |
1201 | return -ENOTSUP; |
1202 | } | |
1203 | ||
9103f1ce | 1204 | sysfspath = g_strdup_printf("/sys/dev/block/%u:%u/queue/max_segments", |
18473467 | 1205 | major(st->st_rdev), minor(st->st_rdev)); |
867eccfe ML |
1206 | sysfd = open(sysfspath, O_RDONLY); |
1207 | if (sysfd == -1) { | |
9103f1ce FZ |
1208 | ret = -errno; |
1209 | goto out; | |
1210 | } | |
1211 | do { | |
867eccfe | 1212 | ret = read(sysfd, buf, sizeof(buf) - 1); |
9103f1ce FZ |
1213 | } while (ret == -1 && errno == EINTR); |
1214 | if (ret < 0) { | |
1215 | ret = -errno; | |
1216 | goto out; | |
1217 | } else if (ret == 0) { | |
1218 | ret = -EIO; | |
1219 | goto out; | |
1220 | } | |
1221 | buf[ret] = 0; | |
1222 | /* The file is ended with '\n', pass 'end' to accept that. */ | |
1223 | ret = qemu_strtol(buf, &end, 10, &max_segments); | |
1224 | if (ret == 0 && end && *end == '\n') { | |
1225 | ret = max_segments; | |
1226 | } | |
1227 | ||
1228 | out: | |
867eccfe ML |
1229 | if (sysfd != -1) { |
1230 | close(sysfd); | |
fed414df | 1231 | } |
9103f1ce FZ |
1232 | g_free(sysfspath); |
1233 | return ret; | |
1234 | #else | |
1235 | return -ENOTSUP; | |
1236 | #endif | |
1237 | } | |
1238 | ||
3baca891 | 1239 | static void raw_refresh_limits(BlockDriverState *bs, Error **errp) |
c25f53b0 PB |
1240 | { |
1241 | BDRVRawState *s = bs->opaque; | |
18473467 | 1242 | struct stat st; |
6f607174 | 1243 | |
18473467 PB |
1244 | raw_probe_alignment(bs, s->fd, errp); |
1245 | bs->bl.min_mem_alignment = s->buf_align; | |
1246 | bs->bl.opt_mem_alignment = MAX(s->buf_align, qemu_real_host_page_size); | |
1247 | ||
1248 | /* | |
1249 | * Maximum transfers are best effort, so it is okay to ignore any | |
1250 | * errors. That said, based on the man page errors in fstat would be | |
1251 | * very much unexpected; the only possible case seems to be ENOMEM. | |
1252 | */ | |
1253 | if (fstat(s->fd, &st)) { | |
1254 | return; | |
1255 | } | |
1256 | ||
1257 | if (bs->sg || S_ISBLK(st.st_mode)) { | |
1258 | int ret = hdev_get_max_hw_transfer(s->fd, &st); | |
867eccfe ML |
1259 | |
1260 | if (ret > 0 && ret <= BDRV_REQUEST_MAX_BYTES) { | |
18473467 | 1261 | bs->bl.max_hw_transfer = ret; |
867eccfe ML |
1262 | } |
1263 | ||
18473467 | 1264 | ret = hdev_get_max_segments(s->fd, &st); |
867eccfe | 1265 | if (ret > 0) { |
01ef8185 | 1266 | bs->bl.max_iov = ret; |
6f607174 FZ |
1267 | } |
1268 | } | |
c25f53b0 | 1269 | } |
83f64091 | 1270 | |
1a9335e4 ET |
1271 | static int check_for_dasd(int fd) |
1272 | { | |
1273 | #ifdef BIODASDINFO2 | |
1274 | struct dasd_information2_t info = {0}; | |
1275 | ||
1276 | return ioctl(fd, BIODASDINFO2, &info); | |
1277 | #else | |
1278 | return -1; | |
1279 | #endif | |
1280 | } | |
1281 | ||
1282 | /** | |
1283 | * Try to get @bs's logical and physical block size. | |
1284 | * On success, store them in @bsz and return zero. | |
1285 | * On failure, return negative errno. | |
1286 | */ | |
1287 | static int hdev_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz) | |
1288 | { | |
1289 | BDRVRawState *s = bs->opaque; | |
1290 | int ret; | |
1291 | ||
1292 | /* If DASD, get blocksizes */ | |
1293 | if (check_for_dasd(s->fd) < 0) { | |
1294 | return -ENOTSUP; | |
1295 | } | |
1296 | ret = probe_logical_blocksize(s->fd, &bsz->log); | |
1297 | if (ret < 0) { | |
1298 | return ret; | |
1299 | } | |
1300 | return probe_physical_blocksize(s->fd, &bsz->phys); | |
1301 | } | |
1302 | ||
1303 | /** | |
1304 | * Try to get @bs's geometry: cyls, heads, sectors. | |
1305 | * On success, store them in @geo and return 0. | |
1306 | * On failure return -errno. | |
1307 | * (Allows block driver to assign default geometry values that guest sees) | |
1308 | */ | |
1309 | #ifdef __linux__ | |
1310 | static int hdev_probe_geometry(BlockDriverState *bs, HDGeometry *geo) | |
1311 | { | |
1312 | BDRVRawState *s = bs->opaque; | |
1313 | struct hd_geometry ioctl_geo = {0}; | |
1a9335e4 ET |
1314 | |
1315 | /* If DASD, get its geometry */ | |
1316 | if (check_for_dasd(s->fd) < 0) { | |
1317 | return -ENOTSUP; | |
1318 | } | |
1319 | if (ioctl(s->fd, HDIO_GETGEO, &ioctl_geo) < 0) { | |
1320 | return -errno; | |
1321 | } | |
1322 | /* HDIO_GETGEO may return success even though geo contains zeros | |
1323 | (e.g. certain multipath setups) */ | |
1324 | if (!ioctl_geo.heads || !ioctl_geo.sectors || !ioctl_geo.cylinders) { | |
1325 | return -ENOTSUP; | |
1326 | } | |
1327 | /* Do not return a geometry for partition */ | |
1328 | if (ioctl_geo.start != 0) { | |
1329 | return -ENOTSUP; | |
1330 | } | |
1331 | geo->heads = ioctl_geo.heads; | |
1332 | geo->sectors = ioctl_geo.sectors; | |
1a9335e4 ET |
1333 | geo->cylinders = ioctl_geo.cylinders; |
1334 | ||
1335 | return 0; | |
1336 | } | |
1337 | #else /* __linux__ */ | |
1338 | static int hdev_probe_geometry(BlockDriverState *bs, HDGeometry *geo) | |
1339 | { | |
1340 | return -ENOTSUP; | |
1341 | } | |
1342 | #endif | |
1343 | ||
03425671 KW |
1344 | #if defined(__linux__) |
1345 | static int handle_aiocb_ioctl(void *opaque) | |
de81a169 | 1346 | { |
03425671 | 1347 | RawPosixAIOData *aiocb = opaque; |
de81a169 PB |
1348 | int ret; |
1349 | ||
d57c44d0 | 1350 | ret = ioctl(aiocb->aio_fildes, aiocb->ioctl.cmd, aiocb->ioctl.buf); |
de81a169 PB |
1351 | if (ret == -1) { |
1352 | return -errno; | |
1353 | } | |
1354 | ||
b608c8dc | 1355 | return 0; |
de81a169 | 1356 | } |
03425671 | 1357 | #endif /* linux */ |
de81a169 | 1358 | |
06dc9bd5 | 1359 | static int handle_aiocb_flush(void *opaque) |
de81a169 | 1360 | { |
06dc9bd5 | 1361 | RawPosixAIOData *aiocb = opaque; |
e5bcf967 | 1362 | BDRVRawState *s = aiocb->bs->opaque; |
de81a169 PB |
1363 | int ret; |
1364 | ||
e5bcf967 | 1365 | if (s->page_cache_inconsistent) { |
c7ddc882 | 1366 | return -s->page_cache_inconsistent; |
e5bcf967 KW |
1367 | } |
1368 | ||
de81a169 PB |
1369 | ret = qemu_fdatasync(aiocb->aio_fildes); |
1370 | if (ret == -1) { | |
60ff2ae2 DB |
1371 | trace_file_flush_fdatasync_failed(errno); |
1372 | ||
e5bcf967 KW |
1373 | /* There is no clear definition of the semantics of a failing fsync(), |
1374 | * so we may have to assume the worst. The sad truth is that this | |
1375 | * assumption is correct for Linux. Some pages are now probably marked | |
1376 | * clean in the page cache even though they are inconsistent with the | |
1377 | * on-disk contents. The next fdatasync() call would succeed, but no | |
1378 | * further writeback attempt will be made. We can't get back to a state | |
1379 | * in which we know what is on disk (we would have to rewrite | |
1380 | * everything that was touched since the last fdatasync() at least), so | |
1381 | * make bdrv_flush() fail permanently. Given that the behaviour isn't | |
1382 | * really defined, I have little hope that other OSes are doing better. | |
1383 | * | |
1384 | * Obviously, this doesn't affect O_DIRECT, which bypasses the page | |
1385 | * cache. */ | |
1386 | if ((s->open_flags & O_DIRECT) == 0) { | |
c7ddc882 | 1387 | s->page_cache_inconsistent = errno; |
e5bcf967 | 1388 | } |
de81a169 PB |
1389 | return -errno; |
1390 | } | |
1391 | return 0; | |
1392 | } | |
1393 | ||
1394 | #ifdef CONFIG_PREADV | |
1395 | ||
1396 | static bool preadv_present = true; | |
1397 | ||
1398 | static ssize_t | |
1399 | qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset) | |
1400 | { | |
1401 | return preadv(fd, iov, nr_iov, offset); | |
1402 | } | |
1403 | ||
1404 | static ssize_t | |
1405 | qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset) | |
1406 | { | |
1407 | return pwritev(fd, iov, nr_iov, offset); | |
1408 | } | |
1409 | ||
1410 | #else | |
1411 | ||
1412 | static bool preadv_present = false; | |
1413 | ||
1414 | static ssize_t | |
1415 | qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset) | |
1416 | { | |
1417 | return -ENOSYS; | |
1418 | } | |
1419 | ||
1420 | static ssize_t | |
1421 | qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset) | |
1422 | { | |
1423 | return -ENOSYS; | |
1424 | } | |
1425 | ||
1426 | #endif | |
1427 | ||
1428 | static ssize_t handle_aiocb_rw_vector(RawPosixAIOData *aiocb) | |
1429 | { | |
1430 | ssize_t len; | |
1431 | ||
1432 | do { | |
1433 | if (aiocb->aio_type & QEMU_AIO_WRITE) | |
1434 | len = qemu_pwritev(aiocb->aio_fildes, | |
d57c44d0 KW |
1435 | aiocb->io.iov, |
1436 | aiocb->io.niov, | |
de81a169 PB |
1437 | aiocb->aio_offset); |
1438 | else | |
1439 | len = qemu_preadv(aiocb->aio_fildes, | |
d57c44d0 KW |
1440 | aiocb->io.iov, |
1441 | aiocb->io.niov, | |
de81a169 PB |
1442 | aiocb->aio_offset); |
1443 | } while (len == -1 && errno == EINTR); | |
1444 | ||
1445 | if (len == -1) { | |
1446 | return -errno; | |
1447 | } | |
1448 | return len; | |
1449 | } | |
1450 | ||
1451 | /* | |
1452 | * Read/writes the data to/from a given linear buffer. | |
1453 | * | |
1454 | * Returns the number of bytes handles or -errno in case of an error. Short | |
1455 | * reads are only returned if the end of the file is reached. | |
1456 | */ | |
1457 | static ssize_t handle_aiocb_rw_linear(RawPosixAIOData *aiocb, char *buf) | |
1458 | { | |
1459 | ssize_t offset = 0; | |
1460 | ssize_t len; | |
1461 | ||
1462 | while (offset < aiocb->aio_nbytes) { | |
1463 | if (aiocb->aio_type & QEMU_AIO_WRITE) { | |
1464 | len = pwrite(aiocb->aio_fildes, | |
1465 | (const char *)buf + offset, | |
1466 | aiocb->aio_nbytes - offset, | |
1467 | aiocb->aio_offset + offset); | |
1468 | } else { | |
1469 | len = pread(aiocb->aio_fildes, | |
1470 | buf + offset, | |
1471 | aiocb->aio_nbytes - offset, | |
1472 | aiocb->aio_offset + offset); | |
1473 | } | |
1474 | if (len == -1 && errno == EINTR) { | |
1475 | continue; | |
61ed73cf SH |
1476 | } else if (len == -1 && errno == EINVAL && |
1477 | (aiocb->bs->open_flags & BDRV_O_NOCACHE) && | |
1478 | !(aiocb->aio_type & QEMU_AIO_WRITE) && | |
1479 | offset > 0) { | |
1480 | /* O_DIRECT pread() may fail with EINVAL when offset is unaligned | |
1481 | * after a short read. Assume that O_DIRECT short reads only occur | |
1482 | * at EOF. Therefore this is a short read, not an I/O error. | |
1483 | */ | |
1484 | break; | |
de81a169 PB |
1485 | } else if (len == -1) { |
1486 | offset = -errno; | |
1487 | break; | |
1488 | } else if (len == 0) { | |
1489 | break; | |
1490 | } | |
1491 | offset += len; | |
1492 | } | |
1493 | ||
1494 | return offset; | |
1495 | } | |
1496 | ||
999e6b69 | 1497 | static int handle_aiocb_rw(void *opaque) |
de81a169 | 1498 | { |
999e6b69 | 1499 | RawPosixAIOData *aiocb = opaque; |
de81a169 PB |
1500 | ssize_t nbytes; |
1501 | char *buf; | |
1502 | ||
1503 | if (!(aiocb->aio_type & QEMU_AIO_MISALIGNED)) { | |
1504 | /* | |
1505 | * If there is just a single buffer, and it is properly aligned | |
1506 | * we can just use plain pread/pwrite without any problems. | |
1507 | */ | |
d57c44d0 | 1508 | if (aiocb->io.niov == 1) { |
54c7ca1b KW |
1509 | nbytes = handle_aiocb_rw_linear(aiocb, aiocb->io.iov->iov_base); |
1510 | goto out; | |
de81a169 PB |
1511 | } |
1512 | /* | |
1513 | * We have more than one iovec, and all are properly aligned. | |
1514 | * | |
1515 | * Try preadv/pwritev first and fall back to linearizing the | |
1516 | * buffer if it's not supported. | |
1517 | */ | |
1518 | if (preadv_present) { | |
1519 | nbytes = handle_aiocb_rw_vector(aiocb); | |
1520 | if (nbytes == aiocb->aio_nbytes || | |
1521 | (nbytes < 0 && nbytes != -ENOSYS)) { | |
54c7ca1b | 1522 | goto out; |
de81a169 PB |
1523 | } |
1524 | preadv_present = false; | |
1525 | } | |
1526 | ||
1527 | /* | |
1528 | * XXX(hch): short read/write. no easy way to handle the reminder | |
1529 | * using these interfaces. For now retry using plain | |
1530 | * pread/pwrite? | |
1531 | */ | |
1532 | } | |
1533 | ||
1534 | /* | |
1535 | * Ok, we have to do it the hard way, copy all segments into | |
1536 | * a single aligned buffer. | |
1537 | */ | |
50d4a858 KW |
1538 | buf = qemu_try_blockalign(aiocb->bs, aiocb->aio_nbytes); |
1539 | if (buf == NULL) { | |
54c7ca1b KW |
1540 | nbytes = -ENOMEM; |
1541 | goto out; | |
50d4a858 KW |
1542 | } |
1543 | ||
de81a169 PB |
1544 | if (aiocb->aio_type & QEMU_AIO_WRITE) { |
1545 | char *p = buf; | |
1546 | int i; | |
1547 | ||
d57c44d0 KW |
1548 | for (i = 0; i < aiocb->io.niov; ++i) { |
1549 | memcpy(p, aiocb->io.iov[i].iov_base, aiocb->io.iov[i].iov_len); | |
1550 | p += aiocb->io.iov[i].iov_len; | |
de81a169 | 1551 | } |
8eb029c2 | 1552 | assert(p - buf == aiocb->aio_nbytes); |
de81a169 PB |
1553 | } |
1554 | ||
1555 | nbytes = handle_aiocb_rw_linear(aiocb, buf); | |
1556 | if (!(aiocb->aio_type & QEMU_AIO_WRITE)) { | |
1557 | char *p = buf; | |
1558 | size_t count = aiocb->aio_nbytes, copy; | |
1559 | int i; | |
1560 | ||
d57c44d0 | 1561 | for (i = 0; i < aiocb->io.niov && count; ++i) { |
de81a169 | 1562 | copy = count; |
d57c44d0 KW |
1563 | if (copy > aiocb->io.iov[i].iov_len) { |
1564 | copy = aiocb->io.iov[i].iov_len; | |
de81a169 | 1565 | } |
d57c44d0 | 1566 | memcpy(aiocb->io.iov[i].iov_base, p, copy); |
8eb029c2 | 1567 | assert(count >= copy); |
de81a169 PB |
1568 | p += copy; |
1569 | count -= copy; | |
1570 | } | |
8eb029c2 | 1571 | assert(count == 0); |
de81a169 PB |
1572 | } |
1573 | qemu_vfree(buf); | |
1574 | ||
54c7ca1b KW |
1575 | out: |
1576 | if (nbytes == aiocb->aio_nbytes) { | |
1577 | return 0; | |
1578 | } else if (nbytes >= 0 && nbytes < aiocb->aio_nbytes) { | |
1579 | if (aiocb->aio_type & QEMU_AIO_WRITE) { | |
1580 | return -EINVAL; | |
1581 | } else { | |
1582 | iov_memset(aiocb->io.iov, aiocb->io.niov, nbytes, | |
1583 | 0, aiocb->aio_nbytes - nbytes); | |
1584 | return 0; | |
1585 | } | |
1586 | } else { | |
1587 | assert(nbytes < 0); | |
1588 | return nbytes; | |
1589 | } | |
de81a169 PB |
1590 | } |
1591 | ||
1486df0e DL |
1592 | static int translate_err(int err) |
1593 | { | |
1594 | if (err == -ENODEV || err == -ENOSYS || err == -EOPNOTSUPP || | |
1595 | err == -ENOTTY) { | |
1596 | err = -ENOTSUP; | |
1597 | } | |
1598 | return err; | |
1599 | } | |
1600 | ||
d50d8222 | 1601 | #ifdef CONFIG_FALLOCATE |
0b991712 DL |
1602 | static int do_fallocate(int fd, int mode, off_t offset, off_t len) |
1603 | { | |
1604 | do { | |
1605 | if (fallocate(fd, mode, offset, len) == 0) { | |
1606 | return 0; | |
1607 | } | |
1608 | } while (errno == EINTR); | |
1609 | return translate_err(-errno); | |
1610 | } | |
1611 | #endif | |
1612 | ||
37cc9f7f | 1613 | static ssize_t handle_aiocb_write_zeroes_block(RawPosixAIOData *aiocb) |
97a2ae34 | 1614 | { |
37cc9f7f | 1615 | int ret = -ENOTSUP; |
97a2ae34 PB |
1616 | BDRVRawState *s = aiocb->bs->opaque; |
1617 | ||
37cc9f7f | 1618 | if (!s->has_write_zeroes) { |
97a2ae34 PB |
1619 | return -ENOTSUP; |
1620 | } | |
1621 | ||
97a2ae34 | 1622 | #ifdef BLKZEROOUT |
738301e1 KW |
1623 | /* The BLKZEROOUT implementation in the kernel doesn't set |
1624 | * BLKDEV_ZERO_NOFALLBACK, so we can't call this if we have to avoid slow | |
1625 | * fallbacks. */ | |
1626 | if (!(aiocb->aio_type & QEMU_AIO_NO_FALLBACK)) { | |
1627 | do { | |
1628 | uint64_t range[2] = { aiocb->aio_offset, aiocb->aio_nbytes }; | |
1629 | if (ioctl(aiocb->aio_fildes, BLKZEROOUT, range) == 0) { | |
1630 | return 0; | |
1631 | } | |
1632 | } while (errno == EINTR); | |
37cc9f7f | 1633 | |
738301e1 | 1634 | ret = translate_err(-errno); |
effecce6 KW |
1635 | if (ret == -ENOTSUP) { |
1636 | s->has_write_zeroes = false; | |
1637 | } | |
738301e1 | 1638 | } |
97a2ae34 | 1639 | #endif |
97a2ae34 | 1640 | |
97a2ae34 PB |
1641 | return ret; |
1642 | } | |
1643 | ||
7154d8ae | 1644 | static int handle_aiocb_write_zeroes(void *opaque) |
37cc9f7f | 1645 | { |
7154d8ae | 1646 | RawPosixAIOData *aiocb = opaque; |
70d9110b | 1647 | #ifdef CONFIG_FALLOCATE |
b2c6f23f | 1648 | BDRVRawState *s = aiocb->bs->opaque; |
70d9110b DL |
1649 | int64_t len; |
1650 | #endif | |
37cc9f7f DL |
1651 | |
1652 | if (aiocb->aio_type & QEMU_AIO_BLKDEV) { | |
1653 | return handle_aiocb_write_zeroes_block(aiocb); | |
1654 | } | |
1655 | ||
b953f075 DL |
1656 | #ifdef CONFIG_FALLOCATE_ZERO_RANGE |
1657 | if (s->has_write_zeroes) { | |
1658 | int ret = do_fallocate(s->fd, FALLOC_FL_ZERO_RANGE, | |
1659 | aiocb->aio_offset, aiocb->aio_nbytes); | |
fa95e9fb TH |
1660 | if (ret == -ENOTSUP) { |
1661 | s->has_write_zeroes = false; | |
1662 | } else if (ret == 0 || ret != -EINVAL) { | |
b953f075 DL |
1663 | return ret; |
1664 | } | |
fa95e9fb TH |
1665 | /* |
1666 | * Note: Some file systems do not like unaligned byte ranges, and | |
1667 | * return EINVAL in such a case, though they should not do it according | |
1668 | * to the man-page of fallocate(). Thus we simply ignore this return | |
1669 | * value and try the other fallbacks instead. | |
1670 | */ | |
b953f075 DL |
1671 | } |
1672 | #endif | |
1673 | ||
1cdc3239 DL |
1674 | #ifdef CONFIG_FALLOCATE_PUNCH_HOLE |
1675 | if (s->has_discard && s->has_fallocate) { | |
1676 | int ret = do_fallocate(s->fd, | |
1677 | FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, | |
1678 | aiocb->aio_offset, aiocb->aio_nbytes); | |
1679 | if (ret == 0) { | |
1680 | ret = do_fallocate(s->fd, 0, aiocb->aio_offset, aiocb->aio_nbytes); | |
1681 | if (ret == 0 || ret != -ENOTSUP) { | |
1682 | return ret; | |
1683 | } | |
1684 | s->has_fallocate = false; | |
73ebf297 TH |
1685 | } else if (ret == -EINVAL) { |
1686 | /* | |
1687 | * Some file systems like older versions of GPFS do not like un- | |
1688 | * aligned byte ranges, and return EINVAL in such a case, though | |
1689 | * they should not do it according to the man-page of fallocate(). | |
1690 | * Warn about the bad filesystem and try the final fallback instead. | |
1691 | */ | |
1692 | warn_report_once("Your file system is misbehaving: " | |
1693 | "fallocate(FALLOC_FL_PUNCH_HOLE) returned EINVAL. " | |
1694 | "Please report this bug to your file sytem " | |
1695 | "vendor."); | |
1cdc3239 DL |
1696 | } else if (ret != -ENOTSUP) { |
1697 | return ret; | |
1698 | } else { | |
1699 | s->has_discard = false; | |
1700 | } | |
1701 | } | |
1702 | #endif | |
1703 | ||
d50d8222 | 1704 | #ifdef CONFIG_FALLOCATE |
70d9110b DL |
1705 | /* Last resort: we are trying to extend the file with zeroed data. This |
1706 | * can be done via fallocate(fd, 0) */ | |
1707 | len = bdrv_getlength(aiocb->bs); | |
1708 | if (s->has_fallocate && len >= 0 && aiocb->aio_offset >= len) { | |
d50d8222 DL |
1709 | int ret = do_fallocate(s->fd, 0, aiocb->aio_offset, aiocb->aio_nbytes); |
1710 | if (ret == 0 || ret != -ENOTSUP) { | |
1711 | return ret; | |
1712 | } | |
1713 | s->has_fallocate = false; | |
1714 | } | |
1715 | #endif | |
1716 | ||
37cc9f7f DL |
1717 | return -ENOTSUP; |
1718 | } | |
1719 | ||
7154d8ae | 1720 | static int handle_aiocb_write_zeroes_unmap(void *opaque) |
34fa110e | 1721 | { |
7154d8ae | 1722 | RawPosixAIOData *aiocb = opaque; |
34fa110e | 1723 | BDRVRawState *s G_GNUC_UNUSED = aiocb->bs->opaque; |
34fa110e KW |
1724 | |
1725 | /* First try to write zeros and unmap at the same time */ | |
1726 | ||
1727 | #ifdef CONFIG_FALLOCATE_PUNCH_HOLE | |
b3ac2b94 SS |
1728 | int ret = do_fallocate(s->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, |
1729 | aiocb->aio_offset, aiocb->aio_nbytes); | |
bae127d4 AD |
1730 | switch (ret) { |
1731 | case -ENOTSUP: | |
1732 | case -EINVAL: | |
ece4fa91 | 1733 | case -EBUSY: |
bae127d4 AD |
1734 | break; |
1735 | default: | |
34fa110e KW |
1736 | return ret; |
1737 | } | |
1738 | #endif | |
1739 | ||
34fa110e KW |
1740 | /* If we couldn't manage to unmap while guaranteed that the area reads as |
1741 | * all-zero afterwards, just write zeroes without unmapping */ | |
b3ac2b94 | 1742 | return handle_aiocb_write_zeroes(aiocb); |
34fa110e KW |
1743 | } |
1744 | ||
1efad060 FZ |
1745 | #ifndef HAVE_COPY_FILE_RANGE |
1746 | static off_t copy_file_range(int in_fd, off_t *in_off, int out_fd, | |
1747 | off_t *out_off, size_t len, unsigned int flags) | |
1748 | { | |
1749 | #ifdef __NR_copy_file_range | |
1750 | return syscall(__NR_copy_file_range, in_fd, in_off, out_fd, | |
1751 | out_off, len, flags); | |
1752 | #else | |
1753 | errno = ENOSYS; | |
1754 | return -1; | |
1755 | #endif | |
1756 | } | |
1757 | #endif | |
1758 | ||
58a209c4 | 1759 | static int handle_aiocb_copy_range(void *opaque) |
1efad060 | 1760 | { |
58a209c4 | 1761 | RawPosixAIOData *aiocb = opaque; |
1efad060 FZ |
1762 | uint64_t bytes = aiocb->aio_nbytes; |
1763 | off_t in_off = aiocb->aio_offset; | |
d57c44d0 | 1764 | off_t out_off = aiocb->copy_range.aio_offset2; |
1efad060 FZ |
1765 | |
1766 | while (bytes) { | |
1767 | ssize_t ret = copy_file_range(aiocb->aio_fildes, &in_off, | |
d57c44d0 | 1768 | aiocb->copy_range.aio_fd2, &out_off, |
1efad060 | 1769 | bytes, 0); |
ecc983a5 | 1770 | trace_file_copy_file_range(aiocb->bs, aiocb->aio_fildes, in_off, |
d57c44d0 KW |
1771 | aiocb->copy_range.aio_fd2, out_off, bytes, |
1772 | 0, ret); | |
c436e3d0 FZ |
1773 | if (ret == 0) { |
1774 | /* No progress (e.g. when beyond EOF), let the caller fall back to | |
1775 | * buffer I/O. */ | |
1776 | return -ENOSPC; | |
1efad060 FZ |
1777 | } |
1778 | if (ret < 0) { | |
c436e3d0 FZ |
1779 | switch (errno) { |
1780 | case ENOSYS: | |
1efad060 | 1781 | return -ENOTSUP; |
c436e3d0 FZ |
1782 | case EINTR: |
1783 | continue; | |
1784 | default: | |
1efad060 FZ |
1785 | return -errno; |
1786 | } | |
1787 | } | |
1efad060 FZ |
1788 | bytes -= ret; |
1789 | } | |
1790 | return 0; | |
1791 | } | |
1792 | ||
46ee0f46 | 1793 | static int handle_aiocb_discard(void *opaque) |
8238010b | 1794 | { |
46ee0f46 | 1795 | RawPosixAIOData *aiocb = opaque; |
8238010b PB |
1796 | int ret = -EOPNOTSUPP; |
1797 | BDRVRawState *s = aiocb->bs->opaque; | |
1798 | ||
7ce21016 PB |
1799 | if (!s->has_discard) { |
1800 | return -ENOTSUP; | |
8238010b PB |
1801 | } |
1802 | ||
1803 | if (aiocb->aio_type & QEMU_AIO_BLKDEV) { | |
1804 | #ifdef BLKDISCARD | |
1805 | do { | |
1806 | uint64_t range[2] = { aiocb->aio_offset, aiocb->aio_nbytes }; | |
1807 | if (ioctl(aiocb->aio_fildes, BLKDISCARD, range) == 0) { | |
1808 | return 0; | |
1809 | } | |
1810 | } while (errno == EINTR); | |
1811 | ||
1812 | ret = -errno; | |
1813 | #endif | |
1814 | } else { | |
8238010b | 1815 | #ifdef CONFIG_FALLOCATE_PUNCH_HOLE |
0b991712 DL |
1816 | ret = do_fallocate(s->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, |
1817 | aiocb->aio_offset, aiocb->aio_nbytes); | |
8238010b PB |
1818 | #endif |
1819 | } | |
1820 | ||
1486df0e DL |
1821 | ret = translate_err(ret); |
1822 | if (ret == -ENOTSUP) { | |
7ce21016 | 1823 | s->has_discard = false; |
8238010b PB |
1824 | } |
1825 | return ret; | |
1826 | } | |
1827 | ||
3a20013f NS |
1828 | /* |
1829 | * Help alignment probing by allocating the first block. | |
1830 | * | |
1831 | * When reading with direct I/O from unallocated area on Gluster backed by XFS, | |
1832 | * reading succeeds regardless of request length. In this case we fallback to | |
1833 | * safe alignment which is not optimal. Allocating the first block avoids this | |
1834 | * fallback. | |
1835 | * | |
1836 | * fd may be opened with O_DIRECT, but we don't know the buffer alignment or | |
1837 | * request alignment, so we use safe values. | |
1838 | * | |
1839 | * Returns: 0 on success, -errno on failure. Since this is an optimization, | |
1840 | * caller may ignore failures. | |
1841 | */ | |
1842 | static int allocate_first_block(int fd, size_t max_size) | |
1843 | { | |
1844 | size_t write_size = (max_size < MAX_BLOCKSIZE) | |
1845 | ? BDRV_SECTOR_SIZE | |
1846 | : MAX_BLOCKSIZE; | |
038adc2f | 1847 | size_t max_align = MAX(MAX_BLOCKSIZE, qemu_real_host_page_size); |
3a20013f NS |
1848 | void *buf; |
1849 | ssize_t n; | |
1850 | int ret; | |
1851 | ||
1852 | buf = qemu_memalign(max_align, write_size); | |
1853 | memset(buf, 0, write_size); | |
1854 | ||
1855 | do { | |
1856 | n = pwrite(fd, buf, write_size, 0); | |
1857 | } while (n == -1 && errno == EINTR); | |
1858 | ||
1859 | ret = (n == -1) ? -errno : 0; | |
1860 | ||
1861 | qemu_vfree(buf); | |
1862 | return ret; | |
1863 | } | |
1864 | ||
29cb4c01 | 1865 | static int handle_aiocb_truncate(void *opaque) |
93f4e2ff | 1866 | { |
29cb4c01 | 1867 | RawPosixAIOData *aiocb = opaque; |
93f4e2ff KW |
1868 | int result = 0; |
1869 | int64_t current_length = 0; | |
1870 | char *buf = NULL; | |
1871 | struct stat st; | |
1872 | int fd = aiocb->aio_fildes; | |
1873 | int64_t offset = aiocb->aio_offset; | |
d57c44d0 KW |
1874 | PreallocMode prealloc = aiocb->truncate.prealloc; |
1875 | Error **errp = aiocb->truncate.errp; | |
93f4e2ff KW |
1876 | |
1877 | if (fstat(fd, &st) < 0) { | |
1878 | result = -errno; | |
1879 | error_setg_errno(errp, -result, "Could not stat file"); | |
1880 | return result; | |
1881 | } | |
1882 | ||
1883 | current_length = st.st_size; | |
d57c44d0 | 1884 | if (current_length > offset && prealloc != PREALLOC_MODE_OFF) { |
93f4e2ff KW |
1885 | error_setg(errp, "Cannot use preallocation for shrinking files"); |
1886 | return -ENOTSUP; | |
1887 | } | |
1888 | ||
d57c44d0 | 1889 | switch (prealloc) { |
93f4e2ff KW |
1890 | #ifdef CONFIG_POSIX_FALLOCATE |
1891 | case PREALLOC_MODE_FALLOC: | |
1892 | /* | |
1893 | * Truncating before posix_fallocate() makes it about twice slower on | |
1894 | * file systems that do not support fallocate(), trying to check if a | |
1895 | * block is allocated before allocating it, so don't do that here. | |
1896 | */ | |
1897 | if (offset != current_length) { | |
1898 | result = -posix_fallocate(fd, current_length, | |
1899 | offset - current_length); | |
1900 | if (result != 0) { | |
1901 | /* posix_fallocate() doesn't set errno. */ | |
1902 | error_setg_errno(errp, -result, | |
1903 | "Could not preallocate new data"); | |
3a20013f NS |
1904 | } else if (current_length == 0) { |
1905 | /* | |
1906 | * posix_fallocate() uses fallocate() if the filesystem | |
1907 | * supports it, or fallback to manually writing zeroes. If | |
1908 | * fallocate() was used, unaligned reads from the fallocated | |
1909 | * area in raw_probe_alignment() will succeed, hence we need to | |
1910 | * allocate the first block. | |
1911 | * | |
1912 | * Optimize future alignment probing; ignore failures. | |
1913 | */ | |
1914 | allocate_first_block(fd, offset); | |
93f4e2ff KW |
1915 | } |
1916 | } else { | |
1917 | result = 0; | |
1918 | } | |
1919 | goto out; | |
1920 | #endif | |
1921 | case PREALLOC_MODE_FULL: | |
1922 | { | |
1923 | int64_t num = 0, left = offset - current_length; | |
1924 | off_t seek_result; | |
1925 | ||
1926 | /* | |
1927 | * Knowing the final size from the beginning could allow the file | |
1928 | * system driver to do less allocations and possibly avoid | |
1929 | * fragmentation of the file. | |
1930 | */ | |
1931 | if (ftruncate(fd, offset) != 0) { | |
1932 | result = -errno; | |
1933 | error_setg_errno(errp, -result, "Could not resize file"); | |
1934 | goto out; | |
1935 | } | |
1936 | ||
1937 | buf = g_malloc0(65536); | |
1938 | ||
1939 | seek_result = lseek(fd, current_length, SEEK_SET); | |
1940 | if (seek_result < 0) { | |
1941 | result = -errno; | |
1942 | error_setg_errno(errp, -result, | |
1943 | "Failed to seek to the old end of file"); | |
1944 | goto out; | |
1945 | } | |
1946 | ||
1947 | while (left > 0) { | |
1948 | num = MIN(left, 65536); | |
1949 | result = write(fd, buf, num); | |
1950 | if (result < 0) { | |
a1c81f4f FZ |
1951 | if (errno == EINTR) { |
1952 | continue; | |
1953 | } | |
93f4e2ff KW |
1954 | result = -errno; |
1955 | error_setg_errno(errp, -result, | |
1956 | "Could not write zeros for preallocation"); | |
1957 | goto out; | |
1958 | } | |
1959 | left -= result; | |
1960 | } | |
1961 | if (result >= 0) { | |
1962 | result = fsync(fd); | |
1963 | if (result < 0) { | |
1964 | result = -errno; | |
1965 | error_setg_errno(errp, -result, | |
1966 | "Could not flush file to disk"); | |
1967 | goto out; | |
1968 | } | |
1969 | } | |
1970 | goto out; | |
1971 | } | |
1972 | case PREALLOC_MODE_OFF: | |
1973 | if (ftruncate(fd, offset) != 0) { | |
1974 | result = -errno; | |
1975 | error_setg_errno(errp, -result, "Could not resize file"); | |
3a20013f NS |
1976 | } else if (current_length == 0 && offset > current_length) { |
1977 | /* Optimize future alignment probing; ignore failures. */ | |
1978 | allocate_first_block(fd, offset); | |
93f4e2ff KW |
1979 | } |
1980 | return result; | |
1981 | default: | |
1982 | result = -ENOTSUP; | |
1983 | error_setg(errp, "Unsupported preallocation mode: %s", | |
d57c44d0 | 1984 | PreallocMode_str(prealloc)); |
93f4e2ff KW |
1985 | return result; |
1986 | } | |
1987 | ||
1988 | out: | |
1989 | if (result < 0) { | |
1990 | if (ftruncate(fd, current_length) < 0) { | |
1991 | error_report("Failed to restore old file length: %s", | |
1992 | strerror(errno)); | |
1993 | } | |
1994 | } | |
1995 | ||
1996 | g_free(buf); | |
1997 | return result; | |
1998 | } | |
1999 | ||
5d5de250 KW |
2000 | static int coroutine_fn raw_thread_pool_submit(BlockDriverState *bs, |
2001 | ThreadPoolFunc func, void *arg) | |
2002 | { | |
2003 | /* @bs can be NULL, bdrv_get_aio_context() returns the main context then */ | |
2004 | ThreadPool *pool = aio_get_thread_pool(bdrv_get_aio_context(bs)); | |
2005 | return thread_pool_submit_co(pool, func, arg); | |
2006 | } | |
2007 | ||
9d52aa3c KW |
2008 | static int coroutine_fn raw_co_prw(BlockDriverState *bs, uint64_t offset, |
2009 | uint64_t bytes, QEMUIOVector *qiov, int type) | |
83f64091 | 2010 | { |
ce1a14dc | 2011 | BDRVRawState *s = bs->opaque; |
999e6b69 | 2012 | RawPosixAIOData acb; |
ce1a14dc | 2013 | |
19cb3738 | 2014 | if (fd_open(bs) < 0) |
2174f12b | 2015 | return -EIO; |
19cb3738 | 2016 | |
f141eafe | 2017 | /* |
c6447510 AM |
2018 | * When using O_DIRECT, the request must be aligned to be able to use |
2019 | * either libaio or io_uring interface. If not fail back to regular thread | |
2020 | * pool read/write code which emulates this for us if we | |
2021 | * set QEMU_AIO_MISALIGNED. | |
f141eafe | 2022 | */ |
c6447510 AM |
2023 | if (s->needs_alignment && !bdrv_qiov_is_aligned(bs, qiov)) { |
2024 | type |= QEMU_AIO_MISALIGNED; | |
2025 | #ifdef CONFIG_LINUX_IO_URING | |
2026 | } else if (s->use_linux_io_uring) { | |
2027 | LuringState *aio = aio_get_linux_io_uring(bdrv_get_aio_context(bs)); | |
2028 | assert(qiov->size == bytes); | |
2029 | return luring_co_submit(bs, aio, s->fd, offset, qiov, type); | |
2030 | #endif | |
e44bd6fc | 2031 | #ifdef CONFIG_LINUX_AIO |
c6447510 AM |
2032 | } else if (s->use_linux_aio) { |
2033 | LinuxAioState *aio = aio_get_linux_aio(bdrv_get_aio_context(bs)); | |
2034 | assert(qiov->size == bytes); | |
2035 | return laio_co_submit(bs, aio, s->fd, offset, qiov, type); | |
e44bd6fc | 2036 | #endif |
9ef91a67 | 2037 | } |
f141eafe | 2038 | |
999e6b69 KW |
2039 | acb = (RawPosixAIOData) { |
2040 | .bs = bs, | |
2041 | .aio_fildes = s->fd, | |
2042 | .aio_type = type, | |
2043 | .aio_offset = offset, | |
2044 | .aio_nbytes = bytes, | |
2045 | .io = { | |
2046 | .iov = qiov->iov, | |
2047 | .niov = qiov->niov, | |
2048 | }, | |
2049 | }; | |
2050 | ||
2051 | assert(qiov->size == bytes); | |
2052 | return raw_thread_pool_submit(bs, handle_aiocb_rw, &acb); | |
2174f12b KW |
2053 | } |
2054 | ||
9d52aa3c KW |
2055 | static int coroutine_fn raw_co_preadv(BlockDriverState *bs, uint64_t offset, |
2056 | uint64_t bytes, QEMUIOVector *qiov, | |
2057 | int flags) | |
2174f12b | 2058 | { |
9d52aa3c | 2059 | return raw_co_prw(bs, offset, bytes, qiov, QEMU_AIO_READ); |
2174f12b KW |
2060 | } |
2061 | ||
9d52aa3c KW |
2062 | static int coroutine_fn raw_co_pwritev(BlockDriverState *bs, uint64_t offset, |
2063 | uint64_t bytes, QEMUIOVector *qiov, | |
2064 | int flags) | |
2174f12b | 2065 | { |
9d52aa3c KW |
2066 | assert(flags == 0); |
2067 | return raw_co_prw(bs, offset, bytes, qiov, QEMU_AIO_WRITE); | |
83f64091 FB |
2068 | } |
2069 | ||
1b3abdcc ML |
2070 | static void raw_aio_plug(BlockDriverState *bs) |
2071 | { | |
c6447510 | 2072 | BDRVRawState __attribute__((unused)) *s = bs->opaque; |
1b3abdcc | 2073 | #ifdef CONFIG_LINUX_AIO |
0a4279d9 | 2074 | if (s->use_linux_aio) { |
0187f5c9 PB |
2075 | LinuxAioState *aio = aio_get_linux_aio(bdrv_get_aio_context(bs)); |
2076 | laio_io_plug(bs, aio); | |
1b3abdcc ML |
2077 | } |
2078 | #endif | |
c6447510 AM |
2079 | #ifdef CONFIG_LINUX_IO_URING |
2080 | if (s->use_linux_io_uring) { | |
2081 | LuringState *aio = aio_get_linux_io_uring(bdrv_get_aio_context(bs)); | |
2082 | luring_io_plug(bs, aio); | |
2083 | } | |
2084 | #endif | |
1b3abdcc ML |
2085 | } |
2086 | ||
2087 | static void raw_aio_unplug(BlockDriverState *bs) | |
2088 | { | |
c6447510 | 2089 | BDRVRawState __attribute__((unused)) *s = bs->opaque; |
1b3abdcc | 2090 | #ifdef CONFIG_LINUX_AIO |
0a4279d9 | 2091 | if (s->use_linux_aio) { |
0187f5c9 PB |
2092 | LinuxAioState *aio = aio_get_linux_aio(bdrv_get_aio_context(bs)); |
2093 | laio_io_unplug(bs, aio); | |
1b3abdcc ML |
2094 | } |
2095 | #endif | |
c6447510 AM |
2096 | #ifdef CONFIG_LINUX_IO_URING |
2097 | if (s->use_linux_io_uring) { | |
2098 | LuringState *aio = aio_get_linux_io_uring(bdrv_get_aio_context(bs)); | |
2099 | luring_io_unplug(bs, aio); | |
2100 | } | |
2101 | #endif | |
1b3abdcc ML |
2102 | } |
2103 | ||
33d70fb6 | 2104 | static int raw_co_flush_to_disk(BlockDriverState *bs) |
b2e12bc6 CH |
2105 | { |
2106 | BDRVRawState *s = bs->opaque; | |
06dc9bd5 | 2107 | RawPosixAIOData acb; |
33d70fb6 | 2108 | int ret; |
b2e12bc6 | 2109 | |
33d70fb6 KW |
2110 | ret = fd_open(bs); |
2111 | if (ret < 0) { | |
2112 | return ret; | |
2113 | } | |
b2e12bc6 | 2114 | |
06dc9bd5 KW |
2115 | acb = (RawPosixAIOData) { |
2116 | .bs = bs, | |
2117 | .aio_fildes = s->fd, | |
2118 | .aio_type = QEMU_AIO_FLUSH, | |
2119 | }; | |
2120 | ||
c6447510 AM |
2121 | #ifdef CONFIG_LINUX_IO_URING |
2122 | if (s->use_linux_io_uring) { | |
2123 | LuringState *aio = aio_get_linux_io_uring(bdrv_get_aio_context(bs)); | |
2124 | return luring_co_submit(bs, aio, s->fd, 0, NULL, QEMU_AIO_FLUSH); | |
2125 | } | |
2126 | #endif | |
06dc9bd5 | 2127 | return raw_thread_pool_submit(bs, handle_aiocb_flush, &acb); |
b2e12bc6 CH |
2128 | } |
2129 | ||
ed6e2161 NA |
2130 | static void raw_aio_attach_aio_context(BlockDriverState *bs, |
2131 | AioContext *new_context) | |
2132 | { | |
c6447510 | 2133 | BDRVRawState __attribute__((unused)) *s = bs->opaque; |
ed6e2161 | 2134 | #ifdef CONFIG_LINUX_AIO |
ed6e2161 | 2135 | if (s->use_linux_aio) { |
cb09104e | 2136 | Error *local_err = NULL; |
ed6e2161 NA |
2137 | if (!aio_setup_linux_aio(new_context, &local_err)) { |
2138 | error_reportf_err(local_err, "Unable to use native AIO, " | |
2139 | "falling back to thread pool: "); | |
2140 | s->use_linux_aio = false; | |
2141 | } | |
2142 | } | |
2143 | #endif | |
c6447510 AM |
2144 | #ifdef CONFIG_LINUX_IO_URING |
2145 | if (s->use_linux_io_uring) { | |
cb8d0851 | 2146 | Error *local_err = NULL; |
c6447510 AM |
2147 | if (!aio_setup_linux_io_uring(new_context, &local_err)) { |
2148 | error_reportf_err(local_err, "Unable to use linux io_uring, " | |
2149 | "falling back to thread pool: "); | |
2150 | s->use_linux_io_uring = false; | |
2151 | } | |
2152 | } | |
2153 | #endif | |
ed6e2161 NA |
2154 | } |
2155 | ||
83f64091 FB |
2156 | static void raw_close(BlockDriverState *bs) |
2157 | { | |
2158 | BDRVRawState *s = bs->opaque; | |
c2f3426c | 2159 | |
19cb3738 | 2160 | if (s->fd >= 0) { |
2e1e79da | 2161 | qemu_close(s->fd); |
19cb3738 FB |
2162 | s->fd = -1; |
2163 | } | |
83f64091 FB |
2164 | } |
2165 | ||
d0bc9e5d HR |
2166 | /** |
2167 | * Truncates the given regular file @fd to @offset and, when growing, fills the | |
2168 | * new space according to @prealloc. | |
2169 | * | |
2170 | * Returns: 0 on success, -errno on failure. | |
2171 | */ | |
93f4e2ff KW |
2172 | static int coroutine_fn |
2173 | raw_regular_truncate(BlockDriverState *bs, int fd, int64_t offset, | |
2174 | PreallocMode prealloc, Error **errp) | |
9f63b07e | 2175 | { |
29cb4c01 | 2176 | RawPosixAIOData acb; |
d0bc9e5d | 2177 | |
29cb4c01 | 2178 | acb = (RawPosixAIOData) { |
93f4e2ff KW |
2179 | .bs = bs, |
2180 | .aio_fildes = fd, | |
2181 | .aio_type = QEMU_AIO_TRUNCATE, | |
2182 | .aio_offset = offset, | |
d57c44d0 KW |
2183 | .truncate = { |
2184 | .prealloc = prealloc, | |
2185 | .errp = errp, | |
2186 | }, | |
93f4e2ff | 2187 | }; |
d0bc9e5d | 2188 | |
29cb4c01 | 2189 | return raw_thread_pool_submit(bs, handle_aiocb_truncate, &acb); |
9f63b07e HR |
2190 | } |
2191 | ||
061ca8a3 | 2192 | static int coroutine_fn raw_co_truncate(BlockDriverState *bs, int64_t offset, |
c80d8b06 | 2193 | bool exact, PreallocMode prealloc, |
92b92799 | 2194 | BdrvRequestFlags flags, Error **errp) |
83f64091 FB |
2195 | { |
2196 | BDRVRawState *s = bs->opaque; | |
55b949c8 | 2197 | struct stat st; |
f59adb32 | 2198 | int ret; |
55b949c8 CH |
2199 | |
2200 | if (fstat(s->fd, &st)) { | |
f59adb32 HR |
2201 | ret = -errno; |
2202 | error_setg_errno(errp, -ret, "Failed to fstat() the file"); | |
2203 | return ret; | |
55b949c8 CH |
2204 | } |
2205 | ||
2206 | if (S_ISREG(st.st_mode)) { | |
82325ae5 | 2207 | /* Always resizes to the exact @offset */ |
93f4e2ff | 2208 | return raw_regular_truncate(bs, s->fd, offset, prealloc, errp); |
35d72602 HR |
2209 | } |
2210 | ||
2211 | if (prealloc != PREALLOC_MODE_OFF) { | |
2212 | error_setg(errp, "Preallocation mode '%s' unsupported for this " | |
977c736f | 2213 | "non-regular file", PreallocMode_str(prealloc)); |
35d72602 HR |
2214 | return -ENOTSUP; |
2215 | } | |
2216 | ||
2217 | if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) { | |
82325ae5 HR |
2218 | int64_t cur_length = raw_getlength(bs); |
2219 | ||
2220 | if (offset != cur_length && exact) { | |
2221 | error_setg(errp, "Cannot resize device files"); | |
2222 | return -ENOTSUP; | |
2223 | } else if (offset > cur_length) { | |
f59adb32 HR |
2224 | error_setg(errp, "Cannot grow device files"); |
2225 | return -EINVAL; | |
2226 | } | |
55b949c8 | 2227 | } else { |
f59adb32 | 2228 | error_setg(errp, "Resizing this file is not supported"); |
55b949c8 CH |
2229 | return -ENOTSUP; |
2230 | } | |
2231 | ||
83f64091 FB |
2232 | return 0; |
2233 | } | |
2234 | ||
128ab2ff BS |
2235 | #ifdef __OpenBSD__ |
2236 | static int64_t raw_getlength(BlockDriverState *bs) | |
2237 | { | |
2238 | BDRVRawState *s = bs->opaque; | |
2239 | int fd = s->fd; | |
2240 | struct stat st; | |
2241 | ||
2242 | if (fstat(fd, &st)) | |
aa729704 | 2243 | return -errno; |
128ab2ff BS |
2244 | if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) { |
2245 | struct disklabel dl; | |
2246 | ||
2247 | if (ioctl(fd, DIOCGDINFO, &dl)) | |
aa729704 | 2248 | return -errno; |
128ab2ff BS |
2249 | return (uint64_t)dl.d_secsize * |
2250 | dl.d_partitions[DISKPART(st.st_rdev)].p_size; | |
2251 | } else | |
2252 | return st.st_size; | |
2253 | } | |
d1f6fd8d CE |
2254 | #elif defined(__NetBSD__) |
2255 | static int64_t raw_getlength(BlockDriverState *bs) | |
2256 | { | |
2257 | BDRVRawState *s = bs->opaque; | |
2258 | int fd = s->fd; | |
2259 | struct stat st; | |
2260 | ||
2261 | if (fstat(fd, &st)) | |
aa729704 | 2262 | return -errno; |
d1f6fd8d CE |
2263 | if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) { |
2264 | struct dkwedge_info dkw; | |
2265 | ||
2266 | if (ioctl(fd, DIOCGWEDGEINFO, &dkw) != -1) { | |
2267 | return dkw.dkw_size * 512; | |
2268 | } else { | |
2269 | struct disklabel dl; | |
2270 | ||
2271 | if (ioctl(fd, DIOCGDINFO, &dl)) | |
aa729704 | 2272 | return -errno; |
d1f6fd8d CE |
2273 | return (uint64_t)dl.d_secsize * |
2274 | dl.d_partitions[DISKPART(st.st_rdev)].p_size; | |
2275 | } | |
2276 | } else | |
2277 | return st.st_size; | |
2278 | } | |
50779cc2 CH |
2279 | #elif defined(__sun__) |
2280 | static int64_t raw_getlength(BlockDriverState *bs) | |
2281 | { | |
2282 | BDRVRawState *s = bs->opaque; | |
2283 | struct dk_minfo minfo; | |
2284 | int ret; | |
aa729704 | 2285 | int64_t size; |
50779cc2 CH |
2286 | |
2287 | ret = fd_open(bs); | |
2288 | if (ret < 0) { | |
2289 | return ret; | |
2290 | } | |
2291 | ||
2292 | /* | |
2293 | * Use the DKIOCGMEDIAINFO ioctl to read the size. | |
2294 | */ | |
2295 | ret = ioctl(s->fd, DKIOCGMEDIAINFO, &minfo); | |
2296 | if (ret != -1) { | |
2297 | return minfo.dki_lbsize * minfo.dki_capacity; | |
2298 | } | |
2299 | ||
2300 | /* | |
2301 | * There are reports that lseek on some devices fails, but | |
2302 | * irc discussion said that contingency on contingency was overkill. | |
2303 | */ | |
aa729704 MA |
2304 | size = lseek(s->fd, 0, SEEK_END); |
2305 | if (size < 0) { | |
2306 | return -errno; | |
2307 | } | |
2308 | return size; | |
50779cc2 CH |
2309 | } |
2310 | #elif defined(CONFIG_BSD) | |
2311 | static int64_t raw_getlength(BlockDriverState *bs) | |
83f64091 FB |
2312 | { |
2313 | BDRVRawState *s = bs->opaque; | |
2314 | int fd = s->fd; | |
2315 | int64_t size; | |
83f64091 | 2316 | struct stat sb; |
a167ba50 | 2317 | #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__) |
9f23011a | 2318 | int reopened = 0; |
83f64091 | 2319 | #endif |
19cb3738 FB |
2320 | int ret; |
2321 | ||
2322 | ret = fd_open(bs); | |
2323 | if (ret < 0) | |
2324 | return ret; | |
83f64091 | 2325 | |
a167ba50 | 2326 | #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__) |
9f23011a BS |
2327 | again: |
2328 | #endif | |
83f64091 FB |
2329 | if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) { |
2330 | #ifdef DIOCGMEDIASIZE | |
7d37435b | 2331 | if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size)) |
c5e97233 BS |
2332 | #elif defined(DIOCGPART) |
2333 | { | |
2334 | struct partinfo pi; | |
2335 | if (ioctl(fd, DIOCGPART, &pi) == 0) | |
2336 | size = pi.media_size; | |
2337 | else | |
2338 | size = 0; | |
2339 | } | |
2340 | if (size == 0) | |
83f64091 | 2341 | #endif |
83affaa6 | 2342 | #if defined(__APPLE__) && defined(__MACH__) |
728dacbd JA |
2343 | { |
2344 | uint64_t sectors = 0; | |
2345 | uint32_t sector_size = 0; | |
2346 | ||
2347 | if (ioctl(fd, DKIOCGETBLOCKCOUNT, §ors) == 0 | |
2348 | && ioctl(fd, DKIOCGETBLOCKSIZE, §or_size) == 0) { | |
2349 | size = sectors * sector_size; | |
2350 | } else { | |
2351 | size = lseek(fd, 0LL, SEEK_END); | |
2352 | if (size < 0) { | |
2353 | return -errno; | |
2354 | } | |
2355 | } | |
2356 | } | |
83f64091 FB |
2357 | #else |
2358 | size = lseek(fd, 0LL, SEEK_END); | |
aa729704 MA |
2359 | if (size < 0) { |
2360 | return -errno; | |
2361 | } | |
9f23011a | 2362 | #endif |
a167ba50 | 2363 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
9f23011a BS |
2364 | switch(s->type) { |
2365 | case FTYPE_CD: | |
2366 | /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */ | |
2367 | if (size == 2048LL * (unsigned)-1) | |
2368 | size = 0; | |
2369 | /* XXX no disc? maybe we need to reopen... */ | |
f3a5d3f8 | 2370 | if (size <= 0 && !reopened && cdrom_reopen(bs) >= 0) { |
9f23011a BS |
2371 | reopened = 1; |
2372 | goto again; | |
2373 | } | |
2374 | } | |
83f64091 | 2375 | #endif |
50779cc2 | 2376 | } else { |
83f64091 | 2377 | size = lseek(fd, 0, SEEK_END); |
aa729704 MA |
2378 | if (size < 0) { |
2379 | return -errno; | |
2380 | } | |
83f64091 | 2381 | } |
83f64091 FB |
2382 | return size; |
2383 | } | |
50779cc2 CH |
2384 | #else |
2385 | static int64_t raw_getlength(BlockDriverState *bs) | |
2386 | { | |
2387 | BDRVRawState *s = bs->opaque; | |
2388 | int ret; | |
aa729704 | 2389 | int64_t size; |
50779cc2 CH |
2390 | |
2391 | ret = fd_open(bs); | |
2392 | if (ret < 0) { | |
2393 | return ret; | |
2394 | } | |
2395 | ||
aa729704 MA |
2396 | size = lseek(s->fd, 0, SEEK_END); |
2397 | if (size < 0) { | |
2398 | return -errno; | |
2399 | } | |
2400 | return size; | |
50779cc2 | 2401 | } |
128ab2ff | 2402 | #endif |
83f64091 | 2403 | |
4a1d5e1f FZ |
2404 | static int64_t raw_get_allocated_file_size(BlockDriverState *bs) |
2405 | { | |
2406 | struct stat st; | |
2407 | BDRVRawState *s = bs->opaque; | |
2408 | ||
2409 | if (fstat(s->fd, &st) < 0) { | |
2410 | return -errno; | |
2411 | } | |
2412 | return (int64_t)st.st_blocks * 512; | |
2413 | } | |
2414 | ||
93f4e2ff KW |
2415 | static int coroutine_fn |
2416 | raw_co_create(BlockdevCreateOptions *options, Error **errp) | |
83f64091 | 2417 | { |
927f11e1 | 2418 | BlockdevCreateOptionsFile *file_opts; |
7c20c808 | 2419 | Error *local_err = NULL; |
83f64091 | 2420 | int fd; |
d815efca | 2421 | uint64_t perm, shared; |
1e37d059 | 2422 | int result = 0; |
83f64091 | 2423 | |
927f11e1 KW |
2424 | /* Validate options and set default values */ |
2425 | assert(options->driver == BLOCKDEV_DRIVER_FILE); | |
2426 | file_opts = &options->u.file; | |
464d9f64 | 2427 | |
927f11e1 KW |
2428 | if (!file_opts->has_nocow) { |
2429 | file_opts->nocow = false; | |
2430 | } | |
2431 | if (!file_opts->has_preallocation) { | |
2432 | file_opts->preallocation = PREALLOC_MODE_OFF; | |
06247428 | 2433 | } |
ffa244c8 KW |
2434 | if (!file_opts->has_extent_size_hint) { |
2435 | file_opts->extent_size_hint = 1 * MiB; | |
2436 | } | |
2437 | if (file_opts->extent_size_hint > UINT32_MAX) { | |
2438 | result = -EINVAL; | |
2439 | error_setg(errp, "Extent size hint is too large"); | |
2440 | goto out; | |
2441 | } | |
83f64091 | 2442 | |
927f11e1 | 2443 | /* Create file */ |
b18a24a9 | 2444 | fd = qemu_create(file_opts->filename, O_RDWR | O_BINARY, 0644, errp); |
1e37d059 SW |
2445 | if (fd < 0) { |
2446 | result = -errno; | |
06247428 HT |
2447 | goto out; |
2448 | } | |
2449 | ||
b8cf1913 HR |
2450 | /* Take permissions: We want to discard everything, so we need |
2451 | * BLK_PERM_WRITE; and truncation to the desired size requires | |
2452 | * BLK_PERM_RESIZE. | |
2453 | * On the other hand, we cannot share the RESIZE permission | |
2454 | * because we promise that after this function, the file has the | |
2455 | * size given in the options. If someone else were to resize it | |
2456 | * concurrently, we could not guarantee that. | |
2457 | * Note that after this function, we can no longer guarantee that | |
2458 | * the file is not touched by a third party, so it may be resized | |
2459 | * then. */ | |
2460 | perm = BLK_PERM_WRITE | BLK_PERM_RESIZE; | |
2461 | shared = BLK_PERM_ALL & ~BLK_PERM_RESIZE; | |
2462 | ||
2463 | /* Step one: Take locks */ | |
2996ffad | 2464 | result = raw_apply_lock_bytes(NULL, fd, perm, ~shared, false, errp); |
b8cf1913 HR |
2465 | if (result < 0) { |
2466 | goto out_close; | |
2467 | } | |
2468 | ||
2469 | /* Step two: Check that nobody else has taken conflicting locks */ | |
2470 | result = raw_check_lock_bytes(fd, perm, shared, errp); | |
2471 | if (result < 0) { | |
b857431d FZ |
2472 | error_append_hint(errp, |
2473 | "Is another process using the image [%s]?\n", | |
2474 | file_opts->filename); | |
7c20c808 | 2475 | goto out_unlock; |
b8cf1913 HR |
2476 | } |
2477 | ||
2478 | /* Clear the file by truncating it to 0 */ | |
93f4e2ff | 2479 | result = raw_regular_truncate(NULL, fd, 0, PREALLOC_MODE_OFF, errp); |
b8cf1913 | 2480 | if (result < 0) { |
7c20c808 | 2481 | goto out_unlock; |
b8cf1913 HR |
2482 | } |
2483 | ||
927f11e1 | 2484 | if (file_opts->nocow) { |
4ab15590 | 2485 | #ifdef __linux__ |
06247428 HT |
2486 | /* Set NOCOW flag to solve performance issue on fs like btrfs. |
2487 | * This is an optimisation. The FS_IOC_SETFLAGS ioctl return value | |
2488 | * will be ignored since any failure of this operation should not | |
2489 | * block the left work. | |
2490 | */ | |
2491 | int attr; | |
2492 | if (ioctl(fd, FS_IOC_GETFLAGS, &attr) == 0) { | |
2493 | attr |= FS_NOCOW_FL; | |
2494 | ioctl(fd, FS_IOC_SETFLAGS, &attr); | |
4ab15590 | 2495 | } |
06247428 HT |
2496 | #endif |
2497 | } | |
ffa244c8 KW |
2498 | #ifdef FS_IOC_FSSETXATTR |
2499 | /* | |
2500 | * Try to set the extent size hint. Failure is not fatal, and a warning is | |
2501 | * only printed if the option was explicitly specified. | |
2502 | */ | |
2503 | { | |
2504 | struct fsxattr attr; | |
2505 | result = ioctl(fd, FS_IOC_FSGETXATTR, &attr); | |
2506 | if (result == 0) { | |
2507 | attr.fsx_xflags |= FS_XFLAG_EXTSIZE; | |
2508 | attr.fsx_extsize = file_opts->extent_size_hint; | |
2509 | result = ioctl(fd, FS_IOC_FSSETXATTR, &attr); | |
2510 | } | |
2511 | if (result < 0 && file_opts->has_extent_size_hint && | |
2512 | file_opts->extent_size_hint) | |
2513 | { | |
2514 | warn_report("Failed to set extent size hint: %s", | |
2515 | strerror(errno)); | |
2516 | } | |
2517 | } | |
2518 | #endif | |
06247428 | 2519 | |
b8cf1913 HR |
2520 | /* Resize and potentially preallocate the file to the desired |
2521 | * final size */ | |
93f4e2ff KW |
2522 | result = raw_regular_truncate(NULL, fd, file_opts->size, |
2523 | file_opts->preallocation, errp); | |
9f63b07e | 2524 | if (result < 0) { |
7c20c808 HR |
2525 | goto out_unlock; |
2526 | } | |
2527 | ||
2528 | out_unlock: | |
2996ffad | 2529 | raw_apply_lock_bytes(NULL, fd, 0, 0, true, &local_err); |
7c20c808 HR |
2530 | if (local_err) { |
2531 | /* The above call should not fail, and if it does, that does | |
2532 | * not mean the whole creation operation has failed. So | |
2533 | * report it the user for their convenience, but do not report | |
2534 | * it to the caller. */ | |
db0754df | 2535 | warn_report_err(local_err); |
1e37d059 | 2536 | } |
06247428 | 2537 | |
5a1dad9d | 2538 | out_close: |
06247428 HT |
2539 | if (qemu_close(fd) != 0 && result == 0) { |
2540 | result = -errno; | |
2541 | error_setg_errno(errp, -result, "Could not close the new file"); | |
2542 | } | |
2543 | out: | |
1e37d059 | 2544 | return result; |
83f64091 FB |
2545 | } |
2546 | ||
b92902df ML |
2547 | static int coroutine_fn raw_co_create_opts(BlockDriver *drv, |
2548 | const char *filename, | |
2549 | QemuOpts *opts, | |
927f11e1 KW |
2550 | Error **errp) |
2551 | { | |
2552 | BlockdevCreateOptions options; | |
2553 | int64_t total_size = 0; | |
ffa244c8 KW |
2554 | int64_t extent_size_hint = 0; |
2555 | bool has_extent_size_hint = false; | |
927f11e1 KW |
2556 | bool nocow = false; |
2557 | PreallocMode prealloc; | |
2558 | char *buf = NULL; | |
2559 | Error *local_err = NULL; | |
2560 | ||
2561 | /* Skip file: protocol prefix */ | |
2562 | strstart(filename, "file:", &filename); | |
2563 | ||
2564 | /* Read out options */ | |
2565 | total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), | |
2566 | BDRV_SECTOR_SIZE); | |
ffa244c8 KW |
2567 | if (qemu_opt_get(opts, BLOCK_OPT_EXTENT_SIZE_HINT)) { |
2568 | has_extent_size_hint = true; | |
2569 | extent_size_hint = | |
2570 | qemu_opt_get_size_del(opts, BLOCK_OPT_EXTENT_SIZE_HINT, -1); | |
2571 | } | |
927f11e1 KW |
2572 | nocow = qemu_opt_get_bool(opts, BLOCK_OPT_NOCOW, false); |
2573 | buf = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC); | |
2574 | prealloc = qapi_enum_parse(&PreallocMode_lookup, buf, | |
2575 | PREALLOC_MODE_OFF, &local_err); | |
2576 | g_free(buf); | |
2577 | if (local_err) { | |
2578 | error_propagate(errp, local_err); | |
2579 | return -EINVAL; | |
2580 | } | |
2581 | ||
2582 | options = (BlockdevCreateOptions) { | |
2583 | .driver = BLOCKDEV_DRIVER_FILE, | |
2584 | .u.file = { | |
2585 | .filename = (char *) filename, | |
2586 | .size = total_size, | |
2587 | .has_preallocation = true, | |
2588 | .preallocation = prealloc, | |
2589 | .has_nocow = true, | |
2590 | .nocow = nocow, | |
ffa244c8 KW |
2591 | .has_extent_size_hint = has_extent_size_hint, |
2592 | .extent_size_hint = extent_size_hint, | |
927f11e1 KW |
2593 | }, |
2594 | }; | |
2595 | return raw_co_create(&options, errp); | |
2596 | } | |
2597 | ||
9bffae14 DHB |
2598 | static int coroutine_fn raw_co_delete_file(BlockDriverState *bs, |
2599 | Error **errp) | |
2600 | { | |
2601 | struct stat st; | |
2602 | int ret; | |
2603 | ||
2604 | if (!(stat(bs->filename, &st) == 0) || !S_ISREG(st.st_mode)) { | |
2605 | error_setg_errno(errp, ENOENT, "%s is not a regular file", | |
2606 | bs->filename); | |
2607 | return -ENOENT; | |
2608 | } | |
2609 | ||
2610 | ret = unlink(bs->filename); | |
2611 | if (ret < 0) { | |
2612 | ret = -errno; | |
2613 | error_setg_errno(errp, -ret, "Error when deleting file %s", | |
2614 | bs->filename); | |
2615 | } | |
2616 | ||
2617 | return ret; | |
2618 | } | |
2619 | ||
d1f06fe6 MA |
2620 | /* |
2621 | * Find allocation range in @bs around offset @start. | |
2622 | * May change underlying file descriptor's file offset. | |
2623 | * If @start is not in a hole, store @start in @data, and the | |
2624 | * beginning of the next hole in @hole, and return 0. | |
2625 | * If @start is in a non-trailing hole, store @start in @hole and the | |
2626 | * beginning of the next non-hole in @data, and return 0. | |
2627 | * If @start is in a trailing hole or beyond EOF, return -ENXIO. | |
2628 | * If we can't find out, return a negative errno other than -ENXIO. | |
2629 | */ | |
2630 | static int find_allocation(BlockDriverState *bs, off_t start, | |
2631 | off_t *data, off_t *hole) | |
4f11aa8a HR |
2632 | { |
2633 | #if defined SEEK_HOLE && defined SEEK_DATA | |
94282e71 | 2634 | BDRVRawState *s = bs->opaque; |
d1f06fe6 | 2635 | off_t offs; |
94282e71 | 2636 | |
d1f06fe6 MA |
2637 | /* |
2638 | * SEEK_DATA cases: | |
2639 | * D1. offs == start: start is in data | |
2640 | * D2. offs > start: start is in a hole, next data at offs | |
2641 | * D3. offs < 0, errno = ENXIO: either start is in a trailing hole | |
2642 | * or start is beyond EOF | |
2643 | * If the latter happens, the file has been truncated behind | |
2644 | * our back since we opened it. All bets are off then. | |
2645 | * Treating like a trailing hole is simplest. | |
2646 | * D4. offs < 0, errno != ENXIO: we learned nothing | |
2647 | */ | |
2648 | offs = lseek(s->fd, start, SEEK_DATA); | |
2649 | if (offs < 0) { | |
2650 | return -errno; /* D3 or D4 */ | |
2651 | } | |
a03083a0 JC |
2652 | |
2653 | if (offs < start) { | |
2654 | /* This is not a valid return by lseek(). We are safe to just return | |
2655 | * -EIO in this case, and we'll treat it like D4. */ | |
2656 | return -EIO; | |
2657 | } | |
d1f06fe6 MA |
2658 | |
2659 | if (offs > start) { | |
2660 | /* D2: in hole, next data at offs */ | |
2661 | *hole = start; | |
2662 | *data = offs; | |
2663 | return 0; | |
5500316d PB |
2664 | } |
2665 | ||
d1f06fe6 MA |
2666 | /* D1: in data, end not yet known */ |
2667 | ||
2668 | /* | |
2669 | * SEEK_HOLE cases: | |
2670 | * H1. offs == start: start is in a hole | |
2671 | * If this happens here, a hole has been dug behind our back | |
2672 | * since the previous lseek(). | |
2673 | * H2. offs > start: either start is in data, next hole at offs, | |
2674 | * or start is in trailing hole, EOF at offs | |
2675 | * Linux treats trailing holes like any other hole: offs == | |
2676 | * start. Solaris seeks to EOF instead: offs > start (blech). | |
2677 | * If that happens here, a hole has been dug behind our back | |
2678 | * since the previous lseek(). | |
2679 | * H3. offs < 0, errno = ENXIO: start is beyond EOF | |
2680 | * If this happens, the file has been truncated behind our | |
2681 | * back since we opened it. Treat it like a trailing hole. | |
2682 | * H4. offs < 0, errno != ENXIO: we learned nothing | |
2683 | * Pretend we know nothing at all, i.e. "forget" about D1. | |
2684 | */ | |
2685 | offs = lseek(s->fd, start, SEEK_HOLE); | |
2686 | if (offs < 0) { | |
2687 | return -errno; /* D1 and (H3 or H4) */ | |
2688 | } | |
a03083a0 JC |
2689 | |
2690 | if (offs < start) { | |
2691 | /* This is not a valid return by lseek(). We are safe to just return | |
2692 | * -EIO in this case, and we'll treat it like H4. */ | |
2693 | return -EIO; | |
2694 | } | |
d1f06fe6 MA |
2695 | |
2696 | if (offs > start) { | |
2697 | /* | |
2698 | * D1 and H2: either in data, next hole at offs, or it was in | |
2699 | * data but is now in a trailing hole. In the latter case, | |
2700 | * all bets are off. Treating it as if it there was data all | |
2701 | * the way to EOF is safe, so simply do that. | |
2702 | */ | |
4f11aa8a | 2703 | *data = start; |
d1f06fe6 MA |
2704 | *hole = offs; |
2705 | return 0; | |
5500316d | 2706 | } |
4f11aa8a | 2707 | |
d1f06fe6 MA |
2708 | /* D1 and H1 */ |
2709 | return -EBUSY; | |
5500316d | 2710 | #else |
4f11aa8a | 2711 | return -ENOTSUP; |
5500316d | 2712 | #endif |
4f11aa8a HR |
2713 | } |
2714 | ||
2715 | /* | |
a290f085 | 2716 | * Returns the allocation status of the specified offset. |
4f11aa8a | 2717 | * |
a290f085 | 2718 | * The block layer guarantees 'offset' and 'bytes' are within bounds. |
4f11aa8a | 2719 | * |
a290f085 EB |
2720 | * 'pnum' is set to the number of bytes (including and immediately following |
2721 | * the specified offset) that are known to be in the same | |
4f11aa8a HR |
2722 | * allocated/unallocated state. |
2723 | * | |
a290f085 | 2724 | * 'bytes' is the max value 'pnum' should be set to. |
4f11aa8a | 2725 | */ |
a290f085 EB |
2726 | static int coroutine_fn raw_co_block_status(BlockDriverState *bs, |
2727 | bool want_zero, | |
2728 | int64_t offset, | |
2729 | int64_t bytes, int64_t *pnum, | |
2730 | int64_t *map, | |
2731 | BlockDriverState **file) | |
2732 | { | |
2733 | off_t data = 0, hole = 0; | |
d7f62751 | 2734 | int ret; |
4f11aa8a | 2735 | |
9c3db310 HR |
2736 | assert(QEMU_IS_ALIGNED(offset | bytes, bs->bl.request_alignment)); |
2737 | ||
4f11aa8a HR |
2738 | ret = fd_open(bs); |
2739 | if (ret < 0) { | |
2740 | return ret; | |
2741 | } | |
2742 | ||
a290f085 EB |
2743 | if (!want_zero) { |
2744 | *pnum = bytes; | |
2745 | *map = offset; | |
2746 | *file = bs; | |
2747 | return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID; | |
e6d7ec32 | 2748 | } |
4f11aa8a | 2749 | |
a290f085 | 2750 | ret = find_allocation(bs, offset, &data, &hole); |
d1f06fe6 MA |
2751 | if (ret == -ENXIO) { |
2752 | /* Trailing hole */ | |
a290f085 | 2753 | *pnum = bytes; |
d1f06fe6 MA |
2754 | ret = BDRV_BLOCK_ZERO; |
2755 | } else if (ret < 0) { | |
2756 | /* No info available, so pretend there are no holes */ | |
a290f085 | 2757 | *pnum = bytes; |
d1f06fe6 | 2758 | ret = BDRV_BLOCK_DATA; |
a290f085 EB |
2759 | } else if (data == offset) { |
2760 | /* On a data extent, compute bytes to the end of the extent, | |
f4a769ab | 2761 | * possibly including a partial sector at EOF. */ |
a290f085 | 2762 | *pnum = MIN(bytes, hole - offset); |
9c3db310 HR |
2763 | |
2764 | /* | |
2765 | * We are not allowed to return partial sectors, though, so | |
2766 | * round up if necessary. | |
2767 | */ | |
2768 | if (!QEMU_IS_ALIGNED(*pnum, bs->bl.request_alignment)) { | |
2769 | int64_t file_length = raw_getlength(bs); | |
2770 | if (file_length > 0) { | |
2771 | /* Ignore errors, this is just a safeguard */ | |
2772 | assert(hole == file_length); | |
2773 | } | |
2774 | *pnum = ROUND_UP(*pnum, bs->bl.request_alignment); | |
2775 | } | |
2776 | ||
d1f06fe6 | 2777 | ret = BDRV_BLOCK_DATA; |
5500316d | 2778 | } else { |
a290f085 EB |
2779 | /* On a hole, compute bytes to the beginning of the next extent. */ |
2780 | assert(hole == offset); | |
2781 | *pnum = MIN(bytes, data - offset); | |
d1f06fe6 | 2782 | ret = BDRV_BLOCK_ZERO; |
5500316d | 2783 | } |
a290f085 | 2784 | *map = offset; |
02650acb | 2785 | *file = bs; |
a290f085 | 2786 | return ret | BDRV_BLOCK_OFFSET_VALID; |
5500316d PB |
2787 | } |
2788 | ||
31be8a2a SH |
2789 | #if defined(__linux__) |
2790 | /* Verify that the file is not in the page cache */ | |
2791 | static void check_cache_dropped(BlockDriverState *bs, Error **errp) | |
2792 | { | |
2793 | const size_t window_size = 128 * 1024 * 1024; | |
2794 | BDRVRawState *s = bs->opaque; | |
2795 | void *window = NULL; | |
2796 | size_t length = 0; | |
2797 | unsigned char *vec; | |
2798 | size_t page_size; | |
2799 | off_t offset; | |
2800 | off_t end; | |
2801 | ||
2802 | /* mincore(2) page status information requires 1 byte per page */ | |
2803 | page_size = sysconf(_SC_PAGESIZE); | |
2804 | vec = g_malloc(DIV_ROUND_UP(window_size, page_size)); | |
2805 | ||
2806 | end = raw_getlength(bs); | |
2807 | ||
2808 | for (offset = 0; offset < end; offset += window_size) { | |
2809 | void *new_window; | |
2810 | size_t new_length; | |
2811 | size_t vec_end; | |
2812 | size_t i; | |
2813 | int ret; | |
2814 | ||
2815 | /* Unmap previous window if size has changed */ | |
2816 | new_length = MIN(end - offset, window_size); | |
2817 | if (new_length != length) { | |
2818 | munmap(window, length); | |
2819 | window = NULL; | |
2820 | length = 0; | |
2821 | } | |
2822 | ||
2823 | new_window = mmap(window, new_length, PROT_NONE, MAP_PRIVATE, | |
2824 | s->fd, offset); | |
2825 | if (new_window == MAP_FAILED) { | |
2826 | error_setg_errno(errp, errno, "mmap failed"); | |
2827 | break; | |
2828 | } | |
2829 | ||
2830 | window = new_window; | |
2831 | length = new_length; | |
2832 | ||
2833 | ret = mincore(window, length, vec); | |
2834 | if (ret < 0) { | |
2835 | error_setg_errno(errp, errno, "mincore failed"); | |
2836 | break; | |
2837 | } | |
2838 | ||
2839 | vec_end = DIV_ROUND_UP(length, page_size); | |
2840 | for (i = 0; i < vec_end; i++) { | |
2841 | if (vec[i] & 0x1) { | |
31be8a2a SH |
2842 | break; |
2843 | } | |
2844 | } | |
77ed971b MA |
2845 | if (i < vec_end) { |
2846 | error_setg(errp, "page cache still in use!"); | |
2847 | break; | |
2848 | } | |
31be8a2a SH |
2849 | } |
2850 | ||
2851 | if (window) { | |
2852 | munmap(window, length); | |
2853 | } | |
2854 | ||
2855 | g_free(vec); | |
2856 | } | |
2857 | #endif /* __linux__ */ | |
2858 | ||
dd577a26 SH |
2859 | static void coroutine_fn raw_co_invalidate_cache(BlockDriverState *bs, |
2860 | Error **errp) | |
2861 | { | |
2862 | BDRVRawState *s = bs->opaque; | |
2863 | int ret; | |
2864 | ||
2865 | ret = fd_open(bs); | |
2866 | if (ret < 0) { | |
2867 | error_setg_errno(errp, -ret, "The file descriptor is not open"); | |
2868 | return; | |
2869 | } | |
2870 | ||
f357fcd8 SH |
2871 | if (!s->drop_cache) { |
2872 | return; | |
2873 | } | |
2874 | ||
dd577a26 SH |
2875 | if (s->open_flags & O_DIRECT) { |
2876 | return; /* No host kernel page cache */ | |
2877 | } | |
2878 | ||
2879 | #if defined(__linux__) | |
2880 | /* This sets the scene for the next syscall... */ | |
2881 | ret = bdrv_co_flush(bs); | |
2882 | if (ret < 0) { | |
2883 | error_setg_errno(errp, -ret, "flush failed"); | |
2884 | return; | |
2885 | } | |
2886 | ||
2887 | /* Linux does not invalidate pages that are dirty, locked, or mmapped by a | |
2888 | * process. These limitations are okay because we just fsynced the file, | |
2889 | * we don't use mmap, and the file should not be in use by other processes. | |
2890 | */ | |
2891 | ret = posix_fadvise(s->fd, 0, 0, POSIX_FADV_DONTNEED); | |
2892 | if (ret != 0) { /* the return value is a positive errno */ | |
2893 | error_setg_errno(errp, ret, "fadvise failed"); | |
2894 | return; | |
2895 | } | |
31be8a2a SH |
2896 | |
2897 | if (s->check_cache_dropped) { | |
2898 | check_cache_dropped(bs, errp); | |
2899 | } | |
dd577a26 SH |
2900 | #else /* __linux__ */ |
2901 | /* Do nothing. Live migration to a remote host with cache.direct=off is | |
2902 | * unsupported on other host operating systems. Cache consistency issues | |
2903 | * may occur but no error is reported here, partly because that's the | |
2904 | * historical behavior and partly because it's hard to differentiate valid | |
2905 | * configurations that should not cause errors. | |
2906 | */ | |
2907 | #endif /* !__linux__ */ | |
2908 | } | |
2909 | ||
1c450366 AN |
2910 | static void raw_account_discard(BDRVRawState *s, uint64_t nbytes, int ret) |
2911 | { | |
2912 | if (ret) { | |
2913 | s->stats.discard_nb_failed++; | |
2914 | } else { | |
2915 | s->stats.discard_nb_ok++; | |
2916 | s->stats.discard_bytes_ok += nbytes; | |
2917 | } | |
2918 | } | |
2919 | ||
33d70fb6 | 2920 | static coroutine_fn int |
46ee0f46 | 2921 | raw_do_pdiscard(BlockDriverState *bs, int64_t offset, int bytes, bool blkdev) |
dce512de | 2922 | { |
dce512de | 2923 | BDRVRawState *s = bs->opaque; |
46ee0f46 | 2924 | RawPosixAIOData acb; |
1c450366 | 2925 | int ret; |
46ee0f46 KW |
2926 | |
2927 | acb = (RawPosixAIOData) { | |
2928 | .bs = bs, | |
2929 | .aio_fildes = s->fd, | |
2930 | .aio_type = QEMU_AIO_DISCARD, | |
2931 | .aio_offset = offset, | |
2932 | .aio_nbytes = bytes, | |
2933 | }; | |
dce512de | 2934 | |
46ee0f46 KW |
2935 | if (blkdev) { |
2936 | acb.aio_type |= QEMU_AIO_BLKDEV; | |
2937 | } | |
2938 | ||
1c450366 AN |
2939 | ret = raw_thread_pool_submit(bs, handle_aiocb_discard, &acb); |
2940 | raw_account_discard(s, bytes, ret); | |
2941 | return ret; | |
46ee0f46 KW |
2942 | } |
2943 | ||
2944 | static coroutine_fn int | |
2945 | raw_co_pdiscard(BlockDriverState *bs, int64_t offset, int bytes) | |
2946 | { | |
2947 | return raw_do_pdiscard(bs, offset, bytes, false); | |
dce512de | 2948 | } |
0e7e1989 | 2949 | |
7154d8ae KW |
2950 | static int coroutine_fn |
2951 | raw_do_pwrite_zeroes(BlockDriverState *bs, int64_t offset, int bytes, | |
2952 | BdrvRequestFlags flags, bool blkdev) | |
260a82e5 PB |
2953 | { |
2954 | BDRVRawState *s = bs->opaque; | |
7154d8ae KW |
2955 | RawPosixAIOData acb; |
2956 | ThreadPoolFunc *handler; | |
2957 | ||
292d06b9 HR |
2958 | #ifdef CONFIG_FALLOCATE |
2959 | if (offset + bytes > bs->total_sectors * BDRV_SECTOR_SIZE) { | |
2960 | BdrvTrackedRequest *req; | |
292d06b9 HR |
2961 | |
2962 | /* | |
2963 | * This is a workaround for a bug in the Linux XFS driver, | |
2964 | * where writes submitted through the AIO interface will be | |
2965 | * discarded if they happen beyond a concurrently running | |
2966 | * fallocate() that increases the file length (i.e., both the | |
2967 | * write and the fallocate() happen beyond the EOF). | |
2968 | * | |
2969 | * To work around it, we extend the tracked request for this | |
2970 | * zero write until INT64_MAX (effectively infinity), and mark | |
2971 | * it as serializing. | |
2972 | * | |
2973 | * We have to enable this workaround for all filesystems and | |
2974 | * AIO modes (not just XFS with aio=native), because for | |
2975 | * remote filesystems we do not know the host configuration. | |
2976 | */ | |
2977 | ||
2978 | req = bdrv_co_get_self_request(bs); | |
2979 | assert(req); | |
2980 | assert(req->type == BDRV_TRACKED_WRITE); | |
2981 | assert(req->offset <= offset); | |
2982 | assert(req->offset + req->bytes >= offset + bytes); | |
2983 | ||
8b117001 VSO |
2984 | req->bytes = BDRV_MAX_LENGTH - req->offset; |
2985 | ||
69b55e03 | 2986 | bdrv_check_request(req->offset, req->bytes, &error_abort); |
292d06b9 | 2987 | |
8ac5aab2 | 2988 | bdrv_make_request_serialising(req, bs->bl.request_alignment); |
292d06b9 HR |
2989 | } |
2990 | #endif | |
2991 | ||
7154d8ae KW |
2992 | acb = (RawPosixAIOData) { |
2993 | .bs = bs, | |
2994 | .aio_fildes = s->fd, | |
2995 | .aio_type = QEMU_AIO_WRITE_ZEROES, | |
2996 | .aio_offset = offset, | |
2997 | .aio_nbytes = bytes, | |
2998 | }; | |
2999 | ||
3000 | if (blkdev) { | |
3001 | acb.aio_type |= QEMU_AIO_BLKDEV; | |
3002 | } | |
738301e1 KW |
3003 | if (flags & BDRV_REQ_NO_FALLBACK) { |
3004 | acb.aio_type |= QEMU_AIO_NO_FALLBACK; | |
3005 | } | |
260a82e5 | 3006 | |
34fa110e | 3007 | if (flags & BDRV_REQ_MAY_UNMAP) { |
7154d8ae KW |
3008 | acb.aio_type |= QEMU_AIO_DISCARD; |
3009 | handler = handle_aiocb_write_zeroes_unmap; | |
3010 | } else { | |
3011 | handler = handle_aiocb_write_zeroes; | |
260a82e5 | 3012 | } |
34fa110e | 3013 | |
7154d8ae KW |
3014 | return raw_thread_pool_submit(bs, handler, &acb); |
3015 | } | |
3016 | ||
3017 | static int coroutine_fn raw_co_pwrite_zeroes( | |
3018 | BlockDriverState *bs, int64_t offset, | |
3019 | int bytes, BdrvRequestFlags flags) | |
3020 | { | |
3021 | return raw_do_pwrite_zeroes(bs, offset, bytes, flags, false); | |
260a82e5 PB |
3022 | } |
3023 | ||
3024 | static int raw_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) | |
3025 | { | |
260a82e5 PB |
3026 | return 0; |
3027 | } | |
3028 | ||
d9245599 AN |
3029 | static BlockStatsSpecificFile get_blockstats_specific_file(BlockDriverState *bs) |
3030 | { | |
3031 | BDRVRawState *s = bs->opaque; | |
3032 | return (BlockStatsSpecificFile) { | |
3033 | .discard_nb_ok = s->stats.discard_nb_ok, | |
3034 | .discard_nb_failed = s->stats.discard_nb_failed, | |
3035 | .discard_bytes_ok = s->stats.discard_bytes_ok, | |
3036 | }; | |
3037 | } | |
3038 | ||
3039 | static BlockStatsSpecific *raw_get_specific_stats(BlockDriverState *bs) | |
3040 | { | |
3041 | BlockStatsSpecific *stats = g_new(BlockStatsSpecific, 1); | |
3042 | ||
3043 | stats->driver = BLOCKDEV_DRIVER_FILE; | |
3044 | stats->u.file = get_blockstats_specific_file(bs); | |
3045 | ||
3046 | return stats; | |
3047 | } | |
3048 | ||
14176c8d | 3049 | #if defined(HAVE_HOST_BLOCK_DEVICE) |
d9245599 AN |
3050 | static BlockStatsSpecific *hdev_get_specific_stats(BlockDriverState *bs) |
3051 | { | |
3052 | BlockStatsSpecific *stats = g_new(BlockStatsSpecific, 1); | |
3053 | ||
3054 | stats->driver = BLOCKDEV_DRIVER_HOST_DEVICE; | |
3055 | stats->u.host_device = get_blockstats_specific_file(bs); | |
3056 | ||
3057 | return stats; | |
3058 | } | |
14176c8d | 3059 | #endif /* HAVE_HOST_BLOCK_DEVICE */ |
d9245599 | 3060 | |
6f482f74 CL |
3061 | static QemuOptsList raw_create_opts = { |
3062 | .name = "raw-create-opts", | |
3063 | .head = QTAILQ_HEAD_INITIALIZER(raw_create_opts.head), | |
3064 | .desc = { | |
3065 | { | |
3066 | .name = BLOCK_OPT_SIZE, | |
3067 | .type = QEMU_OPT_SIZE, | |
3068 | .help = "Virtual disk size" | |
3069 | }, | |
4ab15590 CL |
3070 | { |
3071 | .name = BLOCK_OPT_NOCOW, | |
3072 | .type = QEMU_OPT_BOOL, | |
3073 | .help = "Turn off copy-on-write (valid only on btrfs)" | |
3074 | }, | |
06247428 HT |
3075 | { |
3076 | .name = BLOCK_OPT_PREALLOC, | |
3077 | .type = QEMU_OPT_STRING, | |
abea0053 SG |
3078 | .help = "Preallocation mode (allowed values: off" |
3079 | #ifdef CONFIG_POSIX_FALLOCATE | |
3080 | ", falloc" | |
3081 | #endif | |
3082 | ", full)" | |
06247428 | 3083 | }, |
ffa244c8 KW |
3084 | { |
3085 | .name = BLOCK_OPT_EXTENT_SIZE_HINT, | |
3086 | .type = QEMU_OPT_SIZE, | |
3087 | .help = "Extent size hint for the image file, 0 to disable" | |
3088 | }, | |
6f482f74 CL |
3089 | { /* end of list */ } |
3090 | } | |
0e7e1989 KW |
3091 | }; |
3092 | ||
244a5668 FZ |
3093 | static int raw_check_perm(BlockDriverState *bs, uint64_t perm, uint64_t shared, |
3094 | Error **errp) | |
3095 | { | |
6ceabe6f | 3096 | BDRVRawState *s = bs->opaque; |
72373e40 | 3097 | int input_flags = s->reopen_state ? s->reopen_state->flags : bs->open_flags; |
6ceabe6f KW |
3098 | int open_flags; |
3099 | int ret; | |
3100 | ||
72373e40 VSO |
3101 | /* We may need a new fd if auto-read-only switches the mode */ |
3102 | ret = raw_reconfigure_getfd(bs, input_flags, &open_flags, perm, | |
3103 | false, errp); | |
3104 | if (ret < 0) { | |
3105 | return ret; | |
3106 | } else if (ret != s->fd) { | |
3107 | Error *local_err = NULL; | |
3108 | ||
6ceabe6f | 3109 | /* |
72373e40 VSO |
3110 | * Fail already check_perm() if we can't get a working O_DIRECT |
3111 | * alignment with the new fd. | |
6ceabe6f | 3112 | */ |
72373e40 VSO |
3113 | raw_probe_alignment(bs, ret, &local_err); |
3114 | if (local_err) { | |
3115 | error_propagate(errp, local_err); | |
3116 | return -EINVAL; | |
6ceabe6f | 3117 | } |
72373e40 VSO |
3118 | |
3119 | s->perm_change_fd = ret; | |
3120 | s->perm_change_flags = open_flags; | |
6ceabe6f KW |
3121 | } |
3122 | ||
3123 | /* Prepare permissions on old fd to avoid conflicts between old and new, | |
3124 | * but keep everything locked that new will need. */ | |
3125 | ret = raw_handle_perm_lock(bs, RAW_PL_PREPARE, perm, shared, errp); | |
3126 | if (ret < 0) { | |
3127 | goto fail; | |
3128 | } | |
3129 | ||
3130 | /* Copy locks to the new fd */ | |
eb43ea16 | 3131 | if (s->perm_change_fd && s->use_lock) { |
6ceabe6f KW |
3132 | ret = raw_apply_lock_bytes(NULL, s->perm_change_fd, perm, ~shared, |
3133 | false, errp); | |
3134 | if (ret < 0) { | |
3135 | raw_handle_perm_lock(bs, RAW_PL_ABORT, 0, 0, NULL); | |
3136 | goto fail; | |
3137 | } | |
3138 | } | |
3139 | return 0; | |
3140 | ||
3141 | fail: | |
72373e40 | 3142 | if (s->perm_change_fd) { |
6ceabe6f KW |
3143 | qemu_close(s->perm_change_fd); |
3144 | } | |
3145 | s->perm_change_fd = 0; | |
3146 | return ret; | |
244a5668 FZ |
3147 | } |
3148 | ||
3149 | static void raw_set_perm(BlockDriverState *bs, uint64_t perm, uint64_t shared) | |
3150 | { | |
3151 | BDRVRawState *s = bs->opaque; | |
6ceabe6f KW |
3152 | |
3153 | /* For reopen, we have already switched to the new fd (.bdrv_set_perm is | |
3154 | * called after .bdrv_reopen_commit) */ | |
3155 | if (s->perm_change_fd && s->fd != s->perm_change_fd) { | |
3156 | qemu_close(s->fd); | |
3157 | s->fd = s->perm_change_fd; | |
094e3639 | 3158 | s->open_flags = s->perm_change_flags; |
6ceabe6f KW |
3159 | } |
3160 | s->perm_change_fd = 0; | |
3161 | ||
244a5668 FZ |
3162 | raw_handle_perm_lock(bs, RAW_PL_COMMIT, perm, shared, NULL); |
3163 | s->perm = perm; | |
3164 | s->shared_perm = shared; | |
3165 | } | |
3166 | ||
3167 | static void raw_abort_perm_update(BlockDriverState *bs) | |
3168 | { | |
6ceabe6f KW |
3169 | BDRVRawState *s = bs->opaque; |
3170 | ||
3171 | /* For reopen, .bdrv_reopen_abort is called afterwards and will close | |
3172 | * the file descriptor. */ | |
72373e40 | 3173 | if (s->perm_change_fd) { |
6ceabe6f KW |
3174 | qemu_close(s->perm_change_fd); |
3175 | } | |
3176 | s->perm_change_fd = 0; | |
3177 | ||
244a5668 FZ |
3178 | raw_handle_perm_lock(bs, RAW_PL_ABORT, 0, 0, NULL); |
3179 | } | |
3180 | ||
67b51fb9 VSO |
3181 | static int coroutine_fn raw_co_copy_range_from( |
3182 | BlockDriverState *bs, BdrvChild *src, uint64_t src_offset, | |
3183 | BdrvChild *dst, uint64_t dst_offset, uint64_t bytes, | |
3184 | BdrvRequestFlags read_flags, BdrvRequestFlags write_flags) | |
1efad060 | 3185 | { |
67b51fb9 VSO |
3186 | return bdrv_co_copy_range_to(src, src_offset, dst, dst_offset, bytes, |
3187 | read_flags, write_flags); | |
1efad060 FZ |
3188 | } |
3189 | ||
3190 | static int coroutine_fn raw_co_copy_range_to(BlockDriverState *bs, | |
67b51fb9 VSO |
3191 | BdrvChild *src, |
3192 | uint64_t src_offset, | |
3193 | BdrvChild *dst, | |
3194 | uint64_t dst_offset, | |
3195 | uint64_t bytes, | |
3196 | BdrvRequestFlags read_flags, | |
3197 | BdrvRequestFlags write_flags) | |
1efad060 | 3198 | { |
58a209c4 | 3199 | RawPosixAIOData acb; |
1efad060 FZ |
3200 | BDRVRawState *s = bs->opaque; |
3201 | BDRVRawState *src_s; | |
3202 | ||
3203 | assert(dst->bs == bs); | |
3204 | if (src->bs->drv->bdrv_co_copy_range_to != raw_co_copy_range_to) { | |
3205 | return -ENOTSUP; | |
3206 | } | |
3207 | ||
3208 | src_s = src->bs->opaque; | |
9f850f67 | 3209 | if (fd_open(src->bs) < 0 || fd_open(dst->bs) < 0) { |
1efad060 FZ |
3210 | return -EIO; |
3211 | } | |
58a209c4 KW |
3212 | |
3213 | acb = (RawPosixAIOData) { | |
3214 | .bs = bs, | |
3215 | .aio_type = QEMU_AIO_COPY_RANGE, | |
3216 | .aio_fildes = src_s->fd, | |
3217 | .aio_offset = src_offset, | |
3218 | .aio_nbytes = bytes, | |
3219 | .copy_range = { | |
3220 | .aio_fd2 = s->fd, | |
3221 | .aio_offset2 = dst_offset, | |
3222 | }, | |
3223 | }; | |
3224 | ||
3225 | return raw_thread_pool_submit(bs, handle_aiocb_copy_range, &acb); | |
1efad060 FZ |
3226 | } |
3227 | ||
5f535a94 | 3228 | BlockDriver bdrv_file = { |
84a12e66 CH |
3229 | .format_name = "file", |
3230 | .protocol_name = "file", | |
856ae5c3 | 3231 | .instance_size = sizeof(BDRVRawState), |
030be321 | 3232 | .bdrv_needs_filename = true, |
856ae5c3 | 3233 | .bdrv_probe = NULL, /* no probe for protocols */ |
078896a9 | 3234 | .bdrv_parse_filename = raw_parse_filename, |
66f82cee | 3235 | .bdrv_file_open = raw_open, |
eeb6b45d JC |
3236 | .bdrv_reopen_prepare = raw_reopen_prepare, |
3237 | .bdrv_reopen_commit = raw_reopen_commit, | |
3238 | .bdrv_reopen_abort = raw_reopen_abort, | |
856ae5c3 | 3239 | .bdrv_close = raw_close, |
927f11e1 | 3240 | .bdrv_co_create = raw_co_create, |
efc75e2a | 3241 | .bdrv_co_create_opts = raw_co_create_opts, |
3ac21627 | 3242 | .bdrv_has_zero_init = bdrv_has_zero_init_1, |
a290f085 | 3243 | .bdrv_co_block_status = raw_co_block_status, |
dd577a26 | 3244 | .bdrv_co_invalidate_cache = raw_co_invalidate_cache, |
2ffa76c2 | 3245 | .bdrv_co_pwrite_zeroes = raw_co_pwrite_zeroes, |
9bffae14 | 3246 | .bdrv_co_delete_file = raw_co_delete_file, |
3b46e624 | 3247 | |
9d52aa3c KW |
3248 | .bdrv_co_preadv = raw_co_preadv, |
3249 | .bdrv_co_pwritev = raw_co_pwritev, | |
33d70fb6 KW |
3250 | .bdrv_co_flush_to_disk = raw_co_flush_to_disk, |
3251 | .bdrv_co_pdiscard = raw_co_pdiscard, | |
1efad060 FZ |
3252 | .bdrv_co_copy_range_from = raw_co_copy_range_from, |
3253 | .bdrv_co_copy_range_to = raw_co_copy_range_to, | |
c25f53b0 | 3254 | .bdrv_refresh_limits = raw_refresh_limits, |
1b3abdcc ML |
3255 | .bdrv_io_plug = raw_aio_plug, |
3256 | .bdrv_io_unplug = raw_aio_unplug, | |
ed6e2161 | 3257 | .bdrv_attach_aio_context = raw_aio_attach_aio_context, |
3c529d93 | 3258 | |
061ca8a3 | 3259 | .bdrv_co_truncate = raw_co_truncate, |
83f64091 | 3260 | .bdrv_getlength = raw_getlength, |
260a82e5 | 3261 | .bdrv_get_info = raw_get_info, |
4a1d5e1f FZ |
3262 | .bdrv_get_allocated_file_size |
3263 | = raw_get_allocated_file_size, | |
d9245599 | 3264 | .bdrv_get_specific_stats = raw_get_specific_stats, |
244a5668 FZ |
3265 | .bdrv_check_perm = raw_check_perm, |
3266 | .bdrv_set_perm = raw_set_perm, | |
3267 | .bdrv_abort_perm_update = raw_abort_perm_update, | |
6f482f74 | 3268 | .create_opts = &raw_create_opts, |
8a2ce0bc | 3269 | .mutable_opts = mutable_opts, |
83f64091 FB |
3270 | }; |
3271 | ||
19cb3738 FB |
3272 | /***********************************************/ |
3273 | /* host device */ | |
3274 | ||
14176c8d JD |
3275 | #if defined(HAVE_HOST_BLOCK_DEVICE) |
3276 | ||
83affaa6 | 3277 | #if defined(__APPLE__) && defined(__MACH__) |
98caa5bc JA |
3278 | static kern_return_t GetBSDPath(io_iterator_t mediaIterator, char *bsdPath, |
3279 | CFIndex maxPathSize, int flags); | |
d0855f12 | 3280 | static char *FindEjectableOpticalMedia(io_iterator_t *mediaIterator) |
19cb3738 | 3281 | { |
d0855f12 | 3282 | kern_return_t kernResult = KERN_FAILURE; |
19cb3738 FB |
3283 | mach_port_t masterPort; |
3284 | CFMutableDictionaryRef classesToMatch; | |
d0855f12 JA |
3285 | const char *matching_array[] = {kIODVDMediaClass, kIOCDMediaClass}; |
3286 | char *mediaType = NULL; | |
19cb3738 FB |
3287 | |
3288 | kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort ); | |
3289 | if ( KERN_SUCCESS != kernResult ) { | |
3290 | printf( "IOMasterPort returned %d\n", kernResult ); | |
3291 | } | |
3b46e624 | 3292 | |
d0855f12 JA |
3293 | int index; |
3294 | for (index = 0; index < ARRAY_SIZE(matching_array); index++) { | |
3295 | classesToMatch = IOServiceMatching(matching_array[index]); | |
3296 | if (classesToMatch == NULL) { | |
3297 | error_report("IOServiceMatching returned NULL for %s", | |
3298 | matching_array[index]); | |
3299 | continue; | |
3300 | } | |
3301 | CFDictionarySetValue(classesToMatch, CFSTR(kIOMediaEjectableKey), | |
3302 | kCFBooleanTrue); | |
3303 | kernResult = IOServiceGetMatchingServices(masterPort, classesToMatch, | |
3304 | mediaIterator); | |
3305 | if (kernResult != KERN_SUCCESS) { | |
3306 | error_report("Note: IOServiceGetMatchingServices returned %d", | |
3307 | kernResult); | |
3308 | continue; | |
3309 | } | |
3b46e624 | 3310 | |
d0855f12 JA |
3311 | /* If a match was found, leave the loop */ |
3312 | if (*mediaIterator != 0) { | |
4f7d28d7 | 3313 | trace_file_FindEjectableOpticalMedia(matching_array[index]); |
d0855f12 JA |
3314 | mediaType = g_strdup(matching_array[index]); |
3315 | break; | |
3316 | } | |
3317 | } | |
3318 | return mediaType; | |
19cb3738 FB |
3319 | } |
3320 | ||
98caa5bc JA |
3321 | kern_return_t GetBSDPath(io_iterator_t mediaIterator, char *bsdPath, |
3322 | CFIndex maxPathSize, int flags) | |
19cb3738 FB |
3323 | { |
3324 | io_object_t nextMedia; | |
3325 | kern_return_t kernResult = KERN_FAILURE; | |
3326 | *bsdPath = '\0'; | |
3327 | nextMedia = IOIteratorNext( mediaIterator ); | |
3328 | if ( nextMedia ) | |
3329 | { | |
3330 | CFTypeRef bsdPathAsCFString; | |
3331 | bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 ); | |
3332 | if ( bsdPathAsCFString ) { | |
3333 | size_t devPathLength; | |
3334 | strcpy( bsdPath, _PATH_DEV ); | |
98caa5bc JA |
3335 | if (flags & BDRV_O_NOCACHE) { |
3336 | strcat(bsdPath, "r"); | |
3337 | } | |
19cb3738 FB |
3338 | devPathLength = strlen( bsdPath ); |
3339 | if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) { | |
3340 | kernResult = KERN_SUCCESS; | |
3341 | } | |
3342 | CFRelease( bsdPathAsCFString ); | |
3343 | } | |
3344 | IOObjectRelease( nextMedia ); | |
3345 | } | |
3b46e624 | 3346 | |
19cb3738 FB |
3347 | return kernResult; |
3348 | } | |
3349 | ||
d0855f12 JA |
3350 | /* Sets up a real cdrom for use in QEMU */ |
3351 | static bool setup_cdrom(char *bsd_path, Error **errp) | |
3352 | { | |
3353 | int index, num_of_test_partitions = 2, fd; | |
3354 | char test_partition[MAXPATHLEN]; | |
3355 | bool partition_found = false; | |
3356 | ||
3357 | /* look for a working partition */ | |
3358 | for (index = 0; index < num_of_test_partitions; index++) { | |
3359 | snprintf(test_partition, sizeof(test_partition), "%ss%d", bsd_path, | |
3360 | index); | |
b18a24a9 | 3361 | fd = qemu_open(test_partition, O_RDONLY | O_BINARY | O_LARGEFILE, NULL); |
d0855f12 JA |
3362 | if (fd >= 0) { |
3363 | partition_found = true; | |
3364 | qemu_close(fd); | |
3365 | break; | |
3366 | } | |
3367 | } | |
3368 | ||
3369 | /* if a working partition on the device was not found */ | |
3370 | if (partition_found == false) { | |
3371 | error_setg(errp, "Failed to find a working partition on disc"); | |
3372 | } else { | |
4f7d28d7 | 3373 | trace_file_setup_cdrom(test_partition); |
d0855f12 JA |
3374 | pstrcpy(bsd_path, MAXPATHLEN, test_partition); |
3375 | } | |
3376 | return partition_found; | |
3377 | } | |
3378 | ||
3379 | /* Prints directions on mounting and unmounting a device */ | |
3380 | static void print_unmounting_directions(const char *file_name) | |
3381 | { | |
3382 | error_report("If device %s is mounted on the desktop, unmount" | |
3383 | " it first before using it in QEMU", file_name); | |
3384 | error_report("Command to unmount device: diskutil unmountDisk %s", | |
3385 | file_name); | |
3386 | error_report("Command to mount device: diskutil mountDisk %s", file_name); | |
3387 | } | |
3388 | ||
3389 | #endif /* defined(__APPLE__) && defined(__MACH__) */ | |
19cb3738 | 3390 | |
508c7cb3 CH |
3391 | static int hdev_probe_device(const char *filename) |
3392 | { | |
3393 | struct stat st; | |
3394 | ||
3395 | /* allow a dedicated CD-ROM driver to match with a higher priority */ | |
3396 | if (strstart(filename, "/dev/cdrom", NULL)) | |
3397 | return 50; | |
3398 | ||
3399 | if (stat(filename, &st) >= 0 && | |
3400 | (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) { | |
3401 | return 100; | |
3402 | } | |
3403 | ||
3404 | return 0; | |
3405 | } | |
3406 | ||
7af803d4 HR |
3407 | static void hdev_parse_filename(const char *filename, QDict *options, |
3408 | Error **errp) | |
3409 | { | |
03c320d8 | 3410 | bdrv_parse_filename_strip_prefix(filename, "host_device:", options); |
7af803d4 HR |
3411 | } |
3412 | ||
3307ed7b DA |
3413 | static bool hdev_is_sg(BlockDriverState *bs) |
3414 | { | |
3415 | ||
3416 | #if defined(__linux__) | |
3417 | ||
0d4377b3 | 3418 | BDRVRawState *s = bs->opaque; |
3307ed7b DA |
3419 | struct stat st; |
3420 | struct sg_scsi_id scsiid; | |
3421 | int sg_version; | |
0d4377b3 KW |
3422 | int ret; |
3423 | ||
3424 | if (stat(bs->filename, &st) < 0 || !S_ISCHR(st.st_mode)) { | |
3425 | return false; | |
3426 | } | |
3307ed7b | 3427 | |
0d4377b3 KW |
3428 | ret = ioctl(s->fd, SG_GET_VERSION_NUM, &sg_version); |
3429 | if (ret < 0) { | |
3430 | return false; | |
3431 | } | |
3432 | ||
3433 | ret = ioctl(s->fd, SG_GET_SCSI_ID, &scsiid); | |
3434 | if (ret >= 0) { | |
4f7d28d7 | 3435 | trace_file_hdev_is_sg(scsiid.scsi_type, sg_version); |
3307ed7b DA |
3436 | return true; |
3437 | } | |
3438 | ||
3439 | #endif | |
3440 | ||
3441 | return false; | |
3442 | } | |
3443 | ||
015a1036 HR |
3444 | static int hdev_open(BlockDriverState *bs, QDict *options, int flags, |
3445 | Error **errp) | |
19cb3738 FB |
3446 | { |
3447 | BDRVRawState *s = bs->opaque; | |
da888d37 | 3448 | int ret; |
a76bab49 | 3449 | |
83affaa6 | 3450 | #if defined(__APPLE__) && defined(__MACH__) |
129c7d1c MA |
3451 | /* |
3452 | * Caution: while qdict_get_str() is fine, getting non-string types | |
3453 | * would require more care. When @options come from -blockdev or | |
3454 | * blockdev_add, its members are typed according to the QAPI | |
3455 | * schema, but when they come from -drive, they're all QString. | |
3456 | */ | |
3307ed7b | 3457 | const char *filename = qdict_get_str(options, "filename"); |
d0855f12 JA |
3458 | char bsd_path[MAXPATHLEN] = ""; |
3459 | bool error_occurred = false; | |
3460 | ||
3461 | /* If using a real cdrom */ | |
3462 | if (strcmp(filename, "/dev/cdrom") == 0) { | |
3463 | char *mediaType = NULL; | |
3464 | kern_return_t ret_val; | |
3465 | io_iterator_t mediaIterator = 0; | |
3466 | ||
3467 | mediaType = FindEjectableOpticalMedia(&mediaIterator); | |
3468 | if (mediaType == NULL) { | |
3469 | error_setg(errp, "Please make sure your CD/DVD is in the optical" | |
3470 | " drive"); | |
3471 | error_occurred = true; | |
3472 | goto hdev_open_Mac_error; | |
3473 | } | |
3307ed7b | 3474 | |
d0855f12 JA |
3475 | ret_val = GetBSDPath(mediaIterator, bsd_path, sizeof(bsd_path), flags); |
3476 | if (ret_val != KERN_SUCCESS) { | |
3477 | error_setg(errp, "Could not get BSD path for optical drive"); | |
3478 | error_occurred = true; | |
3479 | goto hdev_open_Mac_error; | |
3480 | } | |
3481 | ||
3482 | /* If a real optical drive was not found */ | |
3483 | if (bsd_path[0] == '\0') { | |
3484 | error_setg(errp, "Failed to obtain bsd path for optical drive"); | |
3485 | error_occurred = true; | |
3486 | goto hdev_open_Mac_error; | |
3487 | } | |
3488 | ||
3489 | /* If using a cdrom disc and finding a partition on the disc failed */ | |
3490 | if (strncmp(mediaType, kIOCDMediaClass, 9) == 0 && | |
3491 | setup_cdrom(bsd_path, errp) == false) { | |
3492 | print_unmounting_directions(bsd_path); | |
3493 | error_occurred = true; | |
3494 | goto hdev_open_Mac_error; | |
19cb3738 | 3495 | } |
3b46e624 | 3496 | |
46f5ac20 | 3497 | qdict_put_str(options, "filename", bsd_path); |
d0855f12 JA |
3498 | |
3499 | hdev_open_Mac_error: | |
3500 | g_free(mediaType); | |
3501 | if (mediaIterator) { | |
3502 | IOObjectRelease(mediaIterator); | |
3503 | } | |
3504 | if (error_occurred) { | |
3505 | return -ENOENT; | |
3506 | } | |
19cb3738 | 3507 | } |
d0855f12 | 3508 | #endif /* defined(__APPLE__) && defined(__MACH__) */ |
19cb3738 FB |
3509 | |
3510 | s->type = FTYPE_FILE; | |
90babde0 | 3511 | |
668f62ec | 3512 | ret = raw_open_common(bs, options, flags, 0, true, errp); |
da888d37 | 3513 | if (ret < 0) { |
d0855f12 JA |
3514 | #if defined(__APPLE__) && defined(__MACH__) |
3515 | if (*bsd_path) { | |
3516 | filename = bsd_path; | |
3517 | } | |
3518 | /* if a physical device experienced an error while being opened */ | |
3519 | if (strncmp(filename, "/dev/", 5) == 0) { | |
3520 | print_unmounting_directions(filename); | |
3521 | } | |
3522 | #endif /* defined(__APPLE__) && defined(__MACH__) */ | |
da888d37 SH |
3523 | return ret; |
3524 | } | |
3525 | ||
3307ed7b DA |
3526 | /* Since this does ioctl the device must be already opened */ |
3527 | bs->sg = hdev_is_sg(bs); | |
3528 | ||
da888d37 | 3529 | return ret; |
19cb3738 FB |
3530 | } |
3531 | ||
03ff3ca3 | 3532 | #if defined(__linux__) |
2f3a7ab3 KW |
3533 | static int coroutine_fn |
3534 | hdev_co_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) | |
221f715d | 3535 | { |
f141eafe | 3536 | BDRVRawState *s = bs->opaque; |
03425671 | 3537 | RawPosixAIOData acb; |
2f3a7ab3 | 3538 | int ret; |
221f715d | 3539 | |
2f3a7ab3 KW |
3540 | ret = fd_open(bs); |
3541 | if (ret < 0) { | |
3542 | return ret; | |
3543 | } | |
c208e8c2 | 3544 | |
7c9e5276 PB |
3545 | if (req == SG_IO && s->pr_mgr) { |
3546 | struct sg_io_hdr *io_hdr = buf; | |
3547 | if (io_hdr->cmdp[0] == PERSISTENT_RESERVE_OUT || | |
3548 | io_hdr->cmdp[0] == PERSISTENT_RESERVE_IN) { | |
3549 | return pr_manager_execute(s->pr_mgr, bdrv_get_aio_context(bs), | |
2f3a7ab3 | 3550 | s->fd, io_hdr); |
7c9e5276 PB |
3551 | } |
3552 | } | |
3553 | ||
03425671 KW |
3554 | acb = (RawPosixAIOData) { |
3555 | .bs = bs, | |
3556 | .aio_type = QEMU_AIO_IOCTL, | |
3557 | .aio_fildes = s->fd, | |
3558 | .aio_offset = 0, | |
3559 | .ioctl = { | |
3560 | .buf = buf, | |
3561 | .cmd = req, | |
3562 | }, | |
3563 | }; | |
3564 | ||
3565 | return raw_thread_pool_submit(bs, handle_aiocb_ioctl, &acb); | |
221f715d | 3566 | } |
f709623b | 3567 | #endif /* linux */ |
221f715d | 3568 | |
33d70fb6 KW |
3569 | static coroutine_fn int |
3570 | hdev_co_pdiscard(BlockDriverState *bs, int64_t offset, int bytes) | |
c36dd8a0 | 3571 | { |
1c450366 | 3572 | BDRVRawState *s = bs->opaque; |
33d70fb6 | 3573 | int ret; |
c36dd8a0 | 3574 | |
33d70fb6 KW |
3575 | ret = fd_open(bs); |
3576 | if (ret < 0) { | |
1c450366 | 3577 | raw_account_discard(s, bytes, ret); |
33d70fb6 | 3578 | return ret; |
c36dd8a0 | 3579 | } |
46ee0f46 | 3580 | return raw_do_pdiscard(bs, offset, bytes, true); |
c36dd8a0 AF |
3581 | } |
3582 | ||
2ffa76c2 | 3583 | static coroutine_fn int hdev_co_pwrite_zeroes(BlockDriverState *bs, |
f5a5ca79 | 3584 | int64_t offset, int bytes, BdrvRequestFlags flags) |
d0b4503e | 3585 | { |
d0b4503e PB |
3586 | int rc; |
3587 | ||
3588 | rc = fd_open(bs); | |
3589 | if (rc < 0) { | |
3590 | return rc; | |
3591 | } | |
34fa110e | 3592 | |
7154d8ae | 3593 | return raw_do_pwrite_zeroes(bs, offset, bytes, flags, true); |
d0b4503e PB |
3594 | } |
3595 | ||
5efa9d5a | 3596 | static BlockDriver bdrv_host_device = { |
0b4ce02e | 3597 | .format_name = "host_device", |
84a12e66 | 3598 | .protocol_name = "host_device", |
0b4ce02e | 3599 | .instance_size = sizeof(BDRVRawState), |
030be321 | 3600 | .bdrv_needs_filename = true, |
0b4ce02e | 3601 | .bdrv_probe_device = hdev_probe_device, |
7af803d4 | 3602 | .bdrv_parse_filename = hdev_parse_filename, |
66f82cee | 3603 | .bdrv_file_open = hdev_open, |
0b4ce02e | 3604 | .bdrv_close = raw_close, |
1bc6b705 JC |
3605 | .bdrv_reopen_prepare = raw_reopen_prepare, |
3606 | .bdrv_reopen_commit = raw_reopen_commit, | |
3607 | .bdrv_reopen_abort = raw_reopen_abort, | |
5a5e7f8c ML |
3608 | .bdrv_co_create_opts = bdrv_co_create_opts_simple, |
3609 | .create_opts = &bdrv_create_opts_simple, | |
8a2ce0bc | 3610 | .mutable_opts = mutable_opts, |
dd577a26 | 3611 | .bdrv_co_invalidate_cache = raw_co_invalidate_cache, |
2ffa76c2 | 3612 | .bdrv_co_pwrite_zeroes = hdev_co_pwrite_zeroes, |
3b46e624 | 3613 | |
9d52aa3c KW |
3614 | .bdrv_co_preadv = raw_co_preadv, |
3615 | .bdrv_co_pwritev = raw_co_pwritev, | |
33d70fb6 KW |
3616 | .bdrv_co_flush_to_disk = raw_co_flush_to_disk, |
3617 | .bdrv_co_pdiscard = hdev_co_pdiscard, | |
1efad060 FZ |
3618 | .bdrv_co_copy_range_from = raw_co_copy_range_from, |
3619 | .bdrv_co_copy_range_to = raw_co_copy_range_to, | |
c25f53b0 | 3620 | .bdrv_refresh_limits = raw_refresh_limits, |
1b3abdcc ML |
3621 | .bdrv_io_plug = raw_aio_plug, |
3622 | .bdrv_io_unplug = raw_aio_unplug, | |
042b757c | 3623 | .bdrv_attach_aio_context = raw_aio_attach_aio_context, |
3c529d93 | 3624 | |
061ca8a3 | 3625 | .bdrv_co_truncate = raw_co_truncate, |
e60f469c | 3626 | .bdrv_getlength = raw_getlength, |
260a82e5 | 3627 | .bdrv_get_info = raw_get_info, |
4a1d5e1f FZ |
3628 | .bdrv_get_allocated_file_size |
3629 | = raw_get_allocated_file_size, | |
d9245599 | 3630 | .bdrv_get_specific_stats = hdev_get_specific_stats, |
244a5668 FZ |
3631 | .bdrv_check_perm = raw_check_perm, |
3632 | .bdrv_set_perm = raw_set_perm, | |
3633 | .bdrv_abort_perm_update = raw_abort_perm_update, | |
1a9335e4 ET |
3634 | .bdrv_probe_blocksizes = hdev_probe_blocksizes, |
3635 | .bdrv_probe_geometry = hdev_probe_geometry, | |
19cb3738 | 3636 | |
f3a5d3f8 | 3637 | /* generic scsi device */ |
63ec93db | 3638 | #ifdef __linux__ |
2f3a7ab3 | 3639 | .bdrv_co_ioctl = hdev_co_ioctl, |
63ec93db | 3640 | #endif |
f3a5d3f8 CH |
3641 | }; |
3642 | ||
18fa1c42 HR |
3643 | #if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
3644 | static void cdrom_parse_filename(const char *filename, QDict *options, | |
3645 | Error **errp) | |
3646 | { | |
03c320d8 | 3647 | bdrv_parse_filename_strip_prefix(filename, "host_cdrom:", options); |
18fa1c42 HR |
3648 | } |
3649 | #endif | |
3650 | ||
3651 | #ifdef __linux__ | |
015a1036 HR |
3652 | static int cdrom_open(BlockDriverState *bs, QDict *options, int flags, |
3653 | Error **errp) | |
f3a5d3f8 CH |
3654 | { |
3655 | BDRVRawState *s = bs->opaque; | |
3656 | ||
f3a5d3f8 CH |
3657 | s->type = FTYPE_CD; |
3658 | ||
19a3da7f | 3659 | /* open will not fail even if no CD is inserted, so add O_NONBLOCK */ |
230ff739 | 3660 | return raw_open_common(bs, options, flags, O_NONBLOCK, true, errp); |
f3a5d3f8 CH |
3661 | } |
3662 | ||
508c7cb3 CH |
3663 | static int cdrom_probe_device(const char *filename) |
3664 | { | |
3baf720e CR |
3665 | int fd, ret; |
3666 | int prio = 0; | |
343f8568 | 3667 | struct stat st; |
3baf720e | 3668 | |
b18a24a9 | 3669 | fd = qemu_open(filename, O_RDONLY | O_NONBLOCK, NULL); |
3baf720e CR |
3670 | if (fd < 0) { |
3671 | goto out; | |
3672 | } | |
343f8568 JS |
3673 | ret = fstat(fd, &st); |
3674 | if (ret == -1 || !S_ISBLK(st.st_mode)) { | |
3675 | goto outc; | |
3676 | } | |
3baf720e CR |
3677 | |
3678 | /* Attempt to detect via a CDROM specific ioctl */ | |
3679 | ret = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT); | |
3680 | if (ret >= 0) | |
3681 | prio = 100; | |
3682 | ||
343f8568 | 3683 | outc: |
2e1e79da | 3684 | qemu_close(fd); |
3baf720e CR |
3685 | out: |
3686 | return prio; | |
508c7cb3 CH |
3687 | } |
3688 | ||
e031f750 | 3689 | static bool cdrom_is_inserted(BlockDriverState *bs) |
f3a5d3f8 CH |
3690 | { |
3691 | BDRVRawState *s = bs->opaque; | |
3692 | int ret; | |
3693 | ||
3694 | ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT); | |
e031f750 | 3695 | return ret == CDS_DISC_OK; |
f3a5d3f8 CH |
3696 | } |
3697 | ||
f36f3949 | 3698 | static void cdrom_eject(BlockDriverState *bs, bool eject_flag) |
f3a5d3f8 CH |
3699 | { |
3700 | BDRVRawState *s = bs->opaque; | |
3701 | ||
3702 | if (eject_flag) { | |
3703 | if (ioctl(s->fd, CDROMEJECT, NULL) < 0) | |
3704 | perror("CDROMEJECT"); | |
3705 | } else { | |
3706 | if (ioctl(s->fd, CDROMCLOSETRAY, NULL) < 0) | |
3707 | perror("CDROMEJECT"); | |
3708 | } | |
f3a5d3f8 CH |
3709 | } |
3710 | ||
025e849a | 3711 | static void cdrom_lock_medium(BlockDriverState *bs, bool locked) |
f3a5d3f8 CH |
3712 | { |
3713 | BDRVRawState *s = bs->opaque; | |
3714 | ||
3715 | if (ioctl(s->fd, CDROM_LOCKDOOR, locked) < 0) { | |
3716 | /* | |
3717 | * Note: an error can happen if the distribution automatically | |
3718 | * mounts the CD-ROM | |
3719 | */ | |
3720 | /* perror("CDROM_LOCKDOOR"); */ | |
3721 | } | |
f3a5d3f8 CH |
3722 | } |
3723 | ||
3724 | static BlockDriver bdrv_host_cdrom = { | |
3725 | .format_name = "host_cdrom", | |
84a12e66 | 3726 | .protocol_name = "host_cdrom", |
f3a5d3f8 | 3727 | .instance_size = sizeof(BDRVRawState), |
030be321 | 3728 | .bdrv_needs_filename = true, |
508c7cb3 | 3729 | .bdrv_probe_device = cdrom_probe_device, |
18fa1c42 | 3730 | .bdrv_parse_filename = cdrom_parse_filename, |
66f82cee | 3731 | .bdrv_file_open = cdrom_open, |
f3a5d3f8 | 3732 | .bdrv_close = raw_close, |
1bc6b705 JC |
3733 | .bdrv_reopen_prepare = raw_reopen_prepare, |
3734 | .bdrv_reopen_commit = raw_reopen_commit, | |
3735 | .bdrv_reopen_abort = raw_reopen_abort, | |
5a5e7f8c ML |
3736 | .bdrv_co_create_opts = bdrv_co_create_opts_simple, |
3737 | .create_opts = &bdrv_create_opts_simple, | |
8a2ce0bc | 3738 | .mutable_opts = mutable_opts, |
dd577a26 | 3739 | .bdrv_co_invalidate_cache = raw_co_invalidate_cache, |
f3a5d3f8 | 3740 | |
9d52aa3c KW |
3741 | .bdrv_co_preadv = raw_co_preadv, |
3742 | .bdrv_co_pwritev = raw_co_pwritev, | |
33d70fb6 | 3743 | .bdrv_co_flush_to_disk = raw_co_flush_to_disk, |
c25f53b0 | 3744 | .bdrv_refresh_limits = raw_refresh_limits, |
1b3abdcc ML |
3745 | .bdrv_io_plug = raw_aio_plug, |
3746 | .bdrv_io_unplug = raw_aio_unplug, | |
042b757c | 3747 | .bdrv_attach_aio_context = raw_aio_attach_aio_context, |
f3a5d3f8 | 3748 | |
061ca8a3 | 3749 | .bdrv_co_truncate = raw_co_truncate, |
b94a2610 KW |
3750 | .bdrv_getlength = raw_getlength, |
3751 | .has_variable_length = true, | |
4a1d5e1f FZ |
3752 | .bdrv_get_allocated_file_size |
3753 | = raw_get_allocated_file_size, | |
f3a5d3f8 CH |
3754 | |
3755 | /* removable device support */ | |
3756 | .bdrv_is_inserted = cdrom_is_inserted, | |
3757 | .bdrv_eject = cdrom_eject, | |
025e849a | 3758 | .bdrv_lock_medium = cdrom_lock_medium, |
f3a5d3f8 CH |
3759 | |
3760 | /* generic scsi device */ | |
2f3a7ab3 | 3761 | .bdrv_co_ioctl = hdev_co_ioctl, |
f3a5d3f8 CH |
3762 | }; |
3763 | #endif /* __linux__ */ | |
3764 | ||
a167ba50 | 3765 | #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__) |
511018e4 AT |
3766 | static int cdrom_open(BlockDriverState *bs, QDict *options, int flags, |
3767 | Error **errp) | |
f3a5d3f8 CH |
3768 | { |
3769 | BDRVRawState *s = bs->opaque; | |
3770 | int ret; | |
3771 | ||
3772 | s->type = FTYPE_CD; | |
3773 | ||
668f62ec | 3774 | ret = raw_open_common(bs, options, flags, 0, true, errp); |
e428e439 | 3775 | if (ret) { |
f3a5d3f8 | 3776 | return ret; |
e428e439 | 3777 | } |
f3a5d3f8 | 3778 | |
9b2260cb | 3779 | /* make sure the door isn't locked at this time */ |
f3a5d3f8 CH |
3780 | ioctl(s->fd, CDIOCALLOW); |
3781 | return 0; | |
3782 | } | |
3783 | ||
508c7cb3 CH |
3784 | static int cdrom_probe_device(const char *filename) |
3785 | { | |
3786 | if (strstart(filename, "/dev/cd", NULL) || | |
3787 | strstart(filename, "/dev/acd", NULL)) | |
3788 | return 100; | |
3789 | return 0; | |
3790 | } | |
3791 | ||
f3a5d3f8 CH |
3792 | static int cdrom_reopen(BlockDriverState *bs) |
3793 | { | |
3794 | BDRVRawState *s = bs->opaque; | |
3795 | int fd; | |
3796 | ||
3797 | /* | |
3798 | * Force reread of possibly changed/newly loaded disc, | |
3799 | * FreeBSD seems to not notice sometimes... | |
3800 | */ | |
3801 | if (s->fd >= 0) | |
2e1e79da | 3802 | qemu_close(s->fd); |
b18a24a9 | 3803 | fd = qemu_open(bs->filename, s->open_flags, NULL); |
f3a5d3f8 CH |
3804 | if (fd < 0) { |
3805 | s->fd = -1; | |
3806 | return -EIO; | |
3807 | } | |
3808 | s->fd = fd; | |
3809 | ||
9b2260cb | 3810 | /* make sure the door isn't locked at this time */ |
f3a5d3f8 CH |
3811 | ioctl(s->fd, CDIOCALLOW); |
3812 | return 0; | |
3813 | } | |
3814 | ||
e031f750 | 3815 | static bool cdrom_is_inserted(BlockDriverState *bs) |
f3a5d3f8 CH |
3816 | { |
3817 | return raw_getlength(bs) > 0; | |
3818 | } | |
3819 | ||
f36f3949 | 3820 | static void cdrom_eject(BlockDriverState *bs, bool eject_flag) |
f3a5d3f8 CH |
3821 | { |
3822 | BDRVRawState *s = bs->opaque; | |
3823 | ||
3824 | if (s->fd < 0) | |
822e1cd1 | 3825 | return; |
f3a5d3f8 CH |
3826 | |
3827 | (void) ioctl(s->fd, CDIOCALLOW); | |
3828 | ||
3829 | if (eject_flag) { | |
3830 | if (ioctl(s->fd, CDIOCEJECT) < 0) | |
3831 | perror("CDIOCEJECT"); | |
3832 | } else { | |
3833 | if (ioctl(s->fd, CDIOCCLOSE) < 0) | |
3834 | perror("CDIOCCLOSE"); | |
3835 | } | |
3836 | ||
822e1cd1 | 3837 | cdrom_reopen(bs); |
f3a5d3f8 CH |
3838 | } |
3839 | ||
025e849a | 3840 | static void cdrom_lock_medium(BlockDriverState *bs, bool locked) |
f3a5d3f8 CH |
3841 | { |
3842 | BDRVRawState *s = bs->opaque; | |
3843 | ||
3844 | if (s->fd < 0) | |
7bf37fed | 3845 | return; |
f3a5d3f8 CH |
3846 | if (ioctl(s->fd, (locked ? CDIOCPREVENT : CDIOCALLOW)) < 0) { |
3847 | /* | |
3848 | * Note: an error can happen if the distribution automatically | |
3849 | * mounts the CD-ROM | |
3850 | */ | |
3851 | /* perror("CDROM_LOCKDOOR"); */ | |
3852 | } | |
f3a5d3f8 CH |
3853 | } |
3854 | ||
3855 | static BlockDriver bdrv_host_cdrom = { | |
3856 | .format_name = "host_cdrom", | |
84a12e66 | 3857 | .protocol_name = "host_cdrom", |
f3a5d3f8 | 3858 | .instance_size = sizeof(BDRVRawState), |
030be321 | 3859 | .bdrv_needs_filename = true, |
508c7cb3 | 3860 | .bdrv_probe_device = cdrom_probe_device, |
18fa1c42 | 3861 | .bdrv_parse_filename = cdrom_parse_filename, |
66f82cee | 3862 | .bdrv_file_open = cdrom_open, |
f3a5d3f8 | 3863 | .bdrv_close = raw_close, |
1bc6b705 JC |
3864 | .bdrv_reopen_prepare = raw_reopen_prepare, |
3865 | .bdrv_reopen_commit = raw_reopen_commit, | |
3866 | .bdrv_reopen_abort = raw_reopen_abort, | |
5a5e7f8c ML |
3867 | .bdrv_co_create_opts = bdrv_co_create_opts_simple, |
3868 | .create_opts = &bdrv_create_opts_simple, | |
8a2ce0bc | 3869 | .mutable_opts = mutable_opts, |
f3a5d3f8 | 3870 | |
9d52aa3c KW |
3871 | .bdrv_co_preadv = raw_co_preadv, |
3872 | .bdrv_co_pwritev = raw_co_pwritev, | |
33d70fb6 | 3873 | .bdrv_co_flush_to_disk = raw_co_flush_to_disk, |
c25f53b0 | 3874 | .bdrv_refresh_limits = raw_refresh_limits, |
1b3abdcc ML |
3875 | .bdrv_io_plug = raw_aio_plug, |
3876 | .bdrv_io_unplug = raw_aio_unplug, | |
042b757c | 3877 | .bdrv_attach_aio_context = raw_aio_attach_aio_context, |
f3a5d3f8 | 3878 | |
061ca8a3 | 3879 | .bdrv_co_truncate = raw_co_truncate, |
b94a2610 KW |
3880 | .bdrv_getlength = raw_getlength, |
3881 | .has_variable_length = true, | |
4a1d5e1f FZ |
3882 | .bdrv_get_allocated_file_size |
3883 | = raw_get_allocated_file_size, | |
f3a5d3f8 | 3884 | |
19cb3738 | 3885 | /* removable device support */ |
f3a5d3f8 CH |
3886 | .bdrv_is_inserted = cdrom_is_inserted, |
3887 | .bdrv_eject = cdrom_eject, | |
025e849a | 3888 | .bdrv_lock_medium = cdrom_lock_medium, |
19cb3738 | 3889 | }; |
f3a5d3f8 | 3890 | #endif /* __FreeBSD__ */ |
5efa9d5a | 3891 | |
14176c8d JD |
3892 | #endif /* HAVE_HOST_BLOCK_DEVICE */ |
3893 | ||
84a12e66 | 3894 | static void bdrv_file_init(void) |
5efa9d5a | 3895 | { |
508c7cb3 CH |
3896 | /* |
3897 | * Register all the drivers. Note that order is important, the driver | |
3898 | * registered last will get probed first. | |
3899 | */ | |
84a12e66 | 3900 | bdrv_register(&bdrv_file); |
14176c8d | 3901 | #if defined(HAVE_HOST_BLOCK_DEVICE) |
5efa9d5a | 3902 | bdrv_register(&bdrv_host_device); |
f3a5d3f8 | 3903 | #ifdef __linux__ |
f3a5d3f8 CH |
3904 | bdrv_register(&bdrv_host_cdrom); |
3905 | #endif | |
a167ba50 | 3906 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
f3a5d3f8 CH |
3907 | bdrv_register(&bdrv_host_cdrom); |
3908 | #endif | |
14176c8d | 3909 | #endif /* HAVE_HOST_BLOCK_DEVICE */ |
5efa9d5a AL |
3910 | } |
3911 | ||
84a12e66 | 3912 | block_init(bdrv_file_init); |