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