Statistics
| Branch: | Revision:

root / block / raw-posix.c @ 57e69b7d

History | View | Annotate | Download (35.9 kB)

1
/*
2
 * Block driver for RAW files (posix)
3
 *
4
 * Copyright (c) 2006 Fabrice Bellard
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24
#include "qemu-common.h"
25
#include "qemu-timer.h"
26
#include "qemu-char.h"
27
#include "qemu-log.h"
28
#include "block_int.h"
29
#include "module.h"
30
#include "block/raw-posix-aio.h"
31

    
32
#ifdef CONFIG_COCOA
33
#include <paths.h>
34
#include <sys/param.h>
35
#include <IOKit/IOKitLib.h>
36
#include <IOKit/IOBSD.h>
37
#include <IOKit/storage/IOMediaBSDClient.h>
38
#include <IOKit/storage/IOMedia.h>
39
#include <IOKit/storage/IOCDMedia.h>
40
//#include <IOKit/storage/IOCDTypes.h>
41
#include <CoreFoundation/CoreFoundation.h>
42
#endif
43

    
44
#ifdef __sun__
45
#define _POSIX_PTHREAD_SEMANTICS 1
46
#include <signal.h>
47
#include <sys/dkio.h>
48
#endif
49
#ifdef __linux__
50
#include <sys/ioctl.h>
51
#include <linux/cdrom.h>
52
#include <linux/fd.h>
53
#endif
54
#if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
55
#include <signal.h>
56
#include <sys/disk.h>
57
#include <sys/cdio.h>
58
#endif
59

    
60
#ifdef __OpenBSD__
61
#include <sys/ioctl.h>
62
#include <sys/disklabel.h>
63
#include <sys/dkio.h>
64
#endif
65

    
66
#ifdef __DragonFly__
67
#include <sys/ioctl.h>
68
#include <sys/diskslice.h>
69
#endif
70

    
71
//#define DEBUG_FLOPPY
72

    
73
//#define DEBUG_BLOCK
74
#if defined(DEBUG_BLOCK)
75
#define DEBUG_BLOCK_PRINT(formatCstr, ...) do { if (qemu_log_enabled()) \
76
    { qemu_log(formatCstr, ## __VA_ARGS__); qemu_log_flush(); } } while (0)
77
#else
78
#define DEBUG_BLOCK_PRINT(formatCstr, ...)
79
#endif
80

    
81
/* OS X does not have O_DSYNC */
82
#ifndef O_DSYNC
83
#ifdef O_SYNC
84
#define O_DSYNC O_SYNC
85
#elif defined(O_FSYNC)
86
#define O_DSYNC O_FSYNC
87
#endif
88
#endif
89

    
90
/* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */
91
#ifndef O_DIRECT
92
#define O_DIRECT O_DSYNC
93
#endif
94

    
95
#define FTYPE_FILE   0
96
#define FTYPE_CD     1
97
#define FTYPE_FD     2
98

    
99
#define ALIGNED_BUFFER_SIZE (32 * 512)
100

    
101
/* if the FD is not accessed during that time (in ms), we try to
102
   reopen it to see if the disk has been changed */
103
#define FD_OPEN_TIMEOUT 1000
104

    
105
typedef struct BDRVRawState {
106
    int fd;
107
    int type;
108
    unsigned int lseek_err_cnt;
109
    int open_flags;
110
#if defined(__linux__)
111
    /* linux floppy specific */
112
    int64_t fd_open_time;
113
    int64_t fd_error_time;
114
    int fd_got_error;
115
    int fd_media_changed;
116
#endif
117
#ifdef CONFIG_LINUX_AIO
118
    int use_aio;
119
    void *aio_ctx;
120
#endif
121
    uint8_t* aligned_buf;
122
} BDRVRawState;
123

    
124
static int fd_open(BlockDriverState *bs);
125
static int64_t raw_getlength(BlockDriverState *bs);
126

    
127
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
128
static int cdrom_reopen(BlockDriverState *bs);
129
#endif
130

    
131
static int raw_open_common(BlockDriverState *bs, const char *filename,
132
                           int bdrv_flags, int open_flags)
