Statistics
| Branch: | Revision:

root / block-raw-posix.c @ 7ffa4767

History | View | Annotate | Download (36.4 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
#include "qemu-timer.h"
26 baf35cb9 aliguori
#include "qemu-char.h"
27 83f64091 bellard
#include "block_int.h"
28 83f64091 bellard
#include <assert.h>
29 414f0dab blueswir1
#ifdef CONFIG_AIO
30 3c529d93 aliguori
#include "posix-aio-compat.h"
31 414f0dab blueswir1
#endif
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 543952ca blueswir1
#include <signal.h>
57 1cb6c3fd ths
#include <sys/disk.h>
58 9f23011a blueswir1
#include <sys/cdio.h>
59 1cb6c3fd ths
#endif
60 83f64091 bellard
61 128ab2ff blueswir1
#ifdef __OpenBSD__
62 128ab2ff blueswir1
#include <sys/ioctl.h>
63 128ab2ff blueswir1
#include <sys/disklabel.h>
64 128ab2ff blueswir1
#include <sys/dkio.h>
65 128ab2ff blueswir1
#endif
66 128ab2ff blueswir1
67 c5e97233 blueswir1
#ifdef __DragonFly__
68 c5e97233 blueswir1
#include <sys/ioctl.h>
69 c5e97233 blueswir1
#include <sys/diskslice.h>
70 c5e97233 blueswir1
#endif
71 c5e97233 blueswir1
72 19cb3738 bellard
//#define DEBUG_FLOPPY
73 83f64091 bellard
74 faf07963 pbrook
//#define DEBUG_BLOCK
75 03ff3ca3 aliguori
#if defined(DEBUG_BLOCK)
76 93fcfe39 aliguori
#define DEBUG_BLOCK_PRINT(formatCstr, args...) do { if (qemu_log_enabled())        \
77 31b1a7b4 aliguori
    { qemu_log(formatCstr, ##args); qemu_log_flush(); } } while (0)
78 8c05dbf9 ths
#else
79 8c05dbf9 ths
#define DEBUG_BLOCK_PRINT(formatCstr, args...)
80 8c05dbf9 ths
#endif
81 8c05dbf9 ths
82 f6465578 aliguori
/* OS X does not have O_DSYNC */
83 f6465578 aliguori
#ifndef O_DSYNC
84 7ab064d2 aliguori
#define O_DSYNC O_SYNC
85 f6465578 aliguori
#endif
86 f6465578 aliguori
87 9f7965c7 aliguori
/* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */
88 9f7965c7 aliguori
#ifndef O_DIRECT
89 9f7965c7 aliguori
#define O_DIRECT O_DSYNC
90 9f7965c7 aliguori
#endif
91 9f7965c7 aliguori
92 19cb3738 bellard
#define FTYPE_FILE   0
93 19cb3738 bellard
#define FTYPE_CD     1
94 19cb3738 bellard
#define FTYPE_FD     2
95 83f64091 bellard
96 bed5cc52 bellard
#define ALIGNED_BUFFER_SIZE (32 * 512)
97 bed5cc52 bellard
98 19cb3738 bellard
/* if the FD is not accessed during that time (in ms), we try to
99 19cb3738 bellard
   reopen it to see if the disk has been changed */
100 19cb3738 bellard
#define FD_OPEN_TIMEOUT 1000
101 83f64091 bellard
102 19cb3738 bellard
typedef struct BDRVRawState {
103 19cb3738 bellard
    int fd;
104 19cb3738 bellard
    int type;
105 8c05dbf9 ths
    unsigned int lseek_err_cnt;
106 19cb3738 bellard
#if defined(__linux__)
107 19cb3738 bellard
    /* linux floppy specific */
108 6dd2db52 blueswir1
    int fd_open_flags;
109 19cb3738 bellard
    int64_t fd_open_time;
110 19cb3738 bellard
    int64_t fd_error_time;
111 19cb3738 bellard
    int fd_got_error;
112 19cb3738 bellard
    int fd_media_changed;
113 83f64091 bellard
#endif
114 9f23011a blueswir1
#if defined(__FreeBSD__)
115 9f23011a blueswir1
    int cd_open_flags;
116 9f23011a blueswir1
#endif
117 bed5cc52 bellard
    uint8_t* aligned_buf;
118 19cb3738 bellard
} BDRVRawState;
119 19cb3738 bellard
120 a76bab49 aliguori
static int posix_aio_init(void);
121 a76bab49 aliguori
122 19cb3738 bellard
static int fd_open(BlockDriverState *bs);
123 83f64091 bellard
124 9f23011a blueswir1
#if defined(__FreeBSD__)
125 9f23011a blueswir1
static int cd_open(BlockDriverState *bs);
126 9f23011a blueswir1
#endif
127 9f23011a blueswir1
128 9f23011a blueswir1
static int raw_is_inserted(BlockDriverState *bs);
129 9f23011a blueswir1
130 83f64091 bellard
static int raw_open(BlockDriverState *bs, const char *filename, int flags)
131 83f64091 bellard
{
132 83f64091 bellard
    BDRVRawState *s = bs->opaque;
133 19cb3738 bellard
    int fd, open_flags, ret;
134 83f64091 bellard
135 a76bab49 aliguori
    posix_aio_init();
136 a76bab49 aliguori
137 8c05dbf9 ths
    s->lseek_err_cnt = 0;
138 8c05dbf9 ths
139 83f64091 bellard
    open_flags = O_BINARY;
140 83f64091 bellard
    if ((flags & BDRV_O_ACCESS) == O_RDWR) {
141 83f64091 bellard
        open_flags |= O_RDWR;
142 83f64091 bellard
    } else {
143 83f64091 bellard
        open_flags |= O_RDONLY;
144 83f64091 bellard
        bs->read_only = 1;
145 83f64091 bellard
    }
146 83f64091 bellard
    if (flags & BDRV_O_CREAT)
147 83f64091 bellard
        open_flags |= O_CREAT | O_TRUNC;
148 9f7965c7 aliguori
149 9f7965c7 aliguori
    /* Use O_DSYNC for write-through caching, no flags for write-back caching,
150 9f7965c7 aliguori
     * and O_DIRECT for no caching. */
151 9f7965c7 aliguori
    if ((flags & BDRV_O_NOCACHE))
152 33f00271 balrog
        open_flags |= O_DIRECT;
153 9f7965c7 aliguori
    else if (!(flags & BDRV_O_CACHE_WB))
154 9f7965c7 aliguori
        open_flags |= O_DSYNC;
155 83f64091 bellard
156 19cb3738 bellard
    s->type = FTYPE_FILE;
157 19cb3738 bellard
158 83f64091 bellard
    fd = open(filename, open_flags, 0644);
159 19cb3738 bellard
    if (fd < 0) {
160 19cb3738 bellard
        ret = -errno;
161 19cb3738 bellard
        if (ret == -EROFS)
162 19cb3738 bellard
            ret = -EACCES;
163 19cb3738 bellard
        return ret;
164 19cb3738 bellard
    }
165 83f64091 bellard
    s->fd = fd;
166 bed5cc52 bellard
    s->aligned_buf = NULL;
167 9f7965c7 aliguori
    if ((flags & BDRV_O_NOCACHE)) {
168 bed5cc52 bellard
        s->aligned_buf = qemu_memalign(512, ALIGNED_BUFFER_SIZE);
169 bed5cc52 bellard
        if (s->aligned_buf == NULL) {
170 bed5cc52 bellard
            ret = -errno;
171 bed5cc52 bellard
            close(fd);
172 bed5cc52 bellard
            return ret;
173 bed5cc52 bellard
        }
174 bed5cc52 bellard
    }
175 83f64091 bellard
    return 0;
176 83f64091 bellard
}
177 83f64091 bellard
178 83f64091 bellard
/* XXX: use host sector size if necessary with:
179 83f64091 bellard
#ifdef DIOCGSECTORSIZE
180 83f64091 bellard
        {
181 83f64091 bellard
            unsigned int sectorsize = 512;
182 83f64091 bellard
            if (!ioctl(fd, DIOCGSECTORSIZE, &sectorsize) &&
183 83f64091 bellard
                sectorsize > bufsize)
184 83f64091 bellard
                bufsize = sectorsize;
185 83f64091 bellard
        }
186 83f64091 bellard
#endif
187 83f64091 bellard
#ifdef CONFIG_COCOA
188 83f64091 bellard
        u_int32_t   blockSize = 512;
189 83f64091 bellard
        if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
190 83f64091 bellard
            bufsize = blockSize;
191 83f64091 bellard
        }
192 83f64091 bellard
#endif
193 83f64091 bellard
*/
194 83f64091 bellard
195 bed5cc52 bellard
/*
196 bed5cc52 bellard
 * offset and count are in bytes, but must be multiples of 512 for files
197 bed5cc52 bellard
 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
198 bed5cc52 bellard
 *
199 bed5cc52 bellard
 * This function may be called without alignment if the caller ensures
200 bed5cc52 bellard
 * that O_DIRECT is not in effect.
201 bed5cc52 bellard
 */
202 bed5cc52 bellard
static int raw_pread_aligned(BlockDriverState *bs, int64_t offset,
203 83f64091 bellard
                     uint8_t *buf, int count)
204 83f64091 bellard
{
205 83f64091 bellard
    BDRVRawState *s = bs->opaque;
206 83f64091 bellard
    int ret;
207 3b46e624 ths
208 19cb3738 bellard
    ret = fd_open(bs);
209 19cb3738 bellard
    if (ret < 0)
210 19cb3738 bellard
        return ret;
211 19cb3738 bellard
212 985a03b0 ths
    if (offset >= 0 && lseek(s->fd, offset, SEEK_SET) == (off_t)-1) {
213 8c05dbf9 ths
        ++(s->lseek_err_cnt);
214 8c05dbf9 ths
        if(s->lseek_err_cnt <= 10) {
215 92868412 j_mayer
            DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
216 92868412 j_mayer
                              "] lseek failed : %d = %s\n",
217 8c05dbf9 ths
                              s->fd, bs->filename, offset, buf, count,
218 8c05dbf9 ths
                              bs->total_sectors, errno, strerror(errno));
219 8c05dbf9 ths
        }
220 8c05dbf9 ths
        return -1;
221 8c05dbf9 ths
    }
222 8c05dbf9 ths
    s->lseek_err_cnt=0;
223 8c05dbf9 ths
224 83f64091 bellard
    ret = read(s->fd, buf, count);
225 8c05dbf9 ths
    if (ret == count)
226 8c05dbf9 ths
        goto label__raw_read__success;
227 8c05dbf9 ths
228 92868412 j_mayer
    DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
229 92868412 j_mayer
                      "] read failed %d : %d = %s\n",
230 8c05dbf9 ths
                      s->fd, bs->filename, offset, buf, count,
231 8c05dbf9 ths
                      bs->total_sectors, ret, errno, strerror(errno));
232 8c05dbf9 ths
233 8c05dbf9 ths
    /* Try harder for CDrom. */
234 8c05dbf9 ths
    if (bs->type == BDRV_TYPE_CDROM) {
235 8c05dbf9 ths
        lseek(s->fd, offset, SEEK_SET);
236 8c05dbf9 ths
        ret = read(s->fd, buf, count);
237 8c05dbf9 ths
        if (ret == count)
238 8c05dbf9 ths
            goto label__raw_read__success;
239 8c05dbf9 ths
        lseek(s->fd, offset, SEEK_SET);
240 8c05dbf9 ths
        ret = read(s->fd, buf, count);
241 8c05dbf9 ths
        if (ret == count)
242 8c05dbf9 ths
            goto label__raw_read__success;
243 8c05dbf9 ths
244 92868412 j_mayer
        DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
245 92868412 j_mayer
                          "] retry read failed %d : %d = %s\n",
246 8c05dbf9 ths
                          s->fd, bs->filename, offset, buf, count,
247 8c05dbf9 ths
                          bs->total_sectors, ret, errno, strerror(errno));
248 8c05dbf9 ths
    }
249 8c05dbf9 ths
250 8c05dbf9 ths
label__raw_read__success:
251 8c05dbf9 ths
252 83f64091 bellard
    return ret;
253 83f64091 bellard
}
254 83f64091 bellard
255 bed5cc52 bellard
/*
256 bed5cc52 bellard
 * offset and count are in bytes, but must be multiples of 512 for files
257 bed5cc52 bellard
 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
258 bed5cc52 bellard
 *
259 bed5cc52 bellard
 * This function may be called without alignment if the caller ensures
260 bed5cc52 bellard
 * that O_DIRECT is not in effect.
261 bed5cc52 bellard
 */
