]>
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" |
87ecb68b | 25 | #include "qemu-timer.h" |
baf35cb9 | 26 | #include "qemu-char.h" |
0bf9e31a | 27 | #include "qemu-log.h" |
83f64091 | 28 | #include "block_int.h" |
5efa9d5a | 29 | #include "module.h" |
9ef91a67 | 30 | #include "block/raw-posix-aio.h" |
83f64091 | 31 | |
83f64091 FB |
32 | #ifdef CONFIG_COCOA |
33 | #include <paths.h> | |
34 | #include <sys/param.h> | |
35 | #include <IOKit/IOKitLib.h> | |
36 | #include <IOKit/IOBSD.h> | |
37 | #include <IOKit/storage/IOMediaBSDClient.h> | |
38 | #include <IOKit/storage/IOMedia.h> | |
39 | #include <IOKit/storage/IOCDMedia.h> | |
40 | //#include <IOKit/storage/IOCDTypes.h> | |
41 | #include <CoreFoundation/CoreFoundation.h> | |
42 | #endif | |
43 | ||
44 | #ifdef __sun__ | |
2e9671da | 45 | #define _POSIX_PTHREAD_SEMANTICS 1 |
83f64091 FB |
46 | #include <sys/dkio.h> |
47 | #endif | |
19cb3738 | 48 | #ifdef __linux__ |
343f8568 JS |
49 | #include <sys/types.h> |
50 | #include <sys/stat.h> | |
19cb3738 | 51 | #include <sys/ioctl.h> |
05acda4d | 52 | #include <sys/param.h> |
19cb3738 FB |
53 | #include <linux/cdrom.h> |
54 | #include <linux/fd.h> | |
55 | #endif | |
a167ba50 | 56 | #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__) |
1cb6c3fd | 57 | #include <sys/disk.h> |
9f23011a | 58 | #include <sys/cdio.h> |
1cb6c3fd | 59 | #endif |
83f64091 | 60 | |
128ab2ff BS |
61 | #ifdef __OpenBSD__ |
62 | #include <sys/ioctl.h> | |
63 | #include <sys/disklabel.h> | |
64 | #include <sys/dkio.h> | |
65 | #endif | |
66 | ||
d1f6fd8d CE |
67 | #ifdef __NetBSD__ |
68 | #include <sys/ioctl.h> | |
69 | #include <sys/disklabel.h> | |
70 | #include <sys/dkio.h> | |
71 | #include <sys/disk.h> | |
72 | #endif | |
73 | ||
c5e97233 BS |
74 | #ifdef __DragonFly__ |
75 | #include <sys/ioctl.h> | |
76 | #include <sys/diskslice.h> | |
77 | #endif | |
78 | ||
dce512de CH |
79 | #ifdef CONFIG_XFS |
80 | #include <xfs/xfs.h> | |
81 | #endif | |
82 | ||
19cb3738 | 83 | //#define DEBUG_FLOPPY |
83f64091 | 84 | |
faf07963 | 85 | //#define DEBUG_BLOCK |
03ff3ca3 | 86 | #if defined(DEBUG_BLOCK) |
001faf32 BS |
87 | #define DEBUG_BLOCK_PRINT(formatCstr, ...) do { if (qemu_log_enabled()) \ |
88 | { qemu_log(formatCstr, ## __VA_ARGS__); qemu_log_flush(); } } while (0) | |
8c05dbf9 | 89 | #else |
001faf32 | 90 | #define DEBUG_BLOCK_PRINT(formatCstr, ...) |
8c05dbf9 TS |
91 | #endif |
92 | ||
f6465578 AL |
93 | /* OS X does not have O_DSYNC */ |
94 | #ifndef O_DSYNC | |
1c27a8b3 | 95 | #ifdef O_SYNC |
7ab064d2 | 96 | #define O_DSYNC O_SYNC |
1c27a8b3 JA |
97 | #elif defined(O_FSYNC) |
98 | #define O_DSYNC O_FSYNC | |
99 | #endif | |
f6465578 AL |
100 | #endif |
101 | ||
9f7965c7 AL |
102 | /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */ |
103 | #ifndef O_DIRECT | |
104 | #define O_DIRECT O_DSYNC | |
105 | #endif | |
106 | ||
19cb3738 FB |
107 | #define FTYPE_FILE 0 |
108 | #define FTYPE_CD 1 | |
109 | #define FTYPE_FD 2 | |
83f64091 | 110 | |
c57c846a | 111 | /* if the FD is not accessed during that time (in ns), we try to |
19cb3738 | 112 | reopen it to see if the disk has been changed */ |
c57c846a | 113 | #define FD_OPEN_TIMEOUT (1000000000) |
83f64091 | 114 | |
581b9e29 CH |
115 | #define MAX_BLOCKSIZE 4096 |
116 | ||
19cb3738 FB |
117 | typedef struct BDRVRawState { |
118 | int fd; | |
119 | int type; | |
0e1d8f4c | 120 | int open_flags; |
19cb3738 FB |
121 | #if defined(__linux__) |
122 | /* linux floppy specific */ | |
19cb3738 FB |
123 | int64_t fd_open_time; |
124 | int64_t fd_error_time; | |
125 | int fd_got_error; | |
126 | int fd_media_changed; | |
83f64091 | 127 | #endif |
e44bd6fc | 128 | #ifdef CONFIG_LINUX_AIO |
5c6c3a6c | 129 | int use_aio; |
1e5b9d2f | 130 | void *aio_ctx; |
e44bd6fc | 131 | #endif |
581b9e29 CH |
132 | uint8_t *aligned_buf; |
133 | unsigned aligned_buf_size; | |
dce512de CH |
134 | #ifdef CONFIG_XFS |
135 | bool is_xfs : 1; | |
136 | #endif | |
19cb3738 FB |
137 | } BDRVRawState; |
138 | ||
139 | static int fd_open(BlockDriverState *bs); | |
22afa7b5 | 140 | static int64_t raw_getlength(BlockDriverState *bs); |
83f64091 | 141 | |
a167ba50 | 142 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
f3a5d3f8 | 143 | static int cdrom_reopen(BlockDriverState *bs); |
9f23011a BS |
144 | #endif |
145 | ||
1de1ae0a CE |
146 | #if defined(__NetBSD__) |
147 | static int raw_normalize_devicepath(const char **filename) | |
148 | { | |
149 | static char namebuf[PATH_MAX]; | |
150 | const char *dp, *fname; | |
151 | struct stat sb; | |
152 | ||
153 | fname = *filename; | |
154 | dp = strrchr(fname, '/'); | |
155 | if (lstat(fname, &sb) < 0) { | |
156 | fprintf(stderr, "%s: stat failed: %s\n", | |
157 | fname, strerror(errno)); | |
158 | return -errno; | |
159 | } | |
160 | ||
161 | if (!S_ISBLK(sb.st_mode)) { | |
162 | return 0; | |
163 | } | |
164 | ||
165 | if (dp == NULL) { | |
166 | snprintf(namebuf, PATH_MAX, "r%s", fname); | |
167 | } else { | |
168 | snprintf(namebuf, PATH_MAX, "%.*s/r%s", | |
169 | (int)(dp - fname), fname, dp + 1); | |
170 | } | |
171 | fprintf(stderr, "%s is a block device", fname); | |
172 | *filename = namebuf; | |
173 | fprintf(stderr, ", using %s\n", *filename); | |
174 | ||
175 | return 0; | |
176 | } | |
177 | #else | |
178 | static int raw_normalize_devicepath(const char **filename) | |
179 | { | |
180 | return 0; | |
181 | } | |
182 | #endif | |
183 | ||
90babde0 | 184 | static int raw_open_common(BlockDriverState *bs, const char *filename, |
19a3da7f | 185 | int bdrv_flags, int open_flags) |
83f64091 FB |
186 | { |
187 | BDRVRawState *s = bs->opaque; | |
0e1d8f4c | 188 | int fd, ret; |
83f64091 | 189 | |
1de1ae0a CE |
190 | ret = raw_normalize_devicepath(&filename); |
191 | if (ret != 0) { | |
192 | return ret; | |
193 | } | |
194 | ||
19a3da7f | 195 | s->open_flags = open_flags | O_BINARY; |
11a1feb6 | 196 | s->open_flags &= ~O_ACCMODE; |
f5edb014 | 197 | if (bdrv_flags & BDRV_O_RDWR) { |
0e1d8f4c | 198 | s->open_flags |= O_RDWR; |
83f64091 | 199 | } else { |
0e1d8f4c | 200 | s->open_flags |= O_RDONLY; |
83f64091 | 201 | } |
9f7965c7 AL |
202 | |
203 | /* Use O_DSYNC for write-through caching, no flags for write-back caching, | |
204 | * and O_DIRECT for no caching. */ | |
19a3da7f | 205 | if ((bdrv_flags & BDRV_O_NOCACHE)) |
0e1d8f4c | 206 | s->open_flags |= O_DIRECT; |
a6599793 | 207 | if (!(bdrv_flags & BDRV_O_CACHE_WB)) |
0e1d8f4c | 208 | s->open_flags |= O_DSYNC; |
83f64091 | 209 | |
90babde0 | 210 | s->fd = -1; |
40ff6d7e | 211 | fd = qemu_open(filename, s->open_flags, 0644); |
19cb3738 FB |
212 | if (fd < 0) { |
213 | ret = -errno; | |
214 | if (ret == -EROFS) | |
215 | ret = -EACCES; | |
216 | return ret; | |
217 | } | |
83f64091 | 218 | s->fd = fd; |
bed5cc52 | 219 | s->aligned_buf = NULL; |
5c6c3a6c | 220 | |
19a3da7f | 221 | if ((bdrv_flags & BDRV_O_NOCACHE)) { |
581b9e29 CH |
222 | /* |
223 | * Allocate a buffer for read/modify/write cycles. Chose the size | |
224 | * pessimistically as we don't know the block size yet. | |
225 | */ | |
226 | s->aligned_buf_size = 32 * MAX_BLOCKSIZE; | |
227 | s->aligned_buf = qemu_memalign(MAX_BLOCKSIZE, s->aligned_buf_size); | |
bed5cc52 | 228 | if (s->aligned_buf == NULL) { |
9ef91a67 | 229 | goto out_close; |
bed5cc52 FB |
230 | } |
231 | } | |
9ef91a67 | 232 | |
f6e8ffc2 FZ |
233 | /* We're falling back to POSIX AIO in some cases so init always */ |
234 | if (paio_init() < 0) { | |
235 | goto out_free_buf; | |
236 | } | |
237 | ||
5c6c3a6c CH |
238 | #ifdef CONFIG_LINUX_AIO |
239 | if ((bdrv_flags & (BDRV_O_NOCACHE|BDRV_O_NATIVE_AIO)) == | |
240 | (BDRV_O_NOCACHE|BDRV_O_NATIVE_AIO)) { | |
d2e46345 | 241 | |
5c6c3a6c CH |
242 | s->aio_ctx = laio_init(); |
243 | if (!s->aio_ctx) { | |
244 | goto out_free_buf; | |
245 | } | |
246 | s->use_aio = 1; | |
247 | } else | |
248 | #endif | |
249 | { | |
e44bd6fc | 250 | #ifdef CONFIG_LINUX_AIO |
5c6c3a6c | 251 | s->use_aio = 0; |
e44bd6fc | 252 | #endif |
9ef91a67 CH |
253 | } |
254 | ||
dce512de CH |
255 | #ifdef CONFIG_XFS |
256 | if (platform_test_xfs_fd(s->fd)) { | |
257 | s->is_xfs = 1; | |
258 | } | |
259 | #endif | |
260 | ||
83f64091 | 261 | return 0; |
9ef91a67 CH |
262 | |
263 | out_free_buf: | |
264 | qemu_vfree(s->aligned_buf); | |
265 | out_close: | |
266 | close(fd); | |
267 | return -errno; | |
83f64091 FB |
268 | } |
269 | ||
90babde0 CH |
270 | static int raw_open(BlockDriverState *bs, const char *filename, int flags) |
271 | { | |
272 | BDRVRawState *s = bs->opaque; | |
273 | ||
274 | s->type = FTYPE_FILE; | |
9a2d77ad | 275 | return raw_open_common(bs, filename, flags, 0); |
90babde0 CH |
276 | } |
277 | ||
83f64091 FB |
278 | /* XXX: use host sector size if necessary with: |
279 | #ifdef DIOCGSECTORSIZE | |
280 | { | |
281 | unsigned int sectorsize = 512; | |
282 | if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) && | |
283 | sectorsize > bufsize) | |
284 | bufsize = sectorsize; | |
285 | } | |
286 | #endif | |
287 | #ifdef CONFIG_COCOA | |
2ee9fb48 | 288 | uint32_t blockSize = 512; |
83f64091 FB |
289 | if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) { |
290 | bufsize = blockSize; | |
291 | } | |
292 | #endif | |
293 | */ | |
294 | ||
bed5cc52 FB |
295 | /* |
296 | * offset and count are in bytes, but must be multiples of 512 for files | |
297 | * opened with O_DIRECT. buf must be aligned to 512 bytes then. | |
298 | * | |
299 | * This function may be called without alignment if the caller ensures | |
300 | * that O_DIRECT is not in effect. | |
301 | */ | |
302 | static int raw_pread_aligned(BlockDriverState *bs, int64_t offset, | |
83f64091 FB |
303 | uint8_t *buf, int count) |
304 | { | |
305 | BDRVRawState *s = bs->opaque; | |
306 | int ret; | |
3b46e624 | 307 | |
19cb3738 FB |
308 | ret = fd_open(bs); |
309 | if (ret < 0) | |
310 | return ret; | |
311 | ||
4899d10d | 312 | ret = pread(s->fd, buf, count, offset); |
8c05dbf9 | 313 | if (ret == count) |
65d21bc7 | 314 | return ret; |
8c05dbf9 | 315 | |
22afa7b5 KW |
316 | /* Allow reads beyond the end (needed for pwrite) */ |
317 | if ((ret == 0) && bs->growable) { | |
318 | int64_t size = raw_getlength(bs); | |
319 | if (offset >= size) { | |
320 | memset(buf, 0, count); | |
65d21bc7 | 321 | return count; |
22afa7b5 KW |
322 | } |
323 | } | |
324 | ||
92868412 JM |
325 | DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64 |
326 | "] read failed %d : %d = %s\n", | |
8c05dbf9 TS |
327 | s->fd, bs->filename, offset, buf, count, |
328 | bs->total_sectors, ret, errno, strerror(errno)); | |
329 | ||
330 | /* Try harder for CDrom. */ | |
65d21bc7 | 331 | if (s->type != FTYPE_FILE) { |
4899d10d | 332 | ret = pread(s->fd, buf, count, offset); |
8c05dbf9 | 333 | if (ret == count) |
65d21bc7 | 334 | return ret; |
4899d10d | 335 | ret = pread(s->fd, buf, count, offset); |
8c05dbf9 | 336 | if (ret == count) |
65d21bc7 | 337 | return ret; |
8c05dbf9 | 338 | |
92868412 JM |
339 | DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64 |
340 | "] retry read failed %d : %d = %s\n", | |
8c05dbf9 TS |
341 | s->fd, bs->filename, offset, buf, count, |
342 | bs->total_sectors, ret, errno, strerror(errno)); | |
343 | } | |
344 | ||
94c6d6d8 | 345 | return (ret < 0) ? -errno : ret; |
83f64091 FB |
346 | } |
347 | ||
bed5cc52 | 348 | /* |
581b9e29 CH |
349 | * offset and count are in bytes, but must be multiples of the sector size |
350 | * for files opened with O_DIRECT. buf must be aligned to sector size bytes | |
351 | * then. | |
bed5cc52 FB |
352 | * |
353 | * This function may be called without alignment if the caller ensures | |
354 | * that O_DIRECT is not in effect. | |
355 | */ | |
356 | static int raw_pwrite_aligned(BlockDriverState *bs, int64_t offset, | |
83f64091 FB |
357 | const uint8_t *buf, int count) |
358 | { | |
359 | BDRVRawState *s = bs->opaque; | |
360 | int ret; | |
3b46e624 | 361 | |
19cb3738 FB |
362 | ret = fd_open(bs); |
363 | if (ret < 0) | |
4141d4c2 | 364 | return -errno; |
19cb3738 | 365 | |
4899d10d | 366 | ret = pwrite(s->fd, buf, count, offset); |
8c05dbf9 | 367 | if (ret == count) |
65d21bc7 | 368 | return ret; |
8c05dbf9 | 369 | |
92868412 JM |
370 | DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%" PRId64 |
371 | "] write failed %d : %d = %s\n", | |
8c05dbf9 TS |
372 | s->fd, bs->filename, offset, buf, count, |
373 | bs->total_sectors, ret, errno, strerror(errno)); | |
374 | ||
4141d4c2 | 375 | return (ret < 0) ? -errno : ret; |
83f64091 FB |
376 | } |
377 | ||
bed5cc52 | 378 | |
bed5cc52 FB |
379 | /* |
380 | * offset and count are in bytes and possibly not aligned. For files opened | |
381 | * with O_DIRECT, necessary alignments are ensured before calling | |
382 | * raw_pread_aligned to do the actual read. | |
383 | */ | |
384 | static int raw_pread(BlockDriverState *bs, int64_t offset, | |
385 | uint8_t *buf, int count) | |
386 | { | |
387 | BDRVRawState *s = bs->opaque; | |
581b9e29 | 388 | unsigned sector_mask = bs->buffer_alignment - 1; |
bed5cc52 FB |
389 | int size, ret, shift, sum; |
390 | ||
391 | sum = 0; | |
392 | ||
393 | if (s->aligned_buf != NULL) { | |
394 | ||
581b9e29 CH |
395 | if (offset & sector_mask) { |
396 | /* align offset on a sector size bytes boundary */ | |
bed5cc52 | 397 | |
581b9e29 CH |
398 | shift = offset & sector_mask; |
399 | size = (shift + count + sector_mask) & ~sector_mask; | |
400 | if (size > s->aligned_buf_size) | |
401 | size = s->aligned_buf_size; | |
bed5cc52 FB |
402 | ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, size); |
403 | if (ret < 0) | |
404 | return ret; | |
405 | ||
581b9e29 | 406 | size = bs->buffer_alignment - shift; |
bed5cc52 FB |
407 | if (size > count) |
408 | size = count; | |
409 | memcpy(buf, s->aligned_buf + shift, size); | |
410 | ||
411 | buf += size; | |
412 | offset += size; | |
413 | count -= size; | |
414 | sum += size; | |
415 | ||
416 | if (count == 0) | |
417 | return sum; | |
418 | } | |
581b9e29 | 419 | if (count & sector_mask || (uintptr_t) buf & sector_mask) { |
bed5cc52 FB |
420 | |
421 | /* read on aligned buffer */ | |
422 | ||
423 | while (count) { | |
424 | ||
581b9e29 CH |
425 | size = (count + sector_mask) & ~sector_mask; |
426 | if (size > s->aligned_buf_size) | |
427 | size = s->aligned_buf_size; | |
bed5cc52 FB |
428 | |
429 | ret = raw_pread_aligned(bs, offset, s->aligned_buf, size); | |
053965c7 | 430 | if (ret < 0) { |
bed5cc52 | 431 | return ret; |
053965c7 KW |
432 | } else if (ret == 0) { |
433 | fprintf(stderr, "raw_pread: read beyond end of file\n"); | |
434 | abort(); | |
435 | } | |
bed5cc52 FB |
436 | |
437 | size = ret; | |
438 | if (size > count) | |
439 | size = count; | |
440 | ||
441 | memcpy(buf, s->aligned_buf, size); | |
442 | ||
443 | buf += size; | |
444 | offset += size; | |
445 | count -= size; | |
446 | sum += size; | |
447 | } | |
448 | ||
449 | return sum; | |
450 | } | |
451 | } | |
452 | ||
453 | return raw_pread_aligned(bs, offset, buf, count) + sum; | |
454 | } | |
455 | ||
eda578e5 AL |
456 | static int raw_read(BlockDriverState *bs, int64_t sector_num, |
457 | uint8_t *buf, int nb_sectors) | |
458 | { | |
537a1d4b AL |
459 | int ret; |
460 | ||
9040385d JS |
461 | ret = raw_pread(bs, sector_num * BDRV_SECTOR_SIZE, buf, |
462 | nb_sectors * BDRV_SECTOR_SIZE); | |
463 | if (ret == (nb_sectors * BDRV_SECTOR_SIZE)) | |
537a1d4b AL |
464 | ret = 0; |
465 | return ret; | |
eda578e5 AL |
466 | } |
467 | ||
bed5cc52 FB |
468 | /* |
469 | * offset and count are in bytes and possibly not aligned. For files opened | |
470 | * with O_DIRECT, necessary alignments are ensured before calling | |
471 | * raw_pwrite_aligned to do the actual write. | |
472 | */ | |
473 | static int raw_pwrite(BlockDriverState *bs, int64_t offset, | |
474 | const uint8_t *buf, int count) | |
475 | { | |
476 | BDRVRawState *s = bs->opaque; | |
581b9e29 | 477 | unsigned sector_mask = bs->buffer_alignment - 1; |
bed5cc52 FB |
478 | int size, ret, shift, sum; |
479 | ||
480 | sum = 0; | |
481 | ||
482 | if (s->aligned_buf != NULL) { | |
483 | ||
581b9e29 CH |
484 | if (offset & sector_mask) { |
485 | /* align offset on a sector size bytes boundary */ | |
486 | shift = offset & sector_mask; | |
487 | ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, | |
488 | bs->buffer_alignment); | |
bed5cc52 FB |
489 | if (ret < 0) |
490 | return ret; | |
491 | ||
581b9e29 | 492 | size = bs->buffer_alignment - shift; |
bed5cc52 FB |
493 | if (size > count) |
494 | size = count; | |
495 | memcpy(s->aligned_buf + shift, buf, size); | |
496 | ||
581b9e29 CH |
497 | ret = raw_pwrite_aligned(bs, offset - shift, s->aligned_buf, |
498 | bs->buffer_alignment); | |
bed5cc52 FB |
499 | if (ret < 0) |
500 | return ret; | |
501 | ||
502 | buf += size; | |
503 | offset += size; | |
504 | count -= size; | |
505 | sum += size; | |
506 | ||
507 | if (count == 0) | |
508 | return sum; | |
509 | } | |
581b9e29 | 510 | if (count & sector_mask || (uintptr_t) buf & sector_mask) { |
bed5cc52 | 511 | |
581b9e29 | 512 | while ((size = (count & ~sector_mask)) != 0) { |
bed5cc52 | 513 | |
581b9e29 CH |
514 | if (size > s->aligned_buf_size) |
515 | size = s->aligned_buf_size; | |
bed5cc52 FB |
516 | |
517 | memcpy(s->aligned_buf, buf, size); | |
518 | ||
519 | ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, size); | |
520 | if (ret < 0) | |
521 | return ret; | |
522 | ||
523 | buf += ret; | |
524 | offset += ret; | |
525 | count -= ret; | |
526 | sum += ret; | |
527 | } | |
11a3cb81 | 528 | /* here, count < sector_size because (count & ~sector_mask) == 0 */ |
bed5cc52 | 529 | if (count) { |
581b9e29 CH |
530 | ret = raw_pread_aligned(bs, offset, s->aligned_buf, |
531 | bs->buffer_alignment); | |
bed5cc52 FB |
532 | if (ret < 0) |
533 | return ret; | |
534 | memcpy(s->aligned_buf, buf, count); | |
535 | ||
581b9e29 CH |
536 | ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, |
537 | bs->buffer_alignment); | |
bed5cc52 FB |
538 | if (ret < 0) |
539 | return ret; | |
540 | if (count < ret) | |
541 | ret = count; | |
542 | ||
543 | sum += ret; | |
544 | } | |
545 | return sum; | |
546 | } | |
547 | } | |
548 | return raw_pwrite_aligned(bs, offset, buf, count) + sum; | |
549 | } | |
550 | ||
eda578e5 AL |
551 | static int raw_write(BlockDriverState *bs, int64_t sector_num, |
552 | const uint8_t *buf, int nb_sectors) | |
553 | { | |
537a1d4b | 554 | int ret; |
9040385d JS |
555 | ret = raw_pwrite(bs, sector_num * BDRV_SECTOR_SIZE, buf, |
556 | nb_sectors * BDRV_SECTOR_SIZE); | |
557 | if (ret == (nb_sectors * BDRV_SECTOR_SIZE)) | |
537a1d4b AL |
558 | ret = 0; |
559 | return ret; | |
eda578e5 AL |
560 | } |
561 | ||
9ef91a67 CH |
562 | /* |
563 | * Check if all memory in this vector is sector aligned. | |
564 | */ | |
581b9e29 | 565 | static int qiov_is_aligned(BlockDriverState *bs, QEMUIOVector *qiov) |
a76bab49 | 566 | { |
9ef91a67 | 567 | int i; |
83f64091 | 568 | |
9ef91a67 | 569 | for (i = 0; i < qiov->niov; i++) { |
581b9e29 | 570 | if ((uintptr_t) qiov->iov[i].iov_base % bs->buffer_alignment) { |
9ef91a67 | 571 | return 0; |
c16b5a2c | 572 | } |
c16b5a2c | 573 | } |
c16b5a2c | 574 | |
9ef91a67 | 575 | return 1; |
c16b5a2c CH |
576 | } |
577 | ||
9ef91a67 CH |
578 | static BlockDriverAIOCB *raw_aio_submit(BlockDriverState *bs, |
579 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, | |
580 | BlockDriverCompletionFunc *cb, void *opaque, int type) | |
83f64091 | 581 | { |
ce1a14dc | 582 | BDRVRawState *s = bs->opaque; |
ce1a14dc | 583 | |
19cb3738 FB |
584 | if (fd_open(bs) < 0) |
585 | return NULL; | |
586 | ||
f141eafe AL |
587 | /* |
588 | * If O_DIRECT is used the buffer needs to be aligned on a sector | |
c1ee7d56 | 589 | * boundary. Check if this is the case or tell the low-level |
9ef91a67 | 590 | * driver that it needs to copy the buffer. |
f141eafe | 591 | */ |
5c6c3a6c | 592 | if (s->aligned_buf) { |
581b9e29 | 593 | if (!qiov_is_aligned(bs, qiov)) { |
5c6c3a6c | 594 | type |= QEMU_AIO_MISALIGNED; |
e44bd6fc | 595 | #ifdef CONFIG_LINUX_AIO |
5c6c3a6c CH |
596 | } else if (s->use_aio) { |
597 | return laio_submit(bs, s->aio_ctx, s->fd, sector_num, qiov, | |
e44bd6fc SW |
598 | nb_sectors, cb, opaque, type); |
599 | #endif | |
5c6c3a6c | 600 | } |
9ef91a67 | 601 | } |
f141eafe | 602 | |
1e5b9d2f | 603 | return paio_submit(bs, s->fd, sector_num, qiov, nb_sectors, |
9ef91a67 | 604 | cb, opaque, type); |
83f64091 FB |
605 | } |
606 | ||
f141eafe AL |
607 | static BlockDriverAIOCB *raw_aio_readv(BlockDriverState *bs, |
608 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, | |
ce1a14dc | 609 | BlockDriverCompletionFunc *cb, void *opaque) |
83f64091 | 610 | { |
9ef91a67 CH |
611 | return raw_aio_submit(bs, sector_num, qiov, nb_sectors, |
612 | cb, opaque, QEMU_AIO_READ); | |
83f64091 FB |
613 | } |
614 | ||
f141eafe AL |
615 | static BlockDriverAIOCB *raw_aio_writev(BlockDriverState *bs, |
616 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, | |
ce1a14dc | 617 | BlockDriverCompletionFunc *cb, void *opaque) |
83f64091 | 618 | { |
9ef91a67 CH |
619 | return raw_aio_submit(bs, sector_num, qiov, nb_sectors, |
620 | cb, opaque, QEMU_AIO_WRITE); | |
83f64091 | 621 | } |
53538725 | 622 | |
b2e12bc6 CH |
623 | static BlockDriverAIOCB *raw_aio_flush(BlockDriverState *bs, |
624 | BlockDriverCompletionFunc *cb, void *opaque) | |
625 | { | |
626 | BDRVRawState *s = bs->opaque; | |
627 | ||
628 | if (fd_open(bs) < 0) | |
629 | return NULL; | |
630 | ||
1e5b9d2f | 631 | return paio_submit(bs, s->fd, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH); |
b2e12bc6 CH |
632 | } |
633 | ||
83f64091 FB |
634 | static void raw_close(BlockDriverState *bs) |
635 | { | |
636 | BDRVRawState *s = bs->opaque; | |
19cb3738 FB |
637 | if (s->fd >= 0) { |
638 | close(s->fd); | |
639 | s->fd = -1; | |
bed5cc52 | 640 | if (s->aligned_buf != NULL) |
f8a83245 | 641 | qemu_vfree(s->aligned_buf); |
19cb3738 | 642 | } |
83f64091 FB |
643 | } |
644 | ||
645 | static int raw_truncate(BlockDriverState *bs, int64_t offset) | |
646 | { | |
647 | BDRVRawState *s = bs->opaque; | |
19cb3738 FB |
648 | if (s->type != FTYPE_FILE) |
649 | return -ENOTSUP; | |
83f64091 FB |
650 | if (ftruncate(s->fd, offset) < 0) |
651 | return -errno; | |
652 | return 0; | |
653 | } | |
654 | ||
128ab2ff BS |
655 | #ifdef __OpenBSD__ |
656 | static int64_t raw_getlength(BlockDriverState *bs) | |
657 | { | |
658 | BDRVRawState *s = bs->opaque; | |
659 | int fd = s->fd; | |
660 | struct stat st; | |
661 | ||
662 | if (fstat(fd, &st)) | |
663 | return -1; | |
664 | if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) { | |
665 | struct disklabel dl; | |
666 | ||
667 | if (ioctl(fd, DIOCGDINFO, &dl)) | |
668 | return -1; | |
669 | return (uint64_t)dl.d_secsize * | |
670 | dl.d_partitions[DISKPART(st.st_rdev)].p_size; | |
671 | } else | |
672 | return st.st_size; | |
673 | } | |
d1f6fd8d CE |
674 | #elif defined(__NetBSD__) |
675 | static int64_t raw_getlength(BlockDriverState *bs) | |
676 | { | |
677 | BDRVRawState *s = bs->opaque; | |
678 | int fd = s->fd; | |
679 | struct stat st; | |
680 | ||
681 | if (fstat(fd, &st)) | |
682 | return -1; | |
683 | if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) { | |
684 | struct dkwedge_info dkw; | |
685 | ||
686 | if (ioctl(fd, DIOCGWEDGEINFO, &dkw) != -1) { | |
687 | return dkw.dkw_size * 512; | |
688 | } else { | |
689 | struct disklabel dl; | |
690 | ||
691 | if (ioctl(fd, DIOCGDINFO, &dl)) | |
692 | return -1; | |
693 | return (uint64_t)dl.d_secsize * | |
694 | dl.d_partitions[DISKPART(st.st_rdev)].p_size; | |
695 | } | |
696 | } else | |
697 | return st.st_size; | |
698 | } | |
50779cc2 CH |
699 | #elif defined(__sun__) |
700 | static int64_t raw_getlength(BlockDriverState *bs) | |
701 | { | |
702 | BDRVRawState *s = bs->opaque; | |
703 | struct dk_minfo minfo; | |
704 | int ret; | |
705 | ||
706 | ret = fd_open(bs); | |
707 | if (ret < 0) { | |
708 | return ret; | |
709 | } | |
710 | ||
711 | /* | |
712 | * Use the DKIOCGMEDIAINFO ioctl to read the size. | |
713 | */ | |
714 | ret = ioctl(s->fd, DKIOCGMEDIAINFO, &minfo); | |
715 | if (ret != -1) { | |
716 | return minfo.dki_lbsize * minfo.dki_capacity; | |
717 | } | |
718 | ||
719 | /* | |
720 | * There are reports that lseek on some devices fails, but | |
721 | * irc discussion said that contingency on contingency was overkill. | |
722 | */ | |
723 | return lseek(s->fd, 0, SEEK_END); | |
724 | } | |
725 | #elif defined(CONFIG_BSD) | |
726 | static int64_t raw_getlength(BlockDriverState *bs) | |
83f64091 FB |
727 | { |
728 | BDRVRawState *s = bs->opaque; | |
729 | int fd = s->fd; | |
730 | int64_t size; | |
83f64091 | 731 | struct stat sb; |
a167ba50 | 732 | #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__) |
9f23011a | 733 | int reopened = 0; |
83f64091 | 734 | #endif |
19cb3738 FB |
735 | int ret; |
736 | ||
737 | ret = fd_open(bs); | |
738 | if (ret < 0) | |
739 | return ret; | |
83f64091 | 740 | |
a167ba50 | 741 | #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__) |
9f23011a BS |
742 | again: |
743 | #endif | |
83f64091 FB |
744 | if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) { |
745 | #ifdef DIOCGMEDIASIZE | |
746 | if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size)) | |
c5e97233 BS |
747 | #elif defined(DIOCGPART) |
748 | { | |
749 | struct partinfo pi; | |
750 | if (ioctl(fd, DIOCGPART, &pi) == 0) | |
751 | size = pi.media_size; | |
752 | else | |
753 | size = 0; | |
754 | } | |
755 | if (size == 0) | |
83f64091 FB |
756 | #endif |
757 | #ifdef CONFIG_COCOA | |
758 | size = LONG_LONG_MAX; | |
759 | #else | |
760 | size = lseek(fd, 0LL, SEEK_END); | |
9f23011a | 761 | #endif |
a167ba50 | 762 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
9f23011a BS |
763 | switch(s->type) { |
764 | case FTYPE_CD: | |
765 | /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */ | |
766 | if (size == 2048LL * (unsigned)-1) | |
767 | size = 0; | |
768 | /* XXX no disc? maybe we need to reopen... */ | |
f3a5d3f8 | 769 | if (size <= 0 && !reopened && cdrom_reopen(bs) >= 0) { |
9f23011a BS |
770 | reopened = 1; |
771 | goto again; | |
772 | } | |
773 | } | |
83f64091 | 774 | #endif |
50779cc2 | 775 | } else { |
83f64091 FB |
776 | size = lseek(fd, 0, SEEK_END); |
777 | } | |
83f64091 FB |
778 | return size; |
779 | } | |
50779cc2 CH |
780 | #else |
781 | static int64_t raw_getlength(BlockDriverState *bs) | |
782 | { | |
783 | BDRVRawState *s = bs->opaque; | |
784 | int ret; | |
785 | ||
786 | ret = fd_open(bs); | |
787 | if (ret < 0) { | |
788 | return ret; | |
789 | } | |
790 | ||
791 | return lseek(s->fd, 0, SEEK_END); | |
792 | } | |
128ab2ff | 793 | #endif |
83f64091 | 794 | |
4a1d5e1f FZ |
795 | static int64_t raw_get_allocated_file_size(BlockDriverState *bs) |
796 | { | |
797 | struct stat st; | |
798 | BDRVRawState *s = bs->opaque; | |
799 | ||
800 | if (fstat(s->fd, &st) < 0) { | |
801 | return -errno; | |
802 | } | |
803 | return (int64_t)st.st_blocks * 512; | |
804 | } | |
805 | ||
0e7e1989 | 806 | static int raw_create(const char *filename, QEMUOptionParameter *options) |
83f64091 FB |
807 | { |
808 | int fd; | |
1e37d059 | 809 | int result = 0; |
0e7e1989 | 810 | int64_t total_size = 0; |
83f64091 | 811 | |
0e7e1989 KW |
812 | /* Read out options */ |
813 | while (options && options->name) { | |
814 | if (!strcmp(options->name, BLOCK_OPT_SIZE)) { | |
9040385d | 815 | total_size = options->value.n / BDRV_SECTOR_SIZE; |
0e7e1989 KW |
816 | } |
817 | options++; | |
818 | } | |
83f64091 | 819 | |
5fafdf24 | 820 | fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, |
83f64091 | 821 | 0644); |
1e37d059 SW |
822 | if (fd < 0) { |
823 | result = -errno; | |
824 | } else { | |
9040385d | 825 | if (ftruncate(fd, total_size * BDRV_SECTOR_SIZE) != 0) { |
1e37d059 SW |
826 | result = -errno; |
827 | } | |
828 | if (close(fd) != 0) { | |
829 | result = -errno; | |
830 | } | |
831 | } | |
832 | return result; | |
83f64091 FB |
833 | } |
834 | ||
205ef796 | 835 | static int raw_flush(BlockDriverState *bs) |
83f64091 FB |
836 | { |
837 | BDRVRawState *s = bs->opaque; | |
205ef796 | 838 | return qemu_fdatasync(s->fd); |
83f64091 FB |
839 | } |
840 | ||
dce512de CH |
841 | #ifdef CONFIG_XFS |
842 | static int xfs_discard(BDRVRawState *s, int64_t sector_num, int nb_sectors) | |
843 | { | |
844 | struct xfs_flock64 fl; | |
845 | ||
846 | memset(&fl, 0, sizeof(fl)); | |
847 | fl.l_whence = SEEK_SET; | |
848 | fl.l_start = sector_num << 9; | |
849 | fl.l_len = (int64_t)nb_sectors << 9; | |
850 | ||
851 | if (xfsctl(NULL, s->fd, XFS_IOC_UNRESVSP64, &fl) < 0) { | |
852 | DEBUG_BLOCK_PRINT("cannot punch hole (%s)\n", strerror(errno)); | |
853 | return -errno; | |
854 | } | |
855 | ||
856 | return 0; | |
857 | } | |
858 | #endif | |
859 | ||
860 | static int raw_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors) | |
861 | { | |
862 | #ifdef CONFIG_XFS | |
863 | BDRVRawState *s = bs->opaque; | |
864 | ||
865 | if (s->is_xfs) { | |
866 | return xfs_discard(s, sector_num, nb_sectors); | |
867 | } | |
868 | #endif | |
869 | ||
870 | return 0; | |
871 | } | |
0e7e1989 KW |
872 | |
873 | static QEMUOptionParameter raw_create_options[] = { | |
db08adf5 KW |
874 | { |
875 | .name = BLOCK_OPT_SIZE, | |
876 | .type = OPT_SIZE, | |
877 | .help = "Virtual disk size" | |
878 | }, | |
0e7e1989 KW |
879 | { NULL } |
880 | }; | |
881 | ||
84a12e66 CH |
882 | static BlockDriver bdrv_file = { |
883 | .format_name = "file", | |
884 | .protocol_name = "file", | |
856ae5c3 BS |
885 | .instance_size = sizeof(BDRVRawState), |
886 | .bdrv_probe = NULL, /* no probe for protocols */ | |
66f82cee | 887 | .bdrv_file_open = raw_open, |
856ae5c3 BS |
888 | .bdrv_read = raw_read, |
889 | .bdrv_write = raw_write, | |
890 | .bdrv_close = raw_close, | |
891 | .bdrv_create = raw_create, | |
892 | .bdrv_flush = raw_flush, | |
dce512de | 893 | .bdrv_discard = raw_discard, |
3b46e624 | 894 | |
f141eafe AL |
895 | .bdrv_aio_readv = raw_aio_readv, |
896 | .bdrv_aio_writev = raw_aio_writev, | |
b2e12bc6 | 897 | .bdrv_aio_flush = raw_aio_flush, |
3c529d93 | 898 | |
83f64091 FB |
899 | .bdrv_truncate = raw_truncate, |
900 | .bdrv_getlength = raw_getlength, | |
4a1d5e1f FZ |
901 | .bdrv_get_allocated_file_size |
902 | = raw_get_allocated_file_size, | |
0e7e1989 KW |
903 | |
904 | .create_options = raw_create_options, | |
83f64091 FB |
905 | }; |
906 | ||
19cb3738 FB |
907 | /***********************************************/ |
908 | /* host device */ | |
909 | ||
910 | #ifdef CONFIG_COCOA | |
911 | static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator ); | |
912 | static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize ); | |
913 | ||
914 | kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator ) | |
915 | { | |
5fafdf24 | 916 | kern_return_t kernResult; |
19cb3738 FB |
917 | mach_port_t masterPort; |
918 | CFMutableDictionaryRef classesToMatch; | |
919 | ||
920 | kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort ); | |
921 | if ( KERN_SUCCESS != kernResult ) { | |
922 | printf( "IOMasterPort returned %d\n", kernResult ); | |
923 | } | |
3b46e624 | 924 | |
5fafdf24 | 925 | classesToMatch = IOServiceMatching( kIOCDMediaClass ); |
19cb3738 FB |
926 | if ( classesToMatch == NULL ) { |
927 | printf( "IOServiceMatching returned a NULL dictionary.\n" ); | |
928 | } else { | |
929 | CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue ); | |
930 | } | |
931 | kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator ); | |
932 | if ( KERN_SUCCESS != kernResult ) | |
933 | { | |
934 | printf( "IOServiceGetMatchingServices returned %d\n", kernResult ); | |
935 | } | |
3b46e624 | 936 | |
19cb3738 FB |
937 | return kernResult; |
938 | } | |
939 | ||
940 | kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize ) | |
941 | { | |
942 | io_object_t nextMedia; | |
943 | kern_return_t kernResult = KERN_FAILURE; | |
944 | *bsdPath = '\0'; | |
945 | nextMedia = IOIteratorNext( mediaIterator ); | |
946 | if ( nextMedia ) | |
947 | { | |
948 | CFTypeRef bsdPathAsCFString; | |
949 | bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 ); | |
950 | if ( bsdPathAsCFString ) { | |
951 | size_t devPathLength; | |
952 | strcpy( bsdPath, _PATH_DEV ); | |
953 | strcat( bsdPath, "r" ); | |
954 | devPathLength = strlen( bsdPath ); | |
955 | if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) { | |
956 | kernResult = KERN_SUCCESS; | |
957 | } | |
958 | CFRelease( bsdPathAsCFString ); | |
959 | } | |
960 | IOObjectRelease( nextMedia ); | |
961 | } | |
3b46e624 | 962 | |
19cb3738 FB |
963 | return kernResult; |
964 | } | |
965 | ||
966 | #endif | |
967 | ||
508c7cb3 CH |
968 | static int hdev_probe_device(const char *filename) |
969 | { | |
970 | struct stat st; | |
971 | ||
972 | /* allow a dedicated CD-ROM driver to match with a higher priority */ | |
973 | if (strstart(filename, "/dev/cdrom", NULL)) | |
974 | return 50; | |
975 | ||
976 | if (stat(filename, &st) >= 0 && | |
977 | (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) { | |
978 | return 100; | |
979 | } | |
980 | ||
981 | return 0; | |
982 | } | |
983 | ||
19cb3738 FB |
984 | static int hdev_open(BlockDriverState *bs, const char *filename, int flags) |
985 | { | |
986 | BDRVRawState *s = bs->opaque; | |
a76bab49 | 987 | |
19cb3738 FB |
988 | #ifdef CONFIG_COCOA |
989 | if (strstart(filename, "/dev/cdrom", NULL)) { | |
990 | kern_return_t kernResult; | |
991 | io_iterator_t mediaIterator; | |
992 | char bsdPath[ MAXPATHLEN ]; | |
993 | int fd; | |
5fafdf24 | 994 | |
19cb3738 FB |
995 | kernResult = FindEjectableCDMedia( &mediaIterator ); |
996 | kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) ); | |
3b46e624 | 997 | |
19cb3738 FB |
998 | if ( bsdPath[ 0 ] != '\0' ) { |
999 | strcat(bsdPath,"s0"); | |
1000 | /* some CDs don't have a partition 0 */ | |
1001 | fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE); | |
1002 | if (fd < 0) { | |
1003 | bsdPath[strlen(bsdPath)-1] = '1'; | |
1004 | } else { | |
1005 | close(fd); | |
1006 | } | |
1007 | filename = bsdPath; | |
1008 | } | |
3b46e624 | 1009 | |
19cb3738 FB |
1010 | if ( mediaIterator ) |
1011 | IOObjectRelease( mediaIterator ); | |
1012 | } | |
1013 | #endif | |
19cb3738 FB |
1014 | |
1015 | s->type = FTYPE_FILE; | |
4dd75c70 | 1016 | #if defined(__linux__) |
05acda4d BK |
1017 | { |
1018 | char resolved_path[ MAXPATHLEN ], *temp; | |
1019 | ||
1020 | temp = realpath(filename, resolved_path); | |
1021 | if (temp && strstart(temp, "/dev/sg", NULL)) { | |
1022 | bs->sg = 1; | |
1023 | } | |
19cb3738 FB |
1024 | } |
1025 | #endif | |
90babde0 | 1026 | |
19a3da7f | 1027 | return raw_open_common(bs, filename, flags, 0); |
19cb3738 FB |
1028 | } |
1029 | ||
03ff3ca3 | 1030 | #if defined(__linux__) |
19cb3738 FB |
1031 | /* Note: we do not have a reliable method to detect if the floppy is |
1032 | present. The current method is to try to open the floppy at every | |
1033 | I/O and to keep it opened during a few hundreds of ms. */ | |
1034 | static int fd_open(BlockDriverState *bs) | |
1035 | { | |
1036 | BDRVRawState *s = bs->opaque; | |
1037 | int last_media_present; | |
1038 | ||
1039 | if (s->type != FTYPE_FD) | |
1040 | return 0; | |
1041 | last_media_present = (s->fd >= 0); | |
5fafdf24 | 1042 | if (s->fd >= 0 && |
c57c846a | 1043 | (get_clock() - s->fd_open_time) >= FD_OPEN_TIMEOUT) { |
19cb3738 FB |
1044 | close(s->fd); |
1045 | s->fd = -1; | |
1046 | #ifdef DEBUG_FLOPPY | |
1047 | printf("Floppy closed\n"); | |
1048 | #endif | |
1049 | } | |
1050 | if (s->fd < 0) { | |
5fafdf24 | 1051 | if (s->fd_got_error && |
c57c846a | 1052 | (get_clock() - s->fd_error_time) < FD_OPEN_TIMEOUT) { |
19cb3738 FB |
1053 | #ifdef DEBUG_FLOPPY |
1054 | printf("No floppy (open delayed)\n"); | |
1055 | #endif | |
1056 | return -EIO; | |
1057 | } | |
0e1d8f4c | 1058 | s->fd = open(bs->filename, s->open_flags & ~O_NONBLOCK); |
19cb3738 | 1059 | if (s->fd < 0) { |
c57c846a | 1060 | s->fd_error_time = get_clock(); |
19cb3738 FB |
1061 | s->fd_got_error = 1; |
1062 | if (last_media_present) | |
1063 | s->fd_media_changed = 1; | |
1064 | #ifdef DEBUG_FLOPPY | |
1065 | printf("No floppy\n"); | |
1066 | #endif | |
1067 | return -EIO; | |
1068 | } | |
1069 | #ifdef DEBUG_FLOPPY | |
1070 | printf("Floppy opened\n"); | |
1071 | #endif | |
1072 | } | |
1073 | if (!last_media_present) | |
1074 | s->fd_media_changed = 1; | |
c57c846a | 1075 | s->fd_open_time = get_clock(); |
19cb3738 FB |
1076 | s->fd_got_error = 0; |
1077 | return 0; | |
1078 | } | |
19cb3738 | 1079 | |
63ec93db | 1080 | static int hdev_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) |
985a03b0 TS |
1081 | { |
1082 | BDRVRawState *s = bs->opaque; | |
1083 | ||
1084 | return ioctl(s->fd, req, buf); | |
1085 | } | |
221f715d | 1086 | |
63ec93db | 1087 | static BlockDriverAIOCB *hdev_aio_ioctl(BlockDriverState *bs, |
221f715d AL |
1088 | unsigned long int req, void *buf, |
1089 | BlockDriverCompletionFunc *cb, void *opaque) | |
1090 | { | |
f141eafe | 1091 | BDRVRawState *s = bs->opaque; |
221f715d | 1092 | |
f141eafe AL |
1093 | if (fd_open(bs) < 0) |
1094 | return NULL; | |
9ef91a67 | 1095 | return paio_ioctl(bs, s->fd, req, buf, cb, opaque); |
221f715d AL |
1096 | } |
1097 | ||
a167ba50 | 1098 | #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
9f23011a BS |
1099 | static int fd_open(BlockDriverState *bs) |
1100 | { | |
1101 | BDRVRawState *s = bs->opaque; | |
1102 | ||
1103 | /* this is just to ensure s->fd is sane (its called by io ops) */ | |
1104 | if (s->fd >= 0) | |
1105 | return 0; | |
1106 | return -EIO; | |
1107 | } | |
9f23011a | 1108 | #else /* !linux && !FreeBSD */ |
19cb3738 | 1109 | |
08af02e2 AL |
1110 | static int fd_open(BlockDriverState *bs) |
1111 | { | |
1112 | return 0; | |
1113 | } | |
1114 | ||
221f715d | 1115 | #endif /* !linux && !FreeBSD */ |
04eeb8b6 | 1116 | |
0e7e1989 | 1117 | static int hdev_create(const char *filename, QEMUOptionParameter *options) |
93c65b47 AL |
1118 | { |
1119 | int fd; | |
1120 | int ret = 0; | |
1121 | struct stat stat_buf; | |
0e7e1989 | 1122 | int64_t total_size = 0; |
93c65b47 | 1123 | |
0e7e1989 KW |
1124 | /* Read out options */ |
1125 | while (options && options->name) { | |
1126 | if (!strcmp(options->name, "size")) { | |
9040385d | 1127 | total_size = options->value.n / BDRV_SECTOR_SIZE; |
0e7e1989 KW |
1128 | } |
1129 | options++; | |
1130 | } | |
93c65b47 AL |
1131 | |
1132 | fd = open(filename, O_WRONLY | O_BINARY); | |
1133 | if (fd < 0) | |
57e69b7d | 1134 | return -errno; |
93c65b47 AL |
1135 | |
1136 | if (fstat(fd, &stat_buf) < 0) | |
57e69b7d | 1137 | ret = -errno; |
4099df58 | 1138 | else if (!S_ISBLK(stat_buf.st_mode) && !S_ISCHR(stat_buf.st_mode)) |
57e69b7d | 1139 | ret = -ENODEV; |
9040385d | 1140 | else if (lseek(fd, 0, SEEK_END) < total_size * BDRV_SECTOR_SIZE) |
93c65b47 AL |
1141 | ret = -ENOSPC; |
1142 | ||
1143 | close(fd); | |
1144 | return ret; | |
1145 | } | |
1146 | ||
336c1c12 KW |
1147 | static int hdev_has_zero_init(BlockDriverState *bs) |
1148 | { | |
1149 | return 0; | |
1150 | } | |
1151 | ||
5efa9d5a | 1152 | static BlockDriver bdrv_host_device = { |
0b4ce02e | 1153 | .format_name = "host_device", |
84a12e66 | 1154 | .protocol_name = "host_device", |
0b4ce02e KW |
1155 | .instance_size = sizeof(BDRVRawState), |
1156 | .bdrv_probe_device = hdev_probe_device, | |
66f82cee | 1157 | .bdrv_file_open = hdev_open, |
0b4ce02e | 1158 | .bdrv_close = raw_close, |
93c65b47 | 1159 | .bdrv_create = hdev_create, |
0b4ce02e | 1160 | .create_options = raw_create_options, |
336c1c12 | 1161 | .bdrv_has_zero_init = hdev_has_zero_init, |
0b4ce02e | 1162 | .bdrv_flush = raw_flush, |
3b46e624 | 1163 | |
f141eafe AL |
1164 | .bdrv_aio_readv = raw_aio_readv, |
1165 | .bdrv_aio_writev = raw_aio_writev, | |
b2e12bc6 | 1166 | .bdrv_aio_flush = raw_aio_flush, |
3c529d93 | 1167 | |
eda578e5 AL |
1168 | .bdrv_read = raw_read, |
1169 | .bdrv_write = raw_write, | |
e60f469c | 1170 | .bdrv_getlength = raw_getlength, |
4a1d5e1f FZ |
1171 | .bdrv_get_allocated_file_size |
1172 | = raw_get_allocated_file_size, | |
19cb3738 | 1173 | |
f3a5d3f8 | 1174 | /* generic scsi device */ |
63ec93db CH |
1175 | #ifdef __linux__ |
1176 | .bdrv_ioctl = hdev_ioctl, | |
63ec93db CH |
1177 | .bdrv_aio_ioctl = hdev_aio_ioctl, |
1178 | #endif | |
f3a5d3f8 CH |
1179 | }; |
1180 | ||
1181 | #ifdef __linux__ | |
1182 | static int floppy_open(BlockDriverState *bs, const char *filename, int flags) | |
1183 | { | |
1184 | BDRVRawState *s = bs->opaque; | |
1185 | int ret; | |
1186 | ||
f3a5d3f8 | 1187 | s->type = FTYPE_FD; |
f3a5d3f8 | 1188 | |
19a3da7f BS |
1189 | /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */ |
1190 | ret = raw_open_common(bs, filename, flags, O_NONBLOCK); | |
f3a5d3f8 CH |
1191 | if (ret) |
1192 | return ret; | |
1193 | ||
1194 | /* close fd so that we can reopen it as needed */ | |
1195 | close(s->fd); | |
1196 | s->fd = -1; | |
1197 | s->fd_media_changed = 1; | |
1198 | ||
1199 | return 0; | |
1200 | } | |
1201 | ||
508c7cb3 CH |
1202 | static int floppy_probe_device(const char *filename) |
1203 | { | |
2ebf7c4b CR |
1204 | int fd, ret; |
1205 | int prio = 0; | |
1206 | struct floppy_struct fdparam; | |
343f8568 | 1207 | struct stat st; |
2ebf7c4b | 1208 | |
508c7cb3 | 1209 | if (strstart(filename, "/dev/fd", NULL)) |
2ebf7c4b CR |
1210 | prio = 50; |
1211 | ||
1212 | fd = open(filename, O_RDONLY | O_NONBLOCK); | |
1213 | if (fd < 0) { | |
1214 | goto out; | |
1215 | } | |
343f8568 JS |
1216 | ret = fstat(fd, &st); |
1217 | if (ret == -1 || !S_ISBLK(st.st_mode)) { | |
1218 | goto outc; | |
1219 | } | |
2ebf7c4b CR |
1220 | |
1221 | /* Attempt to detect via a floppy specific ioctl */ | |
1222 | ret = ioctl(fd, FDGETPRM, &fdparam); | |
1223 | if (ret >= 0) | |
1224 | prio = 100; | |
1225 | ||
343f8568 | 1226 | outc: |
2ebf7c4b CR |
1227 | close(fd); |
1228 | out: | |
1229 | return prio; | |
508c7cb3 CH |
1230 | } |
1231 | ||
1232 | ||
f3a5d3f8 CH |
1233 | static int floppy_is_inserted(BlockDriverState *bs) |
1234 | { | |
1235 | return fd_open(bs) >= 0; | |
1236 | } | |
1237 | ||
1238 | static int floppy_media_changed(BlockDriverState *bs) | |
1239 | { | |
1240 | BDRVRawState *s = bs->opaque; | |
1241 | int ret; | |
1242 | ||
1243 | /* | |
1244 | * XXX: we do not have a true media changed indication. | |
1245 | * It does not work if the floppy is changed without trying to read it. | |
1246 | */ | |
1247 | fd_open(bs); | |
1248 | ret = s->fd_media_changed; | |
1249 | s->fd_media_changed = 0; | |
1250 | #ifdef DEBUG_FLOPPY | |
1251 | printf("Floppy changed=%d\n", ret); | |
1252 | #endif | |
1253 | return ret; | |
1254 | } | |
1255 | ||
822e1cd1 | 1256 | static void floppy_eject(BlockDriverState *bs, int eject_flag) |
f3a5d3f8 CH |
1257 | { |
1258 | BDRVRawState *s = bs->opaque; | |
1259 | int fd; | |
1260 | ||
1261 | if (s->fd >= 0) { | |
1262 | close(s->fd); | |
1263 | s->fd = -1; | |
1264 | } | |
1265 | fd = open(bs->filename, s->open_flags | O_NONBLOCK); | |
1266 | if (fd >= 0) { | |
1267 | if (ioctl(fd, FDEJECT, 0) < 0) | |
1268 | perror("FDEJECT"); | |
1269 | close(fd); | |
1270 | } | |
f3a5d3f8 CH |
1271 | } |
1272 | ||
1273 | static BlockDriver bdrv_host_floppy = { | |
1274 | .format_name = "host_floppy", | |
84a12e66 | 1275 | .protocol_name = "host_floppy", |
f3a5d3f8 | 1276 | .instance_size = sizeof(BDRVRawState), |
508c7cb3 | 1277 | .bdrv_probe_device = floppy_probe_device, |
66f82cee | 1278 | .bdrv_file_open = floppy_open, |
f3a5d3f8 CH |
1279 | .bdrv_close = raw_close, |
1280 | .bdrv_create = hdev_create, | |
0b4ce02e | 1281 | .create_options = raw_create_options, |
336c1c12 | 1282 | .bdrv_has_zero_init = hdev_has_zero_init, |
f3a5d3f8 CH |
1283 | .bdrv_flush = raw_flush, |
1284 | ||
f3a5d3f8 CH |
1285 | .bdrv_aio_readv = raw_aio_readv, |
1286 | .bdrv_aio_writev = raw_aio_writev, | |
b2e12bc6 | 1287 | .bdrv_aio_flush = raw_aio_flush, |
f3a5d3f8 CH |
1288 | |
1289 | .bdrv_read = raw_read, | |
1290 | .bdrv_write = raw_write, | |
1291 | .bdrv_getlength = raw_getlength, | |
4a1d5e1f FZ |
1292 | .bdrv_get_allocated_file_size |
1293 | = raw_get_allocated_file_size, | |
f3a5d3f8 CH |
1294 | |
1295 | /* removable device support */ | |
1296 | .bdrv_is_inserted = floppy_is_inserted, | |
1297 | .bdrv_media_changed = floppy_media_changed, | |
1298 | .bdrv_eject = floppy_eject, | |
f3a5d3f8 CH |
1299 | }; |
1300 | ||
1301 | static int cdrom_open(BlockDriverState *bs, const char *filename, int flags) | |
1302 | { | |
1303 | BDRVRawState *s = bs->opaque; | |
1304 | ||
f3a5d3f8 CH |
1305 | s->type = FTYPE_CD; |
1306 | ||
19a3da7f BS |
1307 | /* open will not fail even if no CD is inserted, so add O_NONBLOCK */ |
1308 | return raw_open_common(bs, filename, flags, O_NONBLOCK); | |
f3a5d3f8 CH |
1309 | } |
1310 | ||
508c7cb3 CH |
1311 | static int cdrom_probe_device(const char *filename) |
1312 | { | |
3baf720e CR |
1313 | int fd, ret; |
1314 | int prio = 0; | |
343f8568 | 1315 | struct stat st; |
3baf720e | 1316 | |
3baf720e CR |
1317 | fd = open(filename, O_RDONLY | O_NONBLOCK); |
1318 | if (fd < 0) { | |
1319 | goto out; | |
1320 | } | |
343f8568 JS |
1321 | ret = fstat(fd, &st); |
1322 | if (ret == -1 || !S_ISBLK(st.st_mode)) { | |
1323 | goto outc; | |
1324 | } | |
3baf720e CR |
1325 | |
1326 | /* Attempt to detect via a CDROM specific ioctl */ | |
1327 | ret = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT); | |
1328 | if (ret >= 0) | |
1329 | prio = 100; | |
1330 | ||
343f8568 | 1331 | outc: |
3baf720e CR |
1332 | close(fd); |
1333 | out: | |
1334 | return prio; | |
508c7cb3 CH |
1335 | } |
1336 | ||
f3a5d3f8 CH |
1337 | static int cdrom_is_inserted(BlockDriverState *bs) |
1338 | { | |
1339 | BDRVRawState *s = bs->opaque; | |
1340 | int ret; | |
1341 | ||
1342 | ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT); | |
1343 | if (ret == CDS_DISC_OK) | |
1344 | return 1; | |
1345 | return 0; | |
1346 | } | |
1347 | ||
822e1cd1 | 1348 | static void cdrom_eject(BlockDriverState *bs, int eject_flag) |
f3a5d3f8 CH |
1349 | { |
1350 | BDRVRawState *s = bs->opaque; | |
1351 | ||
1352 | if (eject_flag) { | |
1353 | if (ioctl(s->fd, CDROMEJECT, NULL) < 0) | |
1354 | perror("CDROMEJECT"); | |
1355 | } else { | |
1356 | if (ioctl(s->fd, CDROMCLOSETRAY, NULL) < 0) | |
1357 | perror("CDROMEJECT"); | |
1358 | } | |
f3a5d3f8 CH |
1359 | } |
1360 | ||
7bf37fed | 1361 | static void cdrom_set_locked(BlockDriverState *bs, int locked) |
f3a5d3f8 CH |
1362 | { |
1363 | BDRVRawState *s = bs->opaque; | |
1364 | ||
1365 | if (ioctl(s->fd, CDROM_LOCKDOOR, locked) < 0) { | |
1366 | /* | |
1367 | * Note: an error can happen if the distribution automatically | |
1368 | * mounts the CD-ROM | |
1369 | */ | |
1370 | /* perror("CDROM_LOCKDOOR"); */ | |
1371 | } | |
f3a5d3f8 CH |
1372 | } |
1373 | ||
1374 | static BlockDriver bdrv_host_cdrom = { | |
1375 | .format_name = "host_cdrom", | |
84a12e66 | 1376 | .protocol_name = "host_cdrom", |
f3a5d3f8 | 1377 | .instance_size = sizeof(BDRVRawState), |
508c7cb3 | 1378 | .bdrv_probe_device = cdrom_probe_device, |
66f82cee | 1379 | .bdrv_file_open = cdrom_open, |
f3a5d3f8 CH |
1380 | .bdrv_close = raw_close, |
1381 | .bdrv_create = hdev_create, | |
0b4ce02e | 1382 | .create_options = raw_create_options, |
336c1c12 | 1383 | .bdrv_has_zero_init = hdev_has_zero_init, |
f3a5d3f8 CH |
1384 | .bdrv_flush = raw_flush, |
1385 | ||
f3a5d3f8 CH |
1386 | .bdrv_aio_readv = raw_aio_readv, |
1387 | .bdrv_aio_writev = raw_aio_writev, | |
b2e12bc6 | 1388 | .bdrv_aio_flush = raw_aio_flush, |
f3a5d3f8 CH |
1389 | |
1390 | .bdrv_read = raw_read, | |
1391 | .bdrv_write = raw_write, | |
1392 | .bdrv_getlength = raw_getlength, | |
4a1d5e1f FZ |
1393 | .bdrv_get_allocated_file_size |
1394 | = raw_get_allocated_file_size, | |
f3a5d3f8 CH |
1395 | |
1396 | /* removable device support */ | |
1397 | .bdrv_is_inserted = cdrom_is_inserted, | |
1398 | .bdrv_eject = cdrom_eject, | |
1399 | .bdrv_set_locked = cdrom_set_locked, | |
1400 | ||
1401 | /* generic scsi device */ | |
63ec93db | 1402 | .bdrv_ioctl = hdev_ioctl, |
63ec93db | 1403 | .bdrv_aio_ioctl = hdev_aio_ioctl, |
f3a5d3f8 CH |
1404 | }; |
1405 | #endif /* __linux__ */ | |
1406 | ||
a167ba50 | 1407 | #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__) |
f3a5d3f8 CH |
1408 | static int cdrom_open(BlockDriverState *bs, const char *filename, int flags) |
1409 | { | |
1410 | BDRVRawState *s = bs->opaque; | |
1411 | int ret; | |
1412 | ||
1413 | s->type = FTYPE_CD; | |
1414 | ||
19a3da7f | 1415 | ret = raw_open_common(bs, filename, flags, 0); |
f3a5d3f8 CH |
1416 | if (ret) |
1417 | return ret; | |
1418 | ||
1419 | /* make sure the door isnt locked at this time */ | |
1420 | ioctl(s->fd, CDIOCALLOW); | |
1421 | return 0; | |
1422 | } | |
1423 | ||
508c7cb3 CH |
1424 | static int cdrom_probe_device(const char *filename) |
1425 | { | |
1426 | if (strstart(filename, "/dev/cd", NULL) || | |
1427 | strstart(filename, "/dev/acd", NULL)) | |
1428 | return 100; | |
1429 | return 0; | |
1430 | } | |
1431 | ||
f3a5d3f8 CH |
1432 | static int cdrom_reopen(BlockDriverState *bs) |
1433 | { | |
1434 | BDRVRawState *s = bs->opaque; | |
1435 | int fd; | |
1436 | ||
1437 | /* | |
1438 | * Force reread of possibly changed/newly loaded disc, | |
1439 | * FreeBSD seems to not notice sometimes... | |
1440 | */ | |
1441 | if (s->fd >= 0) | |
1442 | close(s->fd); | |
1443 | fd = open(bs->filename, s->open_flags, 0644); | |
1444 | if (fd < 0) { | |
1445 | s->fd = -1; | |
1446 | return -EIO; | |
1447 | } | |
1448 | s->fd = fd; | |
1449 | ||
1450 | /* make sure the door isnt locked at this time */ | |
1451 | ioctl(s->fd, CDIOCALLOW); | |
1452 | return 0; | |
1453 | } | |
1454 | ||
1455 | static int cdrom_is_inserted(BlockDriverState *bs) | |
1456 | { | |
1457 | return raw_getlength(bs) > 0; | |
1458 | } | |
1459 | ||
822e1cd1 | 1460 | static void cdrom_eject(BlockDriverState *bs, int eject_flag) |
f3a5d3f8 CH |
1461 | { |
1462 | BDRVRawState *s = bs->opaque; | |
1463 | ||
1464 | if (s->fd < 0) | |
822e1cd1 | 1465 | return; |
f3a5d3f8 CH |
1466 | |
1467 | (void) ioctl(s->fd, CDIOCALLOW); | |
1468 | ||
1469 | if (eject_flag) { | |
1470 | if (ioctl(s->fd, CDIOCEJECT) < 0) | |
1471 | perror("CDIOCEJECT"); | |
1472 | } else { | |
1473 | if (ioctl(s->fd, CDIOCCLOSE) < 0) | |
1474 | perror("CDIOCCLOSE"); | |
1475 | } | |
1476 | ||
822e1cd1 | 1477 | cdrom_reopen(bs); |
f3a5d3f8 CH |
1478 | } |
1479 | ||
7bf37fed | 1480 | static void cdrom_set_locked(BlockDriverState *bs, int locked) |
f3a5d3f8 CH |
1481 | { |
1482 | BDRVRawState *s = bs->opaque; | |
1483 | ||
1484 | if (s->fd < 0) | |
7bf37fed | 1485 | return; |
f3a5d3f8 CH |
1486 | if (ioctl(s->fd, (locked ? CDIOCPREVENT : CDIOCALLOW)) < 0) { |
1487 | /* | |
1488 | * Note: an error can happen if the distribution automatically | |
1489 | * mounts the CD-ROM | |
1490 | */ | |
1491 | /* perror("CDROM_LOCKDOOR"); */ | |
1492 | } | |
f3a5d3f8 CH |
1493 | } |
1494 | ||
1495 | static BlockDriver bdrv_host_cdrom = { | |
1496 | .format_name = "host_cdrom", | |
84a12e66 | 1497 | .protocol_name = "host_cdrom", |
f3a5d3f8 | 1498 | .instance_size = sizeof(BDRVRawState), |
508c7cb3 | 1499 | .bdrv_probe_device = cdrom_probe_device, |
66f82cee | 1500 | .bdrv_file_open = cdrom_open, |
f3a5d3f8 CH |
1501 | .bdrv_close = raw_close, |
1502 | .bdrv_create = hdev_create, | |
0b4ce02e | 1503 | .create_options = raw_create_options, |
336c1c12 | 1504 | .bdrv_has_zero_init = hdev_has_zero_init, |
f3a5d3f8 CH |
1505 | .bdrv_flush = raw_flush, |
1506 | ||
f3a5d3f8 CH |
1507 | .bdrv_aio_readv = raw_aio_readv, |
1508 | .bdrv_aio_writev = raw_aio_writev, | |
b2e12bc6 | 1509 | .bdrv_aio_flush = raw_aio_flush, |
f3a5d3f8 CH |
1510 | |
1511 | .bdrv_read = raw_read, | |
1512 | .bdrv_write = raw_write, | |
1513 | .bdrv_getlength = raw_getlength, | |
4a1d5e1f FZ |
1514 | .bdrv_get_allocated_file_size |
1515 | = raw_get_allocated_file_size, | |
f3a5d3f8 | 1516 | |
19cb3738 | 1517 | /* removable device support */ |
f3a5d3f8 CH |
1518 | .bdrv_is_inserted = cdrom_is_inserted, |
1519 | .bdrv_eject = cdrom_eject, | |
1520 | .bdrv_set_locked = cdrom_set_locked, | |
19cb3738 | 1521 | }; |
f3a5d3f8 | 1522 | #endif /* __FreeBSD__ */ |
5efa9d5a | 1523 | |
84a12e66 | 1524 | static void bdrv_file_init(void) |
5efa9d5a | 1525 | { |
508c7cb3 CH |
1526 | /* |
1527 | * Register all the drivers. Note that order is important, the driver | |
1528 | * registered last will get probed first. | |
1529 | */ | |
84a12e66 | 1530 | bdrv_register(&bdrv_file); |
5efa9d5a | 1531 | bdrv_register(&bdrv_host_device); |
f3a5d3f8 CH |
1532 | #ifdef __linux__ |
1533 | bdrv_register(&bdrv_host_floppy); | |
1534 | bdrv_register(&bdrv_host_cdrom); | |
1535 | #endif | |
a167ba50 | 1536 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
f3a5d3f8 CH |
1537 | bdrv_register(&bdrv_host_cdrom); |
1538 | #endif | |
5efa9d5a AL |
1539 | } |
1540 | ||
84a12e66 | 1541 | block_init(bdrv_file_init); |