133
{
134
    BDRVRawState *s = bs->opaque;
135
    int fd, ret;
136

    
137
    s->lseek_err_cnt = 0;
138

    
139
    s->open_flags = open_flags | O_BINARY;
140
    s->open_flags &= ~O_ACCMODE;
141
    if (bdrv_flags & BDRV_O_RDWR) {
142
        s->open_flags |= O_RDWR;
143
    } else {
144
        s->open_flags |= O_RDONLY;
145
        bs->read_only = 1;
146
    }
147

    
148
    /* Use O_DSYNC for write-through caching, no flags for write-back caching,
149
     * and O_DIRECT for no caching. */
150
    if ((bdrv_flags & BDRV_O_NOCACHE))
151
        s->open_flags |= O_DIRECT;
152
    else if (!(bdrv_flags & BDRV_O_CACHE_WB))
153
        s->open_flags |= O_DSYNC;
154

    
155
    s->fd = -1;
156
    fd = qemu_open(filename, s->open_flags, 0644);
157
    if (fd < 0) {
158
        ret = -errno;
159
        if (ret == -EROFS)
160
            ret = -EACCES;
161
        return ret;
162
    }
163
    s->fd = fd;
164
    s->aligned_buf = NULL;
165

    
166
    if ((bdrv_flags & BDRV_O_NOCACHE)) {
167
        s->aligned_buf = qemu_blockalign(bs, ALIGNED_BUFFER_SIZE);
168
        if (s->aligned_buf == NULL) {
169
            goto out_close;
170
        }
171
    }
172

    
173
#ifdef CONFIG_LINUX_AIO
174
    if ((bdrv_flags & (BDRV_O_NOCACHE|BDRV_O_NATIVE_AIO)) ==
175
                      (BDRV_O_NOCACHE|BDRV_O_NATIVE_AIO)) {
176

    
177
        /* We're falling back to POSIX AIO in some cases */
178
        paio_init();
179

    
180
        s->aio_ctx = laio_init();
181
        if (!s->aio_ctx) {
182
            goto out_free_buf;
183
        }
184
        s->use_aio = 1;
185
    } else
186
#endif
187
    {
188
        if (paio_init() < 0) {
189
            goto out_free_buf;
190
        }
191
#ifdef CONFIG_LINUX_AIO
192
        s->use_aio = 0;
193
#endif
194
    }
195

    
196
    return 0;
197

    
198
out_free_buf:
199
    qemu_vfree(s->aligned_buf);
200
out_close:
201
    close(fd);
202
    return -errno;
203
}
204

    
205
static int raw_open(BlockDriverState *bs, const char *filename, int flags)
206
{
207
    BDRVRawState *s = bs->opaque;
208

    
209
    s->type = FTYPE_FILE;
210
    return raw_open_common(bs, filename, flags, 0);
211
}
212

    
213
/* XXX: use host sector size if necessary with:
214
#ifdef DIOCGSECTORSIZE
215
        {
216
            unsigned int sectorsize = 512;
217
            if (!ioctl(fd, DIOCGSECTORSIZE, &sectorsize) &&
218
                sectorsize > bufsize)
219
                bufsize = sectorsize;
220
        }
221
#endif
222
#ifdef CONFIG_COCOA
223
        u_int32_t   blockSize = 512;
224
        if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
225
            bufsize = blockSize;
226
        }
227
#endif
228
*/
229

    
230
/*
231
 * offset and count are in bytes, but must be multiples of 512 for files
232
 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
233
 *
234
 * This function may be called without alignment if the caller ensures
235
 * that O_DIRECT is not in effect.
236
 */
237
static int raw_pread_aligned(BlockDriverState *bs, int64_t offset,
238
                     uint8_t *buf, int count)
239
{
240
    BDRVRawState *s = bs->opaque;
241
    int ret;
242

    
243
    ret = fd_open(bs);
244
    if (ret < 0)
245
        return ret;
246

    
247
    if (offset >= 0 && lseek(s->fd, offset, SEEK_SET) == (off_t)-1) {
248
        ++(s->lseek_err_cnt);
249
        if(s->lseek_err_cnt <= 10) {
250
            DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
251
                              "] lseek failed : %d = %s\n",
252
                              s->fd, bs->filename, offset, buf, count,
253
                              bs->total_sectors, errno, strerror(errno));
254
        }
255
        return -1;
256
    }
257
    s->lseek_err_cnt=0;
258

    
259
    ret = read(s->fd, buf, count);
260
    if (ret == count)
261
        goto label__raw_read__success;
262

    
263
    /* Allow reads beyond the end (needed for pwrite) */
264
    if ((ret == 0) && bs->growable) {
265
        int64_t size = raw_getlength(bs);
266
        if (offset >= size) {
267
            memset(buf, 0, count);
268
            ret = count;
269
            goto label__raw_read__success;
270
        }
271
    }
272

    
273
    DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
274
                      "] read failed %d : %d = %s\n",
275
                      s->fd, bs->filename, offset, buf, count,
276
                      bs->total_sectors, ret, errno, strerror(errno));
277

    
278
    /* Try harder for CDrom. */
279
    if (bs->type == BDRV_TYPE_CDROM) {
280
        lseek(s->fd, offset, SEEK_SET);
281
        ret = read(s->fd, buf, count);
282
        if (ret == count)
283
            goto label__raw_read__success;
284
        lseek(s->fd, offset, SEEK_SET);
285
        ret = read(s->fd, buf, count);
286
        if (ret == count)
287
            goto label__raw_read__success;
288

    
289
        DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
290
                          "] retry read failed %d : %d = %s\n",
291
                          s->fd, bs->filename, offset, buf, count,
292
                          bs->total_sectors, ret, errno, strerror(errno));
293
    }
294

    
295
label__raw_read__success:
296

    
297
    return  (ret < 0) ? -errno : ret;
298
}
299

    
300
/*
301
 * offset and count are in bytes, but must be multiples of 512 for files
302
 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
303
 *
304
 * This function may be called without alignment if the caller ensures
305
 * that O_DIRECT is not in effect.
306
 */
307
static int raw_pwrite_aligned(BlockDriverState *bs, int64_t offset,
308
                      const uint8_t *buf, int count)
309
{
310
    BDRVRawState *s = bs->opaque;
311
    int ret;
312

    
313
    ret = fd_open(bs);
314
    if (ret < 0)
315
        return -errno;
316

    
317
    if (offset >= 0 && lseek(s->fd, offset, SEEK_SET) == (off_t)-1) {
318
        ++(s->lseek_err_cnt);
319
        if(s->lseek_err_cnt) {
320
            DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%"
321
                              PRId64 "] lseek failed : %d = %s\n",
322
                              s->fd, bs->filename, offset, buf, count,
323
                              bs->total_sectors, errno, strerror(errno));
324
        }
325
        return -EIO;
326
    }
327
    s->lseek_err_cnt = 0;
328

    
329
    ret = write(s->fd, buf, count);
330
    if (ret == count)
331
        goto label__raw_write__success;
332

    
333
    DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
334
                      "] write failed %d : %d = %s\n",
335
                      s->fd, bs->filename, offset, buf, count,
336
                      bs->total_sectors, ret, errno, strerror(errno));
337

    
338
label__raw_write__success:
339

    
340
    return  (ret < 0) ? -errno : ret;
341
}
342

    
343

    
344
/*
345
 * offset and count are in bytes and possibly not aligned. For files opened
346
 * with O_DIRECT, necessary alignments are ensured before calling
347
 * raw_pread_aligned to do the actual read.
348
 */
