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