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