349
static int raw_pread(BlockDriverState *bs, int64_t offset,
350
                     uint8_t *buf, int count)
351
{
352
    BDRVRawState *s = bs->opaque;
353
    int size, ret, shift, sum;
354

    
355
    sum = 0;
356

    
357
    if (s->aligned_buf != NULL)  {
358

    
359
        if (offset & 0x1ff) {
360
            /* align offset on a 512 bytes boundary */
361

    
362
            shift = offset & 0x1ff;
363
            size = (shift + count + 0x1ff) & ~0x1ff;
364
            if (size > ALIGNED_BUFFER_SIZE)
365
                size = ALIGNED_BUFFER_SIZE;
366
            ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, size);
367
            if (ret < 0)
368
                return ret;
369

    
370
            size = 512 - shift;
371
            if (size > count)
372
                size = count;
373
            memcpy(buf, s->aligned_buf + shift, size);
374

    
375
            buf += size;
376
            offset += size;
377
            count -= size;
378
            sum += size;
379

    
380
            if (count == 0)
381
                return sum;
382
        }
383
        if (count & 0x1ff || (uintptr_t) buf & 0x1ff) {
384

    
385
            /* read on aligned buffer */
386

    
387
            while (count) {
388

    
389
                size = (count + 0x1ff) & ~0x1ff;
390
                if (size > ALIGNED_BUFFER_SIZE)
391
                    size = ALIGNED_BUFFER_SIZE;
392

    
393
                ret = raw_pread_aligned(bs, offset, s->aligned_buf, size);
394
                if (ret < 0) {
395
                    return ret;
396
                } else if (ret == 0) {
397
                    fprintf(stderr, "raw_pread: read beyond end of file\n");
398
                    abort();
399
                }
400

    
401
                size = ret;
402
                if (size > count)
403
                    size = count;
404

    
405
                memcpy(buf, s->aligned_buf, size);
406

    
407
                buf += size;
408
                offset += size;
409
                count -= size;
410
                sum += size;
411
            }
412

    
413
            return sum;
414
        }
415
    }
416

    
417
    return raw_pread_aligned(bs, offset, buf, count) + sum;
418
}
419

    
420
static int raw_read(BlockDriverState *bs, int64_t sector_num,
421
                    uint8_t *buf, int nb_sectors)
422
{
423
    int ret;
424

    
425
    ret = raw_pread(bs, sector_num * 512, buf, nb_sectors * 512);
426
    if (ret == (nb_sectors * 512))
427
        ret = 0;
428
    return ret;
429
}
430

    
431
/*
432
 * offset and count are in bytes and possibly not aligned. For files opened
433
 * with O_DIRECT, necessary alignments are ensured before calling
434
 * raw_pwrite_aligned to do the actual write.
435
 */
436
static int raw_pwrite(BlockDriverState *bs, int64_t offset,
437
                      const uint8_t *buf, int count)
438
{
439
    BDRVRawState *s = bs->opaque;
440
    int size, ret, shift, sum;
441

    
442
    sum = 0;
443

    
444
    if (s->aligned_buf != NULL) {
445

    
446
        if (offset & 0x1ff) {
447
            /* align offset on a 512 bytes boundary */
448
            shift = offset & 0x1ff;
449
            ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, 512);
450
            if (ret < 0)
451
                return ret;
452

    
453
            size = 512 - shift;
454
            if (size > count)
455
                size = count;
456
            memcpy(s->aligned_buf + shift, buf, size);
457

    
458
            ret = raw_pwrite_aligned(bs, offset - shift, s->aligned_buf, 512);
459
            if (ret < 0)
460
                return ret;
461

    
462
            buf += size;
463
            offset += size;
464
            count -= size;
465
            sum += size;
466

    
467
            if (count == 0)
468
                return sum;
469
        }
470
        if (count & 0x1ff || (uintptr_t) buf & 0x1ff) {
471

    
472
            while ((size = (count & ~0x1ff)) != 0) {
473

    
474
                if (size > ALIGNED_BUFFER_SIZE)
475
                    size = ALIGNED_BUFFER_SIZE;
476

    
477
                memcpy(s->aligned_buf, buf, size);
478

    
479
                ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, size);
480
                if (ret < 0)
481
                    return ret;
482

    
483
                buf += ret;
484
                offset += ret;
485
                count -= ret;
486
                sum += ret;
487
            }
488
            /* here, count < 512 because (count & ~0x1ff) == 0 */
489
            if (count) {
490
                ret = raw_pread_aligned(bs, offset, s->aligned_buf, 512);
491
                if (ret < 0)
492
                    return ret;
493
                 memcpy(s->aligned_buf, buf, count);
494

    
495
                 ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, 512);
496
                 if (ret < 0)
497
                     return ret;
498
                 if (count < ret)
499
                     ret = count;
500

    
501
                 sum += ret;
502
            }
503
            return sum;
504
        }
505
    }
506
    return raw_pwrite_aligned(bs, offset, buf, count) + sum;
507
}
508

    
509
static int raw_write(BlockDriverState *bs, int64_t sector_num,
510
                     const uint8_t *buf, int nb_sectors)
511
{
512
    int ret;
513
    ret = raw_pwrite(bs, sector_num * 512, buf, nb_sectors * 512);
514
    if (ret == (nb_sectors * 512))
515
        ret = 0;
516
    return ret;
517
}
518

    
519
/*
520
 * Check if all memory in this vector is sector aligned.
521
 */
522
static int qiov_is_aligned(QEMUIOVector *qiov)
523
{
524
    int i;
525

    
526
    for (i = 0; i < qiov->niov; i++) {
527
        if ((uintptr_t) qiov->iov[i].iov_base % 512) {
528
            return 0;
529
        }
530
    }
531

    
532
    return 1;
533
}
534

    
535
static BlockDriverAIOCB *raw_aio_submit(BlockDriverState *bs,
536
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
537
        BlockDriverCompletionFunc *cb, void *opaque, int type)
