Statistics
| Branch: | Revision:

root / block-raw-posix.c @ f36672ae

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