Statistics
| Branch: | Revision:

root / block-raw-posix.c @ 6ce0ca12

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