538
{
539
    BDRVRawState *s = bs->opaque;
540

    
541
    if (fd_open(bs) < 0)
542
        return NULL;
543

    
544
    /*
545
     * If O_DIRECT is used the buffer needs to be aligned on a sector
546
     * boundary.  Check if this is the case or telll the low-level
547
     * driver that it needs to copy the buffer.
548
     */
549
    if (s->aligned_buf) {
550
        if (!qiov_is_aligned(qiov)) {
551
            type |= QEMU_AIO_MISALIGNED;
552
#ifdef CONFIG_LINUX_AIO
553
        } else if (s->use_aio) {
554
            return laio_submit(bs, s->aio_ctx, s->fd, sector_num, qiov,
555
                               nb_sectors, cb, opaque, type);
556
#endif
557
        }
558
    }
559

    
560
    return paio_submit(bs, s->fd, sector_num, qiov, nb_sectors,
561
                       cb, opaque, type);
562
}
563

    
564
static BlockDriverAIOCB *raw_aio_readv(BlockDriverState *bs,
565
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
566
        BlockDriverCompletionFunc *cb, void *opaque)
567
{
568
    return raw_aio_submit(bs, sector_num, qiov, nb_sectors,
569
                          cb, opaque, QEMU_AIO_READ);
570
}
571

    
572
static BlockDriverAIOCB *raw_aio_writev(BlockDriverState *bs,
573
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
574
        BlockDriverCompletionFunc *cb, void *opaque)
575
{
576
    return raw_aio_submit(bs, sector_num, qiov, nb_sectors,
577
                          cb, opaque, QEMU_AIO_WRITE);
578
}
579

    
580
static BlockDriverAIOCB *raw_aio_flush(BlockDriverState *bs,
581
        BlockDriverCompletionFunc *cb, void *opaque)
582
{
583
    BDRVRawState *s = bs->opaque;
584

    
585
    if (fd_open(bs) < 0)
586
        return NULL;
587

    
588
    return paio_submit(bs, s->fd, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH);
589
}
590

    
591
static void raw_close(BlockDriverState *bs)
592
{
593
    BDRVRawState *s = bs->opaque;
594
    if (s->fd >= 0) {
595
        close(s->fd);
596
        s->fd = -1;
597
        if (s->aligned_buf != NULL)
598
            qemu_vfree(s->aligned_buf);
599
    }
600
}
601

    
602
static int raw_truncate(BlockDriverState *bs, int64_t offset)
603
{
604
    BDRVRawState *s = bs->opaque;
605
    if (s->type != FTYPE_FILE)
606
        return -ENOTSUP;
607
    if (ftruncate(s->fd, offset) < 0)
608
        return -errno;
609
    return 0;
610
}
611

    
612
#ifdef __OpenBSD__
613
static int64_t raw_getlength(BlockDriverState *bs)
614
{
615
    BDRVRawState *s = bs->opaque;
616
    int fd = s->fd;
617
    struct stat st;
618

    
619
    if (fstat(fd, &st))
620
        return -1;
621
    if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
622
        struct disklabel dl;
623

    
624
        if (ioctl(fd, DIOCGDINFO, &dl))
625
            return -1;
626
        return (uint64_t)dl.d_secsize *
627
            dl.d_partitions[DISKPART(st.st_rdev)].p_size;
628
    } else
629
        return st.st_size;
630
}
631
#else /* !__OpenBSD__ */
632
static int64_t  raw_getlength(BlockDriverState *bs)
633
{
634
    BDRVRawState *s = bs->opaque;
635
    int fd = s->fd;
636
    int64_t size;
637
#ifdef CONFIG_BSD
638
    struct stat sb;
639
#if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
640
    int reopened = 0;
641
#endif
642
#endif
643
#ifdef __sun__
644
    struct dk_minfo minfo;
645
    int rv;
646
#endif
647
    int ret;
648

    
649
    ret = fd_open(bs);
650
    if (ret < 0)
651
        return ret;
652

    
653
#ifdef CONFIG_BSD
654
#if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
655
again:
656
#endif
657
    if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
658
#ifdef DIOCGMEDIASIZE
659
        if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
660
#elif defined(DIOCGPART)
661
        {
662
                struct partinfo pi;
663
                if (ioctl(fd, DIOCGPART, &pi) == 0)
664
                        size = pi.media_size;
665
                else
666
                        size = 0;
667
        }
668
        if (size == 0)
669
#endif
670
#ifdef CONFIG_COCOA
671
        size = LONG_LONG_MAX;
672
#else
673
        size = lseek(fd, 0LL, SEEK_END);
674
#endif
675
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
676
        switch(s->type) {
677
        case FTYPE_CD:
678
            /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
679
            if (size == 2048LL * (unsigned)-1)
680
                size = 0;
681
            /* XXX no disc?  maybe we need to reopen... */
682
            if (size <= 0 && !reopened && cdrom_reopen(bs) >= 0) {
683
                reopened = 1;
684
                goto again;
685
            }
686
        }
687
#endif
688
    } else
689
#endif
690
#ifdef __sun__
691
    /*
692
     * use the DKIOCGMEDIAINFO ioctl to read the size.
693
     */
694
    rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo );
695
    if ( rv != -1 ) {
696
        size = minfo.dki_lbsize * minfo.dki_capacity;
697
    } else /* there are reports that lseek on some devices
698
              fails, but irc discussion said that contingency
699
              on contingency was overkill */
700
#endif
701
    {
702
        size = lseek(fd, 0, SEEK_END);
703
    }
704
    return size;
705
}
706
#endif
707

    
708
static int raw_create(const char *filename, QEMUOptionParameter *options)
709
{
710
    int fd;
711
    int result = 0;
712
    int64_t total_size = 0;
713

    
714
    /* Read out options */
715
    while (options && options->name) {
716
        if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
717
            total_size = options->value.n / 512;
718
        }
719
        options++;
720
    }
