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