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