262 bed5cc52 bellard
static int raw_pwrite_aligned(BlockDriverState *bs, int64_t offset,
263 83f64091 bellard
                      const uint8_t *buf, int count)
264 83f64091 bellard
{
265 83f64091 bellard
    BDRVRawState *s = bs->opaque;
266 83f64091 bellard
    int ret;
267 3b46e624 ths
268 19cb3738 bellard
    ret = fd_open(bs);
269 19cb3738 bellard
    if (ret < 0)
270 4141d4c2 aliguori
        return -errno;
271 19cb3738 bellard
272 985a03b0 ths
    if (offset >= 0 && lseek(s->fd, offset, SEEK_SET) == (off_t)-1) {
273 8c05dbf9 ths
        ++(s->lseek_err_cnt);
274 8c05dbf9 ths
        if(s->lseek_err_cnt) {
275 92868412 j_mayer
            DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%"
276 92868412 j_mayer
                              PRId64 "] lseek failed : %d = %s\n",
277 8c05dbf9 ths
                              s->fd, bs->filename, offset, buf, count,
278 8c05dbf9 ths
                              bs->total_sectors, errno, strerror(errno));
279 8c05dbf9 ths
        }
280 4141d4c2 aliguori
        return -EIO;
281 8c05dbf9 ths
    }
282 8c05dbf9 ths
    s->lseek_err_cnt = 0;
283 8c05dbf9 ths
284 83f64091 bellard
    ret = write(s->fd, buf, count);
285 8c05dbf9 ths
    if (ret == count)
286 8c05dbf9 ths
        goto label__raw_write__success;
287 8c05dbf9 ths
288 92868412 j_mayer
    DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
289 92868412 j_mayer
                      "] write failed %d : %d = %s\n",
290 8c05dbf9 ths
                      s->fd, bs->filename, offset, buf, count,
291 8c05dbf9 ths
                      bs->total_sectors, ret, errno, strerror(errno));
292 8c05dbf9 ths
293 8c05dbf9 ths
label__raw_write__success:
294 8c05dbf9 ths
295 4141d4c2 aliguori
    return  (ret < 0) ? -errno : ret;
296 83f64091 bellard
}
297 83f64091 bellard
298 bed5cc52 bellard
299 bed5cc52 bellard
/*
300 bed5cc52 bellard
 * offset and count are in bytes and possibly not aligned. For files opened
301 bed5cc52 bellard
 * with O_DIRECT, necessary alignments are ensured before calling
302 bed5cc52 bellard
 * raw_pread_aligned to do the actual read.
303 bed5cc52 bellard
 */
304 bed5cc52 bellard
static int raw_pread(BlockDriverState *bs, int64_t offset,
305 bed5cc52 bellard
                     uint8_t *buf, int count)
306 bed5cc52 bellard
{
307 bed5cc52 bellard
    BDRVRawState *s = bs->opaque;
308 bed5cc52 bellard
    int size, ret, shift, sum;
309 bed5cc52 bellard
310 bed5cc52 bellard
    sum = 0;
311 bed5cc52 bellard
312 bed5cc52 bellard
    if (s->aligned_buf != NULL)  {
313 bed5cc52 bellard
314 bed5cc52 bellard
        if (offset & 0x1ff) {
315 bed5cc52 bellard
            /* align offset on a 512 bytes boundary */
316 bed5cc52 bellard
317 bed5cc52 bellard
            shift = offset & 0x1ff;
318 bed5cc52 bellard
            size = (shift + count + 0x1ff) & ~0x1ff;
319 bed5cc52 bellard
            if (size > ALIGNED_BUFFER_SIZE)
320 bed5cc52 bellard
                size = ALIGNED_BUFFER_SIZE;
321 bed5cc52 bellard
            ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, size);
322 bed5cc52 bellard
            if (ret < 0)
323 bed5cc52 bellard
                return ret;
324 bed5cc52 bellard
325 bed5cc52 bellard
            size = 512 - shift;
326 bed5cc52 bellard
            if (size > count)
327 bed5cc52 bellard
                size = count;
328 bed5cc52 bellard
            memcpy(buf, s->aligned_buf + shift, size);
329 bed5cc52 bellard
330 bed5cc52 bellard
            buf += size;
331 bed5cc52 bellard
            offset += size;
332 bed5cc52 bellard
            count -= size;
333 bed5cc52 bellard
            sum += size;
334 bed5cc52 bellard
335 bed5cc52 bellard
            if (count == 0)
336 bed5cc52 bellard
                return sum;
337 bed5cc52 bellard
        }
338 bed5cc52 bellard
        if (count & 0x1ff || (uintptr_t) buf & 0x1ff) {
339 bed5cc52 bellard
340 bed5cc52 bellard
            /* read on aligned buffer */
341 bed5cc52 bellard
342 bed5cc52 bellard
            while (count) {
343 bed5cc52 bellard
344 bed5cc52 bellard
                size = (count + 0x1ff) & ~0x1ff;
345 bed5cc52 bellard
                if (size > ALIGNED_BUFFER_SIZE)
346 bed5cc52 bellard
                    size = ALIGNED_BUFFER_SIZE;
347 bed5cc52 bellard
348 bed5cc52 bellard
                ret = raw_pread_aligned(bs, offset, s->aligned_buf, size);
349 bed5cc52 bellard
                if (ret < 0)
350 bed5cc52 bellard
                    return ret;
351 bed5cc52 bellard
352 bed5cc52 bellard
                size = ret;
353 bed5cc52 bellard
                if (size > count)
354 bed5cc52 bellard
                    size = count;
355 bed5cc52 bellard
356 bed5cc52 bellard
                memcpy(buf, s->aligned_buf, size);
357 bed5cc52 bellard
358 bed5cc52 bellard
                buf += size;
359 bed5cc52 bellard
                offset += size;
360 bed5cc52 bellard
                count -= size;
361 bed5cc52 bellard
                sum += size;
362 bed5cc52 bellard
            }
363 bed5cc52 bellard
364 bed5cc52 bellard
            return sum;
365 bed5cc52 bellard
        }
366 bed5cc52 bellard
    }
367 bed5cc52 bellard
368 bed5cc52 bellard
    return raw_pread_aligned(bs, offset, buf, count) + sum;
369 bed5cc52 bellard
}
370 bed5cc52 bellard
371 eda578e5 aliguori
static int raw_read(BlockDriverState *bs, int64_t sector_num,
372 eda578e5 aliguori
                    uint8_t *buf, int nb_sectors)
373 eda578e5 aliguori
{
374 537a1d4b aliguori
    int ret;
375 537a1d4b aliguori
376 537a1d4b aliguori
    ret = raw_pread(bs, sector_num * 512, buf, nb_sectors * 512);
377 537a1d4b aliguori
    if (ret == (nb_sectors * 512))
378 537a1d4b aliguori
        ret = 0;
379 537a1d4b aliguori
    return ret;
380 eda578e5 aliguori
}
381 eda578e5 aliguori
382 bed5cc52 bellard
/*
383 bed5cc52 bellard
 * offset and count are in bytes and possibly not aligned. For files opened
384 bed5cc52 bellard
 * with O_DIRECT, necessary alignments are ensured before calling
385 bed5cc52 bellard
 * raw_pwrite_aligned to do the actual write.
386 bed5cc52 bellard
 */
387 bed5cc52 bellard
static int raw_pwrite(BlockDriverState *bs, int64_t offset,
388 bed5cc52 bellard
                      const uint8_t *buf, int count)
