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