]> Git Repo - qemu.git/blame - block-raw-posix.c
Add "cache" parameter to "-drive" (Laurent Vivier).
[qemu.git] / block-raw-posix.c
CommitLineData
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
PB
25#ifndef QEMU_IMG
26#include "qemu-timer.h"
ae5fc450 27#include "exec-all.h"
faf07963 28#endif
83f64091
FB
29#include "block_int.h"
30#include <assert.h>
83f64091
FB
31#include <aio.h>
32
83f64091
FB
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__
2e9671da
TS
46#define _POSIX_PTHREAD_SEMANTICS 1
47#include <signal.h>
83f64091
FB
48#include <sys/dkio.h>
49#endif
19cb3738
FB
50#ifdef __linux__
51#include <sys/ioctl.h>
52#include <linux/cdrom.h>
53#include <linux/fd.h>
54#endif
1cb6c3fd
TS
55#ifdef __FreeBSD__
56#include <sys/disk.h>
57#endif
83f64091 58
19cb3738 59//#define DEBUG_FLOPPY
83f64091 60
faf07963
PB
61//#define DEBUG_BLOCK
62#if defined(DEBUG_BLOCK) && !defined(QEMU_IMG)
a50a6282 63#define DEBUG_BLOCK_PRINT(formatCstr, args...) do { if (loglevel != 0) \
2e03286b 64 { fprintf(logfile, formatCstr, ##args); fflush(logfile); } } while (0)
8c05dbf9
TS
65#else
66#define DEBUG_BLOCK_PRINT(formatCstr, args...)
67#endif
68
19cb3738
FB
69#define FTYPE_FILE 0
70#define FTYPE_CD 1
71#define FTYPE_FD 2
83f64091 72
19cb3738
FB
73/* if the FD is not accessed during that time (in ms), we try to
74 reopen it to see if the disk has been changed */
75#define FD_OPEN_TIMEOUT 1000
83f64091 76
19cb3738
FB
77typedef struct BDRVRawState {
78 int fd;
79 int type;
8c05dbf9 80 unsigned int lseek_err_cnt;
19cb3738
FB
81#if defined(__linux__)
82 /* linux floppy specific */
83 int fd_open_flags;
84 int64_t fd_open_time;
85 int64_t fd_error_time;
86 int fd_got_error;
87 int fd_media_changed;
83f64091 88#endif
19cb3738
FB
89} BDRVRawState;
90
91static int fd_open(BlockDriverState *bs);
83f64091
FB
92
93static int raw_open(BlockDriverState *bs, const char *filename, int flags)
94{
95 BDRVRawState *s = bs->opaque;
19cb3738 96 int fd, open_flags, ret;
83f64091 97
8c05dbf9
TS
98 s->lseek_err_cnt = 0;
99
83f64091
FB
100 open_flags = O_BINARY;
101 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
102 open_flags |= O_RDWR;
103 } else {
104 open_flags |= O_RDONLY;
105 bs->read_only = 1;
106 }
107 if (flags & BDRV_O_CREAT)
108 open_flags |= O_CREAT | O_TRUNC;
33f00271
AZ
109#ifdef O_DIRECT
110 if (flags & BDRV_O_DIRECT)
111 open_flags |= O_DIRECT;
112#endif
83f64091 113
19cb3738
FB
114 s->type = FTYPE_FILE;
115
83f64091 116 fd = open(filename, open_flags, 0644);
19cb3738
FB
117 if (fd < 0) {
118 ret = -errno;
119 if (ret == -EROFS)
120 ret = -EACCES;
121 return ret;
122 }
83f64091
FB
123 s->fd = fd;
124 return 0;
125}
126
127/* XXX: use host sector size if necessary with:
128#ifdef DIOCGSECTORSIZE
129 {
130 unsigned int sectorsize = 512;
131 if (!ioctl(fd, DIOCGSECTORSIZE, &sectorsize) &&
132 sectorsize > bufsize)
133 bufsize = sectorsize;
134 }
135#endif
136#ifdef CONFIG_COCOA
137 u_int32_t blockSize = 512;
138 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
139 bufsize = blockSize;
140 }
141#endif
142*/
143
5fafdf24 144static int raw_pread(BlockDriverState *bs, int64_t offset,
83f64091
FB
145 uint8_t *buf, int count)
146{
147 BDRVRawState *s = bs->opaque;
148 int ret;
3b46e624 149
19cb3738
FB
150 ret = fd_open(bs);
151 if (ret < 0)
152 return ret;
153
8c05dbf9
TS
154 if (lseek(s->fd, offset, SEEK_SET) == (off_t)-1) {
155 ++(s->lseek_err_cnt);
156 if(s->lseek_err_cnt <= 10) {
92868412
JM
157 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
158 "] lseek failed : %d = %s\n",
8c05dbf9
TS
159 s->fd, bs->filename, offset, buf, count,
160 bs->total_sectors, errno, strerror(errno));
161 }
162 return -1;
163 }
164 s->lseek_err_cnt=0;
165
83f64091 166 ret = read(s->fd, buf, count);
8c05dbf9
TS
167 if (ret == count)
168 goto label__raw_read__success;
169
92868412
JM
170 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
171 "] read failed %d : %d = %s\n",
8c05dbf9
TS
172 s->fd, bs->filename, offset, buf, count,
173 bs->total_sectors, ret, errno, strerror(errno));
174
175 /* Try harder for CDrom. */
176 if (bs->type == BDRV_TYPE_CDROM) {
177 lseek(s->fd, offset, SEEK_SET);
178 ret = read(s->fd, buf, count);
179 if (ret == count)
180 goto label__raw_read__success;
181 lseek(s->fd, offset, SEEK_SET);
182 ret = read(s->fd, buf, count);
183 if (ret == count)
184 goto label__raw_read__success;
185
92868412
JM
186 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
187 "] retry read failed %d : %d = %s\n",
8c05dbf9
TS
188 s->fd, bs->filename, offset, buf, count,
189 bs->total_sectors, ret, errno, strerror(errno));
190 }
191
8c05dbf9
TS
192label__raw_read__success:
193
83f64091
FB
194 return ret;
195}
196
5fafdf24 197static int raw_pwrite(BlockDriverState *bs, int64_t offset,
83f64091
FB
198 const uint8_t *buf, int count)
199{
200 BDRVRawState *s = bs->opaque;
201 int ret;
3b46e624 202
19cb3738
FB
203 ret = fd_open(bs);
204 if (ret < 0)
205 return ret;
206
8c05dbf9
TS
207 if (lseek(s->fd, offset, SEEK_SET) == (off_t)-1) {
208 ++(s->lseek_err_cnt);
209 if(s->lseek_err_cnt) {
92868412
JM
210 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%"
211 PRId64 "] lseek failed : %d = %s\n",
8c05dbf9
TS
212 s->fd, bs->filename, offset, buf, count,
213 bs->total_sectors, errno, strerror(errno));
214 }
215 return -1;
216 }
217 s->lseek_err_cnt = 0;
218
83f64091 219 ret = write(s->fd, buf, count);
8c05dbf9
TS
220 if (ret == count)
221 goto label__raw_write__success;
222
92868412
JM
223 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
224 "] write failed %d : %d = %s\n",
8c05dbf9
TS
225 s->fd, bs->filename, offset, buf, count,
226 bs->total_sectors, ret, errno, strerror(errno));
227
8c05dbf9
TS
228label__raw_write__success:
229
83f64091
FB
230 return ret;
231}
232
233/***********************************************************/
19cb3738 234/* Unix AIO using POSIX AIO */
83f64091
FB
235
236typedef struct RawAIOCB {
ce1a14dc 237 BlockDriverAIOCB common;
83f64091 238 struct aiocb aiocb;
ce1a14dc 239 struct RawAIOCB *next;
83f64091
FB
240} RawAIOCB;
241
242static int aio_sig_num = SIGUSR2;
ce1a14dc 243static RawAIOCB *first_aio; /* AIO issued */
979b67ad 244static int aio_initialized = 0;
83f64091 245
83f64091
FB
246static void aio_signal_handler(int signum)
247{
faf07963 248#ifndef QEMU_IMG
83f64091
FB
249 CPUState *env = cpu_single_env;
250 if (env) {
251 /* stop the currently executing cpu because a timer occured */
252 cpu_interrupt(env, CPU_INTERRUPT_EXIT);
253#ifdef USE_KQEMU
254 if (env->kqemu_enabled) {
255 kqemu_cpu_interrupt(env);
256 }
257#endif
258 }
979b67ad 259#endif
83f64091
FB
260}
261
262void qemu_aio_init(void)
263{
264 struct sigaction act;
979b67ad
FB
265
266 aio_initialized = 1;
3b46e624 267
83f64091
FB
268 sigfillset(&act.sa_mask);
269 act.sa_flags = 0; /* do not restart syscalls to interrupt select() */
270 act.sa_handler = aio_signal_handler;
271 sigaction(aio_sig_num, &act, NULL);
272
19cb3738 273#if defined(__GLIBC__) && defined(__linux__)
83f64091 274 {
19cb3738
FB
275 /* XXX: aio thread exit seems to hang on RedHat 9 and this init
276 seems to fix the problem. */
83f64091
FB
277 struct aioinit ai;
278 memset(&ai, 0, sizeof(ai));
19cb3738 279 ai.aio_threads = 1;
83f64091
FB
280 ai.aio_num = 1;
281 ai.aio_idle_time = 365 * 100000;
282 aio_init(&ai);
283 }
19cb3738 284#endif
83f64091 285}
83f64091
FB
286
287void qemu_aio_poll(void)
288{
ce1a14dc 289 RawAIOCB *acb, **pacb;
83f64091
FB
290 int ret;
291
292 for(;;) {
293 pacb = &first_aio;
294 for(;;) {
295 acb = *pacb;
296 if (!acb)
297 goto the_end;
ce1a14dc 298 ret = aio_error(&acb->aiocb);
83f64091
FB
299 if (ret == ECANCELED) {
300 /* remove the request */
ce1a14dc
PB
301 *pacb = acb->next;
302 qemu_aio_release(acb);
83f64091
FB
303 } else if (ret != EINPROGRESS) {
304 /* end of aio */
305 if (ret == 0) {
ce1a14dc
PB
306 ret = aio_return(&acb->aiocb);
307 if (ret == acb->aiocb.aio_nbytes)
83f64091
FB
308 ret = 0;
309 else
19cb3738 310 ret = -EINVAL;
83f64091
FB
311 } else {
312 ret = -ret;
313 }
314 /* remove the request */
ce1a14dc 315 *pacb = acb->next;
83f64091 316 /* call the callback */
ce1a14dc
PB
317 acb->common.cb(acb->common.opaque, ret);
318 qemu_aio_release(acb);
83f64091
FB
319 break;
320 } else {
ce1a14dc 321 pacb = &acb->next;
83f64091
FB
322 }
323 }
324 }
325 the_end: ;
326}
327
6192bc37
PB
328/* Wait for all IO requests to complete. */
329void qemu_aio_flush(void)
330{
331 qemu_aio_wait_start();
332 qemu_aio_poll();
333 while (first_aio) {
334 qemu_aio_wait();
335 }
336 qemu_aio_wait_end();
337}
338
83f64091
FB
339/* wait until at least one AIO was handled */
340static sigset_t wait_oset;
341
342void qemu_aio_wait_start(void)
343{
344 sigset_t set;
979b67ad
FB
345
346 if (!aio_initialized)
347 qemu_aio_init();
83f64091
FB
348 sigemptyset(&set);
349 sigaddset(&set, aio_sig_num);
350 sigprocmask(SIG_BLOCK, &set, &wait_oset);
351}
352
353void qemu_aio_wait(void)
354{
355 sigset_t set;
356 int nb_sigs;
6eb5733a 357
faf07963 358#ifndef QEMU_IMG
6eb5733a
FB
359 if (qemu_bh_poll())
360 return;
361#endif
83f64091
FB
362 sigemptyset(&set);
363 sigaddset(&set, aio_sig_num);
364 sigwait(&set, &nb_sigs);
365 qemu_aio_poll();
366}
367
368void qemu_aio_wait_end(void)
369{
370 sigprocmask(SIG_SETMASK, &wait_oset, NULL);
371}
372
ce1a14dc
PB
373static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
374 int64_t sector_num, uint8_t *buf, int nb_sectors,
375 BlockDriverCompletionFunc *cb, void *opaque)
83f64091 376{
ce1a14dc
PB
377 BDRVRawState *s = bs->opaque;
378 RawAIOCB *acb;
379
19cb3738
FB
380 if (fd_open(bs) < 0)
381 return NULL;
382
ce1a14dc
PB
383 acb = qemu_aio_get(bs, cb, opaque);
384 if (!acb)
385 return NULL;
386 acb->aiocb.aio_fildes = s->fd;
387 acb->aiocb.aio_sigevent.sigev_signo = aio_sig_num;
388 acb->aiocb.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
389 acb->aiocb.aio_buf = buf;
390 acb->aiocb.aio_nbytes = nb_sectors * 512;
391 acb->aiocb.aio_offset = sector_num * 512;
392 acb->next = first_aio;
393 first_aio = acb;
394 return acb;
83f64091
FB
395}
396
ce1a14dc
PB
397static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
398 int64_t sector_num, uint8_t *buf, int nb_sectors,
399 BlockDriverCompletionFunc *cb, void *opaque)
83f64091 400{
ce1a14dc 401 RawAIOCB *acb;
83f64091 402
ce1a14dc
PB
403 acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
404 if (!acb)
405 return NULL;
406 if (aio_read(&acb->aiocb) < 0) {
407 qemu_aio_release(acb);
408 return NULL;
5fafdf24 409 }
ce1a14dc 410 return &acb->common;
83f64091
FB
411}
412
ce1a14dc
PB
413static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
414 int64_t sector_num, const uint8_t *buf, int nb_sectors,
415 BlockDriverCompletionFunc *cb, void *opaque)
83f64091 416{
ce1a14dc 417 RawAIOCB *acb;
83f64091 418
ce1a14dc
PB
419 acb = raw_aio_setup(bs, sector_num, (uint8_t*)buf, nb_sectors, cb, opaque);
420 if (!acb)
421 return NULL;
422 if (aio_write(&acb->aiocb) < 0) {
423 qemu_aio_release(acb);
424 return NULL;
5fafdf24 425 }
ce1a14dc 426 return &acb->common;
83f64091
FB
427}
428
ce1a14dc 429static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
83f64091 430{
83f64091 431 int ret;
ce1a14dc
PB
432 RawAIOCB *acb = (RawAIOCB *)blockacb;
433 RawAIOCB **pacb;
83f64091 434
ce1a14dc 435 ret = aio_cancel(acb->aiocb.aio_fildes, &acb->aiocb);
83f64091
FB
436 if (ret == AIO_NOTCANCELED) {
437 /* fail safe: if the aio could not be canceled, we wait for
438 it */
ce1a14dc 439 while (aio_error(&acb->aiocb) == EINPROGRESS);
83f64091
FB
440 }
441
442 /* remove the callback from the queue */
443 pacb = &first_aio;
444 for(;;) {
445 if (*pacb == NULL) {
446 break;
447 } else if (*pacb == acb) {
ce1a14dc
PB
448 *pacb = acb->next;
449 qemu_aio_release(acb);
83f64091
FB
450 break;
451 }
ce1a14dc 452 pacb = &acb->next;
83f64091
FB
453 }
454}
455
83f64091
FB
456static void raw_close(BlockDriverState *bs)
457{
458 BDRVRawState *s = bs->opaque;
19cb3738
FB
459 if (s->fd >= 0) {
460 close(s->fd);
461 s->fd = -1;
462 }
83f64091
FB
463}
464
465static int raw_truncate(BlockDriverState *bs, int64_t offset)
466{
467 BDRVRawState *s = bs->opaque;
19cb3738
FB
468 if (s->type != FTYPE_FILE)
469 return -ENOTSUP;
83f64091
FB
470 if (ftruncate(s->fd, offset) < 0)
471 return -errno;
472 return 0;
473}
474
475static int64_t raw_getlength(BlockDriverState *bs)
476{
477 BDRVRawState *s = bs->opaque;
478 int fd = s->fd;
479 int64_t size;
480#ifdef _BSD
481 struct stat sb;
482#endif
483#ifdef __sun__
484 struct dk_minfo minfo;
485 int rv;
486#endif
19cb3738
FB
487 int ret;
488
489 ret = fd_open(bs);
490 if (ret < 0)
491 return ret;
83f64091
FB
492
493#ifdef _BSD
494 if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
495#ifdef DIOCGMEDIASIZE
496 if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
497#endif
498#ifdef CONFIG_COCOA
499 size = LONG_LONG_MAX;
500#else
501 size = lseek(fd, 0LL, SEEK_END);
502#endif
503 } else
504#endif
505#ifdef __sun__
506 /*
507 * use the DKIOCGMEDIAINFO ioctl to read the size.
508 */
509 rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo );
510 if ( rv != -1 ) {
511 size = minfo.dki_lbsize * minfo.dki_capacity;
512 } else /* there are reports that lseek on some devices
513 fails, but irc discussion said that contingency
514 on contingency was overkill */
515#endif
516 {
517 size = lseek(fd, 0, SEEK_END);
518 }
83f64091
FB
519 return size;
520}
521
522static int raw_create(const char *filename, int64_t total_size,
523 const char *backing_file, int flags)
524{
525 int fd;
526
527 if (flags || backing_file)
528 return -ENOTSUP;
529
5fafdf24 530 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
83f64091
FB
531 0644);
532 if (fd < 0)
533 return -EIO;
534 ftruncate(fd, total_size * 512);
535 close(fd);
536 return 0;
537}
538
539static void raw_flush(BlockDriverState *bs)
540{
541 BDRVRawState *s = bs->opaque;
542 fsync(s->fd);
543}
544
545BlockDriver bdrv_raw = {
546 "raw",
547 sizeof(BDRVRawState),
548 NULL, /* no probe for protocols */
549 raw_open,
550 NULL,
551 NULL,
552 raw_close,
553 raw_create,
554 raw_flush,
3b46e624 555
83f64091
FB
556 .bdrv_aio_read = raw_aio_read,
557 .bdrv_aio_write = raw_aio_write,
558 .bdrv_aio_cancel = raw_aio_cancel,
ce1a14dc 559 .aiocb_size = sizeof(RawAIOCB),
83f64091
FB
560 .protocol_name = "file",
561 .bdrv_pread = raw_pread,
562 .bdrv_pwrite = raw_pwrite,
563 .bdrv_truncate = raw_truncate,
564 .bdrv_getlength = raw_getlength,
565};
566
19cb3738
FB
567/***********************************************/
568/* host device */
569
570#ifdef CONFIG_COCOA
571static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
572static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
573
574kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
575{
5fafdf24 576 kern_return_t kernResult;
19cb3738
FB
577 mach_port_t masterPort;
578 CFMutableDictionaryRef classesToMatch;
579
580 kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
581 if ( KERN_SUCCESS != kernResult ) {
582 printf( "IOMasterPort returned %d\n", kernResult );
583 }
3b46e624 584
5fafdf24 585 classesToMatch = IOServiceMatching( kIOCDMediaClass );
19cb3738
FB
586 if ( classesToMatch == NULL ) {
587 printf( "IOServiceMatching returned a NULL dictionary.\n" );
588 } else {
589 CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
590 }
591 kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
592 if ( KERN_SUCCESS != kernResult )
593 {
594 printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
595 }
3b46e624 596
19cb3738
FB
597 return kernResult;
598}
599
600kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
601{
602 io_object_t nextMedia;
603 kern_return_t kernResult = KERN_FAILURE;
604 *bsdPath = '\0';
605 nextMedia = IOIteratorNext( mediaIterator );
606 if ( nextMedia )
607 {
608 CFTypeRef bsdPathAsCFString;
609 bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
610 if ( bsdPathAsCFString ) {
611 size_t devPathLength;
612 strcpy( bsdPath, _PATH_DEV );
613 strcat( bsdPath, "r" );
614 devPathLength = strlen( bsdPath );
615 if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
616 kernResult = KERN_SUCCESS;
617 }
618 CFRelease( bsdPathAsCFString );
619 }
620 IOObjectRelease( nextMedia );
621 }
3b46e624 622
19cb3738
FB
623 return kernResult;
624}
625
626#endif
627
628static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
629{
630 BDRVRawState *s = bs->opaque;
631 int fd, open_flags, ret;
632
633#ifdef CONFIG_COCOA
634 if (strstart(filename, "/dev/cdrom", NULL)) {
635 kern_return_t kernResult;
636 io_iterator_t mediaIterator;
637 char bsdPath[ MAXPATHLEN ];
638 int fd;
5fafdf24 639
19cb3738
FB
640 kernResult = FindEjectableCDMedia( &mediaIterator );
641 kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
3b46e624 642
19cb3738
FB
643 if ( bsdPath[ 0 ] != '\0' ) {
644 strcat(bsdPath,"s0");
645 /* some CDs don't have a partition 0 */
646 fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
647 if (fd < 0) {
648 bsdPath[strlen(bsdPath)-1] = '1';
649 } else {
650 close(fd);
651 }
652 filename = bsdPath;
653 }
3b46e624 654
19cb3738
FB
655 if ( mediaIterator )
656 IOObjectRelease( mediaIterator );
657 }
658#endif
659 open_flags = O_BINARY;
660 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
661 open_flags |= O_RDWR;
662 } else {
663 open_flags |= O_RDONLY;
664 bs->read_only = 1;
665 }
33f00271
AZ
666#ifdef O_DIRECT
667 if (flags & BDRV_O_DIRECT)
668 open_flags |= O_DIRECT;
669#endif
19cb3738
FB
670
671 s->type = FTYPE_FILE;
672#if defined(__linux__)
673 if (strstart(filename, "/dev/cd", NULL)) {
674 /* open will not fail even if no CD is inserted */
675 open_flags |= O_NONBLOCK;
676 s->type = FTYPE_CD;
677 } else if (strstart(filename, "/dev/fd", NULL)) {
678 s->type = FTYPE_FD;
679 s->fd_open_flags = open_flags;
680 /* open will not fail even if no floppy is inserted */
681 open_flags |= O_NONBLOCK;
682 }
683#endif
684 fd = open(filename, open_flags, 0644);
685 if (fd < 0) {
686 ret = -errno;
687 if (ret == -EROFS)
688 ret = -EACCES;
689 return ret;
690 }
691 s->fd = fd;
692#if defined(__linux__)
693 /* close fd so that we can reopen it as needed */
694 if (s->type == FTYPE_FD) {
695 close(s->fd);
696 s->fd = -1;
697 s->fd_media_changed = 1;
698 }
699#endif
700 return 0;
701}
702
faf07963 703#if defined(__linux__) && !defined(QEMU_IMG)
19cb3738
FB
704
705/* Note: we do not have a reliable method to detect if the floppy is
706 present. The current method is to try to open the floppy at every
707 I/O and to keep it opened during a few hundreds of ms. */
708static int fd_open(BlockDriverState *bs)
709{
710 BDRVRawState *s = bs->opaque;
711 int last_media_present;
712
713 if (s->type != FTYPE_FD)
714 return 0;
715 last_media_present = (s->fd >= 0);
5fafdf24 716 if (s->fd >= 0 &&
19cb3738
FB
717 (qemu_get_clock(rt_clock) - s->fd_open_time) >= FD_OPEN_TIMEOUT) {
718 close(s->fd);
719 s->fd = -1;
720#ifdef DEBUG_FLOPPY
721 printf("Floppy closed\n");
722#endif
723 }
724 if (s->fd < 0) {
5fafdf24 725 if (s->fd_got_error &&
19cb3738
FB
726 (qemu_get_clock(rt_clock) - s->fd_error_time) < FD_OPEN_TIMEOUT) {
727#ifdef DEBUG_FLOPPY
728 printf("No floppy (open delayed)\n");
729#endif
730 return -EIO;
731 }
732 s->fd = open(bs->filename, s->fd_open_flags);
733 if (s->fd < 0) {
734 s->fd_error_time = qemu_get_clock(rt_clock);
735 s->fd_got_error = 1;
736 if (last_media_present)
737 s->fd_media_changed = 1;
738#ifdef DEBUG_FLOPPY
739 printf("No floppy\n");
740#endif
741 return -EIO;
742 }
743#ifdef DEBUG_FLOPPY
744 printf("Floppy opened\n");
745#endif
746 }
747 if (!last_media_present)
748 s->fd_media_changed = 1;
749 s->fd_open_time = qemu_get_clock(rt_clock);
750 s->fd_got_error = 0;
751 return 0;
752}
753#else
754static int fd_open(BlockDriverState *bs)
755{
756 return 0;
757}
758#endif
759
760#if defined(__linux__)
761
762static int raw_is_inserted(BlockDriverState *bs)
763{
764 BDRVRawState *s = bs->opaque;
765 int ret;
766
767 switch(s->type) {
768 case FTYPE_CD:
769 ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
770 if (ret == CDS_DISC_OK)
771 return 1;
772 else
773 return 0;
774 break;
775 case FTYPE_FD:
776 ret = fd_open(bs);
777 return (ret >= 0);
778 default:
779 return 1;
780 }
781}
782
783/* currently only used by fdc.c, but a CD version would be good too */
784static int raw_media_changed(BlockDriverState *bs)
785{
786 BDRVRawState *s = bs->opaque;
787
788 switch(s->type) {
789 case FTYPE_FD:
790 {
791 int ret;
792 /* XXX: we do not have a true media changed indication. It
793 does not work if the floppy is changed without trying
794 to read it */
795 fd_open(bs);
796 ret = s->fd_media_changed;
797 s->fd_media_changed = 0;
798#ifdef DEBUG_FLOPPY
799 printf("Floppy changed=%d\n", ret);
800#endif
801 return ret;
802 }
803 default:
804 return -ENOTSUP;
805 }
806}
807
808static int raw_eject(BlockDriverState *bs, int eject_flag)
809{
810 BDRVRawState *s = bs->opaque;
811
812 switch(s->type) {
813 case FTYPE_CD:
814 if (eject_flag) {
815 if (ioctl (s->fd, CDROMEJECT, NULL) < 0)
816 perror("CDROMEJECT");
817 } else {
818 if (ioctl (s->fd, CDROMCLOSETRAY, NULL) < 0)
819 perror("CDROMEJECT");
820 }
821 break;
822 case FTYPE_FD:
823 {
824 int fd;
825 if (s->fd >= 0) {
826 close(s->fd);
827 s->fd = -1;
828 }
829 fd = open(bs->filename, s->fd_open_flags | O_NONBLOCK);
830 if (fd >= 0) {
831 if (ioctl(fd, FDEJECT, 0) < 0)
832 perror("FDEJECT");
833 close(fd);
834 }
835 }
836 break;
837 default:
838 return -ENOTSUP;
839 }
840 return 0;
841}
842
843static int raw_set_locked(BlockDriverState *bs, int locked)
844{
845 BDRVRawState *s = bs->opaque;
846
847 switch(s->type) {
848 case FTYPE_CD:
849 if (ioctl (s->fd, CDROM_LOCKDOOR, locked) < 0) {
850 /* Note: an error can happen if the distribution automatically
851 mounts the CD-ROM */
852 // perror("CDROM_LOCKDOOR");
853 }
854 break;
855 default:
856 return -ENOTSUP;
857 }
858 return 0;
859}
860
861#else
862
863static int raw_is_inserted(BlockDriverState *bs)
864{
865 return 1;
866}
867
868static int raw_media_changed(BlockDriverState *bs)
869{
870 return -ENOTSUP;
871}
872
873static int raw_eject(BlockDriverState *bs, int eject_flag)
874{
875 return -ENOTSUP;
876}
877
878static int raw_set_locked(BlockDriverState *bs, int locked)
879{
880 return -ENOTSUP;
881}
882
883#endif /* !linux */
884
885BlockDriver bdrv_host_device = {
886 "host_device",
887 sizeof(BDRVRawState),
888 NULL, /* no probe for protocols */
889 hdev_open,
890 NULL,
891 NULL,
892 raw_close,
893 NULL,
894 raw_flush,
3b46e624 895
19cb3738
FB
896 .bdrv_aio_read = raw_aio_read,
897 .bdrv_aio_write = raw_aio_write,
898 .bdrv_aio_cancel = raw_aio_cancel,
899 .aiocb_size = sizeof(RawAIOCB),
900 .bdrv_pread = raw_pread,
901 .bdrv_pwrite = raw_pwrite,
902 .bdrv_getlength = raw_getlength,
903
904 /* removable device support */
905 .bdrv_is_inserted = raw_is_inserted,
906 .bdrv_media_changed = raw_media_changed,
907 .bdrv_eject = raw_eject,
908 .bdrv_set_locked = raw_set_locked,
909};
This page took 0.239096 seconds and 4 git commands to generate.