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