389 bed5cc52 bellard
{
390 bed5cc52 bellard
    BDRVRawState *s = bs->opaque;
391 bed5cc52 bellard
    int size, ret, shift, sum;
392 bed5cc52 bellard
393 bed5cc52 bellard
    sum = 0;
394 bed5cc52 bellard
395 bed5cc52 bellard
    if (s->aligned_buf != NULL) {
396 bed5cc52 bellard
397 bed5cc52 bellard
        if (offset & 0x1ff) {
398 bed5cc52 bellard
            /* align offset on a 512 bytes boundary */
399 bed5cc52 bellard
            shift = offset & 0x1ff;
400 bed5cc52 bellard
            ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, 512);
401 bed5cc52 bellard
            if (ret < 0)
402 bed5cc52 bellard
                return ret;
403 bed5cc52 bellard
404 bed5cc52 bellard
            size = 512 - shift;
405 bed5cc52 bellard
            if (size > count)
406 bed5cc52 bellard
                size = count;
407 bed5cc52 bellard
            memcpy(s->aligned_buf + shift, buf, size);
408 bed5cc52 bellard
409 bed5cc52 bellard
            ret = raw_pwrite_aligned(bs, offset - shift, s->aligned_buf, 512);
410 bed5cc52 bellard
            if (ret < 0)
411 bed5cc52 bellard
                return ret;
412 bed5cc52 bellard
413 bed5cc52 bellard
            buf += size;
414 bed5cc52 bellard
            offset += size;
415 bed5cc52 bellard
            count -= size;
416 bed5cc52 bellard
            sum += size;
417 bed5cc52 bellard
418 bed5cc52 bellard
            if (count == 0)
419 bed5cc52 bellard
                return sum;
420 bed5cc52 bellard
        }
421 bed5cc52 bellard
        if (count & 0x1ff || (uintptr_t) buf & 0x1ff) {
422 bed5cc52 bellard
423 bed5cc52 bellard
            while ((size = (count & ~0x1ff)) != 0) {
424 bed5cc52 bellard
425 bed5cc52 bellard
                if (size > ALIGNED_BUFFER_SIZE)
426 bed5cc52 bellard
                    size = ALIGNED_BUFFER_SIZE;
427 bed5cc52 bellard
428 bed5cc52 bellard
                memcpy(s->aligned_buf, buf, size);
429 bed5cc52 bellard
430 bed5cc52 bellard
                ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, size);
431 bed5cc52 bellard
                if (ret < 0)
432 bed5cc52 bellard
                    return ret;
433 bed5cc52 bellard
434 bed5cc52 bellard
                buf += ret;
435 bed5cc52 bellard
                offset += ret;
436 bed5cc52 bellard
                count -= ret;
437 bed5cc52 bellard
                sum += ret;
438 bed5cc52 bellard
            }
439 bed5cc52 bellard
            /* here, count < 512 because (count & ~0x1ff) == 0 */
440 bed5cc52 bellard
            if (count) {
441 bed5cc52 bellard
                ret = raw_pread_aligned(bs, offset, s->aligned_buf, 512);
442 bed5cc52 bellard
                if (ret < 0)
443 bed5cc52 bellard
                    return ret;
444 bed5cc52 bellard
                 memcpy(s->aligned_buf, buf, count);
445 bed5cc52 bellard
446 bed5cc52 bellard
                 ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, 512);
447 bed5cc52 bellard
                 if (ret < 0)
448 bed5cc52 bellard
                     return ret;
449 bed5cc52 bellard
                 if (count < ret)
450 bed5cc52 bellard
                     ret = count;
451 bed5cc52 bellard
452 bed5cc52 bellard
                 sum += ret;
453 bed5cc52 bellard
            }
454 bed5cc52 bellard
            return sum;
455 bed5cc52 bellard
        }
456 bed5cc52 bellard
    }
457 bed5cc52 bellard
    return raw_pwrite_aligned(bs, offset, buf, count) + sum;
458 bed5cc52 bellard
}
459 bed5cc52 bellard
460 eda578e5 aliguori
static int raw_write(BlockDriverState *bs, int64_t sector_num,
461 eda578e5 aliguori
                     const uint8_t *buf, int nb_sectors)
462 eda578e5 aliguori
{
463 537a1d4b aliguori
    int ret;
464 537a1d4b aliguori
    ret = raw_pwrite(bs, sector_num * 512, buf, nb_sectors * 512);
465 537a1d4b aliguori
    if (ret == (nb_sectors * 512))
466 537a1d4b aliguori
        ret = 0;
467 537a1d4b aliguori
    return ret;
468 eda578e5 aliguori
}
469 eda578e5 aliguori
470 414f0dab blueswir1
#ifdef CONFIG_AIO
471 83f64091 bellard
/***********************************************************/
472 19cb3738 bellard
/* Unix AIO using POSIX AIO */
473 83f64091 bellard
474 83f64091 bellard
typedef struct RawAIOCB {
475 ce1a14dc pbrook
    BlockDriverAIOCB common;
476 3c529d93 aliguori
    struct qemu_paiocb aiocb;
477 ce1a14dc pbrook
    struct RawAIOCB *next;
478 bed5cc52 bellard
    int ret;
479 83f64091 bellard
} RawAIOCB;
480 83f64091 bellard
481 a76bab49 aliguori
typedef struct PosixAioState
482 a76bab49 aliguori
{
483 9e472e10 aliguori
    int rfd, wfd;
484 a76bab49 aliguori
    RawAIOCB *first_aio;
485 a76bab49 aliguori
} PosixAioState;
486 83f64091 bellard
487 a76bab49 aliguori
static void posix_aio_read(void *opaque)
488 83f64091 bellard
{
489 a76bab49 aliguori
    PosixAioState *s = opaque;
490 ce1a14dc pbrook
    RawAIOCB *acb, **pacb;
491 83f64091 bellard
    int ret;
492 9e472e10 aliguori
    ssize_t len;
493 9e472e10 aliguori
494 e20e830b aliguori
    /* read all bytes from signal pipe */
495 e20e830b aliguori
    for (;;) {
496 e20e830b aliguori
        char bytes[16];
497 9e472e10 aliguori
498 e20e830b aliguori
        len = read(s->rfd, bytes, sizeof(bytes));
499 2c41a5f9 aliguori
        if (len == -1 && errno == EINTR)
500 e20e830b aliguori
            continue; /* try again */
501 e20e830b aliguori
        if (len == sizeof(bytes))
502 e20e830b aliguori
            continue; /* more to read */
503 e20e830b aliguori
        break;
504 e20e830b aliguori
    }
505 83f64091 bellard
506 83f64091 bellard
    for(;;) {
507 a76bab49 aliguori
        pacb = &s->first_aio;
508 83f64091 bellard
        for(;;) {
509 83f64091 bellard
            acb = *pacb;
510 83f64091 bellard
            if (!acb)
511 83f64091 bellard
                goto the_end;
512 3c529d93 aliguori
            ret = qemu_paio_error(&acb->aiocb);
513 83f64091 bellard
            if (ret == ECANCELED) {
514 83f64091 bellard
                /* remove the request */
515 ce1a14dc pbrook
                *pacb = acb->next;
516 ce1a14dc pbrook
                qemu_aio_release(acb);
517 83f64091 bellard
            } else if (ret != EINPROGRESS) {
518 83f64091 bellard
                /* end of aio */
519 83f64091 bellard
                if (ret == 0) {
520 3c529d93 aliguori
                    ret = qemu_paio_return(&acb->aiocb);
521 ce1a14dc pbrook
                    if (ret == acb->aiocb.aio_nbytes)
522 83f64091 bellard
                        ret = 0;
523 83f64091 bellard
                    else
524 19cb3738 bellard
                        ret = -EINVAL;
525 83f64091 bellard
                } else {
526 83f64091 bellard
                    ret = -ret;
527 83f64091 bellard
                }
528 83f64091 bellard
                /* remove the request */
529 ce1a14dc pbrook
                *pacb = acb->next;
530 83f64091 bellard
                /* call the callback */
531 ce1a14dc pbrook
                acb->common.cb(acb->common.opaque, ret);
532 ce1a14dc pbrook
                qemu_aio_release(acb);
533 83f64091 bellard
                break;
534 83f64091 bellard
            } else {
535 ce1a14dc pbrook
                pacb = &acb->next;
536 83f64091 bellard
            }
537 83f64091 bellard
        }
538 83f64091 bellard
    }
539 83f64091 bellard
 the_end: ;
540 83f64091 bellard
}
541 83f64091 bellard
542 a76bab49 aliguori
static int posix_aio_flush(void *opaque)
543 baf35cb9 aliguori
{
544 a76bab49 aliguori
    PosixAioState *s = opaque;
545 a76bab49 aliguori
    return !!s->first_aio;
546 a76bab49 aliguori
}
547 a76bab49 aliguori
548 a76bab49 aliguori
static PosixAioState *posix_aio_state;
549 baf35cb9 aliguori
550 9e472e10 aliguori
static void aio_signal_handler(int signum)
551 9e472e10 aliguori
{
552 9e472e10 aliguori
    if (posix_aio_state) {
553 9e472e10 aliguori
        char byte = 0;
554 9e472e10 aliguori
555 9e472e10 aliguori
        write(posix_aio_state->wfd, &byte, sizeof(byte));
556 9e472e10 aliguori
    }
557 9e472e10 aliguori
558 9e472e10 aliguori
    qemu_service_io();
559 9e472e10 aliguori
}
560 9e472e10 aliguori
561 a76bab49 aliguori
static int posix_aio_init(void)
562 a76bab49 aliguori
{
563 9e472e10 aliguori
    struct sigaction act;
564 a76bab49 aliguori
    PosixAioState *s;
565 9e472e10 aliguori
    int fds[2];
566 3c529d93 aliguori
    struct qemu_paioinit ai;
567 a76bab49 aliguori
  
568 a76bab49 aliguori
    if (posix_aio_state)
569 a76bab49 aliguori
        return 0;
570 ad02ad6f aliguori
571 a76bab49 aliguori
    s = qemu_malloc(sizeof(PosixAioState));
572 baf35cb9 aliguori
573 9e472e10 aliguori
    sigfillset(&act.sa_mask);
574 9e472e10 aliguori
    act.sa_flags = 0; /* do not restart syscalls to interrupt select() */
575 9e472e10 aliguori
    act.sa_handler = aio_signal_handler;
576 9e472e10 aliguori
    sigaction(SIGUSR2, &act, NULL);
577 9e472e10 aliguori
578 a76bab49 aliguori
    s->first_aio = NULL;
579 9e472e10 aliguori
    if (pipe(fds) == -1) {
580 9e472e10 aliguori
        fprintf(stderr, "failed to create pipe\n");
581 27463101 aliguori
        return -errno;
582 27463101 aliguori
    }
583 2c41a5f9 aliguori
584 9e472e10 aliguori
    s->rfd = fds[0];
585 9e472e10 aliguori
    s->wfd = fds[1];
586 9e472e10 aliguori
587 e20e830b aliguori
    fcntl(s->rfd, F_SETFL, O_NONBLOCK);
588 9e472e10 aliguori
    fcntl(s->wfd, F_SETFL, O_NONBLOCK);
589 2c41a5f9 aliguori
590 9e472e10 aliguori
    qemu_aio_set_fd_handler(s->rfd, posix_aio_read, NULL, posix_aio_flush, s);
591 baf35cb9 aliguori
592 3c529d93 aliguori
    memset(&ai, 0, sizeof(ai));
593 3c529d93 aliguori
    ai.aio_threads = 64;
594 3c529d93 aliguori
    ai.aio_num = 64;
595 3c529d93 aliguori
    qemu_paio_init(&ai);
596 acce87f9 aliguori
597 a76bab49 aliguori
    posix_aio_state = s;
598 6eb5733a bellard
599 a76bab49 aliguori
    return 0;
600 83f64091 bellard
}
601 83f64091 bellard
602 f141eafe aliguori
static RawAIOCB *raw_aio_setup(BlockDriverState *bs, int64_t sector_num,
603 f141eafe aliguori
        QEMUIOVector *qiov, int nb_sectors,
604 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque)
605 83f64091 bellard
{
606 ce1a14dc pbrook
    BDRVRawState *s = bs->opaque;
607 ce1a14dc pbrook
    RawAIOCB *acb;
608 ce1a14dc pbrook
609 19cb3738 bellard
    if (fd_open(bs) < 0)
610 19cb3738 bellard
        return NULL;
611 19cb3738 bellard
612 ce1a14dc pbrook
    acb = qemu_aio_get(bs, cb, opaque);
613 ce1a14dc pbrook
    if (!acb)
614 ce1a14dc pbrook
        return NULL;
615 3c529d93 aliguori
    acb->aiocb.aio_fildes = s->fd;
616 55f11ca3 blueswir1
    acb->aiocb.ev_signo = SIGUSR2;
617 f141eafe aliguori
    acb->aiocb.aio_iov = qiov->iov;
618 f141eafe aliguori
    acb->aiocb.aio_niov = qiov->niov;
619 f141eafe aliguori
    acb->aiocb.aio_nbytes = nb_sectors * 512;
620 ce1a14dc pbrook
    acb->aiocb.aio_offset = sector_num * 512;
621 f141eafe aliguori
    acb->aiocb.aio_flags = 0;
622 f141eafe aliguori
623 f141eafe aliguori
    /*
624 f141eafe aliguori
     * If O_DIRECT is used the buffer needs to be aligned on a sector
625 f141eafe aliguori
     * boundary. Tell the low level code to ensure that in case it's
626 f141eafe aliguori
     * not done yet.
627 f141eafe aliguori
     */
628 f141eafe aliguori
    if (s->aligned_buf)
629 f141eafe aliguori
        acb->aiocb.aio_flags |= QEMU_AIO_SECTOR_ALIGNED;
630 f141eafe aliguori
631 a76bab49 aliguori
    acb->next = posix_aio_state->first_aio;
632 a76bab49 aliguori
    posix_aio_state->first_aio = acb;
633 ce1a14dc pbrook
    return acb;
634 83f64091 bellard
}
635 83f64091 bellard
636 22bf1458 aliguori
static void raw_aio_remove(RawAIOCB *acb)
637 22bf1458 aliguori
{
638 22bf1458 aliguori
    RawAIOCB **pacb;
639 22bf1458 aliguori
640 22bf1458 aliguori
    /* remove the callback from the queue */
641 22bf1458 aliguori
    pacb = &posix_aio_state->first_aio;
642 22bf1458 aliguori
    for(;;) {
643 22bf1458 aliguori
        if (*pacb == NULL) {
644 7a11b22e aliguori
            fprintf(stderr, "raw_aio_remove: aio request not found!\n");
645 22bf1458 aliguori
            break;
646 22bf1458 aliguori
        } else if (*pacb == acb) {
647 22bf1458 aliguori
            *pacb = acb->next;
648 22bf1458 aliguori
            qemu_aio_release(acb);
649 22bf1458 aliguori
            break;
650 22bf1458 aliguori
        }
651 7a11b22e aliguori
        pacb = &(*pacb)->next;
652 22bf1458 aliguori
    }
653 22bf1458 aliguori
}
654 22bf1458 aliguori
655 f141eafe aliguori
static BlockDriverAIOCB *raw_aio_readv(BlockDriverState *bs,
656 f141eafe aliguori
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
657 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque)
658 83f64091 bellard
{
659 ce1a14dc pbrook
    RawAIOCB *acb;
660 83f64091 bellard
661 f141eafe aliguori
    acb = raw_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque);
