]> git.proxmox.com Git - mirror_qemu.git/blame - block-raw-posix.c
Move wav_start_capture prototype to avoid a warning with -Wmissing-prototypes
[mirror_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 25#include "qemu-timer.h"
baf35cb9 26#include "qemu-char.h"
83f64091 27#include "block_int.h"
baf35cb9 28#include "compatfd.h"
83f64091 29#include <assert.h>
414f0dab 30#ifdef CONFIG_AIO
83f64091 31#include <aio.h>
414f0dab 32#endif
83f64091 33
83f64091
FB
34#ifdef CONFIG_COCOA
35#include <paths.h>
36#include <sys/param.h>
37#include <IOKit/IOKitLib.h>
38#include <IOKit/IOBSD.h>
39#include <IOKit/storage/IOMediaBSDClient.h>
40#include <IOKit/storage/IOMedia.h>
41#include <IOKit/storage/IOCDMedia.h>
42//#include <IOKit/storage/IOCDTypes.h>
43#include <CoreFoundation/CoreFoundation.h>
44#endif
45
46#ifdef __sun__
2e9671da
TS
47#define _POSIX_PTHREAD_SEMANTICS 1
48#include <signal.h>
83f64091
FB
49#include <sys/dkio.h>
50#endif
19cb3738
FB
51#ifdef __linux__
52#include <sys/ioctl.h>
53#include <linux/cdrom.h>
54#include <linux/fd.h>
55#endif
1cb6c3fd 56#ifdef __FreeBSD__
543952ca 57#include <signal.h>
1cb6c3fd
TS
58#include <sys/disk.h>
59#endif
83f64091 60
128ab2ff
BS
61#ifdef __OpenBSD__
62#include <sys/ioctl.h>
63#include <sys/disklabel.h>
64#include <sys/dkio.h>
65#endif
66
19cb3738 67//#define DEBUG_FLOPPY
83f64091 68
faf07963 69//#define DEBUG_BLOCK
03ff3ca3 70#if defined(DEBUG_BLOCK)
a50a6282 71#define DEBUG_BLOCK_PRINT(formatCstr, args...) do { if (loglevel != 0) \
2e03286b 72 { fprintf(logfile, formatCstr, ##args); fflush(logfile); } } while (0)
8c05dbf9
TS
73#else
74#define DEBUG_BLOCK_PRINT(formatCstr, args...)
75#endif
76
19cb3738
FB
77#define FTYPE_FILE 0
78#define FTYPE_CD 1
79#define FTYPE_FD 2
83f64091 80
bed5cc52
FB
81#define ALIGNED_BUFFER_SIZE (32 * 512)
82
19cb3738
FB
83/* if the FD is not accessed during that time (in ms), we try to
84 reopen it to see if the disk has been changed */
85#define FD_OPEN_TIMEOUT 1000
83f64091 86
53538725
AL
87/* posix-aio doesn't allow multiple outstanding requests to a single file
88 * descriptor. we implement a pool of dup()'d file descriptors to work
89 * around this */
90#define RAW_FD_POOL_SIZE 64
91
19cb3738
FB
92typedef struct BDRVRawState {
93 int fd;
94 int type;
8c05dbf9 95 unsigned int lseek_err_cnt;
53538725 96 int fd_pool[RAW_FD_POOL_SIZE];
19cb3738
FB
97#if defined(__linux__)
98 /* linux floppy specific */
6dd2db52 99 int fd_open_flags;
19cb3738
FB
100 int64_t fd_open_time;
101 int64_t fd_error_time;
102 int fd_got_error;
103 int fd_media_changed;
83f64091 104#endif
03ff3ca3 105#if defined(O_DIRECT)
bed5cc52
FB
106 uint8_t* aligned_buf;
107#endif
19cb3738
FB
108} BDRVRawState;
109
a76bab49
AL
110static int posix_aio_init(void);
111
19cb3738 112static int fd_open(BlockDriverState *bs);
83f64091
FB
113
114static int raw_open(BlockDriverState *bs, const char *filename, int flags)
115{
116 BDRVRawState *s = bs->opaque;
19cb3738 117 int fd, open_flags, ret;
53538725 118 int i;
83f64091 119
a76bab49
AL
120 posix_aio_init();
121
8c05dbf9
TS
122 s->lseek_err_cnt = 0;
123
83f64091
FB
124 open_flags = O_BINARY;
125 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
126 open_flags |= O_RDWR;
127 } else {
128 open_flags |= O_RDONLY;
129 bs->read_only = 1;
130 }
131 if (flags & BDRV_O_CREAT)
132 open_flags |= O_CREAT | O_TRUNC;
33f00271
AZ
133#ifdef O_DIRECT
134 if (flags & BDRV_O_DIRECT)
135 open_flags |= O_DIRECT;
136#endif
83f64091 137
19cb3738
FB
138 s->type = FTYPE_FILE;
139
83f64091 140 fd = open(filename, open_flags, 0644);
19cb3738
FB
141 if (fd < 0) {
142 ret = -errno;
143 if (ret == -EROFS)
144 ret = -EACCES;
145 return ret;
146 }
83f64091 147 s->fd = fd;
53538725
AL
148 for (i = 0; i < RAW_FD_POOL_SIZE; i++)
149 s->fd_pool[i] = -1;
03ff3ca3 150#if defined(O_DIRECT)
bed5cc52
FB
151 s->aligned_buf = NULL;
152 if (flags & BDRV_O_DIRECT) {
153 s->aligned_buf = qemu_memalign(512, ALIGNED_BUFFER_SIZE);
154 if (s->aligned_buf == NULL) {
155 ret = -errno;
156 close(fd);
157 return ret;
158 }
159 }
160#endif
83f64091
FB
161 return 0;
162}
163
164/* XXX: use host sector size if necessary with:
165#ifdef DIOCGSECTORSIZE
166 {
167 unsigned int sectorsize = 512;
168 if (!ioctl(fd, DIOCGSECTORSIZE, &sectorsize) &&
169 sectorsize > bufsize)
170 bufsize = sectorsize;
171 }
172#endif
173#ifdef CONFIG_COCOA
174 u_int32_t blockSize = 512;
175 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
176 bufsize = blockSize;
177 }
178#endif
179*/
180
bed5cc52
FB
181/*
182 * offset and count are in bytes, but must be multiples of 512 for files
183 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
184 *
185 * This function may be called without alignment if the caller ensures
186 * that O_DIRECT is not in effect.
187 */
188static int raw_pread_aligned(BlockDriverState *bs, int64_t offset,
83f64091
FB
189 uint8_t *buf, int count)
190{
191 BDRVRawState *s = bs->opaque;
192 int ret;
3b46e624 193
19cb3738
FB
194 ret = fd_open(bs);
195 if (ret < 0)
196 return ret;
197
985a03b0 198 if (offset >= 0 && lseek(s->fd, offset, SEEK_SET) == (off_t)-1) {
8c05dbf9
TS
199 ++(s->lseek_err_cnt);
200 if(s->lseek_err_cnt <= 10) {
92868412
JM
201 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
202 "] lseek failed : %d = %s\n",
8c05dbf9
TS
203 s->fd, bs->filename, offset, buf, count,
204 bs->total_sectors, errno, strerror(errno));
205 }
206 return -1;
207 }
208 s->lseek_err_cnt=0;
209
83f64091 210 ret = read(s->fd, buf, count);
8c05dbf9
TS
211 if (ret == count)
212 goto label__raw_read__success;
213
92868412
JM
214 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
215 "] read failed %d : %d = %s\n",
8c05dbf9
TS
216 s->fd, bs->filename, offset, buf, count,
217 bs->total_sectors, ret, errno, strerror(errno));
218
219 /* Try harder for CDrom. */
220 if (bs->type == BDRV_TYPE_CDROM) {
221 lseek(s->fd, offset, SEEK_SET);
222 ret = read(s->fd, buf, count);
223 if (ret == count)
224 goto label__raw_read__success;
225 lseek(s->fd, offset, SEEK_SET);
226 ret = read(s->fd, buf, count);
227 if (ret == count)
228 goto label__raw_read__success;
229
92868412
JM
230 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
231 "] retry read failed %d : %d = %s\n",
8c05dbf9
TS
232 s->fd, bs->filename, offset, buf, count,
233 bs->total_sectors, ret, errno, strerror(errno));
234 }
235
8c05dbf9
TS
236label__raw_read__success:
237
83f64091
FB
238 return ret;
239}
240
bed5cc52
FB
241/*
242 * offset and count are in bytes, but must be multiples of 512 for files
243 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
244 *
245 * This function may be called without alignment if the caller ensures
246 * that O_DIRECT is not in effect.
247 */
248static int raw_pwrite_aligned(BlockDriverState *bs, int64_t offset,
83f64091
FB
249 const uint8_t *buf, int count)
250{
251 BDRVRawState *s = bs->opaque;
252 int ret;
3b46e624 253
19cb3738
FB
254 ret = fd_open(bs);
255 if (ret < 0)
256 return ret;
257
985a03b0 258 if (offset >= 0 && lseek(s->fd, offset, SEEK_SET) == (off_t)-1) {
8c05dbf9
TS
259 ++(s->lseek_err_cnt);
260 if(s->lseek_err_cnt) {
92868412
JM
261 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%"
262 PRId64 "] lseek failed : %d = %s\n",
8c05dbf9
TS
263 s->fd, bs->filename, offset, buf, count,
264 bs->total_sectors, errno, strerror(errno));
265 }
266 return -1;
267 }
268 s->lseek_err_cnt = 0;
269
83f64091 270 ret = write(s->fd, buf, count);
8c05dbf9
TS
271 if (ret == count)
272 goto label__raw_write__success;
273
92868412
JM
274 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
275 "] write failed %d : %d = %s\n",
8c05dbf9
TS
276 s->fd, bs->filename, offset, buf, count,
277 bs->total_sectors, ret, errno, strerror(errno));
278
8c05dbf9
TS
279label__raw_write__success:
280
83f64091
FB
281 return ret;
282}
283
bed5cc52 284
03ff3ca3 285#if defined(O_DIRECT)
bed5cc52
FB
286/*
287 * offset and count are in bytes and possibly not aligned. For files opened
288 * with O_DIRECT, necessary alignments are ensured before calling
289 * raw_pread_aligned to do the actual read.
290 */
291static int raw_pread(BlockDriverState *bs, int64_t offset,
292 uint8_t *buf, int count)
293{
294 BDRVRawState *s = bs->opaque;
295 int size, ret, shift, sum;
296
297 sum = 0;
298
299 if (s->aligned_buf != NULL) {
300
301 if (offset & 0x1ff) {
302 /* align offset on a 512 bytes boundary */
303
304 shift = offset & 0x1ff;
305 size = (shift + count + 0x1ff) & ~0x1ff;
306 if (size > ALIGNED_BUFFER_SIZE)
307 size = ALIGNED_BUFFER_SIZE;
308 ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, size);
309 if (ret < 0)
310 return ret;
311
312 size = 512 - shift;
313 if (size > count)
314 size = count;
315 memcpy(buf, s->aligned_buf + shift, size);
316
317 buf += size;
318 offset += size;
319 count -= size;
320 sum += size;
321
322 if (count == 0)
323 return sum;
324 }
325 if (count & 0x1ff || (uintptr_t) buf & 0x1ff) {
326
327 /* read on aligned buffer */
328
329 while (count) {
330
331 size = (count + 0x1ff) & ~0x1ff;
332 if (size > ALIGNED_BUFFER_SIZE)
333 size = ALIGNED_BUFFER_SIZE;
334
335 ret = raw_pread_aligned(bs, offset, s->aligned_buf, size);
336 if (ret < 0)
337 return ret;
338
339 size = ret;
340 if (size > count)
341 size = count;
342
343 memcpy(buf, s->aligned_buf, size);
344
345 buf += size;
346 offset += size;
347 count -= size;
348 sum += size;
349 }
350
351 return sum;
352 }
353 }
354
355 return raw_pread_aligned(bs, offset, buf, count) + sum;
356}
357
358/*
359 * offset and count are in bytes and possibly not aligned. For files opened
360 * with O_DIRECT, necessary alignments are ensured before calling
361 * raw_pwrite_aligned to do the actual write.
362 */
363static int raw_pwrite(BlockDriverState *bs, int64_t offset,
364 const uint8_t *buf, int count)
365{
366 BDRVRawState *s = bs->opaque;
367 int size, ret, shift, sum;
368
369 sum = 0;
370
371 if (s->aligned_buf != NULL) {
372
373 if (offset & 0x1ff) {
374 /* align offset on a 512 bytes boundary */
375 shift = offset & 0x1ff;
376 ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, 512);
377 if (ret < 0)
378 return ret;
379
380 size = 512 - shift;
381 if (size > count)
382 size = count;
383 memcpy(s->aligned_buf + shift, buf, size);
384
385 ret = raw_pwrite_aligned(bs, offset - shift, s->aligned_buf, 512);
386 if (ret < 0)
387 return ret;
388
389 buf += size;
390 offset += size;
391 count -= size;
392 sum += size;
393
394 if (count == 0)
395 return sum;
396 }
397 if (count & 0x1ff || (uintptr_t) buf & 0x1ff) {
398
399 while ((size = (count & ~0x1ff)) != 0) {
400
401 if (size > ALIGNED_BUFFER_SIZE)
402 size = ALIGNED_BUFFER_SIZE;
403
404 memcpy(s->aligned_buf, buf, size);
405
406 ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, size);
407 if (ret < 0)
408 return ret;
409
410 buf += ret;
411 offset += ret;
412 count -= ret;
413 sum += ret;
414 }
415 /* here, count < 512 because (count & ~0x1ff) == 0 */
416 if (count) {
417 ret = raw_pread_aligned(bs, offset, s->aligned_buf, 512);
418 if (ret < 0)
419 return ret;
420 memcpy(s->aligned_buf, buf, count);
421
422 ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, 512);
423 if (ret < 0)
424 return ret;
425 if (count < ret)
426 ret = count;
427
428 sum += ret;
429 }
430 return sum;
431 }
432 }
433 return raw_pwrite_aligned(bs, offset, buf, count) + sum;
434}
435
436#else
437#define raw_pread raw_pread_aligned
438#define raw_pwrite raw_pwrite_aligned
439#endif
440
441
414f0dab 442#ifdef CONFIG_AIO
83f64091 443/***********************************************************/
19cb3738 444/* Unix AIO using POSIX AIO */
83f64091
FB
445
446typedef struct RawAIOCB {
ce1a14dc 447 BlockDriverAIOCB common;
53538725 448 int fd;
83f64091 449 struct aiocb aiocb;
ce1a14dc 450 struct RawAIOCB *next;
bed5cc52 451 int ret;
83f64091
FB
452} RawAIOCB;
453
a76bab49
AL
454typedef struct PosixAioState
455{
456 int fd;
457 RawAIOCB *first_aio;
458} PosixAioState;
83f64091 459
53538725
AL
460static int raw_fd_pool_get(BDRVRawState *s)
461{
462 int i;
463
464 for (i = 0; i < RAW_FD_POOL_SIZE; i++) {
465 /* already in use */
466 if (s->fd_pool[i] != -1)
467 continue;
468
469 /* try to dup file descriptor */
470 s->fd_pool[i] = dup(s->fd);
471 if (s->fd_pool[i] != -1)
472 return s->fd_pool[i];
473 }
474
475 /* we couldn't dup the file descriptor so just use the main one */
476 return s->fd;
477}
478
479static void raw_fd_pool_put(RawAIOCB *acb)
480{
481 BDRVRawState *s = acb->common.bs->opaque;
482 int i;
483
484 for (i = 0; i < RAW_FD_POOL_SIZE; i++) {
485 if (s->fd_pool[i] == acb->fd) {
486 close(s->fd_pool[i]);
487 s->fd_pool[i] = -1;
488 }
489 }
490}
491
a76bab49 492static void posix_aio_read(void *opaque)
83f64091 493{
a76bab49 494 PosixAioState *s = opaque;
ce1a14dc 495 RawAIOCB *acb, **pacb;
83f64091 496 int ret;
2c41a5f9
AL
497 size_t offset;
498 union {
499 struct qemu_signalfd_siginfo siginfo;
500 char buf[128];
501 } sig;
502
503 /* try to read from signalfd, don't freak out if we can't read anything */
504 offset = 0;
505 while (offset < 128) {
506 ssize_t len;
507
a76bab49 508 len = read(s->fd, sig.buf + offset, 128 - offset);
2c41a5f9
AL
509 if (len == -1 && errno == EINTR)
510 continue;
511 if (len == -1 && errno == EAGAIN) {
512 /* there is no natural reason for this to happen,
513 * so we'll spin hard until we get everything just
514 * to be on the safe side. */
515 if (offset > 0)
516 continue;
517 }
518
519 offset += len;
520 }
83f64091
FB
521
522 for(;;) {
a76bab49 523 pacb = &s->first_aio;
83f64091
FB
524 for(;;) {
525 acb = *pacb;
526 if (!acb)
527 goto the_end;
ce1a14dc 528 ret = aio_error(&acb->aiocb);
83f64091
FB
529 if (ret == ECANCELED) {
530 /* remove the request */
ce1a14dc 531 *pacb = acb->next;
53538725 532 raw_fd_pool_put(acb);
ce1a14dc 533 qemu_aio_release(acb);
83f64091
FB
534 } else if (ret != EINPROGRESS) {
535 /* end of aio */
536 if (ret == 0) {
ce1a14dc
PB
537 ret = aio_return(&acb->aiocb);
538 if (ret == acb->aiocb.aio_nbytes)
83f64091
FB
539 ret = 0;
540 else
19cb3738 541 ret = -EINVAL;
83f64091
FB
542 } else {
543 ret = -ret;
544 }
545 /* remove the request */
ce1a14dc 546 *pacb = acb->next;
83f64091 547 /* call the callback */
ce1a14dc 548 acb->common.cb(acb->common.opaque, ret);
53538725 549 raw_fd_pool_put(acb);
ce1a14dc 550 qemu_aio_release(acb);
83f64091
FB
551 break;
552 } else {
ce1a14dc 553 pacb = &acb->next;
83f64091
FB
554 }
555 }
556 }
557 the_end: ;
558}
559
a76bab49 560static int posix_aio_flush(void *opaque)
baf35cb9 561{
a76bab49
AL
562 PosixAioState *s = opaque;
563 return !!s->first_aio;
564}
565
566static PosixAioState *posix_aio_state;
baf35cb9 567
a76bab49
AL
568static int posix_aio_init(void)
569{
570 sigset_t mask;
571 PosixAioState *s;
572
573 if (posix_aio_state)
574 return 0;
ad02ad6f 575
a76bab49
AL
576 s = qemu_malloc(sizeof(PosixAioState));
577 if (s == NULL)
578 return -ENOMEM;
baf35cb9
AL
579
580 /* Make sure to block AIO signal */
581 sigemptyset(&mask);
a76bab49 582 sigaddset(&mask, SIGUSR2);
baf35cb9
AL
583 sigprocmask(SIG_BLOCK, &mask, NULL);
584
a76bab49
AL
585 s->first_aio = NULL;
586 s->fd = qemu_signalfd(&mask);
27463101
AL
587 if (s->fd == -1) {
588 fprintf(stderr, "failed to create signalfd\n");
589 return -errno;
590 }
2c41a5f9 591
a76bab49 592 fcntl(s->fd, F_SETFL, O_NONBLOCK);
2c41a5f9 593
a76bab49 594 qemu_aio_set_fd_handler(s->fd, posix_aio_read, NULL, posix_aio_flush, s);
baf35cb9 595
acce87f9
AL
596#if defined(__linux__)
597 {
598 struct aioinit ai;
599
600 memset(&ai, 0, sizeof(ai));
601#if defined(__GLIBC_PREREQ) && __GLIBC_PREREQ(2, 4)
602 ai.aio_threads = 64;
603 ai.aio_num = 64;
997306fc 604#else
acce87f9
AL
605 /* XXX: aio thread exit seems to hang on RedHat 9 and this init
606 seems to fix the problem. */
607 ai.aio_threads = 1;
608 ai.aio_num = 1;
609 ai.aio_idle_time = 365 * 100000;
610#endif
611 aio_init(&ai);
612 }
baf35cb9 613#endif
a76bab49 614 posix_aio_state = s;
6eb5733a 615
a76bab49 616 return 0;
83f64091
FB
617}
618
ce1a14dc
PB
619static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
620 int64_t sector_num, uint8_t *buf, int nb_sectors,
621 BlockDriverCompletionFunc *cb, void *opaque)
83f64091 622{
ce1a14dc
PB
623 BDRVRawState *s = bs->opaque;
624 RawAIOCB *acb;
625
19cb3738
FB
626 if (fd_open(bs) < 0)
627 return NULL;
628
ce1a14dc
PB
629 acb = qemu_aio_get(bs, cb, opaque);
630 if (!acb)
631 return NULL;
53538725
AL
632 acb->fd = raw_fd_pool_get(s);
633 acb->aiocb.aio_fildes = acb->fd;
a76bab49 634 acb->aiocb.aio_sigevent.sigev_signo = SIGUSR2;
ce1a14dc
PB
635 acb->aiocb.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
636 acb->aiocb.aio_buf = buf;
985a03b0
TS
637 if (nb_sectors < 0)
638 acb->aiocb.aio_nbytes = -nb_sectors;
639 else
640 acb->aiocb.aio_nbytes = nb_sectors * 512;
ce1a14dc 641 acb->aiocb.aio_offset = sector_num * 512;
a76bab49
AL
642 acb->next = posix_aio_state->first_aio;
643 posix_aio_state->first_aio = acb;
ce1a14dc 644 return acb;
83f64091
FB
645}
646
bed5cc52
FB
647static void raw_aio_em_cb(void* opaque)
648{
649 RawAIOCB *acb = opaque;
650 acb->common.cb(acb->common.opaque, acb->ret);
651 qemu_aio_release(acb);
652}
bed5cc52 653
ce1a14dc
PB
654static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
655 int64_t sector_num, uint8_t *buf, int nb_sectors,
656 BlockDriverCompletionFunc *cb, void *opaque)
83f64091 657{
ce1a14dc 658 RawAIOCB *acb;
83f64091 659
bed5cc52
FB
660 /*
661 * If O_DIRECT is used and the buffer is not aligned fall back
662 * to synchronous IO.
663 */
03ff3ca3 664#if defined(O_DIRECT)
bed5cc52
FB
665 BDRVRawState *s = bs->opaque;
666
667 if (unlikely(s->aligned_buf != NULL && ((uintptr_t) buf % 512))) {
668 QEMUBH *bh;
669 acb = qemu_aio_get(bs, cb, opaque);
670 acb->ret = raw_pread(bs, 512 * sector_num, buf, 512 * nb_sectors);
671 bh = qemu_bh_new(raw_aio_em_cb, acb);
672 qemu_bh_schedule(bh);
673 return &acb->common;
674 }
675#endif
676
ce1a14dc
PB
677 acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
678 if (!acb)
679 return NULL;
680 if (aio_read(&acb->aiocb) < 0) {
681 qemu_aio_release(acb);
682 return NULL;
5fafdf24 683 }
ce1a14dc 684 return &acb->common;
83f64091
FB
685}
686
ce1a14dc
PB
687static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
688 int64_t sector_num, const uint8_t *buf, int nb_sectors,
689 BlockDriverCompletionFunc *cb, void *opaque)
83f64091 690{
ce1a14dc 691 RawAIOCB *acb;
83f64091 692
bed5cc52
FB
693 /*
694 * If O_DIRECT is used and the buffer is not aligned fall back
695 * to synchronous IO.
696 */
03ff3ca3 697#if defined(O_DIRECT)
bed5cc52
FB
698 BDRVRawState *s = bs->opaque;
699
700 if (unlikely(s->aligned_buf != NULL && ((uintptr_t) buf % 512))) {
701 QEMUBH *bh;
702 acb = qemu_aio_get(bs, cb, opaque);
703 acb->ret = raw_pwrite(bs, 512 * sector_num, buf, 512 * nb_sectors);
704 bh = qemu_bh_new(raw_aio_em_cb, acb);
705 qemu_bh_schedule(bh);
706 return &acb->common;
707 }
708#endif
709
ce1a14dc
PB
710 acb = raw_aio_setup(bs, sector_num, (uint8_t*)buf, nb_sectors, cb, opaque);
711 if (!acb)
712 return NULL;
713 if (aio_write(&acb->aiocb) < 0) {
714 qemu_aio_release(acb);
715 return NULL;
5fafdf24 716 }
ce1a14dc 717 return &acb->common;
83f64091
FB
718}
719
ce1a14dc 720static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
83f64091 721{
83f64091 722 int ret;
ce1a14dc
PB
723 RawAIOCB *acb = (RawAIOCB *)blockacb;
724 RawAIOCB **pacb;
83f64091 725
ce1a14dc 726 ret = aio_cancel(acb->aiocb.aio_fildes, &acb->aiocb);
83f64091
FB
727 if (ret == AIO_NOTCANCELED) {
728 /* fail safe: if the aio could not be canceled, we wait for
729 it */
ce1a14dc 730 while (aio_error(&acb->aiocb) == EINPROGRESS);
83f64091
FB
731 }
732
733 /* remove the callback from the queue */
a76bab49 734 pacb = &posix_aio_state->first_aio;
83f64091
FB
735 for(;;) {
736 if (*pacb == NULL) {
737 break;
738 } else if (*pacb == acb) {
ce1a14dc 739 *pacb = acb->next;
53538725 740 raw_fd_pool_put(acb);
ce1a14dc 741 qemu_aio_release(acb);
83f64091
FB
742 break;
743 }
ce1a14dc 744 pacb = &acb->next;
83f64091
FB
745 }
746}
747
a76bab49
AL
748#else /* CONFIG_AIO */
749static int posix_aio_init(void)
414f0dab 750{
674a24ac 751 return 0;
414f0dab 752}
414f0dab
BS
753#endif /* CONFIG_AIO */
754
53538725
AL
755static void raw_close_fd_pool(BDRVRawState *s)
756{
757 int i;
758
759 for (i = 0; i < RAW_FD_POOL_SIZE; i++) {
760 if (s->fd_pool[i] != -1) {
761 close(s->fd_pool[i]);
762 s->fd_pool[i] = -1;
763 }
764 }
765}
766
83f64091
FB
767static void raw_close(BlockDriverState *bs)
768{
769 BDRVRawState *s = bs->opaque;
19cb3738
FB
770 if (s->fd >= 0) {
771 close(s->fd);
772 s->fd = -1;
03ff3ca3 773#if defined(O_DIRECT)
bed5cc52
FB
774 if (s->aligned_buf != NULL)
775 qemu_free(s->aligned_buf);
776#endif
19cb3738 777 }
53538725 778 raw_close_fd_pool(s);
83f64091
FB
779}
780
781static int raw_truncate(BlockDriverState *bs, int64_t offset)
782{
783 BDRVRawState *s = bs->opaque;
19cb3738
FB
784 if (s->type != FTYPE_FILE)
785 return -ENOTSUP;
83f64091
FB
786 if (ftruncate(s->fd, offset) < 0)
787 return -errno;
788 return 0;
789}
790
128ab2ff
BS
791#ifdef __OpenBSD__
792static int64_t raw_getlength(BlockDriverState *bs)
793{
794 BDRVRawState *s = bs->opaque;
795 int fd = s->fd;
796 struct stat st;
797
798 if (fstat(fd, &st))
799 return -1;
800 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
801 struct disklabel dl;
802
803 if (ioctl(fd, DIOCGDINFO, &dl))
804 return -1;
805 return (uint64_t)dl.d_secsize *
806 dl.d_partitions[DISKPART(st.st_rdev)].p_size;
807 } else
808 return st.st_size;
809}
810#else /* !__OpenBSD__ */
83f64091
FB
811static int64_t raw_getlength(BlockDriverState *bs)
812{
813 BDRVRawState *s = bs->opaque;
814 int fd = s->fd;
815 int64_t size;
816#ifdef _BSD
817 struct stat sb;
818#endif
819#ifdef __sun__
820 struct dk_minfo minfo;
821 int rv;
822#endif
19cb3738
FB
823 int ret;
824
825 ret = fd_open(bs);
826 if (ret < 0)
827 return ret;
83f64091
FB
828
829#ifdef _BSD
830 if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
831#ifdef DIOCGMEDIASIZE
832 if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
833#endif
834#ifdef CONFIG_COCOA
835 size = LONG_LONG_MAX;
836#else
837 size = lseek(fd, 0LL, SEEK_END);
838#endif
839 } else
840#endif
841#ifdef __sun__
842 /*
843 * use the DKIOCGMEDIAINFO ioctl to read the size.
844 */
845 rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo );
846 if ( rv != -1 ) {
847 size = minfo.dki_lbsize * minfo.dki_capacity;
848 } else /* there are reports that lseek on some devices
849 fails, but irc discussion said that contingency
850 on contingency was overkill */
851#endif
852 {
853 size = lseek(fd, 0, SEEK_END);
854 }
83f64091
FB
855 return size;
856}
128ab2ff 857#endif
83f64091
FB
858
859static int raw_create(const char *filename, int64_t total_size,
860 const char *backing_file, int flags)
861{
862 int fd;
863
864 if (flags || backing_file)
865 return -ENOTSUP;
866
5fafdf24 867 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
83f64091
FB
868 0644);
869 if (fd < 0)
870 return -EIO;
871 ftruncate(fd, total_size * 512);
872 close(fd);
873 return 0;
874}
875
876static void raw_flush(BlockDriverState *bs)
877{
878 BDRVRawState *s = bs->opaque;
879 fsync(s->fd);
880}
881
882BlockDriver bdrv_raw = {
883 "raw",
884 sizeof(BDRVRawState),
885 NULL, /* no probe for protocols */
886 raw_open,
887 NULL,
888 NULL,
889 raw_close,
890 raw_create,
891 raw_flush,
3b46e624 892
414f0dab 893#ifdef CONFIG_AIO
83f64091
FB
894 .bdrv_aio_read = raw_aio_read,
895 .bdrv_aio_write = raw_aio_write,
896 .bdrv_aio_cancel = raw_aio_cancel,
ce1a14dc 897 .aiocb_size = sizeof(RawAIOCB),
414f0dab 898#endif
83f64091
FB
899 .bdrv_pread = raw_pread,
900 .bdrv_pwrite = raw_pwrite,
901 .bdrv_truncate = raw_truncate,
902 .bdrv_getlength = raw_getlength,
903};
904
19cb3738
FB
905/***********************************************/
906/* host device */
907
908#ifdef CONFIG_COCOA
909static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
910static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
911
912kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
913{
5fafdf24 914 kern_return_t kernResult;
19cb3738
FB
915 mach_port_t masterPort;
916 CFMutableDictionaryRef classesToMatch;
917
918 kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
919 if ( KERN_SUCCESS != kernResult ) {
920 printf( "IOMasterPort returned %d\n", kernResult );
921 }
3b46e624 922
5fafdf24 923 classesToMatch = IOServiceMatching( kIOCDMediaClass );
19cb3738
FB
924 if ( classesToMatch == NULL ) {
925 printf( "IOServiceMatching returned a NULL dictionary.\n" );
926 } else {
927 CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
928 }
929 kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
930 if ( KERN_SUCCESS != kernResult )
931 {
932 printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
933 }
3b46e624 934
19cb3738
FB
935 return kernResult;
936}
937
938kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
939{
940 io_object_t nextMedia;
941 kern_return_t kernResult = KERN_FAILURE;
942 *bsdPath = '\0';
943 nextMedia = IOIteratorNext( mediaIterator );
944 if ( nextMedia )
945 {
946 CFTypeRef bsdPathAsCFString;
947 bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
948 if ( bsdPathAsCFString ) {
949 size_t devPathLength;
950 strcpy( bsdPath, _PATH_DEV );
951 strcat( bsdPath, "r" );
952 devPathLength = strlen( bsdPath );
953 if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
954 kernResult = KERN_SUCCESS;
955 }
956 CFRelease( bsdPathAsCFString );
957 }
958 IOObjectRelease( nextMedia );
959 }
3b46e624 960
19cb3738
FB
961 return kernResult;
962}
963
964#endif
965
966static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
967{
968 BDRVRawState *s = bs->opaque;
53538725 969 int fd, open_flags, ret, i;
19cb3738 970
a76bab49
AL
971 posix_aio_init();
972
19cb3738
FB
973#ifdef CONFIG_COCOA
974 if (strstart(filename, "/dev/cdrom", NULL)) {
975 kern_return_t kernResult;
976 io_iterator_t mediaIterator;
977 char bsdPath[ MAXPATHLEN ];
978 int fd;
5fafdf24 979
19cb3738
FB
980 kernResult = FindEjectableCDMedia( &mediaIterator );
981 kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
3b46e624 982
19cb3738
FB
983 if ( bsdPath[ 0 ] != '\0' ) {
984 strcat(bsdPath,"s0");
985 /* some CDs don't have a partition 0 */
986 fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
987 if (fd < 0) {
988 bsdPath[strlen(bsdPath)-1] = '1';
989 } else {
990 close(fd);
991 }
992 filename = bsdPath;
993 }
3b46e624 994
19cb3738
FB
995 if ( mediaIterator )
996 IOObjectRelease( mediaIterator );
997 }
998#endif
999 open_flags = O_BINARY;
1000 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
1001 open_flags |= O_RDWR;
1002 } else {
1003 open_flags |= O_RDONLY;
1004 bs->read_only = 1;
1005 }
33f00271
AZ
1006#ifdef O_DIRECT
1007 if (flags & BDRV_O_DIRECT)
1008 open_flags |= O_DIRECT;
1009#endif
19cb3738
FB
1010
1011 s->type = FTYPE_FILE;
1012#if defined(__linux__)
1013 if (strstart(filename, "/dev/cd", NULL)) {
1014 /* open will not fail even if no CD is inserted */
1015 open_flags |= O_NONBLOCK;
1016 s->type = FTYPE_CD;
1017 } else if (strstart(filename, "/dev/fd", NULL)) {
1018 s->type = FTYPE_FD;
6dd2db52 1019 s->fd_open_flags = open_flags;
19cb3738
FB
1020 /* open will not fail even if no floppy is inserted */
1021 open_flags |= O_NONBLOCK;
985a03b0
TS
1022 } else if (strstart(filename, "/dev/sg", NULL)) {
1023 bs->sg = 1;
19cb3738
FB
1024 }
1025#endif
1026 fd = open(filename, open_flags, 0644);
1027 if (fd < 0) {
1028 ret = -errno;
1029 if (ret == -EROFS)
1030 ret = -EACCES;
1031 return ret;
1032 }
1033 s->fd = fd;
53538725
AL
1034 for (i = 0; i < RAW_FD_POOL_SIZE; i++)
1035 s->fd_pool[i] = -1;
19cb3738
FB
1036#if defined(__linux__)
1037 /* close fd so that we can reopen it as needed */
1038 if (s->type == FTYPE_FD) {
1039 close(s->fd);
1040 s->fd = -1;
1041 s->fd_media_changed = 1;
1042 }
1043#endif
1044 return 0;
1045}
1046
03ff3ca3 1047#if defined(__linux__)
19cb3738
FB
1048/* Note: we do not have a reliable method to detect if the floppy is
1049 present. The current method is to try to open the floppy at every
1050 I/O and to keep it opened during a few hundreds of ms. */
1051static int fd_open(BlockDriverState *bs)
1052{
1053 BDRVRawState *s = bs->opaque;
1054 int last_media_present;
1055
1056 if (s->type != FTYPE_FD)
1057 return 0;
1058 last_media_present = (s->fd >= 0);
5fafdf24 1059 if (s->fd >= 0 &&
19cb3738
FB
1060 (qemu_get_clock(rt_clock) - s->fd_open_time) >= FD_OPEN_TIMEOUT) {
1061 close(s->fd);
1062 s->fd = -1;
53538725 1063 raw_close_fd_pool(s);
19cb3738
FB
1064#ifdef DEBUG_FLOPPY
1065 printf("Floppy closed\n");
1066#endif
1067 }
1068 if (s->fd < 0) {
5fafdf24 1069 if (s->fd_got_error &&
19cb3738
FB
1070 (qemu_get_clock(rt_clock) - s->fd_error_time) < FD_OPEN_TIMEOUT) {
1071#ifdef DEBUG_FLOPPY
1072 printf("No floppy (open delayed)\n");
1073#endif
1074 return -EIO;
1075 }
6dd2db52 1076 s->fd = open(bs->filename, s->fd_open_flags);
19cb3738
FB
1077 if (s->fd < 0) {
1078 s->fd_error_time = qemu_get_clock(rt_clock);
1079 s->fd_got_error = 1;
1080 if (last_media_present)
1081 s->fd_media_changed = 1;
1082#ifdef DEBUG_FLOPPY
1083 printf("No floppy\n");
1084#endif
1085 return -EIO;
1086 }
1087#ifdef DEBUG_FLOPPY
1088 printf("Floppy opened\n");
1089#endif
1090 }
1091 if (!last_media_present)
1092 s->fd_media_changed = 1;
1093 s->fd_open_time = qemu_get_clock(rt_clock);
1094 s->fd_got_error = 0;
1095 return 0;
1096}
19cb3738
FB
1097
1098static int raw_is_inserted(BlockDriverState *bs)
1099{
1100 BDRVRawState *s = bs->opaque;
1101 int ret;
1102
1103 switch(s->type) {
1104 case FTYPE_CD:
1105 ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
1106 if (ret == CDS_DISC_OK)
1107 return 1;
1108 else
1109 return 0;
1110 break;
1111 case FTYPE_FD:
1112 ret = fd_open(bs);
1113 return (ret >= 0);
1114 default:
1115 return 1;
1116 }
1117}
1118
1119/* currently only used by fdc.c, but a CD version would be good too */
1120static int raw_media_changed(BlockDriverState *bs)
1121{
1122 BDRVRawState *s = bs->opaque;
1123
1124 switch(s->type) {
1125 case FTYPE_FD:
1126 {
1127 int ret;
1128 /* XXX: we do not have a true media changed indication. It
1129 does not work if the floppy is changed without trying
1130 to read it */
1131 fd_open(bs);
1132 ret = s->fd_media_changed;
1133 s->fd_media_changed = 0;
1134#ifdef DEBUG_FLOPPY
1135 printf("Floppy changed=%d\n", ret);
1136#endif
1137 return ret;
1138 }
1139 default:
1140 return -ENOTSUP;
1141 }
1142}
1143
1144static int raw_eject(BlockDriverState *bs, int eject_flag)
1145{
1146 BDRVRawState *s = bs->opaque;
1147
1148 switch(s->type) {
1149 case FTYPE_CD:
1150 if (eject_flag) {
1151 if (ioctl (s->fd, CDROMEJECT, NULL) < 0)
1152 perror("CDROMEJECT");
1153 } else {
1154 if (ioctl (s->fd, CDROMCLOSETRAY, NULL) < 0)
1155 perror("CDROMEJECT");
1156 }
1157 break;
1158 case FTYPE_FD:
1159 {
1160 int fd;
1161 if (s->fd >= 0) {
1162 close(s->fd);
1163 s->fd = -1;
53538725 1164 raw_close_fd_pool(s);
19cb3738 1165 }
6dd2db52 1166 fd = open(bs->filename, s->fd_open_flags | O_NONBLOCK);
19cb3738
FB
1167 if (fd >= 0) {
1168 if (ioctl(fd, FDEJECT, 0) < 0)
1169 perror("FDEJECT");
1170 close(fd);
1171 }
1172 }
1173 break;
1174 default:
1175 return -ENOTSUP;
1176 }
1177 return 0;
1178}
1179
1180static int raw_set_locked(BlockDriverState *bs, int locked)
1181{
1182 BDRVRawState *s = bs->opaque;
1183
1184 switch(s->type) {
1185 case FTYPE_CD:
1186 if (ioctl (s->fd, CDROM_LOCKDOOR, locked) < 0) {
1187 /* Note: an error can happen if the distribution automatically
1188 mounts the CD-ROM */
1189 // perror("CDROM_LOCKDOOR");
1190 }
1191 break;
1192 default:
1193 return -ENOTSUP;
1194 }
1195 return 0;
1196}
1197
985a03b0
TS
1198static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1199{
1200 BDRVRawState *s = bs->opaque;
1201
1202 return ioctl(s->fd, req, buf);
1203}
19cb3738
FB
1204#else
1205
08af02e2
AL
1206static int fd_open(BlockDriverState *bs)
1207{
1208 return 0;
1209}
1210
19cb3738
FB
1211static int raw_is_inserted(BlockDriverState *bs)
1212{
1213 return 1;
1214}
1215
1216static int raw_media_changed(BlockDriverState *bs)
1217{
1218 return -ENOTSUP;
1219}
1220
1221static int raw_eject(BlockDriverState *bs, int eject_flag)
1222{
1223 return -ENOTSUP;
1224}
1225
1226static int raw_set_locked(BlockDriverState *bs, int locked)
1227{
1228 return -ENOTSUP;
1229}
1230
985a03b0
TS
1231static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1232{
1233 return -ENOTSUP;
1234}
19cb3738
FB
1235#endif /* !linux */
1236
1237BlockDriver bdrv_host_device = {
1238 "host_device",
1239 sizeof(BDRVRawState),
1240 NULL, /* no probe for protocols */
1241 hdev_open,
1242 NULL,
1243 NULL,
1244 raw_close,
1245 NULL,
1246 raw_flush,
3b46e624 1247
414f0dab 1248#ifdef CONFIG_AIO
19cb3738
FB
1249 .bdrv_aio_read = raw_aio_read,
1250 .bdrv_aio_write = raw_aio_write,
1251 .bdrv_aio_cancel = raw_aio_cancel,
1252 .aiocb_size = sizeof(RawAIOCB),
414f0dab 1253#endif
19cb3738
FB
1254 .bdrv_pread = raw_pread,
1255 .bdrv_pwrite = raw_pwrite,
1256 .bdrv_getlength = raw_getlength,
1257
1258 /* removable device support */
1259 .bdrv_is_inserted = raw_is_inserted,
1260 .bdrv_media_changed = raw_media_changed,
1261 .bdrv_eject = raw_eject,
1262 .bdrv_set_locked = raw_set_locked,
985a03b0
TS
1263 /* generic scsi device */
1264 .bdrv_ioctl = raw_ioctl,
19cb3738 1265};