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