Statistics
| Branch: | Revision:

root / block-raw.c @ 7aaabde7

History | View | Annotate | Download (35.1 kB)

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

1052 ce1a14dc pbrook
    ret = GetOverlappedResult(s->hfile, &acb->ov, &ret_count, TRUE);
1053 ce1a14dc pbrook
    if (!ret || ret_count != acb->count) {
1054 ce1a14dc pbrook
        acb->common.cb(acb->common.opaque, -EIO);
1055 83f64091 bellard
    } else {
1056 ce1a14dc pbrook
        acb->common.cb(acb->common.opaque, 0);
1057 83f64091 bellard
    }
1058 83f64091 bellard
}
1059 436e15b8 bellard
#endif
1060 ce1a14dc pbrook
1061 ce1a14dc pbrook
static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
1062 ce1a14dc pbrook
        int64_t sector_num, uint8_t *buf, int nb_sectors,
1063 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque)
1064 83f64091 bellard
{
1065 ce1a14dc pbrook
    RawAIOCB *acb;
1066 83f64091 bellard
    int64_t offset;
1067 83f64091 bellard
1068 ce1a14dc pbrook
    acb = qemu_aio_get(bs, cb, opaque);
1069 ce1a14dc pbrook
    if (acb->hEvent) {
1070 ce1a14dc pbrook
        acb->hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1071 ce1a14dc pbrook
        if (!acb->hEvent) {
1072 ce1a14dc pbrook
            qemu_aio_release(acb);
1073 ce1a14dc pbrook
            return NULL;
1074 ce1a14dc pbrook
        }
1075 ce1a14dc pbrook
    }
1076 ce1a14dc pbrook
    memset(&acb->ov, 0, sizeof(acb->ov));
1077 83f64091 bellard
    offset = sector_num * 512;
1078 ce1a14dc pbrook
    acb->ov.Offset = offset;
1079 ce1a14dc pbrook
    acb->ov.OffsetHigh = offset >> 32;
1080 ce1a14dc pbrook
    acb->ov.hEvent = acb->hEvent;
1081 ce1a14dc pbrook
    acb->count = nb_sectors * 512;
1082 faf07963 pbrook
#ifndef QEMU_IMG
1083 ce1a14dc pbrook
    qemu_add_wait_object(acb->ov.hEvent, raw_aio_cb, acb);
1084 436e15b8 bellard
#endif
1085 ce1a14dc pbrook
    return acb;
1086 83f64091 bellard
}
1087 83f64091 bellard
1088 ce1a14dc pbrook
static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
1089 ce1a14dc pbrook
        int64_t sector_num, uint8_t *buf, int nb_sectors,
1090 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque)
1091 83f64091 bellard
{
1092 83f64091 bellard
    BDRVRawState *s = bs->opaque;
1093 ce1a14dc pbrook
    RawAIOCB *acb;
1094 83f64091 bellard
    int ret;
1095 83f64091 bellard
1096 ce1a14dc pbrook
    acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
1097 ce1a14dc pbrook
    if (!acb)
1098 ce1a14dc pbrook
        return NULL;
1099 ce1a14dc pbrook
    ret = ReadFile(s->hfile, buf, acb->count, NULL, &acb->ov);
1100 ce1a14dc pbrook
    if (!ret) {
1101 ce1a14dc pbrook
        qemu_aio_release(acb);
1102 ce1a14dc pbrook
        return NULL;
1103 ce1a14dc pbrook
    }
1104 faf07963 pbrook
#ifdef QEMU_IMG
1105 ce1a14dc pbrook
    qemu_aio_release(acb);
1106 436e15b8 bellard
#endif
1107 ce1a14dc pbrook
    return (BlockDriverAIOCB *)acb;
1108 83f64091 bellard
}
1109 83f64091 bellard
1110 ce1a14dc pbrook
static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
1111 ce1a14dc pbrook
        int64_t sector_num, uint8_t *buf, int nb_sectors,
