Statistics
| Branch: | Revision:

root / block-raw-posix.c @ cf7a2fe2

History | View | Annotate | Download (23.5 kB)

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