662 ce1a14dc pbrook
    if (!acb)
663 ce1a14dc pbrook
        return NULL;
664 3c529d93 aliguori
    if (qemu_paio_read(&acb->aiocb) < 0) {
665 22bf1458 aliguori
        raw_aio_remove(acb);
666 ce1a14dc pbrook
        return NULL;
667 5fafdf24 ths
    }
668 ce1a14dc pbrook
    return &acb->common;
669 83f64091 bellard
}
670 83f64091 bellard
671 f141eafe aliguori
static BlockDriverAIOCB *raw_aio_writev(BlockDriverState *bs,
672 f141eafe aliguori
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
673 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque)
674 83f64091 bellard
{
675 ce1a14dc pbrook
    RawAIOCB *acb;
676 83f64091 bellard
677 f141eafe aliguori
    acb = raw_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque);
678 ce1a14dc pbrook
    if (!acb)
679 ce1a14dc pbrook
        return NULL;
680 3c529d93 aliguori
    if (qemu_paio_write(&acb->aiocb) < 0) {
681 22bf1458 aliguori
        raw_aio_remove(acb);
682 ce1a14dc pbrook
        return NULL;
683 5fafdf24 ths
    }
684 ce1a14dc pbrook
    return &acb->common;
685 83f64091 bellard
}
686 83f64091 bellard
687 ce1a14dc pbrook
static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
688 83f64091 bellard
{
689 83f64091 bellard
    int ret;
690 ce1a14dc pbrook
    RawAIOCB *acb = (RawAIOCB *)blockacb;
691 83f64091 bellard
692 3c529d93 aliguori
    ret = qemu_paio_cancel(acb->aiocb.aio_fildes, &acb->aiocb);
693 3c529d93 aliguori
    if (ret == QEMU_PAIO_NOTCANCELED) {
694 83f64091 bellard
        /* fail safe: if the aio could not be canceled, we wait for
695 83f64091 bellard
           it */
696 3c529d93 aliguori
        while (qemu_paio_error(&acb->aiocb) == EINPROGRESS);
697 83f64091 bellard
    }
698 83f64091 bellard
699 22bf1458 aliguori
    raw_aio_remove(acb);
700 83f64091 bellard
}
701 a76bab49 aliguori
#else /* CONFIG_AIO */
702 a76bab49 aliguori
static int posix_aio_init(void)
703 414f0dab blueswir1
{
704 674a24ac blueswir1
    return 0;
705 414f0dab blueswir1
}
706 414f0dab blueswir1
#endif /* CONFIG_AIO */
707 414f0dab blueswir1
708 53538725 aliguori
709 83f64091 bellard
static void raw_close(BlockDriverState *bs)
710 83f64091 bellard
{
711 83f64091 bellard
    BDRVRawState *s = bs->opaque;
712 19cb3738 bellard
    if (s->fd >= 0) {
713 19cb3738 bellard
        close(s->fd);
714 19cb3738 bellard
        s->fd = -1;
715 bed5cc52 bellard
        if (s->aligned_buf != NULL)
716 bed5cc52 bellard
            qemu_free(s->aligned_buf);
717 19cb3738 bellard
    }
718 83f64091 bellard
}
719 83f64091 bellard
720 83f64091 bellard
static int raw_truncate(BlockDriverState *bs, int64_t offset)
721 83f64091 bellard
{
722 83f64091 bellard
    BDRVRawState *s = bs->opaque;
723 19cb3738 bellard
    if (s->type != FTYPE_FILE)
724 19cb3738 bellard
        return -ENOTSUP;
725 83f64091 bellard
    if (ftruncate(s->fd, offset) < 0)
726 83f64091 bellard
        return -errno;
727 83f64091 bellard
    return 0;
728 83f64091 bellard
}
729 83f64091 bellard
730 128ab2ff blueswir1
#ifdef __OpenBSD__
731 128ab2ff blueswir1
static int64_t raw_getlength(BlockDriverState *bs)
732 128ab2ff blueswir1
{
733 128ab2ff blueswir1
    BDRVRawState *s = bs->opaque;
734 128ab2ff blueswir1
    int fd = s->fd;
735 128ab2ff blueswir1
    struct stat st;
736 128ab2ff blueswir1
737 128ab2ff blueswir1
    if (fstat(fd, &st))
738 128ab2ff blueswir1
        return -1;
739 128ab2ff blueswir1
    if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
740 128ab2ff blueswir1
        struct disklabel dl;
741 128ab2ff blueswir1
742 128ab2ff blueswir1
        if (ioctl(fd, DIOCGDINFO, &dl))
743 128ab2ff blueswir1
            return -1;
744 128ab2ff blueswir1
        return (uint64_t)dl.d_secsize *
745 128ab2ff blueswir1
            dl.d_partitions[DISKPART(st.st_rdev)].p_size;
746 128ab2ff blueswir1
    } else