1112 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque)
1113 83f64091 bellard
{
1114 83f64091 bellard
    BDRVRawState *s = bs->opaque;
1115 ce1a14dc pbrook
    RawAIOCB *acb;
1116 ce1a14dc pbrook
    int ret;
1117 83f64091 bellard
1118 ce1a14dc pbrook
    acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
1119 ce1a14dc pbrook
    if (!acb)
1120 ce1a14dc pbrook
        return NULL;
1121 ce1a14dc pbrook
    ret = WriteFile(s->hfile, buf, acb->count, NULL, &acb->ov);
1122 ce1a14dc pbrook
    if (!ret) {
1123 ce1a14dc pbrook
        qemu_aio_release(acb);
1124 ce1a14dc pbrook
        return NULL;
1125 ce1a14dc pbrook
    }
1126 faf07963 pbrook
#ifdef QEMU_IMG
1127 ce1a14dc pbrook
    qemu_aio_release(acb);
1128 436e15b8 bellard
#endif
1129 ce1a14dc pbrook
    return (BlockDriverAIOCB *)acb;
1130 83f64091 bellard
}
1131 83f64091 bellard
1132 ce1a14dc pbrook
static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
1133 83f64091 bellard
{
1134 faf07963 pbrook
#ifndef QEMU_IMG
1135 ce1a14dc pbrook
    RawAIOCB *acb = (RawAIOCB *)blockacb;
1136 ce1a14dc pbrook
    BlockDriverState *bs = acb->common.bs;
1137 ce1a14dc pbrook
    BDRVRawState *s = bs->opaque;
1138 ce1a14dc pbrook
1139 ce1a14dc pbrook
    qemu_del_wait_object(acb->ov.hEvent, raw_aio_cb, acb);
1140 ce1a14dc pbrook
    /* XXX: if more than one async I/O it is not correct */
1141 ce1a14dc pbrook
    CancelIo(s->hfile);
1142 ce1a14dc pbrook
    qemu_aio_release(acb);
1143 ce1a14dc pbrook
#endif
1144 83f64091 bellard
}
1145 3b9f94e1 bellard
#endif /* #if 0 */
1146 83f64091 bellard
1147 83f64091 bellard
static void raw_flush(BlockDriverState *bs)
1148 83f64091 bellard
{
1149 3b9f94e1 bellard
    BDRVRawState *s = bs->opaque;
1150 3b9f94e1 bellard
    FlushFileBuffers(s->hfile);
1151 83f64091 bellard
}
1152 83f64091 bellard
1153 83f64091 bellard
static void raw_close(BlockDriverState *bs)
1154 83f64091 bellard
{
1155 83f64091 bellard
    BDRVRawState *s = bs->opaque;
1156 83f64091 bellard
    CloseHandle(s->hfile);
1157 83f64091 bellard
}
1158 83f64091 bellard
1159 83f64091 bellard
static int raw_truncate(BlockDriverState *bs, int64_t offset)
1160 83f64091 bellard
{
1161 83f64091 bellard
    BDRVRawState *s = bs->opaque;
1162 83f64091 bellard
    DWORD low, high;
1163 83f64091 bellard
1164 979b67ad bellard
    low = offset;
1165 979b67ad bellard
    high = offset >> 32;
1166 83f64091 bellard
    if (!SetFilePointer(s->hfile, low, &high, FILE_BEGIN))
1167 83f64091 bellard
        return -EIO;
1168 83f64091 bellard
    if (!SetEndOfFile(s->hfile))
1169 83f64091 bellard
        return -EIO;
1170 83f64091 bellard
    return 0;
1171 83f64091 bellard
}
1172 83f64091 bellard
1173 f45512fe bellard
static int64_t raw_getlength(BlockDriverState *bs)
1174 83f64091 bellard
{
1175 83f64091 bellard
    BDRVRawState *s = bs->opaque;
1176 83f64091 bellard
    LARGE_INTEGER l;
1177 5fafdf24 ths
    ULARGE_INTEGER available, total, total_free;
1178 e9c08226 ths
    DISK_GEOMETRY_EX dg;
1179 01781963 bellard
    DWORD count;
1180 01781963 bellard
    BOOL status;
1181 436e15b8 bellard
1182 f45512fe bellard
    switch(s->type) {
1183 19cb3738 bellard
    case FTYPE_FILE:
1184 19cb3738 bellard
        l.LowPart = GetFileSize(s->hfile, &l.HighPart);
1185 19cb3738 bellard
        if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
1186 19cb3738 bellard
            return -EIO;
1187 19cb3738 bellard
        break;
1188 19cb3738 bellard
    case FTYPE_CD:
1189 f45512fe bellard
        if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free))
1190 19cb3738 bellard
            return -EIO;
1191 f45512fe bellard
        l.QuadPart = total.QuadPart;
1192 19cb3738 bellard
        break;
