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