747 128ab2ff blueswir1
        return st.st_size;
748 128ab2ff blueswir1
}
749 128ab2ff blueswir1
#else /* !__OpenBSD__ */
750 83f64091 bellard
static int64_t  raw_getlength(BlockDriverState *bs)
751 83f64091 bellard
{
752 83f64091 bellard
    BDRVRawState *s = bs->opaque;
753 83f64091 bellard
    int fd = s->fd;
754 83f64091 bellard
    int64_t size;
755 179a2c19 blueswir1
#ifdef HOST_BSD
756 83f64091 bellard
    struct stat sb;
757 9f23011a blueswir1
#ifdef __FreeBSD__
758 9f23011a blueswir1
    int reopened = 0;
759 9f23011a blueswir1
#endif
760 83f64091 bellard
#endif
761 83f64091 bellard
#ifdef __sun__
762 83f64091 bellard
    struct dk_minfo minfo;
763 83f64091 bellard
    int rv;
764 83f64091 bellard
#endif
765 19cb3738 bellard
    int ret;
766 19cb3738 bellard
767 19cb3738 bellard
    ret = fd_open(bs);
768 19cb3738 bellard
    if (ret < 0)
769 19cb3738 bellard
        return ret;
770 83f64091 bellard
771 179a2c19 blueswir1
#ifdef HOST_BSD
772 9f23011a blueswir1
#ifdef __FreeBSD__
773 9f23011a blueswir1
again:
774 9f23011a blueswir1
#endif
775 83f64091 bellard
    if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
776 83f64091 bellard
#ifdef DIOCGMEDIASIZE
777 83f64091 bellard
        if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
778 c5e97233 blueswir1
#elif defined(DIOCGPART)
779 c5e97233 blueswir1
        {
780 c5e97233 blueswir1
                struct partinfo pi;
781 c5e97233 blueswir1
                if (ioctl(fd, DIOCGPART, &pi) == 0)
782 c5e97233 blueswir1
                        size = pi.media_size;
783 c5e97233 blueswir1
                else
784 c5e97233 blueswir1
                        size = 0;
785 c5e97233 blueswir1
        }
786 c5e97233 blueswir1
        if (size == 0)
787 83f64091 bellard
#endif
788 83f64091 bellard
#ifdef CONFIG_COCOA
789 83f64091 bellard
        size = LONG_LONG_MAX;
790 83f64091 bellard
#else
791 83f64091 bellard
        size = lseek(fd, 0LL, SEEK_END);
792 83f64091 bellard
#endif
793 9f23011a blueswir1
#ifdef __FreeBSD__
794 9f23011a blueswir1
        switch(s->type) {
795 9f23011a blueswir1
        case FTYPE_CD:
796 9f23011a blueswir1
            /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
797 9f23011a blueswir1
            if (size == 2048LL * (unsigned)-1)
798 9f23011a blueswir1
                size = 0;
799 9f23011a blueswir1
            /* XXX no disc?  maybe we need to reopen... */
800 9f23011a blueswir1
            if (size <= 0 && !reopened && cd_open(bs) >= 0) {
801 9f23011a blueswir1
                reopened = 1;
802 9f23011a blueswir1
                goto again;
803 9f23011a blueswir1
            }
804 9f23011a blueswir1
        }
805 9f23011a blueswir1
#endif
806 83f64091 bellard
    } else
807 83f64091 bellard
#endif
808 83f64091 bellard
#ifdef __sun__
809 83f64091 bellard
    /*
810 83f64091 bellard
     * use the DKIOCGMEDIAINFO ioctl to read the size.
811 83f64091 bellard
     */
812 83f64091 bellard
    rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo );
813 83f64091 bellard
    if ( rv != -1 ) {
814 83f64091 bellard
        size = minfo.dki_lbsize * minfo.dki_capacity;
815 83f64091 bellard
    } else /* there are reports that lseek on some devices
816 83f64091 bellard
              fails, but irc discussion said that contingency
817 83f64091 bellard
              on contingency was overkill */
818 83f64091 bellard
#endif
819 83f64091 bellard
    {
820 83f64091 bellard
        size = lseek(fd, 0, SEEK_END);
821 83f64091 bellard
    }
822 83f64091 bellard
    return size;
823 83f64091 bellard
}
824 128ab2ff blueswir1
#endif
825 83f64091 bellard
826 83f64091 bellard
static int raw_create(const char *filename, int64_t total_size,
827 83f64091 bellard
                      const char *backing_file, int flags)
828 83f64091 bellard
{
829 83f64091 bellard
    int fd;
830 83f64091 bellard
831 83f64091 bellard
    if (flags || backing_file)
832 83f64091 bellard
        return -ENOTSUP;
833 83f64091 bellard
834 5fafdf24 ths
    fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
835 83f64091 bellard
              0644);
836 83f64091 bellard
    if (fd < 0)
837 83f64091 bellard
        return -EIO;
838 83f64091 bellard
    ftruncate(fd, total_size * 512);
839 83f64091 bellard
    close(fd);
840 83f64091 bellard
    return 0;
841 83f64091 bellard
}
842 83f64091 bellard
843 83f64091 bellard
static void raw_flush(BlockDriverState *bs)
844 83f64091 bellard
{
845 83f64091 bellard
    BDRVRawState *s = bs->opaque;
846 83f64091 bellard
    fsync(s->fd);
847 83f64091 bellard
}
848 83f64091 bellard
849 83f64091 bellard
BlockDriver bdrv_raw = {
850 856ae5c3 blueswir1
    .format_name = "raw",
851 856ae5c3 blueswir1
    .instance_size = sizeof(BDRVRawState),
852 856ae5c3 blueswir1
    .bdrv_probe = NULL, /* no probe for protocols */
853 856ae5c3 blueswir1
    .bdrv_open = raw_open,
854 856ae5c3 blueswir1
    .bdrv_read = raw_read,
855 856ae5c3 blueswir1
    .bdrv_write = raw_write,
856 856ae5c3 blueswir1
    .bdrv_close = raw_close,
857 856ae5c3 blueswir1
    .bdrv_create = raw_create,
858 856ae5c3 blueswir1
    .bdrv_flush = raw_flush,
859 3b46e624 ths
860 414f0dab blueswir1
#ifdef CONFIG_AIO
861 f141eafe aliguori
    .bdrv_aio_readv = raw_aio_readv,
862 f141eafe aliguori
    .bdrv_aio_writev = raw_aio_writev,
863 83f64091 bellard
    .bdrv_aio_cancel = raw_aio_cancel,
864 ce1a14dc pbrook
    .aiocb_size = sizeof(RawAIOCB),
865 414f0dab blueswir1
#endif
866 3c529d93 aliguori
867 83f64091 bellard
    .bdrv_truncate = raw_truncate,
868 83f64091 bellard
    .bdrv_getlength = raw_getlength,
869 83f64091 bellard
};
870 83f64091 bellard
871 19cb3738 bellard
/***********************************************/
872 19cb3738 bellard
/* host device */
873 19cb3738 bellard
874 19cb3738 bellard
#ifdef CONFIG_COCOA
875 19cb3738 bellard
static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
876 19cb3738 bellard
static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
877 19cb3738 bellard
878 19cb3738 bellard
kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
879 19cb3738 bellard
{
880 5fafdf24 ths
    kern_return_t       kernResult;
881 19cb3738 bellard
    mach_port_t     masterPort;
882 19cb3738 bellard
    CFMutableDictionaryRef  classesToMatch;
883 19cb3738 bellard
884 19cb3738 bellard
    kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
885 19cb3738 bellard
    if ( KERN_SUCCESS != kernResult ) {
886 19cb3738 bellard
        printf( "IOMasterPort returned %d\n", kernResult );
887 19cb3738 bellard
    }
888 3b46e624 ths
889 5fafdf24 ths
    classesToMatch = IOServiceMatching( kIOCDMediaClass );
890 19cb3738 bellard
    if ( classesToMatch == NULL ) {
891 19cb3738 bellard
        printf( "IOServiceMatching returned a NULL dictionary.\n" );
892 19cb3738 bellard
    } else {
893 19cb3738 bellard
    CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
894 19cb3738 bellard
    }
895 19cb3738 bellard
    kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
896 19cb3738 bellard
    if ( KERN_SUCCESS != kernResult )
897 19cb3738 bellard
    {
898 19cb3738 bellard
        printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
899 19cb3738 bellard
    }
900 3b46e624 ths
901 19cb3738 bellard
    return kernResult;
902 19cb3738 bellard
}
903 19cb3738 bellard
904 19cb3738 bellard
kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
905 19cb3738 bellard
{
906 19cb3738 bellard
    io_object_t     nextMedia;
907 19cb3738 bellard
    kern_return_t   kernResult = KERN_FAILURE;
908 19cb3738 bellard
    *bsdPath = '\0';
909 19cb3738 bellard
    nextMedia = IOIteratorNext( mediaIterator );
910 19cb3738 bellard
    if ( nextMedia )
911 19cb3738 bellard
    {
912 19cb3738 bellard
        CFTypeRef   bsdPathAsCFString;
913 19cb3738 bellard
    bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
914 19cb3738 bellard
        if ( bsdPathAsCFString ) {
915 19cb3738 bellard
            size_t devPathLength;
916 19cb3738 bellard
            strcpy( bsdPath, _PATH_DEV );
917 19cb3738 bellard
            strcat( bsdPath, "r" );
918 19cb3738 bellard
            devPathLength = strlen( bsdPath );
919 19cb3738 bellard
            if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
920 19cb3738 bellard
                kernResult = KERN_SUCCESS;
921 19cb3738 bellard
            }
922 19cb3738 bellard
            CFRelease( bsdPathAsCFString );
923 19cb3738 bellard
        }
924 19cb3738 bellard
        IOObjectRelease( nextMedia );
925 19cb3738 bellard
    }
926 3b46e624 ths
927 19cb3738 bellard
    return kernResult;