1193 01781963 bellard
    case FTYPE_HARDDISK:
1194 e9c08226 ths
        status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
1195 01781963 bellard
                                 NULL, 0, &dg, sizeof(dg), &count, NULL);
1196 e9c08226 ths
        if (status != 0) {
1197 e9c08226 ths
            l = dg.DiskSize;
1198 01781963 bellard
        }
1199 01781963 bellard
        break;
1200 19cb3738 bellard
    default:
1201 19cb3738 bellard
        return -EIO;
1202 19cb3738 bellard
    }
1203 83f64091 bellard
    return l.QuadPart;
1204 83f64091 bellard
}
1205 83f64091 bellard
1206 83f64091 bellard
static int raw_create(const char *filename, int64_t total_size,
1207 83f64091 bellard
                      const char *backing_file, int flags)
1208 83f64091 bellard
{
1209 83f64091 bellard
    int fd;
1210 83f64091 bellard
1211 83f64091 bellard
    if (flags || backing_file)
1212 83f64091 bellard
        return -ENOTSUP;
1213 83f64091 bellard
1214 5fafdf24 ths
    fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
1215 83f64091 bellard
              0644);
1216 83f64091 bellard
    if (fd < 0)
1217 83f64091 bellard
        return -EIO;
1218 83f64091 bellard
    set_sparse(fd);
1219 83f64091 bellard
    ftruncate(fd, total_size * 512);
1220 83f64091 bellard
    close(fd);
1221 83f64091 bellard
    return 0;
1222 83f64091 bellard
}
1223 83f64091 bellard
1224 83f64091 bellard
void qemu_aio_init(void)
1225 83f64091 bellard
{
1226 83f64091 bellard
}
1227 83f64091 bellard
1228 83f64091 bellard
void qemu_aio_poll(void)
1229 83f64091 bellard
{
1230 83f64091 bellard
}
1231 83f64091 bellard
1232 62ee0211 ths
void qemu_aio_flush(void)
1233 62ee0211 ths
{
1234 62ee0211 ths
}
1235 62ee0211 ths
1236 83f64091 bellard
void qemu_aio_wait_start(void)
1237 83f64091 bellard
{
1238 83f64091 bellard
}
1239 83f64091 bellard
1240 83f64091 bellard
void qemu_aio_wait(void)
1241 83f64091 bellard
{
1242 faf07963 pbrook
#ifndef QEMU_IMG
1243 fd44d818 bellard
    qemu_bh_poll();
1244 fd44d818 bellard
#endif
1245 83f64091 bellard
}
1246 83f64091 bellard
1247 83f64091 bellard
void qemu_aio_wait_end(void)
1248 83f64091 bellard
{
1249 83f64091 bellard
}
1250 83f64091 bellard
1251 83f64091 bellard
BlockDriver bdrv_raw = {
1252 83f64091 bellard
    "raw",
1253 83f64091 bellard
    sizeof(BDRVRawState),
1254 83f64091 bellard
    NULL, /* no probe for protocols */
1255 83f64091 bellard
    raw_open,
1256 83f64091 bellard
    NULL,
1257 83f64091 bellard
    NULL,
1258 83f64091 bellard
    raw_close,
1259 83f64091 bellard
    raw_create,
1260 83f64091 bellard
    raw_flush,
1261 3b46e624 ths
1262 83f64091 bellard
#if 0
1263 83f64091 bellard
    .bdrv_aio_read = raw_aio_read,
1264 83f64091 bellard
    .bdrv_aio_write = raw_aio_write,
1265 83f64091 bellard
    .bdrv_aio_cancel = raw_aio_cancel,
1266 ce1a14dc pbrook
    .aiocb_size = sizeof(RawAIOCB);
1267 83f64091 bellard
#endif
1268 83f64091 bellard
    .protocol_name = "file",
1269 83f64091 bellard
    .bdrv_pread = raw_pread,
1270 83f64091 bellard
    .bdrv_pwrite = raw_pwrite,
1271 83f64091 bellard
    .bdrv_truncate = raw_truncate,
1272 83f64091 bellard
    .bdrv_getlength = raw_getlength,
1273 83f64091 bellard
};
1274 19cb3738 bellard
1275 19cb3738 bellard
/***********************************************/
1276 19cb3738 bellard
/* host device */
1277 19cb3738 bellard
1278 19cb3738 bellard
static int find_cdrom(char *cdrom_name, int cdrom_name_size)
1279 19cb3738 bellard
{
1280 19cb3738 bellard
    char drives[256], *pdrv = drives;
1281 19cb3738 bellard
    UINT type;
1282 19cb3738 bellard
1283 f45512fe bellard
    memset(drives, 0, sizeof(drives));
1284 19cb3738 bellard
    GetLogicalDriveStrings(sizeof(drives), drives);
1285 19cb3738 bellard
    while(pdrv[0] != '\0') {
1286 19cb3738 bellard
        type = GetDriveType(pdrv);
1287 19cb3738 bellard
        switch(type) {
1288 19cb3738 bellard
        case DRIVE_CDROM:
1289 19cb3738 bellard
            snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]);
1290 19cb3738 bellard
            return 0;
1291 19cb3738 bellard
            break;
1292 19cb3738 bellard
        }
