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