721

    
722
    fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
723
              0644);
724
    if (fd < 0) {
725
        result = -errno;
726
    } else {
727
        if (ftruncate(fd, total_size * 512) != 0) {
728
            result = -errno;
729
        }
730
        if (close(fd) != 0) {
731
            result = -errno;
732
        }
733
    }
734
    return result;
735
}
736

    
737
static void raw_flush(BlockDriverState *bs)
738
{
739
    BDRVRawState *s = bs->opaque;
740
    qemu_fdatasync(s->fd);
741
}
742

    
743

    
744
static QEMUOptionParameter raw_create_options[] = {
745
    {
746
        .name = BLOCK_OPT_SIZE,
747
        .type = OPT_SIZE,
748
        .help = "Virtual disk size"
749
    },
750
    { NULL }
751
};
752

    
753
static BlockDriver bdrv_raw = {
754
    .format_name = "raw",
755
    .instance_size = sizeof(BDRVRawState),
756
    .bdrv_probe = NULL, /* no probe for protocols */
757
    .bdrv_open = raw_open,
758
    .bdrv_read = raw_read,
759
    .bdrv_write = raw_write,
760
    .bdrv_close = raw_close,
761
    .bdrv_create = raw_create,
762
    .bdrv_flush = raw_flush,
763

    
764
    .bdrv_aio_readv = raw_aio_readv,
765
    .bdrv_aio_writev = raw_aio_writev,
766
    .bdrv_aio_flush = raw_aio_flush,
767

    
768
    .bdrv_truncate = raw_truncate,
769
    .bdrv_getlength = raw_getlength,
770

    
771
    .create_options = raw_create_options,
772
};
773

    
774
/***********************************************/
775
/* host device */
776

    
777
#ifdef CONFIG_COCOA
778
static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
779
static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
780

    
781
kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
782
{
783
    kern_return_t       kernResult;
784
    mach_port_t     masterPort;
785
    CFMutableDictionaryRef  classesToMatch;
786

    
787
    kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
788
    if ( KERN_SUCCESS != kernResult ) {
789
        printf( "IOMasterPort returned %d\n", kernResult );
790
    }
791

    
792
    classesToMatch = IOServiceMatching( kIOCDMediaClass );
793
    if ( classesToMatch == NULL ) {
794
        printf( "IOServiceMatching returned a NULL dictionary.\n" );
795
    } else {
796
    CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
797
    }
798
    kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
799
    if ( KERN_SUCCESS != kernResult )
800
    {
801
        printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
802
    }
803

    
804
    return kernResult;
805
}
806

    
807
kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
808
{
809
    io_object_t     nextMedia;
810
    kern_return_t   kernResult = KERN_FAILURE;
811
    *bsdPath = '\0';
812
    nextMedia = IOIteratorNext( mediaIterator );
813
    if ( nextMedia )
814
    {
815
        CFTypeRef   bsdPathAsCFString;
816
    bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
817
        if ( bsdPathAsCFString ) {
818
            size_t devPathLength;
819
            strcpy( bsdPath, _PATH_DEV );
820
            strcat( bsdPath, "r" );
821
            devPathLength = strlen( bsdPath );
822
            if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
823
                kernResult = KERN_SUCCESS;
824
            }
825
            CFRelease( bsdPathAsCFString );
826
        }
827
        IOObjectRelease( nextMedia );
828
    }
829

    
830
    return kernResult;
