blob: 4874b8e4a42b1067bcccc76d90e14e343eeb6889 [file] [log] [blame]
bellard83f64092006-08-01 16:21:11 +00001/*
ths223d4672007-12-15 17:28:36 +00002 * Block driver for RAW files (posix)
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard83f64092006-08-01 16:21:11 +00004 * Copyright (c) 2006 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard83f64092006-08-01 16:21:11 +00006 * 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 */
pbrookfaf07962007-11-11 02:51:17 +000024#include "qemu-common.h"
pbrook87ecb682007-11-17 17:14:51 +000025#include "qemu-timer.h"
aliguoribaf35cb2008-09-10 15:45:19 +000026#include "qemu-char.h"
bellard83f64092006-08-01 16:21:11 +000027#include "block_int.h"
28#include <assert.h>
blueswir1414f0da2008-08-15 18:20:52 +000029#ifdef CONFIG_AIO
aliguori3c529d92008-12-12 16:41:40 +000030#include "posix-aio-compat.h"
blueswir1414f0da2008-08-15 18:20:52 +000031#endif
bellard83f64092006-08-01 16:21:11 +000032
bellard83f64092006-08-01 16:21:11 +000033#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__
ths2e9671d2007-01-05 17:44:41 +000046#define _POSIX_PTHREAD_SEMANTICS 1
47#include <signal.h>
bellard83f64092006-08-01 16:21:11 +000048#include <sys/dkio.h>
49#endif
bellard19cb3732006-08-19 11:45:59 +000050#ifdef __linux__
51#include <sys/ioctl.h>
52#include <linux/cdrom.h>
53#include <linux/fd.h>
54#endif
ths1cb6c3f2006-12-21 19:14:11 +000055#ifdef __FreeBSD__
blueswir1543952c2008-08-24 10:43:12 +000056#include <signal.h>
ths1cb6c3f2006-12-21 19:14:11 +000057#include <sys/disk.h>
blueswir19f230112009-03-28 08:37:13 +000058#include <sys/cdio.h>
ths1cb6c3f2006-12-21 19:14:11 +000059#endif
bellard19cb3732006-08-19 11:45:59 +000060
blueswir1128ab2f2008-08-15 18:33:42 +000061#ifdef __OpenBSD__
62#include <sys/ioctl.h>
63#include <sys/disklabel.h>
64#include <sys/dkio.h>
65#endif
66
blueswir1c5e97232009-03-07 20:06:23 +000067#ifdef __DragonFly__
68#include <sys/ioctl.h>
69#include <sys/diskslice.h>
70#endif
71
bellard19cb3732006-08-19 11:45:59 +000072//#define DEBUG_FLOPPY
73
pbrookfaf07962007-11-11 02:51:17 +000074//#define DEBUG_BLOCK
aliguori03ff3ca2008-09-15 15:51:35 +000075#if defined(DEBUG_BLOCK)
aliguori93fcfe32009-01-15 22:34:14 +000076#define DEBUG_BLOCK_PRINT(formatCstr, args...) do { if (qemu_log_enabled()) \
aliguori31b1a7b2009-01-15 22:35:09 +000077 { qemu_log(formatCstr, ##args); qemu_log_flush(); } } while (0)
ths8c05dbf2007-09-13 12:29:23 +000078#else
79#define DEBUG_BLOCK_PRINT(formatCstr, args...)
80#endif
81
aliguorif6465572008-10-14 18:00:38 +000082/* OS X does not have O_DSYNC */
83#ifndef O_DSYNC
aliguori7ab064d2008-10-14 18:14:47 +000084#define O_DSYNC O_SYNC
aliguorif6465572008-10-14 18:00:38 +000085#endif
86
aliguori9f7965c2008-10-14 14:42:54 +000087/* 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
bellard19cb3732006-08-19 11:45:59 +000092#define FTYPE_FILE 0
93#define FTYPE_CD 1
94#define FTYPE_FD 2
95
bellardbed5cc52008-05-28 09:51:09 +000096#define ALIGNED_BUFFER_SIZE (32 * 512)
97
bellard19cb3732006-08-19 11:45:59 +000098/* 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
bellard83f64092006-08-01 16:21:11 +0000101
102typedef struct BDRVRawState {
103 int fd;
bellard19cb3732006-08-19 11:45:59 +0000104 int type;
ths8c05dbf2007-09-13 12:29:23 +0000105 unsigned int lseek_err_cnt;
bellard19cb3732006-08-19 11:45:59 +0000106#if defined(__linux__)
107 /* linux floppy specific */
blueswir16dd2db52008-05-07 15:26:22 +0000108 int fd_open_flags;
bellard19cb3732006-08-19 11:45:59 +0000109 int64_t fd_open_time;
110 int64_t fd_error_time;
111 int fd_got_error;
112 int fd_media_changed;
113#endif
blueswir19f230112009-03-28 08:37:13 +0000114#if defined(__FreeBSD__)
115 int cd_open_flags;
116#endif
bellardbed5cc52008-05-28 09:51:09 +0000117 uint8_t* aligned_buf;
bellard83f64092006-08-01 16:21:11 +0000118} BDRVRawState;
119
aliguoria76bab42008-09-22 19:17:18 +0000120static int posix_aio_init(void);
121
bellard19cb3732006-08-19 11:45:59 +0000122static int fd_open(BlockDriverState *bs);
bellard83f64092006-08-01 16:21:11 +0000123
blueswir19f230112009-03-28 08:37:13 +0000124#if defined(__FreeBSD__)
125static int cd_open(BlockDriverState *bs);
126#endif
127
128static int raw_is_inserted(BlockDriverState *bs);
129
bellard83f64092006-08-01 16:21:11 +0000130static int raw_open(BlockDriverState *bs, const char *filename, int flags)
131{
132 BDRVRawState *s = bs->opaque;
bellard19cb3732006-08-19 11:45:59 +0000133 int fd, open_flags, ret;
bellard83f64092006-08-01 16:21:11 +0000134
aliguoria76bab42008-09-22 19:17:18 +0000135 posix_aio_init();
136
ths8c05dbf2007-09-13 12:29:23 +0000137 s->lseek_err_cnt = 0;
138
bellard83f64092006-08-01 16:21:11 +0000139 open_flags = O_BINARY;
140 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
141 open_flags |= O_RDWR;
142 } else {
143 open_flags |= O_RDONLY;
144 bs->read_only = 1;
145 }
146 if (flags & BDRV_O_CREAT)
147 open_flags |= O_CREAT | O_TRUNC;
aliguori9f7965c2008-10-14 14:42:54 +0000148
149 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
150 * and O_DIRECT for no caching. */
151 if ((flags & BDRV_O_NOCACHE))
balrog33f00272007-12-24 14:33:24 +0000152 open_flags |= O_DIRECT;
aliguori9f7965c2008-10-14 14:42:54 +0000153 else if (!(flags & BDRV_O_CACHE_WB))
154 open_flags |= O_DSYNC;
bellard83f64092006-08-01 16:21:11 +0000155
bellard19cb3732006-08-19 11:45:59 +0000156 s->type = FTYPE_FILE;
157
bellard83f64092006-08-01 16:21:11 +0000158 fd = open(filename, open_flags, 0644);
bellard19cb3732006-08-19 11:45:59 +0000159 if (fd < 0) {
160 ret = -errno;
161 if (ret == -EROFS)
162 ret = -EACCES;
163 return ret;
164 }
bellard83f64092006-08-01 16:21:11 +0000165 s->fd = fd;
bellardbed5cc52008-05-28 09:51:09 +0000166 s->aligned_buf = NULL;
aliguori9f7965c2008-10-14 14:42:54 +0000167 if ((flags & BDRV_O_NOCACHE)) {
bellardbed5cc52008-05-28 09:51:09 +0000168 s->aligned_buf = qemu_memalign(512, ALIGNED_BUFFER_SIZE);
169 if (s->aligned_buf == NULL) {
170 ret = -errno;
171 close(fd);
172 return ret;
173 }
174 }
bellard83f64092006-08-01 16:21:11 +0000175 return 0;
176}
177
178/* XXX: use host sector size if necessary with:
179#ifdef DIOCGSECTORSIZE
180 {
181 unsigned int sectorsize = 512;
182 if (!ioctl(fd, DIOCGSECTORSIZE, &sectorsize) &&
183 sectorsize > bufsize)
184 bufsize = sectorsize;
185 }
186#endif
187#ifdef CONFIG_COCOA
188 u_int32_t blockSize = 512;
189 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
190 bufsize = blockSize;
191 }
192#endif
193*/
194
bellardbed5cc52008-05-28 09:51:09 +0000195/*
196 * offset and count are in bytes, but must be multiples of 512 for files
197 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
198 *
199 * This function may be called without alignment if the caller ensures
200 * that O_DIRECT is not in effect.
201 */
202static int raw_pread_aligned(BlockDriverState *bs, int64_t offset,
bellard83f64092006-08-01 16:21:11 +0000203 uint8_t *buf, int count)
204{
205 BDRVRawState *s = bs->opaque;
206 int ret;
ths3b46e622007-09-17 08:09:54 +0000207
bellard19cb3732006-08-19 11:45:59 +0000208 ret = fd_open(bs);
209 if (ret < 0)
210 return ret;
211
ths985a03b2007-12-24 16:10:43 +0000212 if (offset >= 0 && lseek(s->fd, offset, SEEK_SET) == (off_t)-1) {
ths8c05dbf2007-09-13 12:29:23 +0000213 ++(s->lseek_err_cnt);
214 if(s->lseek_err_cnt <= 10) {
j_mayer92868412007-09-21 06:09:39 +0000215 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
216 "] lseek failed : %d = %s\n",
ths8c05dbf2007-09-13 12:29:23 +0000217 s->fd, bs->filename, offset, buf, count,
218 bs->total_sectors, errno, strerror(errno));
219 }
220 return -1;
221 }
222 s->lseek_err_cnt=0;
223
bellard83f64092006-08-01 16:21:11 +0000224 ret = read(s->fd, buf, count);
ths8c05dbf2007-09-13 12:29:23 +0000225 if (ret == count)
226 goto label__raw_read__success;
227
j_mayer92868412007-09-21 06:09:39 +0000228 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
229 "] read failed %d : %d = %s\n",
ths8c05dbf2007-09-13 12:29:23 +0000230 s->fd, bs->filename, offset, buf, count,
231 bs->total_sectors, ret, errno, strerror(errno));
232
233 /* Try harder for CDrom. */
234 if (bs->type == BDRV_TYPE_CDROM) {
235 lseek(s->fd, offset, SEEK_SET);
236 ret = read(s->fd, buf, count);
237 if (ret == count)
238 goto label__raw_read__success;
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
j_mayer92868412007-09-21 06:09:39 +0000244 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
245 "] retry read failed %d : %d = %s\n",
ths8c05dbf2007-09-13 12:29:23 +0000246 s->fd, bs->filename, offset, buf, count,
247 bs->total_sectors, ret, errno, strerror(errno));
248 }
249
ths8c05dbf2007-09-13 12:29:23 +0000250label__raw_read__success:
251
bellard83f64092006-08-01 16:21:11 +0000252 return ret;
253}
254
bellardbed5cc52008-05-28 09:51:09 +0000255/*
256 * offset and count are in bytes, but must be multiples of 512 for files
257 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
258 *
259 * This function may be called without alignment if the caller ensures
260 * that O_DIRECT is not in effect.
261 */
262static int raw_pwrite_aligned(BlockDriverState *bs, int64_t offset,
bellard83f64092006-08-01 16:21:11 +0000263 const uint8_t *buf, int count)
264{
265 BDRVRawState *s = bs->opaque;
266 int ret;
ths3b46e622007-09-17 08:09:54 +0000267
bellard19cb3732006-08-19 11:45:59 +0000268 ret = fd_open(bs);
269 if (ret < 0)
aliguori4141d4c2009-01-15 20:44:26 +0000270 return -errno;
bellard19cb3732006-08-19 11:45:59 +0000271
ths985a03b2007-12-24 16:10:43 +0000272 if (offset >= 0 && lseek(s->fd, offset, SEEK_SET) == (off_t)-1) {
ths8c05dbf2007-09-13 12:29:23 +0000273 ++(s->lseek_err_cnt);
274 if(s->lseek_err_cnt) {
j_mayer92868412007-09-21 06:09:39 +0000275 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%"
276 PRId64 "] lseek failed : %d = %s\n",
ths8c05dbf2007-09-13 12:29:23 +0000277 s->fd, bs->filename, offset, buf, count,
278 bs->total_sectors, errno, strerror(errno));
279 }
aliguori4141d4c2009-01-15 20:44:26 +0000280 return -EIO;
ths8c05dbf2007-09-13 12:29:23 +0000281 }
282 s->lseek_err_cnt = 0;
283
bellard83f64092006-08-01 16:21:11 +0000284 ret = write(s->fd, buf, count);
ths8c05dbf2007-09-13 12:29:23 +0000285 if (ret == count)
286 goto label__raw_write__success;
287
j_mayer92868412007-09-21 06:09:39 +0000288 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
289 "] write failed %d : %d = %s\n",
ths8c05dbf2007-09-13 12:29:23 +0000290 s->fd, bs->filename, offset, buf, count,
291 bs->total_sectors, ret, errno, strerror(errno));
292
ths8c05dbf2007-09-13 12:29:23 +0000293label__raw_write__success:
294
aliguori4141d4c2009-01-15 20:44:26 +0000295 return (ret < 0) ? -errno : ret;
bellard83f64092006-08-01 16:21:11 +0000296}
297
bellardbed5cc52008-05-28 09:51:09 +0000298
bellardbed5cc52008-05-28 09:51:09 +0000299/*
300 * offset and count are in bytes and possibly not aligned. For files opened
301 * with O_DIRECT, necessary alignments are ensured before calling
302 * raw_pread_aligned to do the actual read.
303 */
304static int raw_pread(BlockDriverState *bs, int64_t offset,
305 uint8_t *buf, int count)
306{
307 BDRVRawState *s = bs->opaque;
308 int size, ret, shift, sum;
309
310 sum = 0;
311
312 if (s->aligned_buf != NULL) {
313
314 if (offset & 0x1ff) {
315 /* align offset on a 512 bytes boundary */
316
317 shift = offset & 0x1ff;
318 size = (shift + count + 0x1ff) & ~0x1ff;
319 if (size > ALIGNED_BUFFER_SIZE)
320 size = ALIGNED_BUFFER_SIZE;
321 ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, size);
322 if (ret < 0)
323 return ret;
324
325 size = 512 - shift;
326 if (size > count)
327 size = count;
328 memcpy(buf, s->aligned_buf + shift, size);
329
330 buf += size;
331 offset += size;
332 count -= size;
333 sum += size;
334
335 if (count == 0)
336 return sum;
337 }
338 if (count & 0x1ff || (uintptr_t) buf & 0x1ff) {
339
340 /* read on aligned buffer */
341
342 while (count) {
343
344 size = (count + 0x1ff) & ~0x1ff;
345 if (size > ALIGNED_BUFFER_SIZE)
346 size = ALIGNED_BUFFER_SIZE;
347
348 ret = raw_pread_aligned(bs, offset, s->aligned_buf, size);
349 if (ret < 0)
350 return ret;
351
352 size = ret;
353 if (size > count)
354 size = count;
355
356 memcpy(buf, s->aligned_buf, size);
357
358 buf += size;
359 offset += size;
360 count -= size;
361 sum += size;
362 }
363
364 return sum;
365 }
366 }
367
368 return raw_pread_aligned(bs, offset, buf, count) + sum;
369}
370
aliguorieda578e2009-03-12 19:57:16 +0000371static int raw_read(BlockDriverState *bs, int64_t sector_num,
372 uint8_t *buf, int nb_sectors)
373{
aliguori537a1d42009-03-13 03:12:03 +0000374 int ret;
375
376 ret = raw_pread(bs, sector_num * 512, buf, nb_sectors * 512);
377 if (ret == (nb_sectors * 512))
378 ret = 0;
379 return ret;
aliguorieda578e2009-03-12 19:57:16 +0000380}
381
bellardbed5cc52008-05-28 09:51:09 +0000382/*
383 * offset and count are in bytes and possibly not aligned. For files opened
384 * with O_DIRECT, necessary alignments are ensured before calling
385 * raw_pwrite_aligned to do the actual write.
386 */
387static int raw_pwrite(BlockDriverState *bs, int64_t offset,
388 const uint8_t *buf, int count)
389{
390 BDRVRawState *s = bs->opaque;
391 int size, ret, shift, sum;
392
393 sum = 0;
394
395 if (s->aligned_buf != NULL) {
396
397 if (offset & 0x1ff) {
398 /* align offset on a 512 bytes boundary */
399 shift = offset & 0x1ff;
400 ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, 512);
401 if (ret < 0)
402 return ret;
403
404 size = 512 - shift;
405 if (size > count)
406 size = count;
407 memcpy(s->aligned_buf + shift, buf, size);
408
409 ret = raw_pwrite_aligned(bs, offset - shift, s->aligned_buf, 512);
410 if (ret < 0)
411 return ret;
412
413 buf += size;
414 offset += size;
415 count -= size;
416 sum += size;
417
418 if (count == 0)
419 return sum;
420 }
421 if (count & 0x1ff || (uintptr_t) buf & 0x1ff) {
422
423 while ((size = (count & ~0x1ff)) != 0) {
424
425 if (size > ALIGNED_BUFFER_SIZE)
426 size = ALIGNED_BUFFER_SIZE;
427
428 memcpy(s->aligned_buf, buf, size);
429
430 ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, size);
431 if (ret < 0)
432 return ret;
433
434 buf += ret;
435 offset += ret;
436 count -= ret;
437 sum += ret;
438 }
439 /* here, count < 512 because (count & ~0x1ff) == 0 */
440 if (count) {
441 ret = raw_pread_aligned(bs, offset, s->aligned_buf, 512);
442 if (ret < 0)
443 return ret;
444 memcpy(s->aligned_buf, buf, count);
445
446 ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, 512);
447 if (ret < 0)
448 return ret;
449 if (count < ret)
450 ret = count;
451
452 sum += ret;
453 }
454 return sum;
455 }
456 }
457 return raw_pwrite_aligned(bs, offset, buf, count) + sum;
458}
459
aliguorieda578e2009-03-12 19:57:16 +0000460static int raw_write(BlockDriverState *bs, int64_t sector_num,
461 const uint8_t *buf, int nb_sectors)
462{
aliguori537a1d42009-03-13 03:12:03 +0000463 int ret;
464 ret = raw_pwrite(bs, sector_num * 512, buf, nb_sectors * 512);
465 if (ret == (nb_sectors * 512))
466 ret = 0;
467 return ret;
aliguorieda578e2009-03-12 19:57:16 +0000468}
469
blueswir1414f0da2008-08-15 18:20:52 +0000470#ifdef CONFIG_AIO
bellard83f64092006-08-01 16:21:11 +0000471/***********************************************************/
bellard19cb3732006-08-19 11:45:59 +0000472/* Unix AIO using POSIX AIO */
bellard83f64092006-08-01 16:21:11 +0000473
474typedef struct RawAIOCB {
pbrookce1a14d2006-08-07 02:38:06 +0000475 BlockDriverAIOCB common;
aliguori3c529d92008-12-12 16:41:40 +0000476 struct qemu_paiocb aiocb;
pbrookce1a14d2006-08-07 02:38:06 +0000477 struct RawAIOCB *next;
bellardbed5cc52008-05-28 09:51:09 +0000478 int ret;
bellard83f64092006-08-01 16:21:11 +0000479} RawAIOCB;
480
aliguoria76bab42008-09-22 19:17:18 +0000481typedef struct PosixAioState
bellard83f64092006-08-01 16:21:11 +0000482{
aliguori9e472e12008-10-08 19:50:24 +0000483 int rfd, wfd;
aliguoria76bab42008-09-22 19:17:18 +0000484 RawAIOCB *first_aio;
485} PosixAioState;
486
487static void posix_aio_read(void *opaque)
488{
489 PosixAioState *s = opaque;
pbrookce1a14d2006-08-07 02:38:06 +0000490 RawAIOCB *acb, **pacb;
bellard83f64092006-08-01 16:21:11 +0000491 int ret;
aliguori9e472e12008-10-08 19:50:24 +0000492 ssize_t len;
aliguori2c41a5f2008-09-11 14:32:27 +0000493
aliguorie20e8302008-11-13 19:23:17 +0000494 /* read all bytes from signal pipe */
495 for (;;) {
496 char bytes[16];
aliguori2c41a5f2008-09-11 14:32:27 +0000497
aliguorie20e8302008-11-13 19:23:17 +0000498 len = read(s->rfd, bytes, sizeof(bytes));
aliguori2c41a5f2008-09-11 14:32:27 +0000499 if (len == -1 && errno == EINTR)
aliguorie20e8302008-11-13 19:23:17 +0000500 continue; /* try again */
501 if (len == sizeof(bytes))
502 continue; /* more to read */
503 break;
504 }
bellard83f64092006-08-01 16:21:11 +0000505
506 for(;;) {
aliguoria76bab42008-09-22 19:17:18 +0000507 pacb = &s->first_aio;
bellard83f64092006-08-01 16:21:11 +0000508 for(;;) {
509 acb = *pacb;
510 if (!acb)
511 goto the_end;
aliguori3c529d92008-12-12 16:41:40 +0000512 ret = qemu_paio_error(&acb->aiocb);
bellard83f64092006-08-01 16:21:11 +0000513 if (ret == ECANCELED) {
514 /* remove the request */
pbrookce1a14d2006-08-07 02:38:06 +0000515 *pacb = acb->next;
516 qemu_aio_release(acb);
bellard83f64092006-08-01 16:21:11 +0000517 } else if (ret != EINPROGRESS) {
518 /* end of aio */
519 if (ret == 0) {
aliguori3c529d92008-12-12 16:41:40 +0000520 ret = qemu_paio_return(&acb->aiocb);
pbrookce1a14d2006-08-07 02:38:06 +0000521 if (ret == acb->aiocb.aio_nbytes)
bellard83f64092006-08-01 16:21:11 +0000522 ret = 0;
523 else
bellard19cb3732006-08-19 11:45:59 +0000524 ret = -EINVAL;
bellard83f64092006-08-01 16:21:11 +0000525 } else {
526 ret = -ret;
527 }
528 /* remove the request */
pbrookce1a14d2006-08-07 02:38:06 +0000529 *pacb = acb->next;
bellard83f64092006-08-01 16:21:11 +0000530 /* call the callback */
pbrookce1a14d2006-08-07 02:38:06 +0000531 acb->common.cb(acb->common.opaque, ret);
532 qemu_aio_release(acb);
bellard83f64092006-08-01 16:21:11 +0000533 break;
534 } else {
pbrookce1a14d2006-08-07 02:38:06 +0000535 pacb = &acb->next;
bellard83f64092006-08-01 16:21:11 +0000536 }
537 }
538 }
539 the_end: ;
540}
541
aliguoria76bab42008-09-22 19:17:18 +0000542static int posix_aio_flush(void *opaque)
543{
544 PosixAioState *s = opaque;
545 return !!s->first_aio;
546}
547
548static PosixAioState *posix_aio_state;
549
aliguori9e472e12008-10-08 19:50:24 +0000550static void aio_signal_handler(int signum)
551{
552 if (posix_aio_state) {
553 char byte = 0;
554
555 write(posix_aio_state->wfd, &byte, sizeof(byte));
556 }
557
558 qemu_service_io();
559}
560
aliguoria76bab42008-09-22 19:17:18 +0000561static int posix_aio_init(void)
aliguoribaf35cb2008-09-10 15:45:19 +0000562{
aliguori9e472e12008-10-08 19:50:24 +0000563 struct sigaction act;
aliguoria76bab42008-09-22 19:17:18 +0000564 PosixAioState *s;
aliguori9e472e12008-10-08 19:50:24 +0000565 int fds[2];
aliguori3c529d92008-12-12 16:41:40 +0000566 struct qemu_paioinit ai;
aliguoria76bab42008-09-22 19:17:18 +0000567
568 if (posix_aio_state)
569 return 0;
aliguoribaf35cb2008-09-10 15:45:19 +0000570
aliguoria76bab42008-09-22 19:17:18 +0000571 s = qemu_malloc(sizeof(PosixAioState));
aliguoribaf35cb2008-09-10 15:45:19 +0000572
aliguori9e472e12008-10-08 19:50:24 +0000573 sigfillset(&act.sa_mask);
574 act.sa_flags = 0; /* do not restart syscalls to interrupt select() */
575 act.sa_handler = aio_signal_handler;
576 sigaction(SIGUSR2, &act, NULL);
577
aliguoria76bab42008-09-22 19:17:18 +0000578 s->first_aio = NULL;
aliguori9e472e12008-10-08 19:50:24 +0000579 if (pipe(fds) == -1) {
580 fprintf(stderr, "failed to create pipe\n");
aliguori27463102008-09-27 20:58:43 +0000581 return -errno;
582 }
aliguori2c41a5f2008-09-11 14:32:27 +0000583
aliguori9e472e12008-10-08 19:50:24 +0000584 s->rfd = fds[0];
585 s->wfd = fds[1];
aliguori2c41a5f2008-09-11 14:32:27 +0000586
aliguorie20e8302008-11-13 19:23:17 +0000587 fcntl(s->rfd, F_SETFL, O_NONBLOCK);
aliguori9e472e12008-10-08 19:50:24 +0000588 fcntl(s->wfd, F_SETFL, O_NONBLOCK);
589
590 qemu_aio_set_fd_handler(s->rfd, posix_aio_read, NULL, posix_aio_flush, s);
aliguoribaf35cb2008-09-10 15:45:19 +0000591
aliguori3c529d92008-12-12 16:41:40 +0000592 memset(&ai, 0, sizeof(ai));
593 ai.aio_threads = 64;
594 ai.aio_num = 64;
595 qemu_paio_init(&ai);
aliguoriacce87f2008-09-26 16:12:14 +0000596
aliguoria76bab42008-09-22 19:17:18 +0000597 posix_aio_state = s;
aliguoribaf35cb2008-09-10 15:45:19 +0000598
aliguoria76bab42008-09-22 19:17:18 +0000599 return 0;
bellard83f64092006-08-01 16:21:11 +0000600}
601
pbrookce1a14d2006-08-07 02:38:06 +0000602static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
603 int64_t sector_num, uint8_t *buf, int nb_sectors,
604 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +0000605{
pbrookce1a14d2006-08-07 02:38:06 +0000606 BDRVRawState *s = bs->opaque;
607 RawAIOCB *acb;
bellard83f64092006-08-01 16:21:11 +0000608
bellard19cb3732006-08-19 11:45:59 +0000609 if (fd_open(bs) < 0)
610 return NULL;
611
pbrookce1a14d2006-08-07 02:38:06 +0000612 acb = qemu_aio_get(bs, cb, opaque);
613 if (!acb)
614 return NULL;
aliguori3c529d92008-12-12 16:41:40 +0000615 acb->aiocb.aio_fildes = s->fd;
blueswir155f11ca2009-01-24 11:54:21 +0000616 acb->aiocb.ev_signo = SIGUSR2;
pbrookce1a14d2006-08-07 02:38:06 +0000617 acb->aiocb.aio_buf = buf;
ths985a03b2007-12-24 16:10:43 +0000618 if (nb_sectors < 0)
619 acb->aiocb.aio_nbytes = -nb_sectors;
620 else
621 acb->aiocb.aio_nbytes = nb_sectors * 512;
pbrookce1a14d2006-08-07 02:38:06 +0000622 acb->aiocb.aio_offset = sector_num * 512;
aliguoria76bab42008-09-22 19:17:18 +0000623 acb->next = posix_aio_state->first_aio;
624 posix_aio_state->first_aio = acb;
pbrookce1a14d2006-08-07 02:38:06 +0000625 return acb;
bellard83f64092006-08-01 16:21:11 +0000626}
627
bellardbed5cc52008-05-28 09:51:09 +0000628static void raw_aio_em_cb(void* opaque)
629{
630 RawAIOCB *acb = opaque;
631 acb->common.cb(acb->common.opaque, acb->ret);
632 qemu_aio_release(acb);
633}
bellardbed5cc52008-05-28 09:51:09 +0000634
aliguori22bf1452009-01-29 17:02:08 +0000635static void raw_aio_remove(RawAIOCB *acb)
636{
637 RawAIOCB **pacb;
638
639 /* remove the callback from the queue */
640 pacb = &posix_aio_state->first_aio;
641 for(;;) {
642 if (*pacb == NULL) {
aliguori7a11b222009-02-26 16:40:19 +0000643 fprintf(stderr, "raw_aio_remove: aio request not found!\n");
aliguori22bf1452009-01-29 17:02:08 +0000644 break;
645 } else if (*pacb == acb) {
646 *pacb = acb->next;
647 qemu_aio_release(acb);
648 break;
649 }
aliguori7a11b222009-02-26 16:40:19 +0000650 pacb = &(*pacb)->next;
aliguori22bf1452009-01-29 17:02:08 +0000651 }
652}
653
pbrookce1a14d2006-08-07 02:38:06 +0000654static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
655 int64_t sector_num, uint8_t *buf, int nb_sectors,
656 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +0000657{
pbrookce1a14d2006-08-07 02:38:06 +0000658 RawAIOCB *acb;
bellard83f64092006-08-01 16:21:11 +0000659
bellardbed5cc52008-05-28 09:51:09 +0000660 /*
661 * If O_DIRECT is used and the buffer is not aligned fall back
662 * to synchronous IO.
663 */
bellardbed5cc52008-05-28 09:51:09 +0000664 BDRVRawState *s = bs->opaque;
665
666 if (unlikely(s->aligned_buf != NULL && ((uintptr_t) buf % 512))) {
667 QEMUBH *bh;
668 acb = qemu_aio_get(bs, cb, opaque);
669 acb->ret = raw_pread(bs, 512 * sector_num, buf, 512 * nb_sectors);
670 bh = qemu_bh_new(raw_aio_em_cb, acb);
671 qemu_bh_schedule(bh);
672 return &acb->common;
673 }
bellardbed5cc52008-05-28 09:51:09 +0000674
pbrookce1a14d2006-08-07 02:38:06 +0000675 acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
676 if (!acb)
677 return NULL;
aliguori3c529d92008-12-12 16:41:40 +0000678 if (qemu_paio_read(&acb->aiocb) < 0) {
aliguori22bf1452009-01-29 17:02:08 +0000679 raw_aio_remove(acb);
pbrookce1a14d2006-08-07 02:38:06 +0000680 return NULL;
ths5fafdf22007-09-16 21:08:06 +0000681 }
pbrookce1a14d2006-08-07 02:38:06 +0000682 return &acb->common;
bellard83f64092006-08-01 16:21:11 +0000683}
684
pbrookce1a14d2006-08-07 02:38:06 +0000685static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
686 int64_t sector_num, const uint8_t *buf, int nb_sectors,
687 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +0000688{
pbrookce1a14d2006-08-07 02:38:06 +0000689 RawAIOCB *acb;
690
bellardbed5cc52008-05-28 09:51:09 +0000691 /*
692 * If O_DIRECT is used and the buffer is not aligned fall back
693 * to synchronous IO.
694 */
bellardbed5cc52008-05-28 09:51:09 +0000695 BDRVRawState *s = bs->opaque;
696
697 if (unlikely(s->aligned_buf != NULL && ((uintptr_t) buf % 512))) {
698 QEMUBH *bh;
699 acb = qemu_aio_get(bs, cb, opaque);
700 acb->ret = raw_pwrite(bs, 512 * sector_num, buf, 512 * nb_sectors);
701 bh = qemu_bh_new(raw_aio_em_cb, acb);
702 qemu_bh_schedule(bh);
703 return &acb->common;
704 }
bellardbed5cc52008-05-28 09:51:09 +0000705
pbrookce1a14d2006-08-07 02:38:06 +0000706 acb = raw_aio_setup(bs, sector_num, (uint8_t*)buf, nb_sectors, cb, opaque);
707 if (!acb)
708 return NULL;
aliguori3c529d92008-12-12 16:41:40 +0000709 if (qemu_paio_write(&acb->aiocb) < 0) {
aliguori22bf1452009-01-29 17:02:08 +0000710 raw_aio_remove(acb);
pbrookce1a14d2006-08-07 02:38:06 +0000711 return NULL;
ths5fafdf22007-09-16 21:08:06 +0000712 }
pbrookce1a14d2006-08-07 02:38:06 +0000713 return &acb->common;
714}
715
716static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
717{
bellard83f64092006-08-01 16:21:11 +0000718 int ret;
pbrookce1a14d2006-08-07 02:38:06 +0000719 RawAIOCB *acb = (RawAIOCB *)blockacb;
bellard83f64092006-08-01 16:21:11 +0000720
aliguori3c529d92008-12-12 16:41:40 +0000721 ret = qemu_paio_cancel(acb->aiocb.aio_fildes, &acb->aiocb);
722 if (ret == QEMU_PAIO_NOTCANCELED) {
bellard83f64092006-08-01 16:21:11 +0000723 /* fail safe: if the aio could not be canceled, we wait for
724 it */
aliguori3c529d92008-12-12 16:41:40 +0000725 while (qemu_paio_error(&acb->aiocb) == EINPROGRESS);
bellard83f64092006-08-01 16:21:11 +0000726 }
727
aliguori22bf1452009-01-29 17:02:08 +0000728 raw_aio_remove(acb);
bellard83f64092006-08-01 16:21:11 +0000729}
aliguoria76bab42008-09-22 19:17:18 +0000730#else /* CONFIG_AIO */
731static int posix_aio_init(void)
blueswir1414f0da2008-08-15 18:20:52 +0000732{
blueswir1674a24a2008-10-03 19:00:40 +0000733 return 0;
blueswir1414f0da2008-08-15 18:20:52 +0000734}
blueswir1414f0da2008-08-15 18:20:52 +0000735#endif /* CONFIG_AIO */
736
aliguori53538722008-09-26 15:59:29 +0000737
bellard83f64092006-08-01 16:21:11 +0000738static void raw_close(BlockDriverState *bs)
739{
740 BDRVRawState *s = bs->opaque;
bellard19cb3732006-08-19 11:45:59 +0000741 if (s->fd >= 0) {
742 close(s->fd);
743 s->fd = -1;
bellardbed5cc52008-05-28 09:51:09 +0000744 if (s->aligned_buf != NULL)
745 qemu_free(s->aligned_buf);
bellard19cb3732006-08-19 11:45:59 +0000746 }
bellard83f64092006-08-01 16:21:11 +0000747}
748
749static int raw_truncate(BlockDriverState *bs, int64_t offset)
750{
751 BDRVRawState *s = bs->opaque;
bellard19cb3732006-08-19 11:45:59 +0000752 if (s->type != FTYPE_FILE)
753 return -ENOTSUP;
bellard83f64092006-08-01 16:21:11 +0000754 if (ftruncate(s->fd, offset) < 0)
755 return -errno;
756 return 0;
757}
758
blueswir1128ab2f2008-08-15 18:33:42 +0000759#ifdef __OpenBSD__
760static int64_t raw_getlength(BlockDriverState *bs)
761{
762 BDRVRawState *s = bs->opaque;
763 int fd = s->fd;
764 struct stat st;
765
766 if (fstat(fd, &st))
767 return -1;
768 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
769 struct disklabel dl;
770
771 if (ioctl(fd, DIOCGDINFO, &dl))
772 return -1;
773 return (uint64_t)dl.d_secsize *
774 dl.d_partitions[DISKPART(st.st_rdev)].p_size;
775 } else
776 return st.st_size;
777}
778#else /* !__OpenBSD__ */
bellard83f64092006-08-01 16:21:11 +0000779static int64_t raw_getlength(BlockDriverState *bs)
780{
781 BDRVRawState *s = bs->opaque;
782 int fd = s->fd;
783 int64_t size;
blueswir1179a2c12009-03-08 08:23:32 +0000784#ifdef HOST_BSD
bellard83f64092006-08-01 16:21:11 +0000785 struct stat sb;
blueswir19f230112009-03-28 08:37:13 +0000786#ifdef __FreeBSD__
787 int reopened = 0;
788#endif
bellard83f64092006-08-01 16:21:11 +0000789#endif
790#ifdef __sun__
791 struct dk_minfo minfo;
792 int rv;
793#endif
bellard19cb3732006-08-19 11:45:59 +0000794 int ret;
795
796 ret = fd_open(bs);
797 if (ret < 0)
798 return ret;
bellard83f64092006-08-01 16:21:11 +0000799
blueswir1179a2c12009-03-08 08:23:32 +0000800#ifdef HOST_BSD
blueswir19f230112009-03-28 08:37:13 +0000801#ifdef __FreeBSD__
802again:
803#endif
bellard83f64092006-08-01 16:21:11 +0000804 if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
805#ifdef DIOCGMEDIASIZE
806 if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
blueswir1c5e97232009-03-07 20:06:23 +0000807#elif defined(DIOCGPART)
808 {
809 struct partinfo pi;
810 if (ioctl(fd, DIOCGPART, &pi) == 0)
811 size = pi.media_size;
812 else
813 size = 0;
814 }
815 if (size == 0)
bellard83f64092006-08-01 16:21:11 +0000816#endif
817#ifdef CONFIG_COCOA
818 size = LONG_LONG_MAX;
819#else
820 size = lseek(fd, 0LL, SEEK_END);
821#endif
blueswir19f230112009-03-28 08:37:13 +0000822#ifdef __FreeBSD__
823 switch(s->type) {
824 case FTYPE_CD:
825 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
826 if (size == 2048LL * (unsigned)-1)
827 size = 0;
828 /* XXX no disc? maybe we need to reopen... */
829 if (size <= 0 && !reopened && cd_open(bs) >= 0) {
830 reopened = 1;
831 goto again;
832 }
833 }
834#endif
bellard83f64092006-08-01 16:21:11 +0000835 } else
836#endif
837#ifdef __sun__
838 /*
839 * use the DKIOCGMEDIAINFO ioctl to read the size.
840 */
841 rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo );
842 if ( rv != -1 ) {
843 size = minfo.dki_lbsize * minfo.dki_capacity;
844 } else /* there are reports that lseek on some devices
845 fails, but irc discussion said that contingency
846 on contingency was overkill */
847#endif
848 {
849 size = lseek(fd, 0, SEEK_END);
850 }
bellard83f64092006-08-01 16:21:11 +0000851 return size;
852}
blueswir1128ab2f2008-08-15 18:33:42 +0000853#endif
bellard83f64092006-08-01 16:21:11 +0000854
855static int raw_create(const char *filename, int64_t total_size,
856 const char *backing_file, int flags)
857{
858 int fd;
859
860 if (flags || backing_file)
861 return -ENOTSUP;
862
ths5fafdf22007-09-16 21:08:06 +0000863 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
bellard83f64092006-08-01 16:21:11 +0000864 0644);
865 if (fd < 0)
866 return -EIO;
867 ftruncate(fd, total_size * 512);
868 close(fd);
869 return 0;
870}
871
872static void raw_flush(BlockDriverState *bs)
873{
874 BDRVRawState *s = bs->opaque;
875 fsync(s->fd);
876}
877
878BlockDriver bdrv_raw = {
879 "raw",
880 sizeof(BDRVRawState),
881 NULL, /* no probe for protocols */
882 raw_open,
883 NULL,
884 NULL,
885 raw_close,
886 raw_create,
887 raw_flush,
ths3b46e622007-09-17 08:09:54 +0000888
blueswir1414f0da2008-08-15 18:20:52 +0000889#ifdef CONFIG_AIO
bellard83f64092006-08-01 16:21:11 +0000890 .bdrv_aio_read = raw_aio_read,
891 .bdrv_aio_write = raw_aio_write,
892 .bdrv_aio_cancel = raw_aio_cancel,
pbrookce1a14d2006-08-07 02:38:06 +0000893 .aiocb_size = sizeof(RawAIOCB),
blueswir1414f0da2008-08-15 18:20:52 +0000894#endif
aliguori3c529d92008-12-12 16:41:40 +0000895
aliguorieda578e2009-03-12 19:57:16 +0000896 .bdrv_read = raw_read,
897 .bdrv_write = raw_write,
bellard83f64092006-08-01 16:21:11 +0000898 .bdrv_truncate = raw_truncate,
899 .bdrv_getlength = raw_getlength,
900};
901
bellard19cb3732006-08-19 11:45:59 +0000902/***********************************************/
903/* host device */
904
905#ifdef CONFIG_COCOA
906static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
907static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
908
909kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
910{
ths5fafdf22007-09-16 21:08:06 +0000911 kern_return_t kernResult;
bellard19cb3732006-08-19 11:45:59 +0000912 mach_port_t masterPort;
913 CFMutableDictionaryRef classesToMatch;
914
915 kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
916 if ( KERN_SUCCESS != kernResult ) {
917 printf( "IOMasterPort returned %d\n", kernResult );
918 }
ths3b46e622007-09-17 08:09:54 +0000919
ths5fafdf22007-09-16 21:08:06 +0000920 classesToMatch = IOServiceMatching( kIOCDMediaClass );
bellard19cb3732006-08-19 11:45:59 +0000921 if ( classesToMatch == NULL ) {
922 printf( "IOServiceMatching returned a NULL dictionary.\n" );
923 } else {
924 CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
925 }
926 kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
927 if ( KERN_SUCCESS != kernResult )
928 {
929 printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
930 }
ths3b46e622007-09-17 08:09:54 +0000931
bellard19cb3732006-08-19 11:45:59 +0000932 return kernResult;
933}
934
935kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
936{
937 io_object_t nextMedia;
938 kern_return_t kernResult = KERN_FAILURE;
939 *bsdPath = '\0';
940 nextMedia = IOIteratorNext( mediaIterator );
941 if ( nextMedia )
942 {
943 CFTypeRef bsdPathAsCFString;
944 bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
945 if ( bsdPathAsCFString ) {
946 size_t devPathLength;
947 strcpy( bsdPath, _PATH_DEV );
948 strcat( bsdPath, "r" );
949 devPathLength = strlen( bsdPath );
950 if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
951 kernResult = KERN_SUCCESS;
952 }
953 CFRelease( bsdPathAsCFString );
954 }
955 IOObjectRelease( nextMedia );
956 }
ths3b46e622007-09-17 08:09:54 +0000957
bellard19cb3732006-08-19 11:45:59 +0000958 return kernResult;
959}
960
961#endif
962
963static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
964{
965 BDRVRawState *s = bs->opaque;
aliguori3c529d92008-12-12 16:41:40 +0000966 int fd, open_flags, ret;
bellard19cb3732006-08-19 11:45:59 +0000967
aliguoria76bab42008-09-22 19:17:18 +0000968 posix_aio_init();
969
bellard19cb3732006-08-19 11:45:59 +0000970#ifdef CONFIG_COCOA
971 if (strstart(filename, "/dev/cdrom", NULL)) {
972 kern_return_t kernResult;
973 io_iterator_t mediaIterator;
974 char bsdPath[ MAXPATHLEN ];
975 int fd;
ths5fafdf22007-09-16 21:08:06 +0000976
bellard19cb3732006-08-19 11:45:59 +0000977 kernResult = FindEjectableCDMedia( &mediaIterator );
978 kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
ths3b46e622007-09-17 08:09:54 +0000979
bellard19cb3732006-08-19 11:45:59 +0000980 if ( bsdPath[ 0 ] != '\0' ) {
981 strcat(bsdPath,"s0");
982 /* some CDs don't have a partition 0 */
983 fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
984 if (fd < 0) {
985 bsdPath[strlen(bsdPath)-1] = '1';
986 } else {
987 close(fd);
988 }
989 filename = bsdPath;
990 }
ths3b46e622007-09-17 08:09:54 +0000991
bellard19cb3732006-08-19 11:45:59 +0000992 if ( mediaIterator )
993 IOObjectRelease( mediaIterator );
994 }
995#endif
996 open_flags = O_BINARY;
997 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
998 open_flags |= O_RDWR;
999 } else {
1000 open_flags |= O_RDONLY;
1001 bs->read_only = 1;
1002 }
aliguori9f7965c2008-10-14 14:42:54 +00001003 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
1004 * and O_DIRECT for no caching. */
1005 if ((flags & BDRV_O_NOCACHE))
balrog33f00272007-12-24 14:33:24 +00001006 open_flags |= O_DIRECT;
aliguori9f7965c2008-10-14 14:42:54 +00001007 else if (!(flags & BDRV_O_CACHE_WB))
1008 open_flags |= O_DSYNC;
bellard19cb3732006-08-19 11:45:59 +00001009
1010 s->type = FTYPE_FILE;
1011#if defined(__linux__)
1012 if (strstart(filename, "/dev/cd", NULL)) {
1013 /* open will not fail even if no CD is inserted */
1014 open_flags |= O_NONBLOCK;
1015 s->type = FTYPE_CD;
1016 } else if (strstart(filename, "/dev/fd", NULL)) {
1017 s->type = FTYPE_FD;
blueswir16dd2db52008-05-07 15:26:22 +00001018 s->fd_open_flags = open_flags;
bellard19cb3732006-08-19 11:45:59 +00001019 /* open will not fail even if no floppy is inserted */
1020 open_flags |= O_NONBLOCK;
ths985a03b2007-12-24 16:10:43 +00001021 } else if (strstart(filename, "/dev/sg", NULL)) {
1022 bs->sg = 1;
bellard19cb3732006-08-19 11:45:59 +00001023 }
1024#endif
blueswir19f230112009-03-28 08:37:13 +00001025#if defined(__FreeBSD__)
1026 if (strstart(filename, "/dev/cd", NULL) ||
1027 strstart(filename, "/dev/acd", NULL)) {
1028 s->type = FTYPE_CD;
1029 s->cd_open_flags = open_flags;
1030 }
1031#endif
1032 s->fd = -1;
bellard19cb3732006-08-19 11:45:59 +00001033 fd = open(filename, open_flags, 0644);
1034 if (fd < 0) {
1035 ret = -errno;
1036 if (ret == -EROFS)
1037 ret = -EACCES;
1038 return ret;
1039 }
1040 s->fd = fd;
blueswir19f230112009-03-28 08:37:13 +00001041#if defined(__FreeBSD__)
1042 /* make sure the door isnt locked at this time */
1043 if (s->type == FTYPE_CD)
1044 ioctl (s->fd, CDIOCALLOW);
1045#endif
bellard19cb3732006-08-19 11:45:59 +00001046#if defined(__linux__)
1047 /* close fd so that we can reopen it as needed */
1048 if (s->type == FTYPE_FD) {
1049 close(s->fd);
1050 s->fd = -1;
1051 s->fd_media_changed = 1;
1052 }
1053#endif
1054 return 0;
1055}
1056
aliguori03ff3ca2008-09-15 15:51:35 +00001057#if defined(__linux__)
bellard19cb3732006-08-19 11:45:59 +00001058/* Note: we do not have a reliable method to detect if the floppy is
1059 present. The current method is to try to open the floppy at every
1060 I/O and to keep it opened during a few hundreds of ms. */
1061static int fd_open(BlockDriverState *bs)
1062{
1063 BDRVRawState *s = bs->opaque;
1064 int last_media_present;
1065
1066 if (s->type != FTYPE_FD)
1067 return 0;
1068 last_media_present = (s->fd >= 0);
ths5fafdf22007-09-16 21:08:06 +00001069 if (s->fd >= 0 &&
bellard19cb3732006-08-19 11:45:59 +00001070 (qemu_get_clock(rt_clock) - s->fd_open_time) >= FD_OPEN_TIMEOUT) {
1071 close(s->fd);
1072 s->fd = -1;
1073#ifdef DEBUG_FLOPPY
1074 printf("Floppy closed\n");
1075#endif
1076 }
1077 if (s->fd < 0) {
ths5fafdf22007-09-16 21:08:06 +00001078 if (s->fd_got_error &&
bellard19cb3732006-08-19 11:45:59 +00001079 (qemu_get_clock(rt_clock) - s->fd_error_time) < FD_OPEN_TIMEOUT) {
1080#ifdef DEBUG_FLOPPY
1081 printf("No floppy (open delayed)\n");
1082#endif
1083 return -EIO;
1084 }
blueswir16dd2db52008-05-07 15:26:22 +00001085 s->fd = open(bs->filename, s->fd_open_flags);
bellard19cb3732006-08-19 11:45:59 +00001086 if (s->fd < 0) {
1087 s->fd_error_time = qemu_get_clock(rt_clock);
1088 s->fd_got_error = 1;
1089 if (last_media_present)
1090 s->fd_media_changed = 1;
1091#ifdef DEBUG_FLOPPY
1092 printf("No floppy\n");
1093#endif
1094 return -EIO;
1095 }
1096#ifdef DEBUG_FLOPPY
1097 printf("Floppy opened\n");
1098#endif
1099 }
1100 if (!last_media_present)
1101 s->fd_media_changed = 1;
1102 s->fd_open_time = qemu_get_clock(rt_clock);
1103 s->fd_got_error = 0;
1104 return 0;
1105}
bellard19cb3732006-08-19 11:45:59 +00001106
1107static int raw_is_inserted(BlockDriverState *bs)
1108{
1109 BDRVRawState *s = bs->opaque;
1110 int ret;
1111
1112 switch(s->type) {
1113 case FTYPE_CD:
1114 ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
1115 if (ret == CDS_DISC_OK)
1116 return 1;
1117 else
1118 return 0;
1119 break;
1120 case FTYPE_FD:
1121 ret = fd_open(bs);
1122 return (ret >= 0);
1123 default:
1124 return 1;
1125 }
1126}
1127
1128/* currently only used by fdc.c, but a CD version would be good too */
1129static int raw_media_changed(BlockDriverState *bs)
1130{
1131 BDRVRawState *s = bs->opaque;
1132
1133 switch(s->type) {
1134 case FTYPE_FD:
1135 {
1136 int ret;
1137 /* XXX: we do not have a true media changed indication. It
1138 does not work if the floppy is changed without trying
1139 to read it */
1140 fd_open(bs);
1141 ret = s->fd_media_changed;
1142 s->fd_media_changed = 0;
1143#ifdef DEBUG_FLOPPY
1144 printf("Floppy changed=%d\n", ret);
1145#endif
1146 return ret;
1147 }
1148 default:
1149 return -ENOTSUP;
1150 }
1151}
1152
1153static int raw_eject(BlockDriverState *bs, int eject_flag)
1154{
1155 BDRVRawState *s = bs->opaque;
1156
1157 switch(s->type) {
1158 case FTYPE_CD:
1159 if (eject_flag) {
1160 if (ioctl (s->fd, CDROMEJECT, NULL) < 0)
1161 perror("CDROMEJECT");
1162 } else {
1163 if (ioctl (s->fd, CDROMCLOSETRAY, NULL) < 0)
1164 perror("CDROMEJECT");
1165 }
1166 break;
1167 case FTYPE_FD:
1168 {
1169 int fd;
1170 if (s->fd >= 0) {
1171 close(s->fd);
1172 s->fd = -1;
1173 }
blueswir16dd2db52008-05-07 15:26:22 +00001174 fd = open(bs->filename, s->fd_open_flags | O_NONBLOCK);
bellard19cb3732006-08-19 11:45:59 +00001175 if (fd >= 0) {
1176 if (ioctl(fd, FDEJECT, 0) < 0)
1177 perror("FDEJECT");
1178 close(fd);
1179 }
1180 }
1181 break;
1182 default:
1183 return -ENOTSUP;
1184 }
1185 return 0;
1186}
1187
1188static int raw_set_locked(BlockDriverState *bs, int locked)
1189{
1190 BDRVRawState *s = bs->opaque;
1191
1192 switch(s->type) {
1193 case FTYPE_CD:
1194 if (ioctl (s->fd, CDROM_LOCKDOOR, locked) < 0) {
1195 /* Note: an error can happen if the distribution automatically
1196 mounts the CD-ROM */
1197 // perror("CDROM_LOCKDOOR");
1198 }
1199 break;
1200 default:
1201 return -ENOTSUP;
1202 }
1203 return 0;
1204}
1205
ths985a03b2007-12-24 16:10:43 +00001206static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1207{
1208 BDRVRawState *s = bs->opaque;
1209
1210 return ioctl(s->fd, req, buf);
1211}
aliguori221f7152009-03-28 17:28:41 +00001212
1213static BlockDriverAIOCB *raw_aio_ioctl(BlockDriverState *bs,
1214 unsigned long int req, void *buf,
1215 BlockDriverCompletionFunc *cb, void *opaque)
1216{
1217 RawAIOCB *acb;
1218
1219 acb = raw_aio_setup(bs, 0, buf, 0, cb, opaque);
1220 if (!acb)
1221 return NULL;
1222
1223 acb->aiocb.aio_ioctl_cmd = req;
1224 if (qemu_paio_ioctl(&acb->aiocb) < 0) {
1225 raw_aio_remove(acb);
1226 return NULL;
1227 }
1228
1229 return &acb->common;
1230}
1231
blueswir19f230112009-03-28 08:37:13 +00001232#elif defined(__FreeBSD__)
1233
1234static int fd_open(BlockDriverState *bs)
1235{
1236 BDRVRawState *s = bs->opaque;
1237
1238 /* this is just to ensure s->fd is sane (its called by io ops) */
1239 if (s->fd >= 0)
1240 return 0;
1241 return -EIO;
1242}
1243
1244static int cd_open(BlockDriverState *bs)
1245{
1246#if defined(__FreeBSD__)
1247 BDRVRawState *s = bs->opaque;
1248 int fd;
1249
1250 switch(s->type) {
1251 case FTYPE_CD:
1252 /* XXX force reread of possibly changed/newly loaded disc,
1253 * FreeBSD seems to not notice sometimes... */
1254 if (s->fd >= 0)
1255 close (s->fd);
1256 fd = open(bs->filename, s->cd_open_flags, 0644);
1257 if (fd < 0) {
1258 s->fd = -1;
1259 return -EIO;
1260 }
1261 s->fd = fd;
1262 /* make sure the door isnt locked at this time */
1263 ioctl (s->fd, CDIOCALLOW);
1264 }
1265#endif
1266 return 0;
1267}
1268
1269static int raw_is_inserted(BlockDriverState *bs)
1270{
1271 BDRVRawState *s = bs->opaque;
1272
1273 switch(s->type) {
1274 case FTYPE_CD:
1275 return (raw_getlength(bs) > 0);
1276 case FTYPE_FD:
1277 /* XXX handle this */
1278 /* FALLTHRU */
1279 default:
1280 return 1;
1281 }
1282}
1283
1284static int raw_media_changed(BlockDriverState *bs)
1285{
1286 return -ENOTSUP;
1287}
1288
1289static int raw_eject(BlockDriverState *bs, int eject_flag)
1290{
1291 BDRVRawState *s = bs->opaque;
1292
1293 switch(s->type) {
1294 case FTYPE_CD:
1295 if (s->fd < 0)
1296 return -ENOTSUP;
1297 (void) ioctl (s->fd, CDIOCALLOW);
1298 if (eject_flag) {
1299 if (ioctl (s->fd, CDIOCEJECT) < 0)
1300 perror("CDIOCEJECT");
1301 } else {
1302 if (ioctl (s->fd, CDIOCCLOSE) < 0)
1303 perror("CDIOCCLOSE");
1304 }
1305 if (cd_open(bs) < 0)
1306 return -ENOTSUP;
1307 break;
1308 case FTYPE_FD:
1309 /* XXX handle this */
1310 /* FALLTHRU */
1311 default:
1312 return -ENOTSUP;
1313 }
1314 return 0;
1315}
1316
1317static int raw_set_locked(BlockDriverState *bs, int locked)
1318{
1319 BDRVRawState *s = bs->opaque;
1320
1321 switch(s->type) {
1322 case FTYPE_CD:
1323 if (s->fd < 0)
1324 return -ENOTSUP;
1325 if (ioctl (s->fd, (locked ? CDIOCPREVENT : CDIOCALLOW)) < 0) {
1326 /* Note: an error can happen if the distribution automatically
1327 mounts the CD-ROM */
1328 // perror("CDROM_LOCKDOOR");
1329 }
1330 break;
1331 default:
1332 return -ENOTSUP;
1333 }
1334 return 0;
1335}
1336
1337static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1338{
1339 return -ENOTSUP;
1340}
1341#else /* !linux && !FreeBSD */
bellard19cb3732006-08-19 11:45:59 +00001342
aliguori08af02e2008-09-15 16:48:11 +00001343static int fd_open(BlockDriverState *bs)
1344{
1345 return 0;
1346}
1347
bellard19cb3732006-08-19 11:45:59 +00001348static int raw_is_inserted(BlockDriverState *bs)
1349{
1350 return 1;
1351}
1352
1353static int raw_media_changed(BlockDriverState *bs)
1354{
1355 return -ENOTSUP;
1356}
1357
1358static int raw_eject(BlockDriverState *bs, int eject_flag)
1359{
1360 return -ENOTSUP;
1361}
1362
1363static int raw_set_locked(BlockDriverState *bs, int locked)
1364{
1365 return -ENOTSUP;
1366}
1367
ths985a03b2007-12-24 16:10:43 +00001368static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1369{
1370 return -ENOTSUP;
1371}
aliguori221f7152009-03-28 17:28:41 +00001372
1373static BlockDriverAIOCB *raw_aio_ioctl(BlockDriverState *bs,
1374 unsigned long int req, void *buf,
1375 BlockDriverCompletionFunc *cb, void *opaque)
1376{
blueswir1bbeea532009-03-30 17:51:29 +00001377 return NULL;
aliguori221f7152009-03-28 17:28:41 +00001378}
blueswir19f230112009-03-28 08:37:13 +00001379#endif /* !linux && !FreeBSD */
bellard19cb3732006-08-19 11:45:59 +00001380
1381BlockDriver bdrv_host_device = {
aurel32e60f4692009-03-07 22:00:29 +00001382 .format_name = "host_device",
1383 .instance_size = sizeof(BDRVRawState),
1384 .bdrv_open = hdev_open,
1385 .bdrv_close = raw_close,
1386 .bdrv_flush = raw_flush,
ths3b46e622007-09-17 08:09:54 +00001387
blueswir1414f0da2008-08-15 18:20:52 +00001388#ifdef CONFIG_AIO
aurel32e60f4692009-03-07 22:00:29 +00001389 .bdrv_aio_read = raw_aio_read,
1390 .bdrv_aio_write = raw_aio_write,
1391 .bdrv_aio_cancel = raw_aio_cancel,
1392 .aiocb_size = sizeof(RawAIOCB),
blueswir1414f0da2008-08-15 18:20:52 +00001393#endif
aliguori3c529d92008-12-12 16:41:40 +00001394
aliguorieda578e2009-03-12 19:57:16 +00001395 .bdrv_read = raw_read,
1396 .bdrv_write = raw_write,
aurel32e60f4692009-03-07 22:00:29 +00001397 .bdrv_getlength = raw_getlength,
bellard19cb3732006-08-19 11:45:59 +00001398
1399 /* removable device support */
aurel32e60f4692009-03-07 22:00:29 +00001400 .bdrv_is_inserted = raw_is_inserted,
1401 .bdrv_media_changed = raw_media_changed,
1402 .bdrv_eject = raw_eject,
1403 .bdrv_set_locked = raw_set_locked,
ths985a03b2007-12-24 16:10:43 +00001404 /* generic scsi device */
aurel32e60f4692009-03-07 22:00:29 +00001405 .bdrv_ioctl = raw_ioctl,
aliguori221f7152009-03-28 17:28:41 +00001406 .bdrv_aio_ioctl = raw_aio_ioctl,
bellard19cb3732006-08-19 11:45:59 +00001407};