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