928 19cb3738 bellard
}
929 19cb3738 bellard
930 19cb3738 bellard
#endif
931 19cb3738 bellard
932 19cb3738 bellard
static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
933 19cb3738 bellard
{
934 19cb3738 bellard
    BDRVRawState *s = bs->opaque;
935 3c529d93 aliguori
    int fd, open_flags, ret;
936 19cb3738 bellard
937 a76bab49 aliguori
    posix_aio_init();
938 a76bab49 aliguori
939 19cb3738 bellard
#ifdef CONFIG_COCOA
940 19cb3738 bellard
    if (strstart(filename, "/dev/cdrom", NULL)) {
941 19cb3738 bellard
        kern_return_t kernResult;
942 19cb3738 bellard
        io_iterator_t mediaIterator;
943 19cb3738 bellard
        char bsdPath[ MAXPATHLEN ];
944 19cb3738 bellard
        int fd;
945 5fafdf24 ths
946 19cb3738 bellard
        kernResult = FindEjectableCDMedia( &mediaIterator );
947 19cb3738 bellard
        kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
948 3b46e624 ths
949 19cb3738 bellard
        if ( bsdPath[ 0 ] != '\0' ) {
950 19cb3738 bellard
            strcat(bsdPath,"s0");
951 19cb3738 bellard
            /* some CDs don't have a partition 0 */
952 19cb3738 bellard
            fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
953 19cb3738 bellard
            if (fd < 0) {
954 19cb3738 bellard
                bsdPath[strlen(bsdPath)-1] = '1';
955 19cb3738 bellard
            } else {
956 19cb3738 bellard
                close(fd);
957 19cb3738 bellard
            }
958 19cb3738 bellard
            filename = bsdPath;
959 19cb3738 bellard
        }
960 3b46e624 ths
961 19cb3738 bellard
        if ( mediaIterator )
962 19cb3738 bellard
            IOObjectRelease( mediaIterator );
963 19cb3738 bellard
    }
964 19cb3738 bellard
#endif
965 19cb3738 bellard
    open_flags = O_BINARY;
966 19cb3738 bellard
    if ((flags & BDRV_O_ACCESS) == O_RDWR) {
967 19cb3738 bellard
        open_flags |= O_RDWR;
968 19cb3738 bellard
    } else {
969 19cb3738 bellard
        open_flags |= O_RDONLY;
970 19cb3738 bellard
        bs->read_only = 1;
971 19cb3738 bellard
    }
972 9f7965c7 aliguori
    /* Use O_DSYNC for write-through caching, no flags for write-back caching,
973 9f7965c7 aliguori
     * and O_DIRECT for no caching. */
974 9f7965c7 aliguori
    if ((flags & BDRV_O_NOCACHE))
975 33f00271 balrog
        open_flags |= O_DIRECT;
976 9f7965c7 aliguori
    else if (!(flags & BDRV_O_CACHE_WB))
977 9f7965c7 aliguori
        open_flags |= O_DSYNC;
978 19cb3738 bellard
979 19cb3738 bellard
    s->type = FTYPE_FILE;
980 19cb3738 bellard
#if defined(__linux__)
981 19cb3738 bellard
    if (strstart(filename, "/dev/cd", NULL)) {
982 19cb3738 bellard
        /* open will not fail even if no CD is inserted */
983 19cb3738 bellard
        open_flags |= O_NONBLOCK;
984 19cb3738 bellard
        s->type = FTYPE_CD;
985 19cb3738 bellard
    } else if (strstart(filename, "/dev/fd", NULL)) {
986 19cb3738 bellard
        s->type = FTYPE_FD;
987 6dd2db52 blueswir1
        s->fd_open_flags = open_flags;
988 19cb3738 bellard
        /* open will not fail even if no floppy is inserted */
989 19cb3738 bellard
        open_flags |= O_NONBLOCK;
990 8185d2c9 aliguori
#ifdef CONFIG_AIO
991 985a03b0 ths
    } else if (strstart(filename, "/dev/sg", NULL)) {
992 985a03b0 ths
        bs->sg = 1;
993 8185d2c9 aliguori
#endif
994 19cb3738 bellard
    }
995 19cb3738 bellard
#endif
996 9f23011a blueswir1
#if defined(__FreeBSD__)
997 9f23011a blueswir1
    if (strstart(filename, "/dev/cd", NULL) ||
998 9f23011a blueswir1
        strstart(filename, "/dev/acd", NULL)) {
999 9f23011a blueswir1
        s->type = FTYPE_CD;
1000 9f23011a blueswir1
        s->cd_open_flags = open_flags;
1001 9f23011a blueswir1
    }
1002 9f23011a blueswir1
#endif
1003 9f23011a blueswir1
    s->fd = -1;
1004 19cb3738 bellard
    fd = open(filename, open_flags, 0644);
1005 19cb3738 bellard
    if (fd < 0) {
1006 19cb3738 bellard
        ret = -errno;
1007 19cb3738 bellard
        if (ret == -EROFS)
1008 19cb3738 bellard
            ret = -EACCES;
1009 19cb3738 bellard
        return ret;
1010 19cb3738 bellard
    }
1011 19cb3738 bellard
    s->fd = fd;
1012 9f23011a blueswir1
#if defined(__FreeBSD__)
1013 9f23011a blueswir1
    /* make sure the door isnt locked at this time */
1014 9f23011a blueswir1
    if (s->type == FTYPE_CD)
1015 9f23011a blueswir1
        ioctl (s->fd, CDIOCALLOW);
1016 9f23011a blueswir1
#endif
1017 19cb3738 bellard
#if defined(__linux__)
1018 19cb3738 bellard
    /* close fd so that we can reopen it as needed */
1019 19cb3738 bellard
    if (s->type == FTYPE_FD) {
1020 19cb3738 bellard
        close(s->fd);
1021 19cb3738 bellard
        s->fd = -1;
1022 19cb3738 bellard
        s->fd_media_changed = 1;
1023 19cb3738 bellard
    }
1024 19cb3738 bellard
#endif
1025 19cb3738 bellard
    return 0;
1026 19cb3738 bellard
}
1027 19cb3738 bellard
1028 03ff3ca3 aliguori
#if defined(__linux__)
1029 19cb3738 bellard
/* Note: we do not have a reliable method to detect if the floppy is
1030 19cb3738 bellard
   present. The current method is to try to open the floppy at every
1031 19cb3738 bellard
   I/O and to keep it opened during a few hundreds of ms. */
