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