1293 19cb3738 bellard
        pdrv += lstrlen(pdrv) + 1;
1294 19cb3738 bellard
    }
1295 19cb3738 bellard
    return -1;
1296 19cb3738 bellard
}
1297 19cb3738 bellard
1298 f45512fe bellard
static int find_device_type(BlockDriverState *bs, const char *filename)
1299 19cb3738 bellard
{
1300 f45512fe bellard
    BDRVRawState *s = bs->opaque;
1301 19cb3738 bellard
    UINT type;
1302 19cb3738 bellard
    const char *p;
1303 19cb3738 bellard
1304 19cb3738 bellard
    if (strstart(filename, "\\\\.\\", &p) ||
1305 19cb3738 bellard
        strstart(filename, "//./", &p)) {
1306 01781963 bellard
        if (stristart(p, "PhysicalDrive", NULL))
1307 01781963 bellard
            return FTYPE_HARDDISK;
1308 f45512fe bellard
        snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]);
1309 f45512fe bellard
        type = GetDriveType(s->drive_path);
1310 19cb3738 bellard
        if (type == DRIVE_CDROM)
1311 19cb3738 bellard
            return FTYPE_CD;
1312 19cb3738 bellard
        else
1313 19cb3738 bellard
            return FTYPE_FILE;
1314 19cb3738 bellard
    } else {
1315 19cb3738 bellard
        return FTYPE_FILE;
1316 19cb3738 bellard
    }
1317 19cb3738 bellard
}
1318 19cb3738 bellard
1319 19cb3738 bellard
static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
1320 19cb3738 bellard
{
1321 19cb3738 bellard
    BDRVRawState *s = bs->opaque;
1322 19cb3738 bellard
    int access_flags, create_flags;
1323 19cb3738 bellard
    DWORD overlapped;
1324 19cb3738 bellard
    char device_name[64];
1325 19cb3738 bellard
1326 19cb3738 bellard
    if (strstart(filename, "/dev/cdrom", NULL)) {
1327 19cb3738 bellard
        if (find_cdrom(device_name, sizeof(device_name)) < 0)
1328 19cb3738 bellard
            return -ENOENT;
1329 19cb3738 bellard
        filename = device_name;
1330 19cb3738 bellard
    } else {
1331 19cb3738 bellard
        /* transform drive letters into device name */
1332 19cb3738 bellard
        if (((filename[0] >= 'a' && filename[0] <= 'z') ||
1333 19cb3738 bellard
             (filename[0] >= 'A' && filename[0] <= 'Z')) &&
1334 19cb3738 bellard
            filename[1] == ':' && filename[2] == '\0') {
1335 19cb3738 bellard
            snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]);
1336 19cb3738 bellard
            filename = device_name;
1337 19cb3738 bellard
        }
1338 19cb3738 bellard
    }
1339 f45512fe bellard
    s->type = find_device_type(bs, filename);
1340 3b46e624 ths
1341 19cb3738 bellard
    if ((flags & BDRV_O_ACCESS) == O_RDWR) {
1342 19cb3738 bellard
        access_flags = GENERIC_READ | GENERIC_WRITE;
1343 19cb3738 bellard
    } else {
1344 19cb3738 bellard
        access_flags = GENERIC_READ;
1345 19cb3738 bellard
    }
1346 19cb3738 bellard
    create_flags = OPEN_EXISTING;
1347 19cb3738 bellard
1348 faf07963 pbrook
#ifdef QEMU_IMG
1349 3b9f94e1 bellard
    overlapped = FILE_ATTRIBUTE_NORMAL;