831
}
832

    
833
#endif
834

    
835
static int hdev_probe_device(const char *filename)
836
{
837
    struct stat st;
838

    
839
    /* allow a dedicated CD-ROM driver to match with a higher priority */
840
    if (strstart(filename, "/dev/cdrom", NULL))
841
        return 50;
842

    
843
    if (stat(filename, &st) >= 0 &&
844
            (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
845
        return 100;
846
    }
847

    
848
    return 0;
849
}
850

    
851
static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
852
{
853
    BDRVRawState *s = bs->opaque;
854

    
855
#ifdef CONFIG_COCOA
856
    if (strstart(filename, "/dev/cdrom", NULL)) {
857
        kern_return_t kernResult;
858
        io_iterator_t mediaIterator;
859
        char bsdPath[ MAXPATHLEN ];
860
        int fd;
861

    
862
        kernResult = FindEjectableCDMedia( &mediaIterator );
863
        kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
864

    
865
        if ( bsdPath[ 0 ] != '\0' ) {
866
            strcat(bsdPath,"s0");
867
            /* some CDs don't have a partition 0 */
868
            fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
869
            if (fd < 0) {
870
                bsdPath[strlen(bsdPath)-1] = '1';
871
            } else {
872
                close(fd);
873
            }
874
            filename = bsdPath;
875
        }
876

    
877
        if ( mediaIterator )
878
            IOObjectRelease( mediaIterator );
879
    }
880
#endif
881

    
882
    s->type = FTYPE_FILE;
883
#if defined(__linux__)
884
    if (strstart(filename, "/dev/sg", NULL)) {
885
        bs->sg = 1;
886
    }
887
#endif
888

    
889
    return raw_open_common(bs, filename, flags, 0);
890
}
891

    
892
#if defined(__linux__)
893
/* Note: we do not have a reliable method to detect if the floppy is
894
   present. The current method is to try to open the floppy at every
895
   I/O and to keep it opened during a few hundreds of ms. */
896
static int fd_open(BlockDriverState *bs)
897
{
898
    BDRVRawState *s = bs->opaque;
899
    int last_media_present;
900

    
901
    if (s->type != FTYPE_FD)
902
        return 0;
903
    last_media_present = (s->fd >= 0);
904
    if (s->fd >= 0 &&
905
        (qemu_get_clock(rt_clock) - s->fd_open_time) >= FD_OPEN_TIMEOUT) {
906
        close(s->fd);
907
        s->fd = -1;
908
#ifdef DEBUG_FLOPPY
909
        printf("Floppy closed\n");
910
#endif
911
    }
912
    if (s->fd < 0) {
913
        if (s->fd_got_error &&
914
            (qemu_get_clock(rt_clock) - s->fd_error_time) < FD_OPEN_TIMEOUT) {
915
#ifdef DEBUG_FLOPPY
916
            printf("No floppy (open delayed)\n");
917
#endif
918
            return -EIO;
919
        }
920
        s->fd = open(bs->filename, s->open_flags & ~O_NONBLOCK);
921
        if (s->fd < 0) {
922
            s->fd_error_time = qemu_get_clock(rt_clock);
923
            s->fd_got_error = 1;
924
            if (last_media_present)
925
                s->fd_media_changed = 1;
926
#ifdef DEBUG_FLOPPY
927
            printf("No floppy\n");
928
#endif
929
            return -EIO;
930
        }
931
#ifdef DEBUG_FLOPPY
932
        printf("Floppy opened\n");
933
#endif
934
    }
935
    if (!last_media_present)
936
        s->fd_media_changed = 1;
937
    s->fd_open_time = qemu_get_clock(rt_clock);
938
    s->fd_got_error = 0;
939
    return 0;
940
}
941

    
942
static int hdev_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
943
{
944
    BDRVRawState *s = bs->opaque;
945

    
946
    return ioctl(s->fd, req, buf);
947
}
948

    
949
static BlockDriverAIOCB *hdev_aio_ioctl(BlockDriverState *bs,
950
        unsigned long int req, void *buf,
951
        BlockDriverCompletionFunc *cb, void *opaque)
952
{
953
    BDRVRawState *s = bs->opaque;
954

    
955
    if (fd_open(bs) < 0)
956
        return NULL;
957
    return paio_ioctl(bs, s->fd, req, buf, cb, opaque);
958
}
959

    
960
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
961
static int fd_open(BlockDriverState *bs)
962
{
963
    BDRVRawState *s = bs->opaque;
964

    
965
    /* this is just to ensure s->fd is sane (its called by io ops) */
966
    if (s->fd >= 0)
967
        return 0;
968
    return -EIO;
969
}
970
#else /* !linux && !FreeBSD */
971

    
972
static int fd_open(BlockDriverState *bs)
973
{
974
    return 0;
975
}
976

    
977
#endif /* !linux && !FreeBSD */
978

    
979
static int hdev_create(const char *filename, QEMUOptionParameter *options)
980
{
981
    int fd;
982
    int ret = 0;
983
    struct stat stat_buf;
984
    int64_t total_size = 0;
985

    
986
    /* Read out options */
987
    while (options && options->name) {
988
        if (!strcmp(options->name, "size")) {
989
            total_size = options->value.n / 512;
990
        }
991
        options++;
992
    }
993

    
994
    fd = open(filename, O_WRONLY | O_BINARY);
995
    if (fd < 0)
996
        return -errno;
997

    
998
    if (fstat(fd, &stat_buf) < 0)
999
        ret = -errno;
1000
    else if (!S_ISBLK(stat_buf.st_mode) && !S_ISCHR(stat_buf.st_mode))
1001
        ret = -ENODEV;
1002
    else if (lseek(fd, 0, SEEK_END) < total_size * 512)
1003
        ret = -ENOSPC;
1004

    
1005
    close(fd);
1006
    return ret;
1007
}
1008

    
1009
static BlockDriver bdrv_host_device = {
1010
    .format_name        = "host_device",
1011
    .instance_size      = sizeof(BDRVRawState),
1012
    .bdrv_probe_device  = hdev_probe_device,
1013
    .bdrv_open          = hdev_open,
1014
    .bdrv_close         = raw_close,
1015
    .bdrv_create        = hdev_create,
1016
    .create_options     = raw_create_options,
1017
    .no_zero_init       = 1,
1018
    .bdrv_flush         = raw_flush,
1019

    
1020
    .bdrv_aio_readv        = raw_aio_readv,
1021
    .bdrv_aio_writev        = raw_aio_writev,
1022
    .bdrv_aio_flush        = raw_aio_flush,
1023

    
1024
    .bdrv_read          = raw_read,
1025
    .bdrv_write         = raw_write,
1026
    .bdrv_getlength        = raw_getlength,
1027

    
1028
    /* generic scsi device */
1029
#ifdef __linux__
1030
    .bdrv_ioctl         = hdev_ioctl,
1031
    .bdrv_aio_ioctl     = hdev_aio_ioctl,
1032
#endif
1033
};
1034

    
1035
#ifdef __linux__
1036
static int floppy_open(BlockDriverState *bs, const char *filename, int flags)
1037
{
1038
    BDRVRawState *s = bs->opaque;
1039
    int ret;
1040

    
1041
    s->type = FTYPE_FD;
1042

    
1043
    /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
1044
    ret = raw_open_common(bs, filename, flags, O_NONBLOCK);
1045
    if (ret)
1046
        return ret;
1047

    
1048
    /* close fd so that we can reopen it as needed */
1049
    close(s->fd);
1050
    s->fd = -1;
1051
    s->fd_media_changed = 1;
1052

    
1053
    return 0;
1054
}
1055

    
1056
static int floppy_probe_device(const char *filename)
1057
{
1058
    int fd, ret;
1059
    int prio = 0;
1060
    struct floppy_struct fdparam;
1061

    
1062
    if (strstart(filename, "/dev/fd", NULL))
1063
        prio = 50;
1064

    
1065
    fd = open(filename, O_RDONLY | O_NONBLOCK);
1066
    if (fd < 0) {
1067
        goto out;
1068
    }
1069

    
1070
    /* Attempt to detect via a floppy specific ioctl */
1071
    ret = ioctl(fd, FDGETPRM, &fdparam);
1072
    if (ret >= 0)
1073
        prio = 100;
1074

    
1075
    close(fd);
1076
out:
1077
    return prio;
1078
}
1079

    
1080

    
1081
static int floppy_is_inserted(BlockDriverState *bs)
1082
{
1083
    return fd_open(bs) >= 0;
1084
}
1085

    
1086
static int floppy_media_changed(BlockDriverState *bs)
1087
{
1088
    BDRVRawState *s = bs->opaque;
1089
    int ret;
1090

    
1091
    /*
1092
     * XXX: we do not have a true media changed indication.
1093
     * It does not work if the floppy is changed without trying to read it.
1094
     */
1095
    fd_open(bs);
1096
    ret = s->fd_media_changed;
1097
    s->fd_media_changed = 0;
1098
#ifdef DEBUG_FLOPPY
1099
    printf("Floppy changed=%d\n", ret);
1100
#endif
1101
    return ret;
1102
}
1103

    
1104
static int floppy_eject(BlockDriverState *bs, int eject_flag)
1105
{
1106
    BDRVRawState *s = bs->opaque;
1107
    int fd;
1108

    
1109
    if (s->fd >= 0) {
1110
        close(s->fd);
1111
        s->fd = -1;
1112
    }
1113
    fd = open(bs->filename, s->open_flags | O_NONBLOCK);
1114
    if (fd >= 0) {
1115
        if (ioctl(fd, FDEJECT, 0) < 0)
1116
            perror("FDEJECT");
1117
        close(fd);
1118
    }
1119

    
1120
    return 0;
1121
}
1122

    
1123
static BlockDriver bdrv_host_floppy = {
1124
    .format_name        = "host_floppy",
1125
    .instance_size      = sizeof(BDRVRawState),
1126
    .bdrv_probe_device        = floppy_probe_device,
1127
    .bdrv_open          = floppy_open,
1128
    .bdrv_close         = raw_close,
1129
    .bdrv_create        = hdev_create,
1130
    .create_options     = raw_create_options,
1131
    .no_zero_init       = 1,
1132
    .bdrv_flush         = raw_flush,
1133

    
1134
    .bdrv_aio_readv     = raw_aio_readv,
1135
    .bdrv_aio_writev    = raw_aio_writev,
1136
    .bdrv_aio_flush        = raw_aio_flush,
1137

    
1138
    .bdrv_read          = raw_read,
1139
    .bdrv_write         = raw_write,
1140
    .bdrv_getlength        = raw_getlength,
1141

    
1142
    /* removable device support */
1143
    .bdrv_is_inserted   = floppy_is_inserted,
1144
    .bdrv_media_changed = floppy_media_changed,
1145
    .bdrv_eject         = floppy_eject,
1146
};
1147

    
1148
static int cdrom_open(BlockDriverState *bs, const char *filename, int flags)
1149
{
1150
    BDRVRawState *s = bs->opaque;
1151

    
1152
    s->type = FTYPE_CD;
1153

    
1154
    /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
1155
    return raw_open_common(bs, filename, flags, O_NONBLOCK);
1156
}
1157

    
1158
static int cdrom_probe_device(const char *filename)
1159
{
1160
    int fd, ret;
1161
    int prio = 0;
1162

    
1163
    if (strstart(filename, "/dev/cd", NULL))
1164
        prio = 50;
1165

    
1166
    fd = open(filename, O_RDONLY | O_NONBLOCK);
1167
    if (fd < 0) {
1168
        goto out;
1169
    }
1170

    
1171
    /* Attempt to detect via a CDROM specific ioctl */
1172
    ret = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
1173
    if (ret >= 0)
1174
        prio = 100;
1175

    
1176
    close(fd);
1177
out:
1178
    return prio;
1179
}
1180

    
1181
static int cdrom_is_inserted(BlockDriverState *bs)
1182
{
1183
    BDRVRawState *s = bs->opaque;
1184
    int ret;
1185

    
1186
    ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
1187
    if (ret == CDS_DISC_OK)
1188
        return 1;
1189
    return 0;
1190
}
1191

    
1192
static int cdrom_eject(BlockDriverState *bs, int eject_flag)
1193
{
1194
    BDRVRawState *s = bs->opaque;
1195

    
1196
    if (eject_flag) {
1197
        if (ioctl(s->fd, CDROMEJECT, NULL) < 0)
1198
            perror("CDROMEJECT");
1199
    } else {
1200
        if (ioctl(s->fd, CDROMCLOSETRAY, NULL) < 0)
1201
            perror("CDROMEJECT");
1202
    }
1203

    
1204
    return 0;
1205
}
1206

    
1207
static int cdrom_set_locked(BlockDriverState *bs, int locked)
1208
{
1209
    BDRVRawState *s = bs->opaque;
1210

    
1211
    if (ioctl(s->fd, CDROM_LOCKDOOR, locked) < 0) {
1212
        /*
1213
         * Note: an error can happen if the distribution automatically
1214
         * mounts the CD-ROM
1215
         */
1216
        /* perror("CDROM_LOCKDOOR"); */
1217
    }
1218

    
1219
    return 0;
1220
}
1221

    
1222
static BlockDriver bdrv_host_cdrom = {
1223
    .format_name        = "host_cdrom",
1224
    .instance_size      = sizeof(BDRVRawState),
1225
    .bdrv_probe_device        = cdrom_probe_device,
1226
    .bdrv_open          = cdrom_open,
1227
    .bdrv_close         = raw_close,
1228
    .bdrv_create        = hdev_create,
1229
    .create_options     = raw_create_options,
1230
    .no_zero_init       = 1,
1231
    .bdrv_flush         = raw_flush,
1232

    
1233
    .bdrv_aio_readv     = raw_aio_readv,
1234
    .bdrv_aio_writev    = raw_aio_writev,
1235
    .bdrv_aio_flush        = raw_aio_flush,
1236

    
1237
    .bdrv_read          = raw_read,
1238
    .bdrv_write         = raw_write,
1239
    .bdrv_getlength     = raw_getlength,
1240

    
1241
    /* removable device support */
1242
    .bdrv_is_inserted   = cdrom_is_inserted,
1243
    .bdrv_eject         = cdrom_eject,
1244
    .bdrv_set_locked    = cdrom_set_locked,
1245

    
1246
    /* generic scsi device */
1247
    .bdrv_ioctl         = hdev_ioctl,
1248
    .bdrv_aio_ioctl     = hdev_aio_ioctl,
1249
};
1250
#endif /* __linux__ */
1251

    
1252
#if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
1253
static int cdrom_open(BlockDriverState *bs, const char *filename, int flags)
1254
{
1255
    BDRVRawState *s = bs->opaque;
1256
    int ret;
1257

    
1258
    s->type = FTYPE_CD;
1259

    
1260
    ret = raw_open_common(bs, filename, flags, 0);
1261
    if (ret)
1262
        return ret;
1263

    
1264
    /* make sure the door isnt locked at this time */
1265
    ioctl(s->fd, CDIOCALLOW);
1266
    return 0;
1267
}
1268

    
1269
static int cdrom_probe_device(const char *filename)
1270
{
1271
    if (strstart(filename, "/dev/cd", NULL) ||
1272
            strstart(filename, "/dev/acd", NULL))
1273
        return 100;
1274
    return 0;
1275
}
1276

    
1277
static int cdrom_reopen(BlockDriverState *bs)
1278
{
1279
    BDRVRawState *s = bs->opaque;
1280
    int fd;
1281

    
1282
    /*
1283
     * Force reread of possibly changed/newly loaded disc,
1284
     * FreeBSD seems to not notice sometimes...
1285
     */
1286
    if (s->fd >= 0)
1287
        close(s->fd);
1288
    fd = open(bs->filename, s->open_flags, 0644);
1289
    if (fd < 0) {
1290
        s->fd = -1;
1291
        return -EIO;
1292
    }
1293
    s->fd = fd;
1294

    
1295
    /* make sure the door isnt locked at this time */
1296
    ioctl(s->fd, CDIOCALLOW);
1297
    return 0;
1298
}
1299

    
1300
static int cdrom_is_inserted(BlockDriverState *bs)
1301
{
1302
    return raw_getlength(bs) > 0;
1303
}
1304

    
1305
static int cdrom_eject(BlockDriverState *bs, int eject_flag)
1306
{
1307
    BDRVRawState *s = bs->opaque;
1308

    
1309
    if (s->fd < 0)
1310
        return -ENOTSUP;
1311

    
1312
    (void) ioctl(s->fd, CDIOCALLOW);
1313

    
1314
    if (eject_flag) {
1315
        if (ioctl(s->fd, CDIOCEJECT) < 0)
1316
            perror("CDIOCEJECT");
1317
    } else {
1318
        if (ioctl(s->fd, CDIOCCLOSE) < 0)
1319
            perror("CDIOCCLOSE");
1320
    }
1321

    
1322
    if (cdrom_reopen(bs) < 0)
1323
        return -ENOTSUP;
1324
    return 0;
1325
}
1326

    
1327
static int cdrom_set_locked(BlockDriverState *bs, int locked)
1328
{
1329
    BDRVRawState *s = bs->opaque;
1330

    
1331
    if (s->fd < 0)
1332
        return -ENOTSUP;
1333
    if (ioctl(s->fd, (locked ? CDIOCPREVENT : CDIOCALLOW)) < 0) {
1334
        /*
1335
         * Note: an error can happen if the distribution automatically
1336
         * mounts the CD-ROM
1337
         */
1338
        /* perror("CDROM_LOCKDOOR"); */
1339
    }
1340

    
1341
    return 0;
1342
}
1343

    
1344
static BlockDriver bdrv_host_cdrom = {
1345
    .format_name        = "host_cdrom",
1346
    .instance_size      = sizeof(BDRVRawState),
1347
    .bdrv_probe_device        = cdrom_probe_device,
1348
    .bdrv_open          = cdrom_open,
1349
    .bdrv_close         = raw_close,
1350
    .bdrv_create        = hdev_create,
1351
    .create_options     = raw_create_options,
1352
    .no_zero_init       = 1,
1353
    .bdrv_flush         = raw_flush,
1354

    
1355
    .bdrv_aio_readv     = raw_aio_readv,
1356
    .bdrv_aio_writev    = raw_aio_writev,
1357
    .bdrv_aio_flush        = raw_aio_flush,
1358

    
1359
    .bdrv_read          = raw_read,
1360
    .bdrv_write         = raw_write,
1361
    .bdrv_getlength     = raw_getlength,
1362

    
1363
    /* removable device support */
1364
    .bdrv_is_inserted   = cdrom_is_inserted,
1365
    .bdrv_eject         = cdrom_eject,
1366
    .bdrv_set_locked    = cdrom_set_locked,
1367
};
1368
#endif /* __FreeBSD__ */
1369

    
1370
static void bdrv_raw_init(void)
1371
{
1372
    /*
1373
     * Register all the drivers.  Note that order is important, the driver
1374
     * registered last will get probed first.
1375
     */
1376
    bdrv_register(&bdrv_raw);
1377
    bdrv_register(&bdrv_host_device);
1378
#ifdef __linux__
1379
    bdrv_register(&bdrv_host_floppy);
1380
    bdrv_register(&bdrv_host_cdrom);
1381
#endif
1382
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1383
    bdrv_register(&bdrv_host_cdrom);
1384
#endif
1385
}
1386

    
1387
block_init(bdrv_raw_init);