]>
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" |
1de7afc9 PB |
25 | #include "qemu/timer.h" |
26 | #include "qemu/log.h" | |
737e150e | 27 | #include "block/block_int.h" |
1de7afc9 | 28 | #include "qemu/module.h" |
de81a169 | 29 | #include "trace.h" |
737e150e | 30 | #include "block/thread-pool.h" |
1de7afc9 | 31 | #include "qemu/iov.h" |
9f8540ec | 32 | #include "raw-aio.h" |
83f64091 | 33 | |
83affaa6 | 34 | #if defined(__APPLE__) && (__MACH__) |
83f64091 FB |
35 | #include <paths.h> |
36 | #include <sys/param.h> | |
37 | #include <IOKit/IOKitLib.h> | |
38 | #include <IOKit/IOBSD.h> | |
39 | #include <IOKit/storage/IOMediaBSDClient.h> | |
40 | #include <IOKit/storage/IOMedia.h> | |
41 | #include <IOKit/storage/IOCDMedia.h> | |
42 | //#include <IOKit/storage/IOCDTypes.h> | |
43 | #include <CoreFoundation/CoreFoundation.h> | |
44 | #endif | |
45 | ||
46 | #ifdef __sun__ | |
2e9671da | 47 | #define _POSIX_PTHREAD_SEMANTICS 1 |
83f64091 FB |
48 | #include <sys/dkio.h> |
49 | #endif | |
19cb3738 | 50 | #ifdef __linux__ |
343f8568 JS |
51 | #include <sys/types.h> |
52 | #include <sys/stat.h> | |
19cb3738 | 53 | #include <sys/ioctl.h> |
05acda4d | 54 | #include <sys/param.h> |
19cb3738 FB |
55 | #include <linux/cdrom.h> |
56 | #include <linux/fd.h> | |
5500316d PB |
57 | #include <linux/fs.h> |
58 | #endif | |
59 | #ifdef CONFIG_FIEMAP | |
60 | #include <linux/fiemap.h> | |
19cb3738 | 61 | #endif |
3d4fa43e KK |
62 | #ifdef CONFIG_FALLOCATE_PUNCH_HOLE |
63 | #include <linux/falloc.h> | |
64 | #endif | |
a167ba50 | 65 | #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__) |
1cb6c3fd | 66 | #include <sys/disk.h> |
9f23011a | 67 | #include <sys/cdio.h> |
1cb6c3fd | 68 | #endif |
83f64091 | 69 | |
128ab2ff BS |
70 | #ifdef __OpenBSD__ |
71 | #include <sys/ioctl.h> | |
72 | #include <sys/disklabel.h> | |
73 | #include <sys/dkio.h> | |
74 | #endif | |
75 | ||
d1f6fd8d CE |
76 | #ifdef __NetBSD__ |
77 | #include <sys/ioctl.h> | |
78 | #include <sys/disklabel.h> | |
79 | #include <sys/dkio.h> | |
80 | #include <sys/disk.h> | |
81 | #endif | |
82 | ||
c5e97233 BS |
83 | #ifdef __DragonFly__ |
84 | #include <sys/ioctl.h> | |
85 | #include <sys/diskslice.h> | |
86 | #endif | |
87 | ||
dce512de CH |
88 | #ifdef CONFIG_XFS |
89 | #include <xfs/xfs.h> | |
90 | #endif | |
91 | ||
19cb3738 | 92 | //#define DEBUG_FLOPPY |
83f64091 | 93 | |
faf07963 | 94 | //#define DEBUG_BLOCK |
03ff3ca3 | 95 | #if defined(DEBUG_BLOCK) |
001faf32 BS |
96 | #define DEBUG_BLOCK_PRINT(formatCstr, ...) do { if (qemu_log_enabled()) \ |
97 | { qemu_log(formatCstr, ## __VA_ARGS__); qemu_log_flush(); } } while (0) | |
8c05dbf9 | 98 | #else |
001faf32 | 99 | #define DEBUG_BLOCK_PRINT(formatCstr, ...) |
8c05dbf9 TS |
100 | #endif |
101 | ||
f6465578 AL |
102 | /* OS X does not have O_DSYNC */ |
103 | #ifndef O_DSYNC | |
1c27a8b3 | 104 | #ifdef O_SYNC |
7ab064d2 | 105 | #define O_DSYNC O_SYNC |
1c27a8b3 | 106 | #elif defined(O_FSYNC) |
107 | #define O_DSYNC O_FSYNC | |
108 | #endif | |
f6465578 AL |
109 | #endif |
110 | ||
9f7965c7 AL |
111 | /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */ |
112 | #ifndef O_DIRECT | |
113 | #define O_DIRECT O_DSYNC | |
114 | #endif | |
115 | ||
19cb3738 FB |
116 | #define FTYPE_FILE 0 |
117 | #define FTYPE_CD 1 | |
118 | #define FTYPE_FD 2 | |
83f64091 | 119 | |
c57c846a | 120 | /* if the FD is not accessed during that time (in ns), we try to |
19cb3738 | 121 | reopen it to see if the disk has been changed */ |
c57c846a | 122 | #define FD_OPEN_TIMEOUT (1000000000) |
83f64091 | 123 | |
581b9e29 CH |
124 | #define MAX_BLOCKSIZE 4096 |
125 | ||
19cb3738 FB |
126 | typedef struct BDRVRawState { |
127 | int fd; | |
128 | int type; | |
0e1d8f4c | 129 | int open_flags; |
19cb3738 FB |
130 | #if defined(__linux__) |
131 | /* linux floppy specific */ | |
19cb3738 FB |
132 | int64_t fd_open_time; |
133 | int64_t fd_error_time; | |
134 | int fd_got_error; | |
135 | int fd_media_changed; | |
83f64091 | 136 | #endif |
e44bd6fc | 137 | #ifdef CONFIG_LINUX_AIO |
5c6c3a6c | 138 | int use_aio; |
1e5b9d2f | 139 | void *aio_ctx; |
e44bd6fc | 140 | #endif |
dce512de CH |
141 | #ifdef CONFIG_XFS |
142 | bool is_xfs : 1; | |
143 | #endif | |
c85191e5 | 144 | bool has_discard : 1; |
19cb3738 FB |
145 | } BDRVRawState; |
146 | ||
eeb6b45d JC |
147 | typedef struct BDRVRawReopenState { |
148 | int fd; | |
149 | int open_flags; | |
150 | #ifdef CONFIG_LINUX_AIO | |
151 | int use_aio; | |
152 | #endif | |
153 | } BDRVRawReopenState; | |
154 | ||
19cb3738 | 155 | static int fd_open(BlockDriverState *bs); |
22afa7b5 | 156 | static int64_t raw_getlength(BlockDriverState *bs); |
83f64091 | 157 | |
de81a169 PB |
158 | typedef struct RawPosixAIOData { |
159 | BlockDriverState *bs; | |
160 | int aio_fildes; | |
161 | union { | |
162 | struct iovec *aio_iov; | |
163 | void *aio_ioctl_buf; | |
164 | }; | |
165 | int aio_niov; | |
8238010b | 166 | uint64_t aio_nbytes; |
de81a169 PB |
167 | #define aio_ioctl_cmd aio_nbytes /* for QEMU_AIO_IOCTL */ |
168 | off_t aio_offset; | |
169 | int aio_type; | |
170 | } RawPosixAIOData; | |
171 | ||
a167ba50 | 172 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
f3a5d3f8 | 173 | static int cdrom_reopen(BlockDriverState *bs); |
9f23011a BS |
174 | #endif |
175 | ||
1de1ae0a CE |
176 | #if defined(__NetBSD__) |
177 | static int raw_normalize_devicepath(const char **filename) | |
178 | { | |
179 | static char namebuf[PATH_MAX]; | |
180 | const char *dp, *fname; | |
181 | struct stat sb; | |
182 | ||
183 | fname = *filename; | |
184 | dp = strrchr(fname, '/'); | |
185 | if (lstat(fname, &sb) < 0) { | |
186 | fprintf(stderr, "%s: stat failed: %s\n", | |
187 | fname, strerror(errno)); | |
188 | return -errno; | |
189 | } | |
190 | ||
191 | if (!S_ISBLK(sb.st_mode)) { | |
192 | return 0; | |
193 | } | |
194 | ||
195 | if (dp == NULL) { | |
196 | snprintf(namebuf, PATH_MAX, "r%s", fname); | |
197 | } else { | |
198 | snprintf(namebuf, PATH_MAX, "%.*s/r%s", | |
199 | (int)(dp - fname), fname, dp + 1); | |
200 | } | |
201 | fprintf(stderr, "%s is a block device", fname); | |
202 | *filename = namebuf; | |
203 | fprintf(stderr, ", using %s\n", *filename); | |
204 | ||
205 | return 0; | |
206 | } | |
207 | #else | |
208 | static int raw_normalize_devicepath(const char **filename) | |
209 | { | |
210 | return 0; | |
211 | } | |
212 | #endif | |
213 | ||
6a8dc042 JC |
214 | static void raw_parse_flags(int bdrv_flags, int *open_flags) |
215 | { | |
216 | assert(open_flags != NULL); | |
217 | ||
218 | *open_flags |= O_BINARY; | |
219 | *open_flags &= ~O_ACCMODE; | |
220 | if (bdrv_flags & BDRV_O_RDWR) { | |
221 | *open_flags |= O_RDWR; | |
222 | } else { | |
223 | *open_flags |= O_RDONLY; | |
224 | } | |
225 | ||
226 | /* Use O_DSYNC for write-through caching, no flags for write-back caching, | |
227 | * and O_DIRECT for no caching. */ | |
228 | if ((bdrv_flags & BDRV_O_NOCACHE)) { | |
229 | *open_flags |= O_DIRECT; | |
230 | } | |
6a8dc042 JC |
231 | } |
232 | ||
fc32a72d JC |
233 | #ifdef CONFIG_LINUX_AIO |
234 | static int raw_set_aio(void **aio_ctx, int *use_aio, int bdrv_flags) | |
235 | { | |
236 | int ret = -1; | |
237 | assert(aio_ctx != NULL); | |
238 | assert(use_aio != NULL); | |
239 | /* | |
240 | * Currently Linux do AIO only for files opened with O_DIRECT | |
241 | * specified so check NOCACHE flag too | |
242 | */ | |
243 | if ((bdrv_flags & (BDRV_O_NOCACHE|BDRV_O_NATIVE_AIO)) == | |
244 | (BDRV_O_NOCACHE|BDRV_O_NATIVE_AIO)) { | |
245 | ||
246 | /* if non-NULL, laio_init() has already been run */ | |
247 | if (*aio_ctx == NULL) { | |
248 | *aio_ctx = laio_init(); | |
249 | if (!*aio_ctx) { | |
250 | goto error; | |
251 | } | |
252 | } | |
253 | *use_aio = 1; | |
254 | } else { | |
255 | *use_aio = 0; | |
256 | } | |
257 | ||
258 | ret = 0; | |
259 | ||
260 | error: | |
261 | return ret; | |
262 | } | |
263 | #endif | |
264 | ||
90babde0 | 265 | static int raw_open_common(BlockDriverState *bs, const char *filename, |
19a3da7f | 266 | int bdrv_flags, int open_flags) |
83f64091 FB |
267 | { |
268 | BDRVRawState *s = bs->opaque; | |
0e1d8f4c | 269 | int fd, ret; |
83f64091 | 270 | |
1de1ae0a CE |
271 | ret = raw_normalize_devicepath(&filename); |
272 | if (ret != 0) { | |
273 | return ret; | |
274 | } | |
275 | ||
6a8dc042 JC |
276 | s->open_flags = open_flags; |
277 | raw_parse_flags(bdrv_flags, &s->open_flags); | |
83f64091 | 278 | |
90babde0 | 279 | s->fd = -1; |
40ff6d7e | 280 | fd = qemu_open(filename, s->open_flags, 0644); |
19cb3738 FB |
281 | if (fd < 0) { |
282 | ret = -errno; | |
283 | if (ret == -EROFS) | |
284 | ret = -EACCES; | |
285 | return ret; | |
286 | } | |
83f64091 | 287 | s->fd = fd; |
9ef91a67 | 288 | |
5c6c3a6c | 289 | #ifdef CONFIG_LINUX_AIO |
fc32a72d | 290 | if (raw_set_aio(&s->aio_ctx, &s->use_aio, bdrv_flags)) { |
47e6b251 PB |
291 | qemu_close(fd); |
292 | return -errno; | |
9ef91a67 | 293 | } |
fc32a72d | 294 | #endif |
9ef91a67 | 295 | |
c85191e5 | 296 | s->has_discard = 1; |
dce512de CH |
297 | #ifdef CONFIG_XFS |
298 | if (platform_test_xfs_fd(s->fd)) { | |
299 | s->is_xfs = 1; | |
300 | } | |
301 | #endif | |
302 | ||
83f64091 FB |
303 | return 0; |
304 | } | |
305 | ||
787e4a85 KW |
306 | static int raw_open(BlockDriverState *bs, const char *filename, |
307 | QDict *options, int flags) | |
90babde0 CH |
308 | { |
309 | BDRVRawState *s = bs->opaque; | |
310 | ||
311 | s->type = FTYPE_FILE; | |
9a2d77ad | 312 | return raw_open_common(bs, filename, flags, 0); |
90babde0 CH |
313 | } |
314 | ||
eeb6b45d JC |
315 | static int raw_reopen_prepare(BDRVReopenState *state, |
316 | BlockReopenQueue *queue, Error **errp) | |
317 | { | |
318 | BDRVRawState *s; | |
319 | BDRVRawReopenState *raw_s; | |
320 | int ret = 0; | |
321 | ||
322 | assert(state != NULL); | |
323 | assert(state->bs != NULL); | |
324 | ||
325 | s = state->bs->opaque; | |
326 | ||
327 | state->opaque = g_malloc0(sizeof(BDRVRawReopenState)); | |
328 | raw_s = state->opaque; | |
329 | ||
330 | #ifdef CONFIG_LINUX_AIO | |
331 | raw_s->use_aio = s->use_aio; | |
332 | ||
333 | /* we can use s->aio_ctx instead of a copy, because the use_aio flag is | |
334 | * valid in the 'false' condition even if aio_ctx is set, and raw_set_aio() | |
335 | * won't override aio_ctx if aio_ctx is non-NULL */ | |
336 | if (raw_set_aio(&s->aio_ctx, &raw_s->use_aio, state->flags)) { | |
337 | return -1; | |
338 | } | |
339 | #endif | |
340 | ||
1bc6b705 JC |
341 | if (s->type == FTYPE_FD || s->type == FTYPE_CD) { |
342 | raw_s->open_flags |= O_NONBLOCK; | |
343 | } | |
344 | ||
eeb6b45d JC |
345 | raw_parse_flags(state->flags, &raw_s->open_flags); |
346 | ||
347 | raw_s->fd = -1; | |
348 | ||
fdf263f6 | 349 | int fcntl_flags = O_APPEND | O_NONBLOCK; |
eeb6b45d JC |
350 | #ifdef O_NOATIME |
351 | fcntl_flags |= O_NOATIME; | |
352 | #endif | |
353 | ||
fdf263f6 AF |
354 | #ifdef O_ASYNC |
355 | /* Not all operating systems have O_ASYNC, and those that don't | |
356 | * will not let us track the state into raw_s->open_flags (typically | |
357 | * you achieve the same effect with an ioctl, for example I_SETSIG | |
358 | * on Solaris). But we do not use O_ASYNC, so that's fine. | |
359 | */ | |
360 | assert((s->open_flags & O_ASYNC) == 0); | |
361 | #endif | |
362 | ||
eeb6b45d JC |
363 | if ((raw_s->open_flags & ~fcntl_flags) == (s->open_flags & ~fcntl_flags)) { |
364 | /* dup the original fd */ | |
365 | /* TODO: use qemu fcntl wrapper */ | |
366 | #ifdef F_DUPFD_CLOEXEC | |
367 | raw_s->fd = fcntl(s->fd, F_DUPFD_CLOEXEC, 0); | |
368 | #else | |
369 | raw_s->fd = dup(s->fd); | |
370 | if (raw_s->fd != -1) { | |
371 | qemu_set_cloexec(raw_s->fd); | |
372 | } | |
373 | #endif | |
374 | if (raw_s->fd >= 0) { | |
375 | ret = fcntl_setfl(raw_s->fd, raw_s->open_flags); | |
376 | if (ret) { | |
377 | qemu_close(raw_s->fd); | |
378 | raw_s->fd = -1; | |
379 | } | |
380 | } | |
381 | } | |
382 | ||
383 | /* If we cannot use fcntl, or fcntl failed, fall back to qemu_open() */ | |
384 | if (raw_s->fd == -1) { | |
385 | assert(!(raw_s->open_flags & O_CREAT)); | |
386 | raw_s->fd = qemu_open(state->bs->filename, raw_s->open_flags); | |
387 | if (raw_s->fd == -1) { | |
388 | ret = -1; | |
389 | } | |
390 | } | |
391 | return ret; | |
392 | } | |
393 | ||
394 | ||
395 | static void raw_reopen_commit(BDRVReopenState *state) | |
396 | { | |
397 | BDRVRawReopenState *raw_s = state->opaque; | |
398 | BDRVRawState *s = state->bs->opaque; | |
399 | ||
400 | s->open_flags = raw_s->open_flags; | |
401 | ||
402 | qemu_close(s->fd); | |
403 | s->fd = raw_s->fd; | |
404 | #ifdef CONFIG_LINUX_AIO | |
405 | s->use_aio = raw_s->use_aio; | |
406 | #endif | |
407 | ||
408 | g_free(state->opaque); | |
409 | state->opaque = NULL; | |
410 | } | |
411 | ||
412 | ||
413 | static void raw_reopen_abort(BDRVReopenState *state) | |
414 | { | |
415 | BDRVRawReopenState *raw_s = state->opaque; | |
416 | ||
417 | /* nothing to do if NULL, we didn't get far enough */ | |
418 | if (raw_s == NULL) { | |
419 | return; | |
420 | } | |
421 | ||
422 | if (raw_s->fd >= 0) { | |
423 | qemu_close(raw_s->fd); | |
424 | raw_s->fd = -1; | |
425 | } | |
426 | g_free(state->opaque); | |
427 | state->opaque = NULL; | |
428 | } | |
429 | ||
430 | ||
83f64091 FB |
431 | /* XXX: use host sector size if necessary with: |
432 | #ifdef DIOCGSECTORSIZE | |
433 | { | |
434 | unsigned int sectorsize = 512; | |
435 | if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) && | |
436 | sectorsize > bufsize) | |
437 | bufsize = sectorsize; | |
438 | } | |
439 | #endif | |
440 | #ifdef CONFIG_COCOA | |
2ee9fb48 | 441 | uint32_t blockSize = 512; |
83f64091 FB |
442 | if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) { |
443 | bufsize = blockSize; | |
444 | } | |
445 | #endif | |
446 | */ | |
447 | ||
de81a169 PB |
448 | static ssize_t handle_aiocb_ioctl(RawPosixAIOData *aiocb) |
449 | { | |
450 | int ret; | |
451 | ||
452 | ret = ioctl(aiocb->aio_fildes, aiocb->aio_ioctl_cmd, aiocb->aio_ioctl_buf); | |
453 | if (ret == -1) { | |
454 | return -errno; | |
455 | } | |
456 | ||
b608c8dc | 457 | return 0; |
de81a169 PB |
458 | } |
459 | ||
460 | static ssize_t handle_aiocb_flush(RawPosixAIOData *aiocb) | |
461 | { | |
462 | int ret; | |
463 | ||
464 | ret = qemu_fdatasync(aiocb->aio_fildes); | |
465 | if (ret == -1) { | |
466 | return -errno; | |
467 | } | |
468 | return 0; | |
469 | } | |
470 | ||
471 | #ifdef CONFIG_PREADV | |
472 | ||
473 | static bool preadv_present = true; | |
474 | ||
475 | static ssize_t | |
476 | qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset) | |
477 | { | |
478 | return preadv(fd, iov, nr_iov, offset); | |
479 | } | |
480 | ||
481 | static ssize_t | |
482 | qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset) | |
483 | { | |
484 | return pwritev(fd, iov, nr_iov, offset); | |
485 | } | |
486 | ||
487 | #else | |
488 | ||
489 | static bool preadv_present = false; | |
490 | ||
491 | static ssize_t | |
492 | qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset) | |
493 | { | |
494 | return -ENOSYS; | |
495 | } | |
496 | ||
497 | static ssize_t | |
498 | qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset) | |
499 | { | |
500 | return -ENOSYS; | |
501 | } | |
502 | ||
503 | #endif | |
504 | ||
505 | static ssize_t handle_aiocb_rw_vector(RawPosixAIOData *aiocb) | |
506 | { | |
507 | ssize_t len; | |
508 | ||
509 | do { | |
510 | if (aiocb->aio_type & QEMU_AIO_WRITE) | |
511 | len = qemu_pwritev(aiocb->aio_fildes, | |
512 | aiocb->aio_iov, | |
513 | aiocb->aio_niov, | |
514 | aiocb->aio_offset); | |
515 | else | |
516 | len = qemu_preadv(aiocb->aio_fildes, | |
517 | aiocb->aio_iov, | |
518 | aiocb->aio_niov, | |
519 | aiocb->aio_offset); | |
520 | } while (len == -1 && errno == EINTR); | |
521 | ||
522 | if (len == -1) { | |
523 | return -errno; | |
524 | } | |
525 | return len; | |
526 | } | |
527 | ||
528 | /* | |
529 | * Read/writes the data to/from a given linear buffer. | |
530 | * | |
531 | * Returns the number of bytes handles or -errno in case of an error. Short | |
532 | * reads are only returned if the end of the file is reached. | |
533 | */ | |
534 | static ssize_t handle_aiocb_rw_linear(RawPosixAIOData *aiocb, char *buf) | |
535 | { | |
536 | ssize_t offset = 0; | |
537 | ssize_t len; | |
538 | ||
539 | while (offset < aiocb->aio_nbytes) { | |
540 | if (aiocb->aio_type & QEMU_AIO_WRITE) { | |
541 | len = pwrite(aiocb->aio_fildes, | |
542 | (const char *)buf + offset, | |
543 | aiocb->aio_nbytes - offset, | |
544 | aiocb->aio_offset + offset); | |
545 | } else { | |
546 | len = pread(aiocb->aio_fildes, | |
547 | buf + offset, | |
548 | aiocb->aio_nbytes - offset, | |
549 | aiocb->aio_offset + offset); | |
550 | } | |
551 | if (len == -1 && errno == EINTR) { | |
552 | continue; | |
553 | } else if (len == -1) { | |
554 | offset = -errno; | |
555 | break; | |
556 | } else if (len == 0) { | |
557 | break; | |
558 | } | |
559 | offset += len; | |
560 | } | |
561 | ||
562 | return offset; | |
563 | } | |
564 | ||
565 | static ssize_t handle_aiocb_rw(RawPosixAIOData *aiocb) | |
566 | { | |
567 | ssize_t nbytes; | |
568 | char *buf; | |
569 | ||
570 | if (!(aiocb->aio_type & QEMU_AIO_MISALIGNED)) { | |
571 | /* | |
572 | * If there is just a single buffer, and it is properly aligned | |
573 | * we can just use plain pread/pwrite without any problems. | |
574 | */ | |
575 | if (aiocb->aio_niov == 1) { | |
576 | return handle_aiocb_rw_linear(aiocb, aiocb->aio_iov->iov_base); | |
577 | } | |
578 | /* | |
579 | * We have more than one iovec, and all are properly aligned. | |
580 | * | |
581 | * Try preadv/pwritev first and fall back to linearizing the | |
582 | * buffer if it's not supported. | |
583 | */ | |
584 | if (preadv_present) { | |
585 | nbytes = handle_aiocb_rw_vector(aiocb); | |
586 | if (nbytes == aiocb->aio_nbytes || | |
587 | (nbytes < 0 && nbytes != -ENOSYS)) { | |
588 | return nbytes; | |
589 | } | |
590 | preadv_present = false; | |
591 | } | |
592 | ||
593 | /* | |
594 | * XXX(hch): short read/write. no easy way to handle the reminder | |
595 | * using these interfaces. For now retry using plain | |
596 | * pread/pwrite? | |
597 | */ | |
598 | } | |
599 | ||
600 | /* | |
601 | * Ok, we have to do it the hard way, copy all segments into | |
602 | * a single aligned buffer. | |
603 | */ | |
604 | buf = qemu_blockalign(aiocb->bs, aiocb->aio_nbytes); | |
605 | if (aiocb->aio_type & QEMU_AIO_WRITE) { | |
606 | char *p = buf; | |
607 | int i; | |
608 | ||
609 | for (i = 0; i < aiocb->aio_niov; ++i) { | |
610 | memcpy(p, aiocb->aio_iov[i].iov_base, aiocb->aio_iov[i].iov_len); | |
611 | p += aiocb->aio_iov[i].iov_len; | |
612 | } | |
613 | } | |
614 | ||
615 | nbytes = handle_aiocb_rw_linear(aiocb, buf); | |
616 | if (!(aiocb->aio_type & QEMU_AIO_WRITE)) { | |
617 | char *p = buf; | |
618 | size_t count = aiocb->aio_nbytes, copy; | |
619 | int i; | |
620 | ||
621 | for (i = 0; i < aiocb->aio_niov && count; ++i) { | |
622 | copy = count; | |
623 | if (copy > aiocb->aio_iov[i].iov_len) { | |
624 | copy = aiocb->aio_iov[i].iov_len; | |
625 | } | |
626 | memcpy(aiocb->aio_iov[i].iov_base, p, copy); | |
627 | p += copy; | |
628 | count -= copy; | |
629 | } | |
630 | } | |
631 | qemu_vfree(buf); | |
632 | ||
633 | return nbytes; | |
634 | } | |
635 | ||
8238010b PB |
636 | #ifdef CONFIG_XFS |
637 | static int xfs_discard(BDRVRawState *s, int64_t offset, uint64_t bytes) | |
638 | { | |
639 | struct xfs_flock64 fl; | |
640 | ||
641 | memset(&fl, 0, sizeof(fl)); | |
642 | fl.l_whence = SEEK_SET; | |
643 | fl.l_start = offset; | |
644 | fl.l_len = bytes; | |
645 | ||
646 | if (xfsctl(NULL, s->fd, XFS_IOC_UNRESVSP64, &fl) < 0) { | |
647 | DEBUG_BLOCK_PRINT("cannot punch hole (%s)\n", strerror(errno)); | |
648 | return -errno; | |
649 | } | |
650 | ||
651 | return 0; | |
652 | } | |
653 | #endif | |
654 | ||
655 | static ssize_t handle_aiocb_discard(RawPosixAIOData *aiocb) | |
656 | { | |
657 | int ret = -EOPNOTSUPP; | |
658 | BDRVRawState *s = aiocb->bs->opaque; | |
659 | ||
660 | if (s->has_discard == 0) { | |
661 | return 0; | |
662 | } | |
663 | ||
664 | if (aiocb->aio_type & QEMU_AIO_BLKDEV) { | |
665 | #ifdef BLKDISCARD | |
666 | do { | |
667 | uint64_t range[2] = { aiocb->aio_offset, aiocb->aio_nbytes }; | |
668 | if (ioctl(aiocb->aio_fildes, BLKDISCARD, range) == 0) { | |
669 | return 0; | |
670 | } | |
671 | } while (errno == EINTR); | |
672 | ||
673 | ret = -errno; | |
674 | #endif | |
675 | } else { | |
676 | #ifdef CONFIG_XFS | |
677 | if (s->is_xfs) { | |
678 | return xfs_discard(s, aiocb->aio_offset, aiocb->aio_nbytes); | |
679 | } | |
680 | #endif | |
681 | ||
682 | #ifdef CONFIG_FALLOCATE_PUNCH_HOLE | |
683 | do { | |
684 | if (fallocate(s->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, | |
685 | aiocb->aio_offset, aiocb->aio_nbytes) == 0) { | |
686 | return 0; | |
687 | } | |
688 | } while (errno == EINTR); | |
689 | ||
690 | ret = -errno; | |
691 | #endif | |
692 | } | |
693 | ||
694 | if (ret == -ENODEV || ret == -ENOSYS || ret == -EOPNOTSUPP || | |
695 | ret == -ENOTTY) { | |
696 | s->has_discard = 0; | |
697 | ret = 0; | |
698 | } | |
699 | return ret; | |
700 | } | |
701 | ||
de81a169 PB |
702 | static int aio_worker(void *arg) |
703 | { | |
704 | RawPosixAIOData *aiocb = arg; | |
705 | ssize_t ret = 0; | |
706 | ||
707 | switch (aiocb->aio_type & QEMU_AIO_TYPE_MASK) { | |
708 | case QEMU_AIO_READ: | |
709 | ret = handle_aiocb_rw(aiocb); | |
710 | if (ret >= 0 && ret < aiocb->aio_nbytes && aiocb->bs->growable) { | |
711 | iov_memset(aiocb->aio_iov, aiocb->aio_niov, ret, | |
712 | 0, aiocb->aio_nbytes - ret); | |
713 | ||
714 | ret = aiocb->aio_nbytes; | |
715 | } | |
716 | if (ret == aiocb->aio_nbytes) { | |
717 | ret = 0; | |
718 | } else if (ret >= 0 && ret < aiocb->aio_nbytes) { | |
719 | ret = -EINVAL; | |
720 | } | |
721 | break; | |
722 | case QEMU_AIO_WRITE: | |
723 | ret = handle_aiocb_rw(aiocb); | |
724 | if (ret == aiocb->aio_nbytes) { | |
725 | ret = 0; | |
726 | } else if (ret >= 0 && ret < aiocb->aio_nbytes) { | |
727 | ret = -EINVAL; | |
728 | } | |
729 | break; | |
730 | case QEMU_AIO_FLUSH: | |
731 | ret = handle_aiocb_flush(aiocb); | |
732 | break; | |
733 | case QEMU_AIO_IOCTL: | |
734 | ret = handle_aiocb_ioctl(aiocb); | |
735 | break; | |
8238010b PB |
736 | case QEMU_AIO_DISCARD: |
737 | ret = handle_aiocb_discard(aiocb); | |
738 | break; | |
de81a169 PB |
739 | default: |
740 | fprintf(stderr, "invalid aio request (0x%x)\n", aiocb->aio_type); | |
741 | ret = -EINVAL; | |
742 | break; | |
743 | } | |
744 | ||
745 | g_slice_free(RawPosixAIOData, aiocb); | |
746 | return ret; | |
747 | } | |
748 | ||
749 | static BlockDriverAIOCB *paio_submit(BlockDriverState *bs, int fd, | |
750 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, | |
751 | BlockDriverCompletionFunc *cb, void *opaque, int type) | |
752 | { | |
753 | RawPosixAIOData *acb = g_slice_new(RawPosixAIOData); | |
c4d9d196 | 754 | ThreadPool *pool; |
de81a169 PB |
755 | |
756 | acb->bs = bs; | |
757 | acb->aio_type = type; | |
758 | acb->aio_fildes = fd; | |
759 | ||
760 | if (qiov) { | |
761 | acb->aio_iov = qiov->iov; | |
762 | acb->aio_niov = qiov->niov; | |
763 | } | |
764 | acb->aio_nbytes = nb_sectors * 512; | |
765 | acb->aio_offset = sector_num * 512; | |
766 | ||
767 | trace_paio_submit(acb, opaque, sector_num, nb_sectors, type); | |
c4d9d196 SH |
768 | pool = aio_get_thread_pool(bdrv_get_aio_context(bs)); |
769 | return thread_pool_submit_aio(pool, aio_worker, acb, cb, opaque); | |
de81a169 PB |
770 | } |
771 | ||
9ef91a67 CH |
772 | static BlockDriverAIOCB *raw_aio_submit(BlockDriverState *bs, |
773 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, | |
774 | BlockDriverCompletionFunc *cb, void *opaque, int type) | |
83f64091 | 775 | { |
ce1a14dc | 776 | BDRVRawState *s = bs->opaque; |
ce1a14dc | 777 | |
19cb3738 FB |
778 | if (fd_open(bs) < 0) |
779 | return NULL; | |
780 | ||
f141eafe AL |
781 | /* |
782 | * If O_DIRECT is used the buffer needs to be aligned on a sector | |
c1ee7d56 | 783 | * boundary. Check if this is the case or tell the low-level |
9ef91a67 | 784 | * driver that it needs to copy the buffer. |
f141eafe | 785 | */ |
9acc5a06 | 786 | if ((bs->open_flags & BDRV_O_NOCACHE)) { |
c53b1c51 | 787 | if (!bdrv_qiov_is_aligned(bs, qiov)) { |
5c6c3a6c | 788 | type |= QEMU_AIO_MISALIGNED; |
e44bd6fc | 789 | #ifdef CONFIG_LINUX_AIO |
5c6c3a6c CH |
790 | } else if (s->use_aio) { |
791 | return laio_submit(bs, s->aio_ctx, s->fd, sector_num, qiov, | |
e44bd6fc SW |
792 | nb_sectors, cb, opaque, type); |
793 | #endif | |
5c6c3a6c | 794 | } |
9ef91a67 | 795 | } |
f141eafe | 796 | |
1e5b9d2f | 797 | return paio_submit(bs, s->fd, sector_num, qiov, nb_sectors, |
9ef91a67 | 798 | cb, opaque, type); |
83f64091 FB |
799 | } |
800 | ||
f141eafe AL |
801 | static BlockDriverAIOCB *raw_aio_readv(BlockDriverState *bs, |
802 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, | |
ce1a14dc | 803 | BlockDriverCompletionFunc *cb, void *opaque) |
83f64091 | 804 | { |
9ef91a67 CH |
805 | return raw_aio_submit(bs, sector_num, qiov, nb_sectors, |
806 | cb, opaque, QEMU_AIO_READ); | |
83f64091 FB |
807 | } |
808 | ||
f141eafe AL |
809 | static BlockDriverAIOCB *raw_aio_writev(BlockDriverState *bs, |
810 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, | |
ce1a14dc | 811 | BlockDriverCompletionFunc *cb, void *opaque) |
83f64091 | 812 | { |
9ef91a67 CH |
813 | return raw_aio_submit(bs, sector_num, qiov, nb_sectors, |
814 | cb, opaque, QEMU_AIO_WRITE); | |
83f64091 | 815 | } |
53538725 | 816 | |
b2e12bc6 CH |
817 | static BlockDriverAIOCB *raw_aio_flush(BlockDriverState *bs, |
818 | BlockDriverCompletionFunc *cb, void *opaque) | |
819 | { | |
820 | BDRVRawState *s = bs->opaque; | |
821 | ||
822 | if (fd_open(bs) < 0) | |
823 | return NULL; | |
824 | ||
1e5b9d2f | 825 | return paio_submit(bs, s->fd, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH); |
b2e12bc6 CH |
826 | } |
827 | ||
83f64091 FB |
828 | static void raw_close(BlockDriverState *bs) |
829 | { | |
830 | BDRVRawState *s = bs->opaque; | |
19cb3738 | 831 | if (s->fd >= 0) { |
2e1e79da | 832 | qemu_close(s->fd); |
19cb3738 FB |
833 | s->fd = -1; |
834 | } | |
83f64091 FB |
835 | } |
836 | ||
837 | static int raw_truncate(BlockDriverState *bs, int64_t offset) | |
838 | { | |
839 | BDRVRawState *s = bs->opaque; | |
55b949c8 CH |
840 | struct stat st; |
841 | ||
842 | if (fstat(s->fd, &st)) { | |
83f64091 | 843 | return -errno; |
55b949c8 CH |
844 | } |
845 | ||
846 | if (S_ISREG(st.st_mode)) { | |
847 | if (ftruncate(s->fd, offset) < 0) { | |
848 | return -errno; | |
849 | } | |
850 | } else if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) { | |
851 | if (offset > raw_getlength(bs)) { | |
852 | return -EINVAL; | |
853 | } | |
854 | } else { | |
855 | return -ENOTSUP; | |
856 | } | |
857 | ||
83f64091 FB |
858 | return 0; |
859 | } | |
860 | ||
128ab2ff BS |
861 | #ifdef __OpenBSD__ |
862 | static int64_t raw_getlength(BlockDriverState *bs) | |
863 | { | |
864 | BDRVRawState *s = bs->opaque; | |
865 | int fd = s->fd; | |
866 | struct stat st; | |
867 | ||
868 | if (fstat(fd, &st)) | |
869 | return -1; | |
870 | if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) { | |
871 | struct disklabel dl; | |
872 | ||
873 | if (ioctl(fd, DIOCGDINFO, &dl)) | |
874 | return -1; | |
875 | return (uint64_t)dl.d_secsize * | |
876 | dl.d_partitions[DISKPART(st.st_rdev)].p_size; | |
877 | } else | |
878 | return st.st_size; | |
879 | } | |
d1f6fd8d CE |
880 | #elif defined(__NetBSD__) |
881 | static int64_t raw_getlength(BlockDriverState *bs) | |
882 | { | |
883 | BDRVRawState *s = bs->opaque; | |
884 | int fd = s->fd; | |
885 | struct stat st; | |
886 | ||
887 | if (fstat(fd, &st)) | |
888 | return -1; | |
889 | if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) { | |
890 | struct dkwedge_info dkw; | |
891 | ||
892 | if (ioctl(fd, DIOCGWEDGEINFO, &dkw) != -1) { | |
893 | return dkw.dkw_size * 512; | |
894 | } else { | |
895 | struct disklabel dl; | |
896 | ||
897 | if (ioctl(fd, DIOCGDINFO, &dl)) | |
898 | return -1; | |
899 | return (uint64_t)dl.d_secsize * | |
900 | dl.d_partitions[DISKPART(st.st_rdev)].p_size; | |
901 | } | |
902 | } else | |
903 | return st.st_size; | |
904 | } | |
50779cc2 CH |
905 | #elif defined(__sun__) |
906 | static int64_t raw_getlength(BlockDriverState *bs) | |
907 | { | |
908 | BDRVRawState *s = bs->opaque; | |
909 | struct dk_minfo minfo; | |
910 | int ret; | |
911 | ||
912 | ret = fd_open(bs); | |
913 | if (ret < 0) { | |
914 | return ret; | |
915 | } | |
916 | ||
917 | /* | |
918 | * Use the DKIOCGMEDIAINFO ioctl to read the size. | |
919 | */ | |
920 | ret = ioctl(s->fd, DKIOCGMEDIAINFO, &minfo); | |
921 | if (ret != -1) { | |
922 | return minfo.dki_lbsize * minfo.dki_capacity; | |
923 | } | |
924 | ||
925 | /* | |
926 | * There are reports that lseek on some devices fails, but | |
927 | * irc discussion said that contingency on contingency was overkill. | |
928 | */ | |
929 | return lseek(s->fd, 0, SEEK_END); | |
930 | } | |
931 | #elif defined(CONFIG_BSD) | |
932 | static int64_t raw_getlength(BlockDriverState *bs) | |
83f64091 FB |
933 | { |
934 | BDRVRawState *s = bs->opaque; | |
935 | int fd = s->fd; | |
936 | int64_t size; | |
83f64091 | 937 | struct stat sb; |
a167ba50 | 938 | #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__) |
9f23011a | 939 | int reopened = 0; |
83f64091 | 940 | #endif |
19cb3738 FB |
941 | int ret; |
942 | ||
943 | ret = fd_open(bs); | |
944 | if (ret < 0) | |
945 | return ret; | |
83f64091 | 946 | |
a167ba50 | 947 | #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__) |
9f23011a BS |
948 | again: |
949 | #endif | |
83f64091 FB |
950 | if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) { |
951 | #ifdef DIOCGMEDIASIZE | |
952 | if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size)) | |
c5e97233 BS |
953 | #elif defined(DIOCGPART) |
954 | { | |
955 | struct partinfo pi; | |
956 | if (ioctl(fd, DIOCGPART, &pi) == 0) | |
957 | size = pi.media_size; | |
958 | else | |
959 | size = 0; | |
960 | } | |
961 | if (size == 0) | |
83f64091 | 962 | #endif |
83affaa6 | 963 | #if defined(__APPLE__) && defined(__MACH__) |
83f64091 FB |
964 | size = LONG_LONG_MAX; |
965 | #else | |
966 | size = lseek(fd, 0LL, SEEK_END); | |
9f23011a | 967 | #endif |
a167ba50 | 968 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
9f23011a BS |
969 | switch(s->type) { |
970 | case FTYPE_CD: | |
971 | /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */ | |
972 | if (size == 2048LL * (unsigned)-1) | |
973 | size = 0; | |
974 | /* XXX no disc? maybe we need to reopen... */ | |
f3a5d3f8 | 975 | if (size <= 0 && !reopened && cdrom_reopen(bs) >= 0) { |
9f23011a BS |
976 | reopened = 1; |
977 | goto again; | |
978 | } | |
979 | } | |
83f64091 | 980 | #endif |
50779cc2 | 981 | } else { |
83f64091 FB |
982 | size = lseek(fd, 0, SEEK_END); |
983 | } | |
83f64091 FB |
984 | return size; |
985 | } | |
50779cc2 CH |
986 | #else |
987 | static int64_t raw_getlength(BlockDriverState *bs) | |
988 | { | |
989 | BDRVRawState *s = bs->opaque; | |
990 | int ret; | |
991 | ||
992 | ret = fd_open(bs); | |
993 | if (ret < 0) { | |
994 | return ret; | |
995 | } | |
996 | ||
997 | return lseek(s->fd, 0, SEEK_END); | |
998 | } | |
128ab2ff | 999 | #endif |
83f64091 | 1000 | |
4a1d5e1f FZ |
1001 | static int64_t raw_get_allocated_file_size(BlockDriverState *bs) |
1002 | { | |
1003 | struct stat st; | |
1004 | BDRVRawState *s = bs->opaque; | |
1005 | ||
1006 | if (fstat(s->fd, &st) < 0) { | |
1007 | return -errno; | |
1008 | } | |
1009 | return (int64_t)st.st_blocks * 512; | |
1010 | } | |
1011 | ||
0e7e1989 | 1012 | static int raw_create(const char *filename, QEMUOptionParameter *options) |
83f64091 FB |
1013 | { |
1014 | int fd; | |
1e37d059 | 1015 | int result = 0; |
0e7e1989 | 1016 | int64_t total_size = 0; |
83f64091 | 1017 | |
0e7e1989 KW |
1018 | /* Read out options */ |
1019 | while (options && options->name) { | |
1020 | if (!strcmp(options->name, BLOCK_OPT_SIZE)) { | |
9040385d | 1021 | total_size = options->value.n / BDRV_SECTOR_SIZE; |
0e7e1989 KW |
1022 | } |
1023 | options++; | |
1024 | } | |
83f64091 | 1025 | |
6165f4d8 CB |
1026 | fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, |
1027 | 0644); | |
1e37d059 SW |
1028 | if (fd < 0) { |
1029 | result = -errno; | |
1030 | } else { | |
9040385d | 1031 | if (ftruncate(fd, total_size * BDRV_SECTOR_SIZE) != 0) { |
1e37d059 SW |
1032 | result = -errno; |
1033 | } | |
2e1e79da | 1034 | if (qemu_close(fd) != 0) { |
1e37d059 SW |
1035 | result = -errno; |
1036 | } | |
1037 | } | |
1038 | return result; | |
83f64091 FB |
1039 | } |
1040 | ||
5500316d PB |
1041 | /* |
1042 | * Returns true iff the specified sector is present in the disk image. Drivers | |
1043 | * not implementing the functionality are assumed to not support backing files, | |
1044 | * hence all their sectors are reported as allocated. | |
1045 | * | |
1046 | * If 'sector_num' is beyond the end of the disk image the return value is 0 | |
1047 | * and 'pnum' is set to 0. | |
1048 | * | |
1049 | * 'pnum' is set to the number of sectors (including and immediately following | |
1050 | * the specified sector) that are known to be in the same | |
1051 | * allocated/unallocated state. | |
1052 | * | |
1053 | * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes | |
1054 | * beyond the end of the disk image it will be clamped. | |
1055 | */ | |
1056 | static int coroutine_fn raw_co_is_allocated(BlockDriverState *bs, | |
1057 | int64_t sector_num, | |
1058 | int nb_sectors, int *pnum) | |
1059 | { | |
5500316d PB |
1060 | off_t start, data, hole; |
1061 | int ret; | |
1062 | ||
1063 | ret = fd_open(bs); | |
1064 | if (ret < 0) { | |
1065 | return ret; | |
1066 | } | |
1067 | ||
1068 | start = sector_num * BDRV_SECTOR_SIZE; | |
94282e71 | 1069 | |
5500316d | 1070 | #ifdef CONFIG_FIEMAP |
94282e71 KW |
1071 | |
1072 | BDRVRawState *s = bs->opaque; | |
5500316d PB |
1073 | struct { |
1074 | struct fiemap fm; | |
1075 | struct fiemap_extent fe; | |
1076 | } f; | |
94282e71 | 1077 | |
5500316d PB |
1078 | f.fm.fm_start = start; |
1079 | f.fm.fm_length = (int64_t)nb_sectors * BDRV_SECTOR_SIZE; | |
1080 | f.fm.fm_flags = 0; | |
1081 | f.fm.fm_extent_count = 1; | |
1082 | f.fm.fm_reserved = 0; | |
1083 | if (ioctl(s->fd, FS_IOC_FIEMAP, &f) == -1) { | |
1084 | /* Assume everything is allocated. */ | |
1085 | *pnum = nb_sectors; | |
1086 | return 1; | |
1087 | } | |
1088 | ||
1089 | if (f.fm.fm_mapped_extents == 0) { | |
1090 | /* No extents found, data is beyond f.fm.fm_start + f.fm.fm_length. | |
1091 | * f.fm.fm_start + f.fm.fm_length must be clamped to the file size! | |
1092 | */ | |
1093 | off_t length = lseek(s->fd, 0, SEEK_END); | |
1094 | hole = f.fm.fm_start; | |
1095 | data = MIN(f.fm.fm_start + f.fm.fm_length, length); | |
1096 | } else { | |
1097 | data = f.fe.fe_logical; | |
1098 | hole = f.fe.fe_logical + f.fe.fe_length; | |
1099 | } | |
94282e71 | 1100 | |
5500316d | 1101 | #elif defined SEEK_HOLE && defined SEEK_DATA |
94282e71 KW |
1102 | |
1103 | BDRVRawState *s = bs->opaque; | |
1104 | ||
5500316d PB |
1105 | hole = lseek(s->fd, start, SEEK_HOLE); |
1106 | if (hole == -1) { | |
1107 | /* -ENXIO indicates that sector_num was past the end of the file. | |
1108 | * There is a virtual hole there. */ | |
1109 | assert(errno != -ENXIO); | |
1110 | ||
1111 | /* Most likely EINVAL. Assume everything is allocated. */ | |
1112 | *pnum = nb_sectors; | |
1113 | return 1; | |
1114 | } | |
1115 | ||
1116 | if (hole > start) { | |
1117 | data = start; | |
1118 | } else { | |
1119 | /* On a hole. We need another syscall to find its end. */ | |
1120 | data = lseek(s->fd, start, SEEK_DATA); | |
1121 | if (data == -1) { | |
1122 | data = lseek(s->fd, 0, SEEK_END); | |
1123 | } | |
1124 | } | |
1125 | #else | |
1126 | *pnum = nb_sectors; | |
1127 | return 1; | |
1128 | #endif | |
1129 | ||
1130 | if (data <= start) { | |
1131 | /* On a data extent, compute sectors to the end of the extent. */ | |
1132 | *pnum = MIN(nb_sectors, (hole - start) / BDRV_SECTOR_SIZE); | |
1133 | return 1; | |
1134 | } else { | |
1135 | /* On a hole, compute sectors to the beginning of the next extent. */ | |
1136 | *pnum = MIN(nb_sectors, (data - start) / BDRV_SECTOR_SIZE); | |
1137 | return 0; | |
1138 | } | |
1139 | } | |
1140 | ||
8238010b PB |
1141 | static coroutine_fn BlockDriverAIOCB *raw_aio_discard(BlockDriverState *bs, |
1142 | int64_t sector_num, int nb_sectors, | |
1143 | BlockDriverCompletionFunc *cb, void *opaque) | |
dce512de | 1144 | { |
dce512de CH |
1145 | BDRVRawState *s = bs->opaque; |
1146 | ||
8238010b PB |
1147 | return paio_submit(bs, s->fd, sector_num, NULL, nb_sectors, |
1148 | cb, opaque, QEMU_AIO_DISCARD); | |
dce512de | 1149 | } |
0e7e1989 KW |
1150 | |
1151 | static QEMUOptionParameter raw_create_options[] = { | |
db08adf5 KW |
1152 | { |
1153 | .name = BLOCK_OPT_SIZE, | |
1154 | .type = OPT_SIZE, | |
1155 | .help = "Virtual disk size" | |
1156 | }, | |
0e7e1989 KW |
1157 | { NULL } |
1158 | }; | |
1159 | ||
84a12e66 CH |
1160 | static BlockDriver bdrv_file = { |
1161 | .format_name = "file", | |
1162 | .protocol_name = "file", | |
856ae5c3 BS |
1163 | .instance_size = sizeof(BDRVRawState), |
1164 | .bdrv_probe = NULL, /* no probe for protocols */ | |
66f82cee | 1165 | .bdrv_file_open = raw_open, |
eeb6b45d JC |
1166 | .bdrv_reopen_prepare = raw_reopen_prepare, |
1167 | .bdrv_reopen_commit = raw_reopen_commit, | |
1168 | .bdrv_reopen_abort = raw_reopen_abort, | |
856ae5c3 BS |
1169 | .bdrv_close = raw_close, |
1170 | .bdrv_create = raw_create, | |
5500316d | 1171 | .bdrv_co_is_allocated = raw_co_is_allocated, |
3b46e624 | 1172 | |
f141eafe AL |
1173 | .bdrv_aio_readv = raw_aio_readv, |
1174 | .bdrv_aio_writev = raw_aio_writev, | |
b2e12bc6 | 1175 | .bdrv_aio_flush = raw_aio_flush, |
8238010b | 1176 | .bdrv_aio_discard = raw_aio_discard, |
3c529d93 | 1177 | |
83f64091 FB |
1178 | .bdrv_truncate = raw_truncate, |
1179 | .bdrv_getlength = raw_getlength, | |
4a1d5e1f FZ |
1180 | .bdrv_get_allocated_file_size |
1181 | = raw_get_allocated_file_size, | |
0e7e1989 KW |
1182 | |
1183 | .create_options = raw_create_options, | |
83f64091 FB |
1184 | }; |
1185 | ||
19cb3738 FB |
1186 | /***********************************************/ |
1187 | /* host device */ | |
1188 | ||
83affaa6 | 1189 | #if defined(__APPLE__) && defined(__MACH__) |
19cb3738 FB |
1190 | static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator ); |
1191 | static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize ); | |
1192 | ||
1193 | kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator ) | |
1194 | { | |
5fafdf24 | 1195 | kern_return_t kernResult; |
19cb3738 FB |
1196 | mach_port_t masterPort; |
1197 | CFMutableDictionaryRef classesToMatch; | |
1198 | ||
1199 | kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort ); | |
1200 | if ( KERN_SUCCESS != kernResult ) { | |
1201 | printf( "IOMasterPort returned %d\n", kernResult ); | |
1202 | } | |
3b46e624 | 1203 | |
5fafdf24 | 1204 | classesToMatch = IOServiceMatching( kIOCDMediaClass ); |
19cb3738 FB |
1205 | if ( classesToMatch == NULL ) { |
1206 | printf( "IOServiceMatching returned a NULL dictionary.\n" ); | |
1207 | } else { | |
1208 | CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue ); | |
1209 | } | |
1210 | kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator ); | |
1211 | if ( KERN_SUCCESS != kernResult ) | |
1212 | { | |
1213 | printf( "IOServiceGetMatchingServices returned %d\n", kernResult ); | |
1214 | } | |
3b46e624 | 1215 | |
19cb3738 FB |
1216 | return kernResult; |
1217 | } | |
1218 | ||
1219 | kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize ) | |
1220 | { | |
1221 | io_object_t nextMedia; | |
1222 | kern_return_t kernResult = KERN_FAILURE; | |
1223 | *bsdPath = '\0'; | |
1224 | nextMedia = IOIteratorNext( mediaIterator ); | |
1225 | if ( nextMedia ) | |
1226 | { | |
1227 | CFTypeRef bsdPathAsCFString; | |
1228 | bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 ); | |
1229 | if ( bsdPathAsCFString ) { | |
1230 | size_t devPathLength; | |
1231 | strcpy( bsdPath, _PATH_DEV ); | |
1232 | strcat( bsdPath, "r" ); | |
1233 | devPathLength = strlen( bsdPath ); | |
1234 | if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) { | |
1235 | kernResult = KERN_SUCCESS; | |
1236 | } | |
1237 | CFRelease( bsdPathAsCFString ); | |
1238 | } | |
1239 | IOObjectRelease( nextMedia ); | |
1240 | } | |
3b46e624 | 1241 | |
19cb3738 FB |
1242 | return kernResult; |
1243 | } | |
1244 | ||
1245 | #endif | |
1246 | ||
508c7cb3 CH |
1247 | static int hdev_probe_device(const char *filename) |
1248 | { | |
1249 | struct stat st; | |
1250 | ||
1251 | /* allow a dedicated CD-ROM driver to match with a higher priority */ | |
1252 | if (strstart(filename, "/dev/cdrom", NULL)) | |
1253 | return 50; | |
1254 | ||
1255 | if (stat(filename, &st) >= 0 && | |
1256 | (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) { | |
1257 | return 100; | |
1258 | } | |
1259 | ||
1260 | return 0; | |
1261 | } | |
1262 | ||
da888d37 SH |
1263 | static int check_hdev_writable(BDRVRawState *s) |
1264 | { | |
1265 | #if defined(BLKROGET) | |
1266 | /* Linux block devices can be configured "read-only" using blockdev(8). | |
1267 | * This is independent of device node permissions and therefore open(2) | |
1268 | * with O_RDWR succeeds. Actual writes fail with EPERM. | |
1269 | * | |
1270 | * bdrv_open() is supposed to fail if the disk is read-only. Explicitly | |
1271 | * check for read-only block devices so that Linux block devices behave | |
1272 | * properly. | |
1273 | */ | |
1274 | struct stat st; | |
1275 | int readonly = 0; | |
1276 | ||
1277 | if (fstat(s->fd, &st)) { | |
1278 | return -errno; | |
1279 | } | |
1280 | ||
1281 | if (!S_ISBLK(st.st_mode)) { | |
1282 | return 0; | |
1283 | } | |
1284 | ||
1285 | if (ioctl(s->fd, BLKROGET, &readonly) < 0) { | |
1286 | return -errno; | |
1287 | } | |
1288 | ||
1289 | if (readonly) { | |
1290 | return -EACCES; | |
1291 | } | |
1292 | #endif /* defined(BLKROGET) */ | |
1293 | return 0; | |
1294 | } | |
1295 | ||
787e4a85 KW |
1296 | static int hdev_open(BlockDriverState *bs, const char *filename, |
1297 | QDict *options, int flags) | |
19cb3738 FB |
1298 | { |
1299 | BDRVRawState *s = bs->opaque; | |
da888d37 | 1300 | int ret; |
a76bab49 | 1301 | |
83affaa6 | 1302 | #if defined(__APPLE__) && defined(__MACH__) |
19cb3738 FB |
1303 | if (strstart(filename, "/dev/cdrom", NULL)) { |
1304 | kern_return_t kernResult; | |
1305 | io_iterator_t mediaIterator; | |
1306 | char bsdPath[ MAXPATHLEN ]; | |
1307 | int fd; | |
5fafdf24 | 1308 | |
19cb3738 FB |
1309 | kernResult = FindEjectableCDMedia( &mediaIterator ); |
1310 | kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) ); | |
3b46e624 | 1311 | |
19cb3738 FB |
1312 | if ( bsdPath[ 0 ] != '\0' ) { |
1313 | strcat(bsdPath,"s0"); | |
1314 | /* some CDs don't have a partition 0 */ | |
6165f4d8 | 1315 | fd = qemu_open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE); |
19cb3738 FB |
1316 | if (fd < 0) { |
1317 | bsdPath[strlen(bsdPath)-1] = '1'; | |
1318 | } else { | |
2e1e79da | 1319 | qemu_close(fd); |
19cb3738 FB |
1320 | } |
1321 | filename = bsdPath; | |
1322 | } | |
3b46e624 | 1323 | |
19cb3738 FB |
1324 | if ( mediaIterator ) |
1325 | IOObjectRelease( mediaIterator ); | |
1326 | } | |
1327 | #endif | |
19cb3738 FB |
1328 | |
1329 | s->type = FTYPE_FILE; | |
4dd75c70 | 1330 | #if defined(__linux__) |
05acda4d BK |
1331 | { |
1332 | char resolved_path[ MAXPATHLEN ], *temp; | |
1333 | ||
1334 | temp = realpath(filename, resolved_path); | |
1335 | if (temp && strstart(temp, "/dev/sg", NULL)) { | |
1336 | bs->sg = 1; | |
1337 | } | |
19cb3738 FB |
1338 | } |
1339 | #endif | |
90babde0 | 1340 | |
da888d37 SH |
1341 | ret = raw_open_common(bs, filename, flags, 0); |
1342 | if (ret < 0) { | |
1343 | return ret; | |
1344 | } | |
1345 | ||
1346 | if (flags & BDRV_O_RDWR) { | |
1347 | ret = check_hdev_writable(s); | |
1348 | if (ret < 0) { | |
1349 | raw_close(bs); | |
1350 | return ret; | |
1351 | } | |
1352 | } | |
1353 | ||
1354 | return ret; | |
19cb3738 FB |
1355 | } |
1356 | ||
03ff3ca3 | 1357 | #if defined(__linux__) |
19cb3738 FB |
1358 | /* Note: we do not have a reliable method to detect if the floppy is |
1359 | present. The current method is to try to open the floppy at every | |
1360 | I/O and to keep it opened during a few hundreds of ms. */ | |
1361 | static int fd_open(BlockDriverState *bs) | |
1362 | { | |
1363 | BDRVRawState *s = bs->opaque; | |
1364 | int last_media_present; | |
1365 | ||
1366 | if (s->type != FTYPE_FD) | |
1367 | return 0; | |
1368 | last_media_present = (s->fd >= 0); | |
5fafdf24 | 1369 | if (s->fd >= 0 && |
c57c846a | 1370 | (get_clock() - s->fd_open_time) >= FD_OPEN_TIMEOUT) { |
2e1e79da | 1371 | qemu_close(s->fd); |
19cb3738 FB |
1372 | s->fd = -1; |
1373 | #ifdef DEBUG_FLOPPY | |
1374 | printf("Floppy closed\n"); | |
1375 | #endif | |
1376 | } | |
1377 | if (s->fd < 0) { | |
5fafdf24 | 1378 | if (s->fd_got_error && |
c57c846a | 1379 | (get_clock() - s->fd_error_time) < FD_OPEN_TIMEOUT) { |
19cb3738 FB |
1380 | #ifdef DEBUG_FLOPPY |
1381 | printf("No floppy (open delayed)\n"); | |
1382 | #endif | |
1383 | return -EIO; | |
1384 | } | |
6165f4d8 | 1385 | s->fd = qemu_open(bs->filename, s->open_flags & ~O_NONBLOCK); |
19cb3738 | 1386 | if (s->fd < 0) { |
c57c846a | 1387 | s->fd_error_time = get_clock(); |
19cb3738 FB |
1388 | s->fd_got_error = 1; |
1389 | if (last_media_present) | |
1390 | s->fd_media_changed = 1; | |
1391 | #ifdef DEBUG_FLOPPY | |
1392 | printf("No floppy\n"); | |
1393 | #endif | |
1394 | return -EIO; | |
1395 | } | |
1396 | #ifdef DEBUG_FLOPPY | |
1397 | printf("Floppy opened\n"); | |
1398 | #endif | |
1399 | } | |
1400 | if (!last_media_present) | |
1401 | s->fd_media_changed = 1; | |
c57c846a | 1402 | s->fd_open_time = get_clock(); |
19cb3738 FB |
1403 | s->fd_got_error = 0; |
1404 | return 0; | |
1405 | } | |
19cb3738 | 1406 | |
63ec93db | 1407 | static int hdev_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) |
985a03b0 TS |
1408 | { |
1409 | BDRVRawState *s = bs->opaque; | |
1410 | ||
1411 | return ioctl(s->fd, req, buf); | |
1412 | } | |
221f715d | 1413 | |
63ec93db | 1414 | static BlockDriverAIOCB *hdev_aio_ioctl(BlockDriverState *bs, |
221f715d AL |
1415 | unsigned long int req, void *buf, |
1416 | BlockDriverCompletionFunc *cb, void *opaque) | |
1417 | { | |
f141eafe | 1418 | BDRVRawState *s = bs->opaque; |
c208e8c2 | 1419 | RawPosixAIOData *acb; |
c4d9d196 | 1420 | ThreadPool *pool; |
221f715d | 1421 | |
f141eafe AL |
1422 | if (fd_open(bs) < 0) |
1423 | return NULL; | |
c208e8c2 PB |
1424 | |
1425 | acb = g_slice_new(RawPosixAIOData); | |
1426 | acb->bs = bs; | |
1427 | acb->aio_type = QEMU_AIO_IOCTL; | |
1428 | acb->aio_fildes = s->fd; | |
1429 | acb->aio_offset = 0; | |
1430 | acb->aio_ioctl_buf = buf; | |
1431 | acb->aio_ioctl_cmd = req; | |
c4d9d196 SH |
1432 | pool = aio_get_thread_pool(bdrv_get_aio_context(bs)); |
1433 | return thread_pool_submit_aio(pool, aio_worker, acb, cb, opaque); | |
221f715d AL |
1434 | } |
1435 | ||
a167ba50 | 1436 | #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
9f23011a BS |
1437 | static int fd_open(BlockDriverState *bs) |
1438 | { | |
1439 | BDRVRawState *s = bs->opaque; | |
1440 | ||
1441 | /* this is just to ensure s->fd is sane (its called by io ops) */ | |
1442 | if (s->fd >= 0) | |
1443 | return 0; | |
1444 | return -EIO; | |
1445 | } | |
9f23011a | 1446 | #else /* !linux && !FreeBSD */ |
19cb3738 | 1447 | |
08af02e2 AL |
1448 | static int fd_open(BlockDriverState *bs) |
1449 | { | |
1450 | return 0; | |
1451 | } | |
1452 | ||
221f715d | 1453 | #endif /* !linux && !FreeBSD */ |
04eeb8b6 | 1454 | |
c36dd8a0 AF |
1455 | static coroutine_fn BlockDriverAIOCB *hdev_aio_discard(BlockDriverState *bs, |
1456 | int64_t sector_num, int nb_sectors, | |
1457 | BlockDriverCompletionFunc *cb, void *opaque) | |
1458 | { | |
1459 | BDRVRawState *s = bs->opaque; | |
1460 | ||
1461 | if (fd_open(bs) < 0) { | |
1462 | return NULL; | |
1463 | } | |
1464 | return paio_submit(bs, s->fd, sector_num, NULL, nb_sectors, | |
1465 | cb, opaque, QEMU_AIO_DISCARD|QEMU_AIO_BLKDEV); | |
1466 | } | |
1467 | ||
0e7e1989 | 1468 | static int hdev_create(const char *filename, QEMUOptionParameter *options) |
93c65b47 AL |
1469 | { |
1470 | int fd; | |
1471 | int ret = 0; | |
1472 | struct stat stat_buf; | |
0e7e1989 | 1473 | int64_t total_size = 0; |
93c65b47 | 1474 | |
0e7e1989 KW |
1475 | /* Read out options */ |
1476 | while (options && options->name) { | |
1477 | if (!strcmp(options->name, "size")) { | |
9040385d | 1478 | total_size = options->value.n / BDRV_SECTOR_SIZE; |
0e7e1989 KW |
1479 | } |
1480 | options++; | |
1481 | } | |
93c65b47 | 1482 | |
6165f4d8 | 1483 | fd = qemu_open(filename, O_WRONLY | O_BINARY); |
93c65b47 | 1484 | if (fd < 0) |
57e69b7d | 1485 | return -errno; |
93c65b47 AL |
1486 | |
1487 | if (fstat(fd, &stat_buf) < 0) | |
57e69b7d | 1488 | ret = -errno; |
4099df58 | 1489 | else if (!S_ISBLK(stat_buf.st_mode) && !S_ISCHR(stat_buf.st_mode)) |
57e69b7d | 1490 | ret = -ENODEV; |
9040385d | 1491 | else if (lseek(fd, 0, SEEK_END) < total_size * BDRV_SECTOR_SIZE) |
93c65b47 AL |
1492 | ret = -ENOSPC; |
1493 | ||
2e1e79da | 1494 | qemu_close(fd); |
93c65b47 AL |
1495 | return ret; |
1496 | } | |
1497 | ||
336c1c12 KW |
1498 | static int hdev_has_zero_init(BlockDriverState *bs) |
1499 | { | |
1500 | return 0; | |
1501 | } | |
1502 | ||
5efa9d5a | 1503 | static BlockDriver bdrv_host_device = { |
0b4ce02e | 1504 | .format_name = "host_device", |
84a12e66 | 1505 | .protocol_name = "host_device", |
0b4ce02e KW |
1506 | .instance_size = sizeof(BDRVRawState), |
1507 | .bdrv_probe_device = hdev_probe_device, | |
66f82cee | 1508 | .bdrv_file_open = hdev_open, |
0b4ce02e | 1509 | .bdrv_close = raw_close, |
1bc6b705 JC |
1510 | .bdrv_reopen_prepare = raw_reopen_prepare, |
1511 | .bdrv_reopen_commit = raw_reopen_commit, | |
1512 | .bdrv_reopen_abort = raw_reopen_abort, | |
93c65b47 | 1513 | .bdrv_create = hdev_create, |
0b4ce02e | 1514 | .create_options = raw_create_options, |
336c1c12 | 1515 | .bdrv_has_zero_init = hdev_has_zero_init, |
3b46e624 | 1516 | |
f141eafe AL |
1517 | .bdrv_aio_readv = raw_aio_readv, |
1518 | .bdrv_aio_writev = raw_aio_writev, | |
b2e12bc6 | 1519 | .bdrv_aio_flush = raw_aio_flush, |
8238010b | 1520 | .bdrv_aio_discard = hdev_aio_discard, |
3c529d93 | 1521 | |
55b949c8 | 1522 | .bdrv_truncate = raw_truncate, |
e60f469c | 1523 | .bdrv_getlength = raw_getlength, |
4a1d5e1f FZ |
1524 | .bdrv_get_allocated_file_size |
1525 | = raw_get_allocated_file_size, | |
19cb3738 | 1526 | |
f3a5d3f8 | 1527 | /* generic scsi device */ |
63ec93db CH |
1528 | #ifdef __linux__ |
1529 | .bdrv_ioctl = hdev_ioctl, | |
63ec93db CH |
1530 | .bdrv_aio_ioctl = hdev_aio_ioctl, |
1531 | #endif | |
f3a5d3f8 CH |
1532 | }; |
1533 | ||
1534 | #ifdef __linux__ | |
787e4a85 KW |
1535 | static int floppy_open(BlockDriverState *bs, const char *filename, |
1536 | QDict *options, int flags) | |
f3a5d3f8 CH |
1537 | { |
1538 | BDRVRawState *s = bs->opaque; | |
1539 | int ret; | |
1540 | ||
f3a5d3f8 | 1541 | s->type = FTYPE_FD; |
f3a5d3f8 | 1542 | |
19a3da7f BS |
1543 | /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */ |
1544 | ret = raw_open_common(bs, filename, flags, O_NONBLOCK); | |
f3a5d3f8 CH |
1545 | if (ret) |
1546 | return ret; | |
1547 | ||
1548 | /* close fd so that we can reopen it as needed */ | |
2e1e79da | 1549 | qemu_close(s->fd); |
f3a5d3f8 CH |
1550 | s->fd = -1; |
1551 | s->fd_media_changed = 1; | |
1552 | ||
1553 | return 0; | |
1554 | } | |
1555 | ||
508c7cb3 CH |
1556 | static int floppy_probe_device(const char *filename) |
1557 | { | |
2ebf7c4b CR |
1558 | int fd, ret; |
1559 | int prio = 0; | |
1560 | struct floppy_struct fdparam; | |
343f8568 | 1561 | struct stat st; |
2ebf7c4b | 1562 | |
e1740828 CB |
1563 | if (strstart(filename, "/dev/fd", NULL) && |
1564 | !strstart(filename, "/dev/fdset/", NULL)) { | |
2ebf7c4b | 1565 | prio = 50; |
e1740828 | 1566 | } |
2ebf7c4b | 1567 | |
6165f4d8 | 1568 | fd = qemu_open(filename, O_RDONLY | O_NONBLOCK); |
2ebf7c4b CR |
1569 | if (fd < 0) { |
1570 | goto out; | |
1571 | } | |
343f8568 JS |
1572 | ret = fstat(fd, &st); |
1573 | if (ret == -1 || !S_ISBLK(st.st_mode)) { | |
1574 | goto outc; | |
1575 | } | |
2ebf7c4b CR |
1576 | |
1577 | /* Attempt to detect via a floppy specific ioctl */ | |
1578 | ret = ioctl(fd, FDGETPRM, &fdparam); | |
1579 | if (ret >= 0) | |
1580 | prio = 100; | |
1581 | ||
343f8568 | 1582 | outc: |
2e1e79da | 1583 | qemu_close(fd); |
2ebf7c4b CR |
1584 | out: |
1585 | return prio; | |
508c7cb3 CH |
1586 | } |
1587 | ||
1588 | ||
f3a5d3f8 CH |
1589 | static int floppy_is_inserted(BlockDriverState *bs) |
1590 | { | |
1591 | return fd_open(bs) >= 0; | |
1592 | } | |
1593 | ||
1594 | static int floppy_media_changed(BlockDriverState *bs) | |
1595 | { | |
1596 | BDRVRawState *s = bs->opaque; | |
1597 | int ret; | |
1598 | ||
1599 | /* | |
1600 | * XXX: we do not have a true media changed indication. | |
1601 | * It does not work if the floppy is changed without trying to read it. | |
1602 | */ | |
1603 | fd_open(bs); | |
1604 | ret = s->fd_media_changed; | |
1605 | s->fd_media_changed = 0; | |
1606 | #ifdef DEBUG_FLOPPY | |
1607 | printf("Floppy changed=%d\n", ret); | |
1608 | #endif | |
1609 | return ret; | |
1610 | } | |
1611 | ||
f36f3949 | 1612 | static void floppy_eject(BlockDriverState *bs, bool eject_flag) |
f3a5d3f8 CH |
1613 | { |
1614 | BDRVRawState *s = bs->opaque; | |
1615 | int fd; | |
1616 | ||
1617 | if (s->fd >= 0) { | |
2e1e79da | 1618 | qemu_close(s->fd); |
f3a5d3f8 CH |
1619 | s->fd = -1; |
1620 | } | |
6165f4d8 | 1621 | fd = qemu_open(bs->filename, s->open_flags | O_NONBLOCK); |
f3a5d3f8 CH |
1622 | if (fd >= 0) { |
1623 | if (ioctl(fd, FDEJECT, 0) < 0) | |
1624 | perror("FDEJECT"); | |
2e1e79da | 1625 | qemu_close(fd); |
f3a5d3f8 | 1626 | } |
f3a5d3f8 CH |
1627 | } |
1628 | ||
1629 | static BlockDriver bdrv_host_floppy = { | |
1630 | .format_name = "host_floppy", | |
84a12e66 | 1631 | .protocol_name = "host_floppy", |
f3a5d3f8 | 1632 | .instance_size = sizeof(BDRVRawState), |
508c7cb3 | 1633 | .bdrv_probe_device = floppy_probe_device, |
66f82cee | 1634 | .bdrv_file_open = floppy_open, |
f3a5d3f8 | 1635 | .bdrv_close = raw_close, |
1bc6b705 JC |
1636 | .bdrv_reopen_prepare = raw_reopen_prepare, |
1637 | .bdrv_reopen_commit = raw_reopen_commit, | |
1638 | .bdrv_reopen_abort = raw_reopen_abort, | |
f3a5d3f8 | 1639 | .bdrv_create = hdev_create, |
0b4ce02e | 1640 | .create_options = raw_create_options, |
336c1c12 | 1641 | .bdrv_has_zero_init = hdev_has_zero_init, |
f3a5d3f8 | 1642 | |
f3a5d3f8 CH |
1643 | .bdrv_aio_readv = raw_aio_readv, |
1644 | .bdrv_aio_writev = raw_aio_writev, | |
b2e12bc6 | 1645 | .bdrv_aio_flush = raw_aio_flush, |
f3a5d3f8 | 1646 | |
55b949c8 | 1647 | .bdrv_truncate = raw_truncate, |
f3a5d3f8 | 1648 | .bdrv_getlength = raw_getlength, |
4a1d5e1f FZ |
1649 | .bdrv_get_allocated_file_size |
1650 | = raw_get_allocated_file_size, | |
f3a5d3f8 CH |
1651 | |
1652 | /* removable device support */ | |
1653 | .bdrv_is_inserted = floppy_is_inserted, | |
1654 | .bdrv_media_changed = floppy_media_changed, | |
1655 | .bdrv_eject = floppy_eject, | |
f3a5d3f8 CH |
1656 | }; |
1657 | ||
787e4a85 KW |
1658 | static int cdrom_open(BlockDriverState *bs, const char *filename, |
1659 | QDict *options, int flags) | |
f3a5d3f8 CH |
1660 | { |
1661 | BDRVRawState *s = bs->opaque; | |
1662 | ||
f3a5d3f8 CH |
1663 | s->type = FTYPE_CD; |
1664 | ||
19a3da7f BS |
1665 | /* open will not fail even if no CD is inserted, so add O_NONBLOCK */ |
1666 | return raw_open_common(bs, filename, flags, O_NONBLOCK); | |
f3a5d3f8 CH |
1667 | } |
1668 | ||
508c7cb3 CH |
1669 | static int cdrom_probe_device(const char *filename) |
1670 | { | |
3baf720e CR |
1671 | int fd, ret; |
1672 | int prio = 0; | |
343f8568 | 1673 | struct stat st; |
3baf720e | 1674 | |
6165f4d8 | 1675 | fd = qemu_open(filename, O_RDONLY | O_NONBLOCK); |
3baf720e CR |
1676 | if (fd < 0) { |
1677 | goto out; | |
1678 | } | |
343f8568 JS |
1679 | ret = fstat(fd, &st); |
1680 | if (ret == -1 || !S_ISBLK(st.st_mode)) { | |
1681 | goto outc; | |
1682 | } | |
3baf720e CR |
1683 | |
1684 | /* Attempt to detect via a CDROM specific ioctl */ | |
1685 | ret = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT); | |
1686 | if (ret >= 0) | |
1687 | prio = 100; | |
1688 | ||
343f8568 | 1689 | outc: |
2e1e79da | 1690 | qemu_close(fd); |
3baf720e CR |
1691 | out: |
1692 | return prio; | |
508c7cb3 CH |
1693 | } |
1694 | ||
f3a5d3f8 CH |
1695 | static int cdrom_is_inserted(BlockDriverState *bs) |
1696 | { | |
1697 | BDRVRawState *s = bs->opaque; | |
1698 | int ret; | |
1699 | ||
1700 | ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT); | |
1701 | if (ret == CDS_DISC_OK) | |
1702 | return 1; | |
1703 | return 0; | |
1704 | } | |
1705 | ||
f36f3949 | 1706 | static void cdrom_eject(BlockDriverState *bs, bool eject_flag) |
f3a5d3f8 CH |
1707 | { |
1708 | BDRVRawState *s = bs->opaque; | |
1709 | ||
1710 | if (eject_flag) { | |
1711 | if (ioctl(s->fd, CDROMEJECT, NULL) < 0) | |
1712 | perror("CDROMEJECT"); | |
1713 | } else { | |
1714 | if (ioctl(s->fd, CDROMCLOSETRAY, NULL) < 0) | |
1715 | perror("CDROMEJECT"); | |
1716 | } | |
f3a5d3f8 CH |
1717 | } |
1718 | ||
025e849a | 1719 | static void cdrom_lock_medium(BlockDriverState *bs, bool locked) |
f3a5d3f8 CH |
1720 | { |
1721 | BDRVRawState *s = bs->opaque; | |
1722 | ||
1723 | if (ioctl(s->fd, CDROM_LOCKDOOR, locked) < 0) { | |
1724 | /* | |
1725 | * Note: an error can happen if the distribution automatically | |
1726 | * mounts the CD-ROM | |
1727 | */ | |
1728 | /* perror("CDROM_LOCKDOOR"); */ | |
1729 | } | |
f3a5d3f8 CH |
1730 | } |
1731 | ||
1732 | static BlockDriver bdrv_host_cdrom = { | |
1733 | .format_name = "host_cdrom", | |
84a12e66 | 1734 | .protocol_name = "host_cdrom", |
f3a5d3f8 | 1735 | .instance_size = sizeof(BDRVRawState), |
508c7cb3 | 1736 | .bdrv_probe_device = cdrom_probe_device, |
66f82cee | 1737 | .bdrv_file_open = cdrom_open, |
f3a5d3f8 | 1738 | .bdrv_close = raw_close, |
1bc6b705 JC |
1739 | .bdrv_reopen_prepare = raw_reopen_prepare, |
1740 | .bdrv_reopen_commit = raw_reopen_commit, | |
1741 | .bdrv_reopen_abort = raw_reopen_abort, | |
f3a5d3f8 | 1742 | .bdrv_create = hdev_create, |
0b4ce02e | 1743 | .create_options = raw_create_options, |
336c1c12 | 1744 | .bdrv_has_zero_init = hdev_has_zero_init, |
f3a5d3f8 | 1745 | |
f3a5d3f8 CH |
1746 | .bdrv_aio_readv = raw_aio_readv, |
1747 | .bdrv_aio_writev = raw_aio_writev, | |
b2e12bc6 | 1748 | .bdrv_aio_flush = raw_aio_flush, |
f3a5d3f8 | 1749 | |
55b949c8 | 1750 | .bdrv_truncate = raw_truncate, |
f3a5d3f8 | 1751 | .bdrv_getlength = raw_getlength, |
4a1d5e1f FZ |
1752 | .bdrv_get_allocated_file_size |
1753 | = raw_get_allocated_file_size, | |
f3a5d3f8 CH |
1754 | |
1755 | /* removable device support */ | |
1756 | .bdrv_is_inserted = cdrom_is_inserted, | |
1757 | .bdrv_eject = cdrom_eject, | |
025e849a | 1758 | .bdrv_lock_medium = cdrom_lock_medium, |
f3a5d3f8 CH |
1759 | |
1760 | /* generic scsi device */ | |
63ec93db | 1761 | .bdrv_ioctl = hdev_ioctl, |
63ec93db | 1762 | .bdrv_aio_ioctl = hdev_aio_ioctl, |
f3a5d3f8 CH |
1763 | }; |
1764 | #endif /* __linux__ */ | |
1765 | ||
a167ba50 | 1766 | #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__) |
787e4a85 KW |
1767 | static int cdrom_open(BlockDriverState *bs, const char *filename, |
1768 | QDict *options, int flags) | |
f3a5d3f8 CH |
1769 | { |
1770 | BDRVRawState *s = bs->opaque; | |
1771 | int ret; | |
1772 | ||
1773 | s->type = FTYPE_CD; | |
1774 | ||
19a3da7f | 1775 | ret = raw_open_common(bs, filename, flags, 0); |
f3a5d3f8 CH |
1776 | if (ret) |
1777 | return ret; | |
1778 | ||
9b2260cb | 1779 | /* make sure the door isn't locked at this time */ |
f3a5d3f8 CH |
1780 | ioctl(s->fd, CDIOCALLOW); |
1781 | return 0; | |
1782 | } | |
1783 | ||
508c7cb3 CH |
1784 | static int cdrom_probe_device(const char *filename) |
1785 | { | |
1786 | if (strstart(filename, "/dev/cd", NULL) || | |
1787 | strstart(filename, "/dev/acd", NULL)) | |
1788 | return 100; | |
1789 | return 0; | |
1790 | } | |
1791 | ||
f3a5d3f8 CH |
1792 | static int cdrom_reopen(BlockDriverState *bs) |
1793 | { | |
1794 | BDRVRawState *s = bs->opaque; | |
1795 | int fd; | |
1796 | ||
1797 | /* | |
1798 | * Force reread of possibly changed/newly loaded disc, | |
1799 | * FreeBSD seems to not notice sometimes... | |
1800 | */ | |
1801 | if (s->fd >= 0) | |
2e1e79da | 1802 | qemu_close(s->fd); |
6165f4d8 | 1803 | fd = qemu_open(bs->filename, s->open_flags, 0644); |
f3a5d3f8 CH |
1804 | if (fd < 0) { |
1805 | s->fd = -1; | |
1806 | return -EIO; | |
1807 | } | |
1808 | s->fd = fd; | |
1809 | ||
9b2260cb | 1810 | /* make sure the door isn't locked at this time */ |
f3a5d3f8 CH |
1811 | ioctl(s->fd, CDIOCALLOW); |
1812 | return 0; | |
1813 | } | |
1814 | ||
1815 | static int cdrom_is_inserted(BlockDriverState *bs) | |
1816 | { | |
1817 | return raw_getlength(bs) > 0; | |
1818 | } | |
1819 | ||
f36f3949 | 1820 | static void cdrom_eject(BlockDriverState *bs, bool eject_flag) |
f3a5d3f8 CH |
1821 | { |
1822 | BDRVRawState *s = bs->opaque; | |
1823 | ||
1824 | if (s->fd < 0) | |
822e1cd1 | 1825 | return; |
f3a5d3f8 CH |
1826 | |
1827 | (void) ioctl(s->fd, CDIOCALLOW); | |
1828 | ||
1829 | if (eject_flag) { | |
1830 | if (ioctl(s->fd, CDIOCEJECT) < 0) | |
1831 | perror("CDIOCEJECT"); | |
1832 | } else { | |
1833 | if (ioctl(s->fd, CDIOCCLOSE) < 0) | |
1834 | perror("CDIOCCLOSE"); | |
1835 | } | |
1836 | ||
822e1cd1 | 1837 | cdrom_reopen(bs); |
f3a5d3f8 CH |
1838 | } |
1839 | ||
025e849a | 1840 | static void cdrom_lock_medium(BlockDriverState *bs, bool locked) |
f3a5d3f8 CH |
1841 | { |
1842 | BDRVRawState *s = bs->opaque; | |
1843 | ||
1844 | if (s->fd < 0) | |
7bf37fed | 1845 | return; |
f3a5d3f8 CH |
1846 | if (ioctl(s->fd, (locked ? CDIOCPREVENT : CDIOCALLOW)) < 0) { |
1847 | /* | |
1848 | * Note: an error can happen if the distribution automatically | |
1849 | * mounts the CD-ROM | |
1850 | */ | |
1851 | /* perror("CDROM_LOCKDOOR"); */ | |
1852 | } | |
f3a5d3f8 CH |
1853 | } |
1854 | ||
1855 | static BlockDriver bdrv_host_cdrom = { | |
1856 | .format_name = "host_cdrom", | |
84a12e66 | 1857 | .protocol_name = "host_cdrom", |
f3a5d3f8 | 1858 | .instance_size = sizeof(BDRVRawState), |
508c7cb3 | 1859 | .bdrv_probe_device = cdrom_probe_device, |
66f82cee | 1860 | .bdrv_file_open = cdrom_open, |
f3a5d3f8 | 1861 | .bdrv_close = raw_close, |
1bc6b705 JC |
1862 | .bdrv_reopen_prepare = raw_reopen_prepare, |
1863 | .bdrv_reopen_commit = raw_reopen_commit, | |
1864 | .bdrv_reopen_abort = raw_reopen_abort, | |
f3a5d3f8 | 1865 | .bdrv_create = hdev_create, |
0b4ce02e | 1866 | .create_options = raw_create_options, |
336c1c12 | 1867 | .bdrv_has_zero_init = hdev_has_zero_init, |
f3a5d3f8 | 1868 | |
f3a5d3f8 CH |
1869 | .bdrv_aio_readv = raw_aio_readv, |
1870 | .bdrv_aio_writev = raw_aio_writev, | |
b2e12bc6 | 1871 | .bdrv_aio_flush = raw_aio_flush, |
f3a5d3f8 | 1872 | |
55b949c8 | 1873 | .bdrv_truncate = raw_truncate, |
f3a5d3f8 | 1874 | .bdrv_getlength = raw_getlength, |
4a1d5e1f FZ |
1875 | .bdrv_get_allocated_file_size |
1876 | = raw_get_allocated_file_size, | |
f3a5d3f8 | 1877 | |
19cb3738 | 1878 | /* removable device support */ |
f3a5d3f8 CH |
1879 | .bdrv_is_inserted = cdrom_is_inserted, |
1880 | .bdrv_eject = cdrom_eject, | |
025e849a | 1881 | .bdrv_lock_medium = cdrom_lock_medium, |
19cb3738 | 1882 | }; |
f3a5d3f8 | 1883 | #endif /* __FreeBSD__ */ |
5efa9d5a | 1884 | |
4065742a SH |
1885 | #ifdef CONFIG_LINUX_AIO |
1886 | /** | |
1887 | * Return the file descriptor for Linux AIO | |
1888 | * | |
1889 | * This function is a layering violation and should be removed when it becomes | |
1890 | * possible to call the block layer outside the global mutex. It allows the | |
1891 | * caller to hijack the file descriptor so I/O can be performed outside the | |
1892 | * block layer. | |
1893 | */ | |
1894 | int raw_get_aio_fd(BlockDriverState *bs) | |
1895 | { | |
1896 | BDRVRawState *s; | |
1897 | ||
1898 | if (!bs->drv) { | |
1899 | return -ENOMEDIUM; | |
1900 | } | |
1901 | ||
1902 | if (bs->drv == bdrv_find_format("raw")) { | |
1903 | bs = bs->file; | |
1904 | } | |
1905 | ||
1906 | /* raw-posix has several protocols so just check for raw_aio_readv */ | |
1907 | if (bs->drv->bdrv_aio_readv != raw_aio_readv) { | |
1908 | return -ENOTSUP; | |
1909 | } | |
1910 | ||
1911 | s = bs->opaque; | |
1912 | if (!s->use_aio) { | |
1913 | return -ENOTSUP; | |
1914 | } | |
1915 | return s->fd; | |
1916 | } | |
1917 | #endif /* CONFIG_LINUX_AIO */ | |
1918 | ||
84a12e66 | 1919 | static void bdrv_file_init(void) |
5efa9d5a | 1920 | { |
508c7cb3 CH |
1921 | /* |
1922 | * Register all the drivers. Note that order is important, the driver | |
1923 | * registered last will get probed first. | |
1924 | */ | |
84a12e66 | 1925 | bdrv_register(&bdrv_file); |
5efa9d5a | 1926 | bdrv_register(&bdrv_host_device); |
f3a5d3f8 CH |
1927 | #ifdef __linux__ |
1928 | bdrv_register(&bdrv_host_floppy); | |
1929 | bdrv_register(&bdrv_host_cdrom); | |
1930 | #endif | |
a167ba50 | 1931 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
f3a5d3f8 CH |
1932 | bdrv_register(&bdrv_host_cdrom); |
1933 | #endif | |
5efa9d5a AL |
1934 | } |
1935 | ||
84a12e66 | 1936 | block_init(bdrv_file_init); |