1032 19cb3738 bellard
static int fd_open(BlockDriverState *bs)
1033 19cb3738 bellard
{
1034 19cb3738 bellard
    BDRVRawState *s = bs->opaque;
1035 19cb3738 bellard
    int last_media_present;
1036 19cb3738 bellard
1037 19cb3738 bellard
    if (s->type != FTYPE_FD)
1038 19cb3738 bellard
        return 0;
1039 19cb3738 bellard
    last_media_present = (s->fd >= 0);
1040 5fafdf24 ths
    if (s->fd >= 0 &&
1041 19cb3738 bellard
        (qemu_get_clock(rt_clock) - s->fd_open_time) >= FD_OPEN_TIMEOUT) {
1042 19cb3738 bellard
        close(s->fd);
1043 19cb3738 bellard
        s->fd = -1;
1044 19cb3738 bellard
#ifdef DEBUG_FLOPPY
1045 19cb3738 bellard
        printf("Floppy closed\n");
1046 19cb3738 bellard
#endif
1047 19cb3738 bellard
    }
1048 19cb3738 bellard
    if (s->fd < 0) {
1049 5fafdf24 ths
        if (s->fd_got_error &&
1050 19cb3738 bellard
            (qemu_get_clock(rt_clock) - s->fd_error_time) < FD_OPEN_TIMEOUT) {
1051 19cb3738 bellard
#ifdef DEBUG_FLOPPY
1052 19cb3738 bellard
            printf("No floppy (open delayed)\n");
1053 19cb3738 bellard
#endif
1054 19cb3738 bellard
            return -EIO;
1055 19cb3738 bellard
        }
1056 6dd2db52 blueswir1
        s->fd = open(bs->filename, s->fd_open_flags);
1057 19cb3738 bellard
        if (s->fd < 0) {
1058 19cb3738 bellard
            s->fd_error_time = qemu_get_clock(rt_clock);
1059 19cb3738 bellard
            s->fd_got_error = 1;
1060 19cb3738 bellard
            if (last_media_present)
1061 19cb3738 bellard
                s->fd_media_changed = 1;
1062 19cb3738 bellard
#ifdef DEBUG_FLOPPY
1063 19cb3738 bellard
            printf("No floppy\n");
1064 19cb3738 bellard
#endif
1065 19cb3738 bellard
            return -EIO;
1066 19cb3738 bellard
        }
1067 19cb3738 bellard
#ifdef DEBUG_FLOPPY
1068 19cb3738 bellard
        printf("Floppy opened\n");
1069 19cb3738 bellard
#endif
1070 19cb3738 bellard
    }
1071 19cb3738 bellard
    if (!last_media_present)
1072 19cb3738 bellard
        s->fd_media_changed = 1;
1073 19cb3738 bellard
    s->fd_open_time = qemu_get_clock(rt_clock);
1074 19cb3738 bellard
    s->fd_got_error = 0;
1075 19cb3738 bellard
    return 0;
1076 19cb3738 bellard
}
1077 19cb3738 bellard
1078 19cb3738 bellard
static int raw_is_inserted(BlockDriverState *bs)
1079 19cb3738 bellard
{
1080 19cb3738 bellard
    BDRVRawState *s = bs->opaque;
1081 19cb3738 bellard
    int ret;
1082 19cb3738 bellard
1083 19cb3738 bellard
    switch(s->type) {
1084 19cb3738 bellard
    case FTYPE_CD:
1085 19cb3738 bellard
        ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
1086 19cb3738 bellard
        if (ret == CDS_DISC_OK)
1087 19cb3738 bellard
            return 1;
1088 19cb3738 bellard
        else
1089 19cb3738 bellard
            return 0;
1090 19cb3738 bellard
        break;
1091 19cb3738 bellard
    case FTYPE_FD:
1092 19cb3738 bellard
        ret = fd_open(bs);
1093 19cb3738 bellard
        return (ret >= 0);
1094 19cb3738 bellard
    default:
1095 19cb3738 bellard
        return 1;
1096 19cb3738 bellard
    }
1097 19cb3738 bellard
}
1098 19cb3738 bellard
1099 19cb3738 bellard
/* currently only used by fdc.c, but a CD version would be good too */
1100 19cb3738 bellard
static int raw_media_changed(BlockDriverState *bs)
1101 19cb3738 bellard
{
1102 19cb3738 bellard
    BDRVRawState *s = bs->opaque;
1103 19cb3738 bellard
1104 19cb3738 bellard
    switch(s->type) {
1105 19cb3738 bellard
    case FTYPE_FD:
1106 19cb3738 bellard
        {
1107 19cb3738 bellard
            int ret;
1108 19cb3738 bellard
            /* XXX: we do not have a true media changed indication. It
1109 19cb3738 bellard
               does not work if the floppy is changed without trying
1110 19cb3738 bellard
               to read it */
1111 19cb3738 bellard
            fd_open(bs);
1112 19cb3738 bellard
            ret = s->fd_media_changed;
1113 19cb3738 bellard
            s->fd_media_changed = 0;
1114 19cb3738 bellard
#ifdef DEBUG_FLOPPY
1115 19cb3738 bellard
            printf("Floppy changed=%d\n", ret);
1116 19cb3738 bellard
#endif
1117 19cb3738 bellard
            return ret;
1118 19cb3738 bellard
        }
1119 19cb3738 bellard
    default:
1120 19cb3738 bellard
        return -ENOTSUP;
1121 19cb3738 bellard
    }
1122 19cb3738 bellard
}
1123 19cb3738 bellard
1124 19cb3738 bellard
static int raw_eject(BlockDriverState *bs, int eject_flag)
1125 19cb3738 bellard
{
1126 19cb3738 bellard
    BDRVRawState *s = bs->opaque;
1127 19cb3738 bellard
1128 19cb3738 bellard
    switch(s->type) {
1129 19cb3738 bellard
    case FTYPE_CD:
1130 19cb3738 bellard
        if (eject_flag) {
1131 19cb3738 bellard
            if (ioctl (s->fd, CDROMEJECT, NULL) < 0)
1132 19cb3738 bellard
                perror("CDROMEJECT");
1133 19cb3738 bellard
        } else {
1134 19cb3738 bellard
            if (ioctl (s->fd, CDROMCLOSETRAY, NULL) < 0)
1135 19cb3738 bellard
                perror("CDROMEJECT");
1136 19cb3738 bellard
        }
1137 19cb3738 bellard
        break;
1138 19cb3738 bellard
    case FTYPE_FD:
1139 19cb3738 bellard
        {
1140 19cb3738 bellard
            int fd;
1141 19cb3738 bellard
            if (s->fd >= 0) {
1142 19cb3738 bellard
                close(s->fd);
1143 19cb3738 bellard
                s->fd = -1;
1144 19cb3738 bellard
            }
1145 6dd2db52 blueswir1
            fd = open(bs->filename, s->fd_open_flags | O_NONBLOCK);
1146 19cb3738 bellard
            if (fd >= 0) {
1147 19cb3738 bellard
                if (ioctl(fd, FDEJECT, 0) < 0)
1148 19cb3738 bellard
                    perror("FDEJECT");
1149 19cb3738 bellard
                close(fd);
1150 19cb3738 bellard
            }
1151 19cb3738 bellard
        }
1152 19cb3738 bellard
        break;
1153 19cb3738 bellard
    default:
1154 19cb3738 bellard
        return -ENOTSUP;
1155 19cb3738 bellard
    }
1156 19cb3738 bellard
    return 0;
1157 19cb3738 bellard
}
1158 19cb3738 bellard
1159 19cb3738 bellard
static int raw_set_locked(BlockDriverState *bs, int locked)
1160 19cb3738 bellard
{
1161 19cb3738 bellard
    BDRVRawState *s = bs->opaque;
1162 19cb3738 bellard
1163 19cb3738 bellard
    switch(s->type) {
1164 19cb3738 bellard
    case FTYPE_CD:
1165 19cb3738 bellard
        if (ioctl (s->fd, CDROM_LOCKDOOR, locked) < 0) {
1166 19cb3738 bellard
            /* Note: an error can happen if the distribution automatically
1167 19cb3738 bellard
               mounts the CD-ROM */
1168 19cb3738 bellard
            //        perror("CDROM_LOCKDOOR");
1169 19cb3738 bellard
        }
1170 19cb3738 bellard
        break;
1171 19cb3738 bellard
    default:
1172 19cb3738 bellard
        return -ENOTSUP;
1173 19cb3738 bellard
    }
1174 19cb3738 bellard
    return 0;
1175 19cb3738 bellard
}
1176 19cb3738 bellard
1177 985a03b0 ths
static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1178 985a03b0 ths
{
1179 985a03b0 ths
    BDRVRawState *s = bs->opaque;
1180 985a03b0 ths
1181 985a03b0 ths
    return ioctl(s->fd, req, buf);
1182 985a03b0 ths
}
1183 221f715d aliguori
1184 8185d2c9 aliguori
#ifdef CONFIG_AIO
1185 221f715d aliguori
static BlockDriverAIOCB *raw_aio_ioctl(BlockDriverState *bs,
1186 221f715d aliguori
        unsigned long int req, void *buf,
1187 221f715d aliguori
        BlockDriverCompletionFunc *cb, void *opaque)
