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