1350 19cb3738 bellard
#else
1351 19cb3738 bellard
    overlapped = FILE_FLAG_OVERLAPPED;
1352 19cb3738 bellard
#endif
1353 5fafdf24 ths
    s->hfile = CreateFile(filename, access_flags,
1354 19cb3738 bellard
                          FILE_SHARE_READ, NULL,
1355 3b9f94e1 bellard
                          create_flags, overlapped, NULL);
1356 54421cb1 ths
    if (s->hfile == INVALID_HANDLE_VALUE) {
1357 54421cb1 ths
        int err = GetLastError();
1358 54421cb1 ths
1359 54421cb1 ths
        if (err == ERROR_ACCESS_DENIED)
1360 54421cb1 ths
            return -EACCES;
1361 19cb3738 bellard
        return -1;
1362 54421cb1 ths
    }
1363 19cb3738 bellard
    return 0;
1364 19cb3738 bellard
}
1365 19cb3738 bellard
1366 19cb3738 bellard
#if 0
1367 19cb3738 bellard
/***********************************************/
1368 aa1f17c1 ths
/* removable device additional commands */
1369 19cb3738 bellard

1370 19cb3738 bellard
static int raw_is_inserted(BlockDriverState *bs)
1371 19cb3738 bellard
{
1372 19cb3738 bellard
    return 1;
1373 19cb3738 bellard
}
1374 19cb3738 bellard

1375 19cb3738 bellard
static int raw_media_changed(BlockDriverState *bs)
1376 19cb3738 bellard
{
1377 19cb3738 bellard
    return -ENOTSUP;
1378 19cb3738 bellard
}
1379 19cb3738 bellard

1380 19cb3738 bellard
static int raw_eject(BlockDriverState *bs, int eject_flag)
1381 19cb3738 bellard
{
1382 19cb3738 bellard
    DWORD ret_count;
1383 19cb3738 bellard

1384 19cb3738 bellard
    if (s->type == FTYPE_FILE)
1385 19cb3738 bellard
        return -ENOTSUP;
1386 19cb3738 bellard
    if (eject_flag) {
1387 5fafdf24 ths
        DeviceIoControl(s->hfile, IOCTL_STORAGE_EJECT_MEDIA,
1388 19cb3738 bellard
                        NULL, 0, NULL, 0, &lpBytesReturned, NULL);
1389 19cb3738 bellard
    } else {
1390 5fafdf24 ths
        DeviceIoControl(s->hfile, IOCTL_STORAGE_LOAD_MEDIA,
1391 19cb3738 bellard
                        NULL, 0, NULL, 0, &lpBytesReturned, NULL);
1392 19cb3738 bellard
    }
1393 19cb3738 bellard
}
1394 19cb3738 bellard

1395 19cb3738 bellard
static int raw_set_locked(BlockDriverState *bs, int locked)
1396 19cb3738 bellard
{
1397 19cb3738 bellard
    return -ENOTSUP;
1398 19cb3738 bellard
}
1399 19cb3738 bellard
#endif
1400 19cb3738 bellard
1401 19cb3738 bellard
BlockDriver bdrv_host_device = {
1402 19cb3738 bellard
    "host_device",
1403 19cb3738 bellard
    sizeof(BDRVRawState),
1404 19cb3738 bellard
    NULL, /* no probe for protocols */
1405 19cb3738 bellard
    hdev_open,
1406 19cb3738 bellard
    NULL,
1407 19cb3738 bellard
    NULL,
1408 19cb3738 bellard
    raw_close,
1409 19cb3738 bellard
    NULL,
1410 19cb3738 bellard
    raw_flush,
1411 3b46e624 ths
1412 19cb3738 bellard
#if 0
1413 19cb3738 bellard
    .bdrv_aio_read = raw_aio_read,
1414 19cb3738 bellard
    .bdrv_aio_write = raw_aio_write,
1415 19cb3738 bellard
    .bdrv_aio_cancel = raw_aio_cancel,
1416 19cb3738 bellard
    .aiocb_size = sizeof(RawAIOCB);
1417 19cb3738 bellard
#endif
1418 19cb3738 bellard
    .bdrv_pread = raw_pread,
1419 19cb3738 bellard
    .bdrv_pwrite = raw_pwrite,
1420 19cb3738 bellard
    .bdrv_getlength = raw_getlength,
1421 19cb3738 bellard
};
1422 83f64091 bellard
#endif /* _WIN32 */