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