1188 221f715d aliguori
{
1189 f141eafe aliguori
    BDRVRawState *s = bs->opaque;
1190 221f715d aliguori
    RawAIOCB *acb;
1191 221f715d aliguori
1192 f141eafe aliguori
    if (fd_open(bs) < 0)
1193 f141eafe aliguori
        return NULL;
1194 f141eafe aliguori
1195 f141eafe aliguori
    acb = qemu_aio_get(bs, cb, opaque);
1196 221f715d aliguori
    if (!acb)
1197 221f715d aliguori
        return NULL;
1198 f141eafe aliguori
    acb->aiocb.aio_fildes = s->fd;
1199 f141eafe aliguori
    acb->aiocb.ev_signo = SIGUSR2;
1200 f141eafe aliguori
    acb->aiocb.aio_offset = 0;
1201 f141eafe aliguori
    acb->aiocb.aio_flags = 0;
1202 f141eafe aliguori
1203 f141eafe aliguori
    acb->next = posix_aio_state->first_aio;
1204 f141eafe aliguori
    posix_aio_state->first_aio = acb;
1205 221f715d aliguori
1206 f141eafe aliguori
    acb->aiocb.aio_ioctl_buf = buf;
1207 221f715d aliguori
    acb->aiocb.aio_ioctl_cmd = req;
1208 221f715d aliguori
    if (qemu_paio_ioctl(&acb->aiocb) < 0) {
1209 221f715d aliguori
        raw_aio_remove(acb);
1210 221f715d aliguori
        return NULL;
1211 221f715d aliguori
    }
1212 221f715d aliguori
1213 221f715d aliguori
    return &acb->common;
1214 221f715d aliguori
}
1215 8185d2c9 aliguori
#endif
1216 221f715d aliguori
1217 9f23011a blueswir1
#elif defined(__FreeBSD__)
1218 9f23011a blueswir1
1219 9f23011a blueswir1
static int fd_open(BlockDriverState *bs)
1220 9f23011a blueswir1
{
1221 9f23011a blueswir1
    BDRVRawState *s = bs->opaque;
1222 9f23011a blueswir1
1223 9f23011a blueswir1
    /* this is just to ensure s->fd is sane (its called by io ops) */
1224 9f23011a blueswir1
    if (s->fd >= 0)
1225 9f23011a blueswir1
        return 0;
1226 9f23011a blueswir1
    return -EIO;
1227 9f23011a blueswir1
}
1228 9f23011a blueswir1
1229 9f23011a blueswir1
static int cd_open(BlockDriverState *bs)
1230 9f23011a blueswir1
{
1231 9f23011a blueswir1
#if defined(__FreeBSD__)
1232 9f23011a blueswir1
    BDRVRawState *s = bs->opaque;
1233 9f23011a blueswir1
    int fd;
1234 9f23011a blueswir1
1235 9f23011a blueswir1
    switch(s->type) {
1236 9f23011a blueswir1
    case FTYPE_CD:
1237 9f23011a blueswir1
        /* XXX force reread of possibly changed/newly loaded disc,
1238 9f23011a blueswir1
         * FreeBSD seems to not notice sometimes... */
1239 9f23011a blueswir1
        if (s->fd >= 0)
1240 9f23011a blueswir1
            close (s->fd);
1241 9f23011a blueswir1
        fd = open(bs->filename, s->cd_open_flags, 0644);
1242 9f23011a blueswir1
        if (fd < 0) {
1243 9f23011a blueswir1
            s->fd = -1;
1244 9f23011a blueswir1
            return -EIO;
1245 9f23011a blueswir1
        }
1246 9f23011a blueswir1
        s->fd = fd;
1247 9f23011a blueswir1
        /* make sure the door isnt locked at this time */
1248 9f23011a blueswir1
        ioctl (s->fd, CDIOCALLOW);
1249 9f23011a blueswir1
    }
1250 9f23011a blueswir1
#endif
1251 9f23011a blueswir1
    return 0;
1252 9f23011a blueswir1
}
1253 9f23011a blueswir1
1254 9f23011a blueswir1
static int raw_is_inserted(BlockDriverState *bs)
1255 9f23011a blueswir1
{
1256 9f23011a blueswir1
    BDRVRawState *s = bs->opaque;
1257 9f23011a blueswir1
1258 9f23011a blueswir1
    switch(s->type) {
1259 9f23011a blueswir1
    case FTYPE_CD:
1260 9f23011a blueswir1
        return (raw_getlength(bs) > 0);
1261 9f23011a blueswir1
    case FTYPE_FD:
1262 9f23011a blueswir1
        /* XXX handle this */
1263 9f23011a blueswir1
        /* FALLTHRU */
1264 9f23011a blueswir1
    default:
1265 9f23011a blueswir1
        return 1;
1266 9f23011a blueswir1
    }
1267 9f23011a blueswir1
}
1268 9f23011a blueswir1
1269 9f23011a blueswir1
static int raw_media_changed(BlockDriverState *bs)
1270 9f23011a blueswir1
{
1271 9f23011a blueswir1
    return -ENOTSUP;
1272 9f23011a blueswir1
}
1273 9f23011a blueswir1
1274 9f23011a blueswir1
static int raw_eject(BlockDriverState *bs, int eject_flag)
1275 9f23011a blueswir1
{
1276 9f23011a blueswir1
    BDRVRawState *s = bs->opaque;
1277 9f23011a blueswir1
1278 9f23011a blueswir1
    switch(s->type) {
1279 9f23011a blueswir1
    case FTYPE_CD:
1280 9f23011a blueswir1
        if (s->fd < 0)
1281 9f23011a blueswir1
            return -ENOTSUP;
1282 9f23011a blueswir1
        (void) ioctl (s->fd, CDIOCALLOW);
1283 9f23011a blueswir1
        if (eject_flag) {
1284 9f23011a blueswir1
            if (ioctl (s->fd, CDIOCEJECT) < 0)
1285 9f23011a blueswir1
                perror("CDIOCEJECT");
1286 9f23011a blueswir1
        } else {
1287 9f23011a blueswir1
            if (ioctl (s->fd, CDIOCCLOSE) < 0)
1288 9f23011a blueswir1
                perror("CDIOCCLOSE");
1289 9f23011a blueswir1
        }
1290 9f23011a blueswir1
        if (cd_open(bs) < 0)
1291 9f23011a blueswir1
            return -ENOTSUP;
1292 9f23011a blueswir1
        break;
1293 9f23011a blueswir1
    case FTYPE_FD:
1294 9f23011a blueswir1
        /* XXX handle this */
1295 9f23011a blueswir1
        /* FALLTHRU */
1296 9f23011a blueswir1
    default:
1297 9f23011a blueswir1
        return -ENOTSUP;
1298 9f23011a blueswir1
    }
1299 9f23011a blueswir1
    return 0;
1300 9f23011a blueswir1
}
1301 9f23011a blueswir1
1302 9f23011a blueswir1
static int raw_set_locked(BlockDriverState *bs, int locked)
1303 9f23011a blueswir1
{
1304 9f23011a blueswir1
    BDRVRawState *s = bs->opaque;
1305 9f23011a blueswir1
1306 9f23011a blueswir1
    switch(s->type) {
1307 9f23011a blueswir1
    case FTYPE_CD:
1308 9f23011a blueswir1
        if (s->fd < 0)
1309 9f23011a blueswir1
            return -ENOTSUP;
1310 9f23011a blueswir1
        if (ioctl (s->fd, (locked ? CDIOCPREVENT : CDIOCALLOW)) < 0) {
1311 9f23011a blueswir1
            /* Note: an error can happen if the distribution automatically
1312 9f23011a blueswir1
               mounts the CD-ROM */
1313 9f23011a blueswir1
            //        perror("CDROM_LOCKDOOR");
1314 9f23011a blueswir1
        }
1315 9f23011a blueswir1
        break;
1316 9f23011a blueswir1
    default:
1317 9f23011a blueswir1
        return -ENOTSUP;
1318 9f23011a blueswir1
    }
1319 9f23011a blueswir1
    return 0;
1320 9f23011a blueswir1
}
1321 9f23011a blueswir1
1322 9f23011a blueswir1
static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1323 9f23011a blueswir1
{
1324 9f23011a blueswir1
    return -ENOTSUP;
1325 9f23011a blueswir1
}
1326 9f23011a blueswir1
#else /* !linux && !FreeBSD */
1327 19cb3738 bellard
1328 08af02e2 aliguori
static int fd_open(BlockDriverState *bs)
1329 08af02e2 aliguori
{
1330 08af02e2 aliguori
    return 0;
1331 08af02e2 aliguori
}
1332 08af02e2 aliguori
1333 19cb3738 bellard
static int raw_is_inserted(BlockDriverState *bs)
1334 19cb3738 bellard
{
1335 19cb3738 bellard
    return 1;
1336 19cb3738 bellard
}
1337 19cb3738 bellard
1338 19cb3738 bellard
static int raw_media_changed(BlockDriverState *bs)
1339 19cb3738 bellard
{
1340 19cb3738 bellard
    return -ENOTSUP;
1341 19cb3738 bellard
}
1342 19cb3738 bellard
1343 19cb3738 bellard
static int raw_eject(BlockDriverState *bs, int eject_flag)
1344 19cb3738 bellard
{
1345 19cb3738 bellard
    return -ENOTSUP;
1346 19cb3738 bellard
}
1347 19cb3738 bellard
1348 19cb3738 bellard
static int raw_set_locked(BlockDriverState *bs, int locked)
1349 19cb3738 bellard
{
1350 19cb3738 bellard
    return -ENOTSUP;
1351 19cb3738 bellard
}
1352 19cb3738 bellard
1353 985a03b0 ths
static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1354 985a03b0 ths
{
1355 985a03b0 ths
    return -ENOTSUP;
1356 985a03b0 ths
}
1357 04eeb8b6 aliguori
1358 221f715d aliguori
static BlockDriverAIOCB *raw_aio_ioctl(BlockDriverState *bs,
1359 221f715d aliguori
        unsigned long int req, void *buf,
1360 221f715d aliguori
        BlockDriverCompletionFunc *cb, void *opaque)
1361 04eeb8b6 aliguori
{
1362 bbeea539 blueswir1
    return NULL;
1363 04eeb8b6 aliguori
}
1364 221f715d aliguori
#endif /* !linux && !FreeBSD */
1365 04eeb8b6 aliguori
1366 93c65b47 aliguori
#if defined(__linux__) || defined(__FreeBSD__)
1367 93c65b47 aliguori
static int hdev_create(const char *filename, int64_t total_size,
1368 93c65b47 aliguori
                       const char *backing_file, int flags)
1369 93c65b47 aliguori
{
1370 93c65b47 aliguori
    int fd;
1371 93c65b47 aliguori
    int ret = 0;
1372 93c65b47 aliguori
    struct stat stat_buf;
1373 93c65b47 aliguori
1374 93c65b47 aliguori
    if (flags || backing_file)
1375 93c65b47 aliguori
        return -ENOTSUP;
1376 93c65b47 aliguori
1377 93c65b47 aliguori
    fd = open(filename, O_WRONLY | O_BINARY);
1378 93c65b47 aliguori
    if (fd < 0)
1379 93c65b47 aliguori
        return -EIO;
1380 93c65b47 aliguori
1381 93c65b47 aliguori
    if (fstat(fd, &stat_buf) < 0)
1382 93c65b47 aliguori
        ret = -EIO;
1383 93c65b47 aliguori
    else if (!S_ISBLK(stat_buf.st_mode))
1384 93c65b47 aliguori
        ret = -EIO;
1385 93c65b47 aliguori
    else if (lseek(fd, 0, SEEK_END) < total_size * 512)
1386 93c65b47 aliguori
        ret = -ENOSPC;
1387 93c65b47 aliguori
1388 93c65b47 aliguori
    close(fd);
1389 93c65b47 aliguori
    return ret;
1390 93c65b47 aliguori
}
1391 93c65b47 aliguori
1392 93c65b47 aliguori
#else  /* !(linux || freebsd) */
1393 93c65b47 aliguori
1394 93c65b47 aliguori
static int hdev_create(const char *filename, int64_t total_size,
1395 93c65b47 aliguori
                       const char *backing_file, int flags)
1396 93c65b47 aliguori
{
1397 93c65b47 aliguori
    return -ENOTSUP;
1398 93c65b47 aliguori
}
1399 93c65b47 aliguori
#endif
1400 93c65b47 aliguori
1401 19cb3738 bellard
BlockDriver bdrv_host_device = {
1402 e60f469c aurel32
    .format_name        = "host_device",
1403 e60f469c aurel32
    .instance_size        = sizeof(BDRVRawState),
1404 e60f469c aurel32
    .bdrv_open                = hdev_open,
1405 e60f469c aurel32
    .bdrv_close                = raw_close,
1406 93c65b47 aliguori
    .bdrv_create        = hdev_create,
1407 e60f469c aurel32
    .bdrv_flush                = raw_flush,
1408 3b46e624 ths
1409 414f0dab blueswir1
#ifdef CONFIG_AIO
1410 f141eafe aliguori
    .bdrv_aio_readv        = raw_aio_readv,
1411 f141eafe aliguori
    .bdrv_aio_writev        = raw_aio_writev,
1412 e60f469c aurel32
    .bdrv_aio_cancel        = raw_aio_cancel,
1413 e60f469c aurel32
    .aiocb_size                = sizeof(RawAIOCB),
1414 414f0dab blueswir1
#endif
1415 3c529d93 aliguori
1416 eda578e5 aliguori
    .bdrv_read          = raw_read,
1417 eda578e5 aliguori
    .bdrv_write         = raw_write,
1418 e60f469c aurel32
    .bdrv_getlength        = raw_getlength,
1419 19cb3738 bellard
1420 19cb3738 bellard
    /* removable device support */
1421 e60f469c aurel32
    .bdrv_is_inserted        = raw_is_inserted,
1422 e60f469c aurel32
    .bdrv_media_changed        = raw_media_changed,
1423 e60f469c aurel32
    .bdrv_eject                = raw_eject,
1424 e60f469c aurel32
    .bdrv_set_locked        = raw_set_locked,
1425 985a03b0 ths
    /* generic scsi device */
1426 e60f469c aurel32
    .bdrv_ioctl                = raw_ioctl,
1427 8185d2c9 aliguori
#ifdef CONFIG_AIO
1428 221f715d aliguori
    .bdrv_aio_ioctl        = raw_aio_ioctl,
1429 8185d2c9 aliguori
#endif
1430 19cb3738 bellard
};