Statistics
| Branch: | Revision:

root / block / raw-posix.c @ dc7588c1

History | View | Annotate | Download (49.1 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/log.h"
27
#include "block/block_int.h"
28
#include "qemu/module.h"
29
#include "trace.h"
30
#include "block/thread-pool.h"
31
#include "qemu/iov.h"
32
#include "raw-aio.h"
33

    
34
#if defined(__APPLE__) && (__MACH__)
35
#include <paths.h>
36
#include <sys/param.h>
37
#include <IOKit/IOKitLib.h>
38
#include <IOKit/IOBSD.h>
39
#include <IOKit/storage/IOMediaBSDClient.h>
40
#include <IOKit/storage/IOMedia.h>
41
#include <IOKit/storage/IOCDMedia.h>
42
//#include <IOKit/storage/IOCDTypes.h>
43
#include <CoreFoundation/CoreFoundation.h>
44
#endif
45

    
46
#ifdef __sun__
47
#define _POSIX_PTHREAD_SEMANTICS 1
48
#include <sys/dkio.h>
49
#endif
50
#ifdef __linux__
51
#include <sys/types.h>
52
#include <sys/stat.h>
53
#include <sys/ioctl.h>
54
#include <sys/param.h>
55
#include <linux/cdrom.h>
56
#include <linux/fd.h>
57
#include <linux/fs.h>
58
#endif
59
#ifdef CONFIG_FIEMAP
60
#include <linux/fiemap.h>
61
#endif
62
#ifdef CONFIG_FALLOCATE_PUNCH_HOLE
63
#include <linux/falloc.h>
64
#endif
65
#if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
66
#include <sys/disk.h>
67
#include <sys/cdio.h>
68
#endif
69

    
70
#ifdef __OpenBSD__
71
#include <sys/ioctl.h>
72
#include <sys/disklabel.h>
73
#include <sys/dkio.h>
74
#endif
75

    
76
#ifdef __NetBSD__
77
#include <sys/ioctl.h>
78
#include <sys/disklabel.h>
79
#include <sys/dkio.h>
80
#include <sys/disk.h>
81
#endif
82

    
83
#ifdef __DragonFly__
84
#include <sys/ioctl.h>
85
#include <sys/diskslice.h>
86
#endif
87

    
88
#ifdef CONFIG_XFS
89
#include <xfs/xfs.h>
90
#endif
91

    
92
//#define DEBUG_FLOPPY
93

    
94
//#define DEBUG_BLOCK
95
#if defined(DEBUG_BLOCK)
96
#define DEBUG_BLOCK_PRINT(formatCstr, ...) do { if (qemu_log_enabled()) \
97
    { qemu_log(formatCstr, ## __VA_ARGS__); qemu_log_flush(); } } while (0)
98
#else
99
#define DEBUG_BLOCK_PRINT(formatCstr, ...)
100
#endif
101

    
102
/* OS X does not have O_DSYNC */
103
#ifndef O_DSYNC
104
#ifdef O_SYNC
105
#define O_DSYNC O_SYNC
106
#elif defined(O_FSYNC)
107
#define O_DSYNC O_FSYNC
108
#endif
109
#endif
110

    
111
/* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */
112
#ifndef O_DIRECT
113
#define O_DIRECT O_DSYNC
114
#endif
115

    
116
#define FTYPE_FILE   0
117
#define FTYPE_CD     1
118
#define FTYPE_FD     2
119

    
120
/* if the FD is not accessed during that time (in ns), we try to
121
   reopen it to see if the disk has been changed */
122
#define FD_OPEN_TIMEOUT (1000000000)
123

    
124
#define MAX_BLOCKSIZE        4096
125

    
126
typedef struct BDRVRawState {
127
    int fd;
128
    int type;
129
    int open_flags;
130
#if defined(__linux__)
131
    /* linux floppy specific */
132
    int64_t fd_open_time;
133
    int64_t fd_error_time;
134
    int fd_got_error;
135
    int fd_media_changed;
136
#endif
137
#ifdef CONFIG_LINUX_AIO
138
    int use_aio;
139
    void *aio_ctx;
140
#endif
141
#ifdef CONFIG_XFS
142
    bool is_xfs : 1;
143
#endif
144
    bool has_discard : 1;
145
} BDRVRawState;
146

    
147
typedef struct BDRVRawReopenState {
148
    int fd;
149
    int open_flags;
150
#ifdef CONFIG_LINUX_AIO
151
    int use_aio;
152
#endif
153
} BDRVRawReopenState;
154

    
155
static int fd_open(BlockDriverState *bs);
156
static int64_t raw_getlength(BlockDriverState *bs);
157

    
158
typedef struct RawPosixAIOData {
159
    BlockDriverState *bs;
160
    int aio_fildes;
161
    union {
162
        struct iovec *aio_iov;
163
        void *aio_ioctl_buf;
164
    };
165
    int aio_niov;
166
    uint64_t aio_nbytes;
167
#define aio_ioctl_cmd   aio_nbytes /* for QEMU_AIO_IOCTL */
168
    off_t aio_offset;
169
    int aio_type;
170
} RawPosixAIOData;
171

    
172
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
173
static int cdrom_reopen(BlockDriverState *bs);
174
#endif
175

    
176
#if defined(__NetBSD__)
177
static int raw_normalize_devicepath(const char **filename)
178
{
179
    static char namebuf[PATH_MAX];
180
    const char *dp, *fname;
181
    struct stat sb;
182

    
183
    fname = *filename;
184
    dp = strrchr(fname, '/');
185
    if (lstat(fname, &sb) < 0) {
186
        fprintf(stderr, "%s: stat failed: %s\n",
187
            fname, strerror(errno));
188
        return -errno;
189
    }
190

    
191
    if (!S_ISBLK(sb.st_mode)) {
192
        return 0;
193
    }
194

    
195
    if (dp == NULL) {
196
        snprintf(namebuf, PATH_MAX, "r%s", fname);
197
    } else {
198
        snprintf(namebuf, PATH_MAX, "%.*s/r%s",
199
            (int)(dp - fname), fname, dp + 1);
200
    }
201
    fprintf(stderr, "%s is a block device", fname);
202
    *filename = namebuf;
203
    fprintf(stderr, ", using %s\n", *filename);
204

    
205
    return 0;
206
}
207
#else
208
static int raw_normalize_devicepath(const char **filename)
209
{
210
    return 0;
211
}
212
#endif
213

    
214
static void raw_parse_flags(int bdrv_flags, int *open_flags)
215
{
216
    assert(open_flags != NULL);
217

    
218
    *open_flags |= O_BINARY;
219
    *open_flags &= ~O_ACCMODE;
220
    if (bdrv_flags & BDRV_O_RDWR) {
221
        *open_flags |= O_RDWR;
222
    } else {
223
        *open_flags |= O_RDONLY;
224
    }
225

    
226
    /* Use O_DSYNC for write-through caching, no flags for write-back caching,
227
     * and O_DIRECT for no caching. */
228
    if ((bdrv_flags & BDRV_O_NOCACHE)) {
229
        *open_flags |= O_DIRECT;
230
    }
231
}
232

    
233
#ifdef CONFIG_LINUX_AIO
234
static int raw_set_aio(void **aio_ctx, int *use_aio, int bdrv_flags)
235
{
236
    int ret = -1;
237
    assert(aio_ctx != NULL);
238
    assert(use_aio != NULL);
239
    /*
240
     * Currently Linux do AIO only for files opened with O_DIRECT
241
     * specified so check NOCACHE flag too
242
     */
243
    if ((bdrv_flags & (BDRV_O_NOCACHE|BDRV_O_NATIVE_AIO)) ==
244
                      (BDRV_O_NOCACHE|BDRV_O_NATIVE_AIO)) {
245

    
246
        /* if non-NULL, laio_init() has already been run */
247
        if (*aio_ctx == NULL) {
248
            *aio_ctx = laio_init();
249
            if (!*aio_ctx) {
250
                goto error;
251
            }
252
        }
253
        *use_aio = 1;
254
    } else {
255
        *use_aio = 0;
256
    }
257

    
258
    ret = 0;
259

    
260
error:
261
    return ret;
262
}
263
#endif
264

    
265
static int raw_open_common(BlockDriverState *bs, const char *filename,
266
                           int bdrv_flags, int open_flags)
267
{
268
    BDRVRawState *s = bs->opaque;
269
    int fd, ret;
270

    
271
    ret = raw_normalize_devicepath(&filename);
272
    if (ret != 0) {
273
        return ret;
274
    }
275

    
276
    s->open_flags = open_flags;
277
    raw_parse_flags(bdrv_flags, &s->open_flags);
278

    
279
    s->fd = -1;
280
    fd = qemu_open(filename, s->open_flags, 0644);
281
    if (fd < 0) {
282
        ret = -errno;
283
        if (ret == -EROFS)
284
            ret = -EACCES;
285
        return ret;
286
    }
287
    s->fd = fd;
288

    
289
#ifdef CONFIG_LINUX_AIO
290
    if (raw_set_aio(&s->aio_ctx, &s->use_aio, bdrv_flags)) {
291
        qemu_close(fd);
292
        return -errno;
293
    }
294
#endif
295

    
296
    s->has_discard = 1;
297
#ifdef CONFIG_XFS
298
    if (platform_test_xfs_fd(s->fd)) {
299
        s->is_xfs = 1;
300
    }
301
#endif
302

    
303
    return 0;
304
}
305

    
306
static int raw_open(BlockDriverState *bs, const char *filename,
307
                    QDict *options, int flags)
308
{
309
    BDRVRawState *s = bs->opaque;
310

    
311
    s->type = FTYPE_FILE;
312
    return raw_open_common(bs, filename, flags, 0);
313
}
314

    
315
static int raw_reopen_prepare(BDRVReopenState *state,
316
                              BlockReopenQueue *queue, Error **errp)
317
{
318
    BDRVRawState *s;
319
    BDRVRawReopenState *raw_s;
320
    int ret = 0;
321

    
322
    assert(state != NULL);
323
    assert(state->bs != NULL);
324

    
325
    s = state->bs->opaque;
326

    
327
    state->opaque = g_malloc0(sizeof(BDRVRawReopenState));
328
    raw_s = state->opaque;
329

    
330
#ifdef CONFIG_LINUX_AIO
331
    raw_s->use_aio = s->use_aio;
332

    
333
    /* we can use s->aio_ctx instead of a copy, because the use_aio flag is
334
     * valid in the 'false' condition even if aio_ctx is set, and raw_set_aio()
335
     * won't override aio_ctx if aio_ctx is non-NULL */
336
    if (raw_set_aio(&s->aio_ctx, &raw_s->use_aio, state->flags)) {
337
        return -1;
338
    }
339
#endif
340

    
341
    if (s->type == FTYPE_FD || s->type == FTYPE_CD) {
342
        raw_s->open_flags |= O_NONBLOCK;
343
    }
344

    
345
    raw_parse_flags(state->flags, &raw_s->open_flags);
346

    
347
    raw_s->fd = -1;
348

    
349
    int fcntl_flags = O_APPEND | O_NONBLOCK;
350
#ifdef O_NOATIME
351
    fcntl_flags |= O_NOATIME;
352
#endif
353

    
354
#ifdef O_ASYNC
355
    /* Not all operating systems have O_ASYNC, and those that don't
356
     * will not let us track the state into raw_s->open_flags (typically
357
     * you achieve the same effect with an ioctl, for example I_SETSIG
358
     * on Solaris). But we do not use O_ASYNC, so that's fine.
359
     */
360
    assert((s->open_flags & O_ASYNC) == 0);
361
#endif
362

    
363
    if ((raw_s->open_flags & ~fcntl_flags) == (s->open_flags & ~fcntl_flags)) {
364
        /* dup the original fd */
365
        /* TODO: use qemu fcntl wrapper */
366
#ifdef F_DUPFD_CLOEXEC
367
        raw_s->fd = fcntl(s->fd, F_DUPFD_CLOEXEC, 0);
368
#else
369
        raw_s->fd = dup(s->fd);
370
        if (raw_s->fd != -1) {
371
            qemu_set_cloexec(raw_s->fd);
372
        }
373
#endif
374
        if (raw_s->fd >= 0) {
375
            ret = fcntl_setfl(raw_s->fd, raw_s->open_flags);
376
            if (ret) {
377
                qemu_close(raw_s->fd);
378
                raw_s->fd = -1;
379
            }
380
        }
381
    }
382

    
383
    /* If we cannot use fcntl, or fcntl failed, fall back to qemu_open() */
384
    if (raw_s->fd == -1) {
385
        assert(!(raw_s->open_flags & O_CREAT));
386
        raw_s->fd = qemu_open(state->bs->filename, raw_s->open_flags);
387
        if (raw_s->fd == -1) {
388
            ret = -1;
389
        }
390
    }
391
    return ret;
392
}
393

    
394

    
395
static void raw_reopen_commit(BDRVReopenState *state)
396
{
397
    BDRVRawReopenState *raw_s = state->opaque;
398
    BDRVRawState *s = state->bs->opaque;
399

    
400
    s->open_flags = raw_s->open_flags;
401

    
402
    qemu_close(s->fd);
403
    s->fd = raw_s->fd;
404
#ifdef CONFIG_LINUX_AIO
405
    s->use_aio = raw_s->use_aio;
406
#endif
407

    
408
    g_free(state->opaque);
409
    state->opaque = NULL;
410
}
411

    
412

    
413
static void raw_reopen_abort(BDRVReopenState *state)
414
{
415
    BDRVRawReopenState *raw_s = state->opaque;
416

    
417
     /* nothing to do if NULL, we didn't get far enough */
418
    if (raw_s == NULL) {
419
        return;
420
    }
421

    
422
    if (raw_s->fd >= 0) {
423
        qemu_close(raw_s->fd);
424
        raw_s->fd = -1;
425
    }
426
    g_free(state->opaque);
427
    state->opaque = NULL;
428
}
429

    
430

    
431
/* XXX: use host sector size if necessary with:
432
#ifdef DIOCGSECTORSIZE
433
        {
434
            unsigned int sectorsize = 512;
435
            if (!ioctl(fd, DIOCGSECTORSIZE, &sectorsize) &&
436
                sectorsize > bufsize)
437
                bufsize = sectorsize;
438
        }
439
#endif
440
#ifdef CONFIG_COCOA
441
        uint32_t blockSize = 512;
442
        if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
443
            bufsize = blockSize;
444
        }
445
#endif
446
*/
447

    
448
static ssize_t handle_aiocb_ioctl(RawPosixAIOData *aiocb)
449
{
450
    int ret;
451

    
452
    ret = ioctl(aiocb->aio_fildes, aiocb->aio_ioctl_cmd, aiocb->aio_ioctl_buf);
453
    if (ret == -1) {
454
        return -errno;
455
    }
456

    
457
    return 0;
458
}
459

    
460
static ssize_t handle_aiocb_flush(RawPosixAIOData *aiocb)
461
{
462
    int ret;
463

    
464
    ret = qemu_fdatasync(aiocb->aio_fildes);
465
    if (ret == -1) {
466
        return -errno;
467
    }
468
    return 0;
469
}
470

    
471
#ifdef CONFIG_PREADV
472

    
473
static bool preadv_present = true;
474

    
475
static ssize_t
476
qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset)
477
{
478
    return preadv(fd, iov, nr_iov, offset);
479
}
480

    
481
static ssize_t
482
qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset)
483
{
484
    return pwritev(fd, iov, nr_iov, offset);
485
}
486

    
487
#else
488

    
489
static bool preadv_present = false;
490

    
491
static ssize_t
492
qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset)
493
{
494
    return -ENOSYS;
495
}
496

    
497
static ssize_t
498
qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset)
499
{
500
    return -ENOSYS;
501
}
502

    
503
#endif
504

    
505
static ssize_t handle_aiocb_rw_vector(RawPosixAIOData *aiocb)
506
{
507
    ssize_t len;
508

    
509
    do {
510
        if (aiocb->aio_type & QEMU_AIO_WRITE)
511
            len = qemu_pwritev(aiocb->aio_fildes,
512
                               aiocb->aio_iov,
513
                               aiocb->aio_niov,
514
                               aiocb->aio_offset);
515
         else
516
            len = qemu_preadv(aiocb->aio_fildes,
517
                              aiocb->aio_iov,
518
                              aiocb->aio_niov,
519
                              aiocb->aio_offset);
520
    } while (len == -1 && errno == EINTR);
521

    
522
    if (len == -1) {
523
        return -errno;
524
    }
525
    return len;
526
}
527

    
528
/*
529
 * Read/writes the data to/from a given linear buffer.
530
 *
531
 * Returns the number of bytes handles or -errno in case of an error. Short
532
 * reads are only returned if the end of the file is reached.
533
 */
534
static ssize_t handle_aiocb_rw_linear(RawPosixAIOData *aiocb, char *buf)
535
{
536
    ssize_t offset = 0;
537
    ssize_t len;
538

    
539
    while (offset < aiocb->aio_nbytes) {
540
        if (aiocb->aio_type & QEMU_AIO_WRITE) {
541
            len = pwrite(aiocb->aio_fildes,
542
                         (const char *)buf + offset,
543
                         aiocb->aio_nbytes - offset,
544
                         aiocb->aio_offset + offset);
545
        } else {
546
            len = pread(aiocb->aio_fildes,
547
                        buf + offset,
548
                        aiocb->aio_nbytes - offset,
549
                        aiocb->aio_offset + offset);
550
        }
551
        if (len == -1 && errno == EINTR) {
552
            continue;
553
        } else if (len == -1) {
554
            offset = -errno;
555
            break;
556
        } else if (len == 0) {
557
            break;
558
        }
559
        offset += len;
560
    }
561

    
562
    return offset;
563
}
564

    
565
static ssize_t handle_aiocb_rw(RawPosixAIOData *aiocb)
566
{
567
    ssize_t nbytes;
568
    char *buf;
569

    
570
    if (!(aiocb->aio_type & QEMU_AIO_MISALIGNED)) {
571
        /*
572
         * If there is just a single buffer, and it is properly aligned
573
         * we can just use plain pread/pwrite without any problems.
574
         */
575
        if (aiocb->aio_niov == 1) {
576
             return handle_aiocb_rw_linear(aiocb, aiocb->aio_iov->iov_base);
577
        }
578
        /*
579
         * We have more than one iovec, and all are properly aligned.
580
         *
581
         * Try preadv/pwritev first and fall back to linearizing the
582
         * buffer if it's not supported.
583
         */
584
        if (preadv_present) {
585
            nbytes = handle_aiocb_rw_vector(aiocb);
586
            if (nbytes == aiocb->aio_nbytes ||
587
                (nbytes < 0 && nbytes != -ENOSYS)) {
588
                return nbytes;
589
            }
590
            preadv_present = false;
591
        }
592

    
593
        /*
594
         * XXX(hch): short read/write.  no easy way to handle the reminder
595
         * using these interfaces.  For now retry using plain
596
         * pread/pwrite?
597
         */
598
    }
599

    
600
    /*
601
     * Ok, we have to do it the hard way, copy all segments into
602
     * a single aligned buffer.
603
     */
604
    buf = qemu_blockalign(aiocb->bs, aiocb->aio_nbytes);
605
    if (aiocb->aio_type & QEMU_AIO_WRITE) {
606
        char *p = buf;
607
        int i;
608

    
609
        for (i = 0; i < aiocb->aio_niov; ++i) {
610
            memcpy(p, aiocb->aio_iov[i].iov_base, aiocb->aio_iov[i].iov_len);
611
            p += aiocb->aio_iov[i].iov_len;
612
        }
613
    }
614

    
615
    nbytes = handle_aiocb_rw_linear(aiocb, buf);
616
    if (!(aiocb->aio_type & QEMU_AIO_WRITE)) {
617
        char *p = buf;
618
        size_t count = aiocb->aio_nbytes, copy;
619
        int i;
620

    
621
        for (i = 0; i < aiocb->aio_niov && count; ++i) {
622
            copy = count;
623
            if (copy > aiocb->aio_iov[i].iov_len) {
624
                copy = aiocb->aio_iov[i].iov_len;
625
            }
626
            memcpy(aiocb->aio_iov[i].iov_base, p, copy);
627
            p     += copy;
628
            count -= copy;
629
        }
630
    }
631
    qemu_vfree(buf);
632

    
633
    return nbytes;
634
}
635

    
636
#ifdef CONFIG_XFS
637
static int xfs_discard(BDRVRawState *s, int64_t offset, uint64_t bytes)
638
{
639
    struct xfs_flock64 fl;
640

    
641
    memset(&fl, 0, sizeof(fl));
642
    fl.l_whence = SEEK_SET;
643
    fl.l_start = offset;
644
    fl.l_len = bytes;
645

    
646
    if (xfsctl(NULL, s->fd, XFS_IOC_UNRESVSP64, &fl) < 0) {
647
        DEBUG_BLOCK_PRINT("cannot punch hole (%s)\n", strerror(errno));
648
        return -errno;
649
    }
650

    
651
    return 0;
652
}
653
#endif
654

    
655
static ssize_t handle_aiocb_discard(RawPosixAIOData *aiocb)
656
{
657
    int ret = -EOPNOTSUPP;
658
    BDRVRawState *s = aiocb->bs->opaque;
659

    
660
    if (s->has_discard == 0) {
661
        return 0;
662
    }
663

    
664
    if (aiocb->aio_type & QEMU_AIO_BLKDEV) {
665
#ifdef BLKDISCARD
666
        do {
667
            uint64_t range[2] = { aiocb->aio_offset, aiocb->aio_nbytes };
668
            if (ioctl(aiocb->aio_fildes, BLKDISCARD, range) == 0) {
669
                return 0;
670
            }
671
        } while (errno == EINTR);
672

    
673
        ret = -errno;
674
#endif
675
    } else {
676
#ifdef CONFIG_XFS
677
        if (s->is_xfs) {
678
            return xfs_discard(s, aiocb->aio_offset, aiocb->aio_nbytes);
679
        }
680
#endif
681

    
682
#ifdef CONFIG_FALLOCATE_PUNCH_HOLE
683
        do {
684
            if (fallocate(s->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
685
                          aiocb->aio_offset, aiocb->aio_nbytes) == 0) {
686
                return 0;
687
            }
688
        } while (errno == EINTR);
689

    
690
        ret = -errno;
691
#endif
692
    }
693

    
694
    if (ret == -ENODEV || ret == -ENOSYS || ret == -EOPNOTSUPP ||
695
        ret == -ENOTTY) {
696
        s->has_discard = 0;
697
        ret = 0;
698
    }
699
    return ret;
700
}
701

    
702
static int aio_worker(void *arg)
703
{
704
    RawPosixAIOData *aiocb = arg;
705
    ssize_t ret = 0;
706

    
707
    switch (aiocb->aio_type & QEMU_AIO_TYPE_MASK) {
708
    case QEMU_AIO_READ:
709
        ret = handle_aiocb_rw(aiocb);
710
        if (ret >= 0 && ret < aiocb->aio_nbytes && aiocb->bs->growable) {
711
            iov_memset(aiocb->aio_iov, aiocb->aio_niov, ret,
712
                      0, aiocb->aio_nbytes - ret);
713

    
714
            ret = aiocb->aio_nbytes;
715
        }
716
        if (ret == aiocb->aio_nbytes) {
717
            ret = 0;
718
        } else if (ret >= 0 && ret < aiocb->aio_nbytes) {
719
            ret = -EINVAL;
720
        }
721
        break;
722
    case QEMU_AIO_WRITE:
723
        ret = handle_aiocb_rw(aiocb);
724
        if (ret == aiocb->aio_nbytes) {
725
            ret = 0;
726
        } else if (ret >= 0 && ret < aiocb->aio_nbytes) {
727
            ret = -EINVAL;
728
        }
729
        break;
730
    case QEMU_AIO_FLUSH:
731
        ret = handle_aiocb_flush(aiocb);
732
        break;
733
    case QEMU_AIO_IOCTL:
734
        ret = handle_aiocb_ioctl(aiocb);
735
        break;
736
    case QEMU_AIO_DISCARD:
737
        ret = handle_aiocb_discard(aiocb);
738
        break;
739
    default:
740
        fprintf(stderr, "invalid aio request (0x%x)\n", aiocb->aio_type);
741
        ret = -EINVAL;
742
        break;
743
    }
744

    
745
    g_slice_free(RawPosixAIOData, aiocb);
746
    return ret;
747
}
748

    
749
static BlockDriverAIOCB *paio_submit(BlockDriverState *bs, int fd,
750
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
751
        BlockDriverCompletionFunc *cb, void *opaque, int type)
752
{
753
    RawPosixAIOData *acb = g_slice_new(RawPosixAIOData);
754
    ThreadPool *pool;
755

    
756
    acb->bs = bs;
757
    acb->aio_type = type;
758
    acb->aio_fildes = fd;
759

    
760
    if (qiov) {
761
        acb->aio_iov = qiov->iov;
762
        acb->aio_niov = qiov->niov;
763
    }
764
    acb->aio_nbytes = nb_sectors * 512;
765
    acb->aio_offset = sector_num * 512;
766

    
767
    trace_paio_submit(acb, opaque, sector_num, nb_sectors, type);
768
    pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
769
    return thread_pool_submit_aio(pool, aio_worker, acb, cb, opaque);
770
}
771

    
772
static BlockDriverAIOCB *raw_aio_submit(BlockDriverState *bs,
773
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
774
        BlockDriverCompletionFunc *cb, void *opaque, int type)
775
{
776
    BDRVRawState *s = bs->opaque;
777

    
778
    if (fd_open(bs) < 0)
779
        return NULL;
780

    
781
    /*
782
     * If O_DIRECT is used the buffer needs to be aligned on a sector
783
     * boundary.  Check if this is the case or tell the low-level
784
     * driver that it needs to copy the buffer.
785
     */
786
    if ((bs->open_flags & BDRV_O_NOCACHE)) {
787
        if (!bdrv_qiov_is_aligned(bs, qiov)) {
788
            type |= QEMU_AIO_MISALIGNED;
789
#ifdef CONFIG_LINUX_AIO
790
        } else if (s->use_aio) {
791
            return laio_submit(bs, s->aio_ctx, s->fd, sector_num, qiov,
792
                               nb_sectors, cb, opaque, type);
793
#endif
794
        }
795
    }
796

    
797
    return paio_submit(bs, s->fd, sector_num, qiov, nb_sectors,
798
                       cb, opaque, type);
799
}
800

    
801
static BlockDriverAIOCB *raw_aio_readv(BlockDriverState *bs,
802
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
803
        BlockDriverCompletionFunc *cb, void *opaque)
804
{
805
    return raw_aio_submit(bs, sector_num, qiov, nb_sectors,
806
                          cb, opaque, QEMU_AIO_READ);
807
}
808

    
809
static BlockDriverAIOCB *raw_aio_writev(BlockDriverState *bs,
810
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
811
        BlockDriverCompletionFunc *cb, void *opaque)
812
{
813
    return raw_aio_submit(bs, sector_num, qiov, nb_sectors,
814
                          cb, opaque, QEMU_AIO_WRITE);
815
}
816

    
817
static BlockDriverAIOCB *raw_aio_flush(BlockDriverState *bs,
818
        BlockDriverCompletionFunc *cb, void *opaque)
819
{
820
    BDRVRawState *s = bs->opaque;
821

    
822
    if (fd_open(bs) < 0)
823
        return NULL;
824

    
825
    return paio_submit(bs, s->fd, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH);
826
}
827

    
828
static void raw_close(BlockDriverState *bs)
829
{
830
    BDRVRawState *s = bs->opaque;
831
    if (s->fd >= 0) {
832
        qemu_close(s->fd);
833
        s->fd = -1;
834
    }
835
}
836

    
837
static int raw_truncate(BlockDriverState *bs, int64_t offset)
838
{
839
    BDRVRawState *s = bs->opaque;
840
    struct stat st;
841

    
842
    if (fstat(s->fd, &st)) {
843
        return -errno;
844
    }
845

    
846
    if (S_ISREG(st.st_mode)) {
847
        if (ftruncate(s->fd, offset) < 0) {
848
            return -errno;
849
        }
850
    } else if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
851
       if (offset > raw_getlength(bs)) {
852
           return -EINVAL;
853
       }
854
    } else {
855
        return -ENOTSUP;
856
    }
857

    
858
    return 0;
859
}
860

    
861
#ifdef __OpenBSD__
862
static int64_t raw_getlength(BlockDriverState *bs)
863
{
864
    BDRVRawState *s = bs->opaque;
865
    int fd = s->fd;
866
    struct stat st;
867

    
868
    if (fstat(fd, &st))
869
        return -1;
870
    if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
871
        struct disklabel dl;
872

    
873
        if (ioctl(fd, DIOCGDINFO, &dl))
874
            return -1;
875
        return (uint64_t)dl.d_secsize *
876
            dl.d_partitions[DISKPART(st.st_rdev)].p_size;
877
    } else
878
        return st.st_size;
879
}
880
#elif defined(__NetBSD__)
881
static int64_t raw_getlength(BlockDriverState *bs)
882
{
883
    BDRVRawState *s = bs->opaque;
884
    int fd = s->fd;
885
    struct stat st;
886

    
887
    if (fstat(fd, &st))
888
        return -1;
889
    if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
890
        struct dkwedge_info dkw;
891

    
892
        if (ioctl(fd, DIOCGWEDGEINFO, &dkw) != -1) {
893
            return dkw.dkw_size * 512;
894
        } else {
895
            struct disklabel dl;
896

    
897
            if (ioctl(fd, DIOCGDINFO, &dl))
898
                return -1;
899
            return (uint64_t)dl.d_secsize *
900
                dl.d_partitions[DISKPART(st.st_rdev)].p_size;
901
        }
902
    } else
903
        return st.st_size;
904
}
905
#elif defined(__sun__)
906
static int64_t raw_getlength(BlockDriverState *bs)
907
{
908
    BDRVRawState *s = bs->opaque;
909
    struct dk_minfo minfo;
910
    int ret;
911

    
912
    ret = fd_open(bs);
913
    if (ret < 0) {
914
        return ret;
915
    }
916

    
917
    /*
918
     * Use the DKIOCGMEDIAINFO ioctl to read the size.
919
     */
920
    ret = ioctl(s->fd, DKIOCGMEDIAINFO, &minfo);
921
    if (ret != -1) {
922
        return minfo.dki_lbsize * minfo.dki_capacity;
923
    }
924

    
925
    /*
926
     * There are reports that lseek on some devices fails, but
927
     * irc discussion said that contingency on contingency was overkill.
928
     */
929
    return lseek(s->fd, 0, SEEK_END);
930
}
931
#elif defined(CONFIG_BSD)
932
static int64_t raw_getlength(BlockDriverState *bs)
933
{
934
    BDRVRawState *s = bs->opaque;
935
    int fd = s->fd;
936
    int64_t size;
937
    struct stat sb;
938
#if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
939
    int reopened = 0;
940
#endif
941
    int ret;
942

    
943
    ret = fd_open(bs);
944
    if (ret < 0)
945
        return ret;
946

    
947
#if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
948
again:
949
#endif
950
    if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
951
#ifdef DIOCGMEDIASIZE
952
        if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
953
#elif defined(DIOCGPART)
954
        {
955
                struct partinfo pi;
956
                if (ioctl(fd, DIOCGPART, &pi) == 0)
957
                        size = pi.media_size;
958
                else
959
                        size = 0;
960
        }
961
        if (size == 0)
962
#endif
963
#if defined(__APPLE__) && defined(__MACH__)
964
        size = LONG_LONG_MAX;
965
#else
966
        size = lseek(fd, 0LL, SEEK_END);
967
#endif
968
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
969
        switch(s->type) {
970
        case FTYPE_CD:
971
            /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
972
            if (size == 2048LL * (unsigned)-1)
973
                size = 0;
974
            /* XXX no disc?  maybe we need to reopen... */
975
            if (size <= 0 && !reopened && cdrom_reopen(bs) >= 0) {
976
                reopened = 1;
977
                goto again;
978
            }
979
        }
980
#endif
981
    } else {
982
        size = lseek(fd, 0, SEEK_END);
983
    }
984
    return size;
985
}
986
#else
987
static int64_t raw_getlength(BlockDriverState *bs)
988
{
989
    BDRVRawState *s = bs->opaque;
990
    int ret;
991

    
992
    ret = fd_open(bs);
993
    if (ret < 0) {
994
        return ret;
995
    }
996

    
997
    return lseek(s->fd, 0, SEEK_END);
998
}
999
#endif
1000

    
1001
static int64_t raw_get_allocated_file_size(BlockDriverState *bs)
1002
{
1003
    struct stat st;
1004
    BDRVRawState *s = bs->opaque;
1005

    
1006
    if (fstat(s->fd, &st) < 0) {
1007
        return -errno;
1008
    }
1009
    return (int64_t)st.st_blocks * 512;
1010
}
1011

    
1012
static int raw_create(const char *filename, QEMUOptionParameter *options)
1013
{
1014
    int fd;
1015
    int result = 0;
1016
    int64_t total_size = 0;
1017

    
1018
    /* Read out options */
1019
    while (options && options->name) {
1020
        if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
1021
            total_size = options->value.n / BDRV_SECTOR_SIZE;
1022
        }
1023
        options++;
1024
    }
1025

    
1026
    fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
1027
                   0644);
1028
    if (fd < 0) {
1029
        result = -errno;
1030
    } else {
1031
        if (ftruncate(fd, total_size * BDRV_SECTOR_SIZE) != 0) {
1032
            result = -errno;
1033
        }
1034
        if (qemu_close(fd) != 0) {
1035
            result = -errno;
1036
        }
1037
    }
1038
    return result;
1039
}
1040

    
1041
/*
1042
 * Returns true iff the specified sector is present in the disk image. Drivers
1043
 * not implementing the functionality are assumed to not support backing files,
1044
 * hence all their sectors are reported as allocated.
1045
 *
1046
 * If 'sector_num' is beyond the end of the disk image the return value is 0
1047
 * and 'pnum' is set to 0.
1048
 *
1049
 * 'pnum' is set to the number of sectors (including and immediately following
1050
 * the specified sector) that are known to be in the same
1051
 * allocated/unallocated state.
1052
 *
1053
 * 'nb_sectors' is the max value 'pnum' should be set to.  If nb_sectors goes
1054
 * beyond the end of the disk image it will be clamped.
1055
 */
1056
static int coroutine_fn raw_co_is_allocated(BlockDriverState *bs,
1057
                                            int64_t sector_num,
1058
                                            int nb_sectors, int *pnum)
1059
{
1060
    off_t start, data, hole;
1061
    int ret;
1062

    
1063
    ret = fd_open(bs);
1064
    if (ret < 0) {
1065
        return ret;
1066
    }
1067

    
1068
    start = sector_num * BDRV_SECTOR_SIZE;
1069

    
1070
#ifdef CONFIG_FIEMAP
1071

    
1072
    BDRVRawState *s = bs->opaque;
1073
    struct {
1074
        struct fiemap fm;
1075
        struct fiemap_extent fe;
1076
    } f;
1077

    
1078
    f.fm.fm_start = start;
1079
    f.fm.fm_length = (int64_t)nb_sectors * BDRV_SECTOR_SIZE;
1080
    f.fm.fm_flags = 0;
1081
    f.fm.fm_extent_count = 1;
1082
    f.fm.fm_reserved = 0;
1083
    if (ioctl(s->fd, FS_IOC_FIEMAP, &f) == -1) {
1084
        /* Assume everything is allocated.  */
1085
        *pnum = nb_sectors;
1086
        return 1;
1087
    }
1088

    
1089
    if (f.fm.fm_mapped_extents == 0) {
1090
        /* No extents found, data is beyond f.fm.fm_start + f.fm.fm_length.
1091
         * f.fm.fm_start + f.fm.fm_length must be clamped to the file size!
1092
         */
1093
        off_t length = lseek(s->fd, 0, SEEK_END);
1094
        hole = f.fm.fm_start;
1095
        data = MIN(f.fm.fm_start + f.fm.fm_length, length);
1096
    } else {
1097
        data = f.fe.fe_logical;
1098
        hole = f.fe.fe_logical + f.fe.fe_length;
1099
    }
1100

    
1101
#elif defined SEEK_HOLE && defined SEEK_DATA
1102

    
1103
    BDRVRawState *s = bs->opaque;
1104

    
1105
    hole = lseek(s->fd, start, SEEK_HOLE);
1106
    if (hole == -1) {
1107
        /* -ENXIO indicates that sector_num was past the end of the file.
1108
         * There is a virtual hole there.  */
1109
        assert(errno != -ENXIO);
1110

    
1111
        /* Most likely EINVAL.  Assume everything is allocated.  */
1112
        *pnum = nb_sectors;
1113
        return 1;
1114
    }
1115

    
1116
    if (hole > start) {
1117
        data = start;
1118
    } else {
1119
        /* On a hole.  We need another syscall to find its end.  */
1120
        data = lseek(s->fd, start, SEEK_DATA);
1121
        if (data == -1) {
1122
            data = lseek(s->fd, 0, SEEK_END);
1123
        }
1124
    }
1125
#else
1126
    *pnum = nb_sectors;
1127
    return 1;
1128
#endif
1129

    
1130
    if (data <= start) {
1131
        /* On a data extent, compute sectors to the end of the extent.  */
1132
        *pnum = MIN(nb_sectors, (hole - start) / BDRV_SECTOR_SIZE);
1133
        return 1;
1134
    } else {
1135
        /* On a hole, compute sectors to the beginning of the next extent.  */
1136
        *pnum = MIN(nb_sectors, (data - start) / BDRV_SECTOR_SIZE);
1137
        return 0;
1138
    }
1139
}
1140

    
1141
static coroutine_fn BlockDriverAIOCB *raw_aio_discard(BlockDriverState *bs,
1142
    int64_t sector_num, int nb_sectors,
1143
    BlockDriverCompletionFunc *cb, void *opaque)
1144
{
1145
    BDRVRawState *s = bs->opaque;
1146

    
1147
    return paio_submit(bs, s->fd, sector_num, NULL, nb_sectors,
1148
                       cb, opaque, QEMU_AIO_DISCARD);
1149
}
1150

    
1151
static QEMUOptionParameter raw_create_options[] = {
1152
    {
1153
        .name = BLOCK_OPT_SIZE,
1154
        .type = OPT_SIZE,
1155
        .help = "Virtual disk size"
1156
    },
1157
    { NULL }
1158
};
1159

    
1160
static BlockDriver bdrv_file = {
1161
    .format_name = "file",
1162
    .protocol_name = "file",
1163
    .instance_size = sizeof(BDRVRawState),
1164
    .bdrv_probe = NULL, /* no probe for protocols */
1165
    .bdrv_file_open = raw_open,
1166
    .bdrv_reopen_prepare = raw_reopen_prepare,
1167
    .bdrv_reopen_commit = raw_reopen_commit,
1168
    .bdrv_reopen_abort = raw_reopen_abort,
1169
    .bdrv_close = raw_close,
1170
    .bdrv_create = raw_create,
1171
    .bdrv_co_is_allocated = raw_co_is_allocated,
1172

    
1173
    .bdrv_aio_readv = raw_aio_readv,
1174
    .bdrv_aio_writev = raw_aio_writev,
1175
    .bdrv_aio_flush = raw_aio_flush,
1176
    .bdrv_aio_discard = raw_aio_discard,
1177

    
1178
    .bdrv_truncate = raw_truncate,
1179
    .bdrv_getlength = raw_getlength,
1180
    .bdrv_get_allocated_file_size
1181
                        = raw_get_allocated_file_size,
1182

    
1183
    .create_options = raw_create_options,
1184
};
1185

    
1186
/***********************************************/
1187
/* host device */
1188

    
1189
#if defined(__APPLE__) && defined(__MACH__)
1190
static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
1191
static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
1192

    
1193
kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
1194
{
1195
    kern_return_t       kernResult;
1196
    mach_port_t     masterPort;
1197
    CFMutableDictionaryRef  classesToMatch;
1198

    
1199
    kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
1200
    if ( KERN_SUCCESS != kernResult ) {
1201
        printf( "IOMasterPort returned %d\n", kernResult );
1202
    }
1203

    
1204
    classesToMatch = IOServiceMatching( kIOCDMediaClass );
1205
    if ( classesToMatch == NULL ) {
1206
        printf( "IOServiceMatching returned a NULL dictionary.\n" );
1207
    } else {
1208
    CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
1209
    }
1210
    kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
1211
    if ( KERN_SUCCESS != kernResult )
1212
    {
1213
        printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
1214
    }
1215

    
1216
    return kernResult;
1217
}
1218

    
1219
kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
1220
{
1221
    io_object_t     nextMedia;
1222
    kern_return_t   kernResult = KERN_FAILURE;
1223
    *bsdPath = '\0';
1224
    nextMedia = IOIteratorNext( mediaIterator );
1225
    if ( nextMedia )
1226
    {
1227
        CFTypeRef   bsdPathAsCFString;
1228
    bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
1229
        if ( bsdPathAsCFString ) {
1230
            size_t devPathLength;
1231
            strcpy( bsdPath, _PATH_DEV );
1232
            strcat( bsdPath, "r" );
1233
            devPathLength = strlen( bsdPath );
1234
            if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
1235
                kernResult = KERN_SUCCESS;
1236
            }
1237
            CFRelease( bsdPathAsCFString );
1238
        }
1239
        IOObjectRelease( nextMedia );
1240
    }
1241

    
1242
    return kernResult;
1243
}
1244

    
1245
#endif
1246

    
1247
static int hdev_probe_device(const char *filename)
1248
{
1249
    struct stat st;
1250

    
1251
    /* allow a dedicated CD-ROM driver to match with a higher priority */
1252
    if (strstart(filename, "/dev/cdrom", NULL))
1253
        return 50;
1254

    
1255
    if (stat(filename, &st) >= 0 &&
1256
            (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
1257
        return 100;
1258
    }
1259

    
1260
    return 0;
1261
}
1262

    
1263
static int check_hdev_writable(BDRVRawState *s)
1264
{
1265
#if defined(BLKROGET)
1266
    /* Linux block devices can be configured "read-only" using blockdev(8).
1267
     * This is independent of device node permissions and therefore open(2)
1268
     * with O_RDWR succeeds.  Actual writes fail with EPERM.
1269
     *
1270
     * bdrv_open() is supposed to fail if the disk is read-only.  Explicitly
1271
     * check for read-only block devices so that Linux block devices behave
1272
     * properly.
1273
     */
1274
    struct stat st;
1275
    int readonly = 0;
1276

    
1277
    if (fstat(s->fd, &st)) {
1278
        return -errno;
1279
    }
1280

    
1281
    if (!S_ISBLK(st.st_mode)) {
1282
        return 0;
1283
    }
1284

    
1285
    if (ioctl(s->fd, BLKROGET, &readonly) < 0) {
1286
        return -errno;
1287
    }
1288

    
1289
    if (readonly) {
1290
        return -EACCES;
1291
    }
1292
#endif /* defined(BLKROGET) */
1293
    return 0;
1294
}
1295

    
1296
static int hdev_open(BlockDriverState *bs, const char *filename,
1297
                     QDict *options, int flags)
1298
{
1299
    BDRVRawState *s = bs->opaque;
1300
    int ret;
1301

    
1302
#if defined(__APPLE__) && defined(__MACH__)
1303
    if (strstart(filename, "/dev/cdrom", NULL)) {
1304
        kern_return_t kernResult;
1305
        io_iterator_t mediaIterator;
1306
        char bsdPath[ MAXPATHLEN ];
1307
        int fd;
1308

    
1309
        kernResult = FindEjectableCDMedia( &mediaIterator );
1310
        kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
1311

    
1312
        if ( bsdPath[ 0 ] != '\0' ) {
1313
            strcat(bsdPath,"s0");
1314
            /* some CDs don't have a partition 0 */
1315
            fd = qemu_open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
1316
            if (fd < 0) {
1317
                bsdPath[strlen(bsdPath)-1] = '1';
1318
            } else {
1319
                qemu_close(fd);
1320
            }
1321
            filename = bsdPath;
1322
        }
1323

    
1324
        if ( mediaIterator )
1325
            IOObjectRelease( mediaIterator );
1326
    }
1327
#endif
1328

    
1329
    s->type = FTYPE_FILE;
1330
#if defined(__linux__)
1331
    {
1332
        char resolved_path[ MAXPATHLEN ], *temp;
1333

    
1334
        temp = realpath(filename, resolved_path);
1335
        if (temp && strstart(temp, "/dev/sg", NULL)) {
1336
            bs->sg = 1;
1337
        }
1338
    }
1339
#endif
1340

    
1341
    ret = raw_open_common(bs, filename, flags, 0);
1342
    if (ret < 0) {
1343
        return ret;
1344
    }
1345

    
1346
    if (flags & BDRV_O_RDWR) {
1347
        ret = check_hdev_writable(s);
1348
        if (ret < 0) {
1349
            raw_close(bs);
1350
            return ret;
1351
        }
1352
    }
1353

    
1354
    return ret;
1355
}
1356

    
1357
#if defined(__linux__)
1358
/* Note: we do not have a reliable method to detect if the floppy is
1359
   present. The current method is to try to open the floppy at every
1360
   I/O and to keep it opened during a few hundreds of ms. */
1361
static int fd_open(BlockDriverState *bs)
1362
{
1363
    BDRVRawState *s = bs->opaque;
1364
    int last_media_present;
1365

    
1366
    if (s->type != FTYPE_FD)
1367
        return 0;
1368
    last_media_present = (s->fd >= 0);
1369
    if (s->fd >= 0 &&
1370
        (get_clock() - s->fd_open_time) >= FD_OPEN_TIMEOUT) {
1371
        qemu_close(s->fd);
1372
        s->fd = -1;
1373
#ifdef DEBUG_FLOPPY
1374
        printf("Floppy closed\n");
1375
#endif
1376
    }
1377
    if (s->fd < 0) {
1378
        if (s->fd_got_error &&
1379
            (get_clock() - s->fd_error_time) < FD_OPEN_TIMEOUT) {
1380
#ifdef DEBUG_FLOPPY
1381
            printf("No floppy (open delayed)\n");
1382
#endif
1383
            return -EIO;
1384
        }
1385
        s->fd = qemu_open(bs->filename, s->open_flags & ~O_NONBLOCK);
1386
        if (s->fd < 0) {
1387
            s->fd_error_time = get_clock();
1388
            s->fd_got_error = 1;
1389
            if (last_media_present)
1390
                s->fd_media_changed = 1;
1391
#ifdef DEBUG_FLOPPY
1392
            printf("No floppy\n");
1393
#endif
1394
            return -EIO;
1395
        }
1396
#ifdef DEBUG_FLOPPY
1397
        printf("Floppy opened\n");
1398
#endif
1399
    }
1400
    if (!last_media_present)
1401
        s->fd_media_changed = 1;
1402
    s->fd_open_time = get_clock();
1403
    s->fd_got_error = 0;
1404
    return 0;
1405
}
1406

    
1407
static int hdev_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1408
{
1409
    BDRVRawState *s = bs->opaque;
1410

    
1411
    return ioctl(s->fd, req, buf);
1412
}
1413

    
1414
static BlockDriverAIOCB *hdev_aio_ioctl(BlockDriverState *bs,
1415
        unsigned long int req, void *buf,
1416
        BlockDriverCompletionFunc *cb, void *opaque)
1417
{
1418
    BDRVRawState *s = bs->opaque;
1419
    RawPosixAIOData *acb;
1420
    ThreadPool *pool;
1421

    
1422
    if (fd_open(bs) < 0)
1423
        return NULL;
1424

    
1425
    acb = g_slice_new(RawPosixAIOData);
1426
    acb->bs = bs;
1427
    acb->aio_type = QEMU_AIO_IOCTL;
1428
    acb->aio_fildes = s->fd;
1429
    acb->aio_offset = 0;
1430
    acb->aio_ioctl_buf = buf;
1431
    acb->aio_ioctl_cmd = req;
1432
    pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
1433
    return thread_pool_submit_aio(pool, aio_worker, acb, cb, opaque);
1434
}
1435

    
1436
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1437
static int fd_open(BlockDriverState *bs)
1438
{
1439
    BDRVRawState *s = bs->opaque;
1440

    
1441
    /* this is just to ensure s->fd is sane (its called by io ops) */
1442
    if (s->fd >= 0)
1443
        return 0;
1444
    return -EIO;
1445
}
1446
#else /* !linux && !FreeBSD */
1447

    
1448
static int fd_open(BlockDriverState *bs)
1449
{
1450
    return 0;
1451
}
1452

    
1453
#endif /* !linux && !FreeBSD */
1454

    
1455
static coroutine_fn BlockDriverAIOCB *hdev_aio_discard(BlockDriverState *bs,
1456
    int64_t sector_num, int nb_sectors,
1457
    BlockDriverCompletionFunc *cb, void *opaque)
1458
{
1459
    BDRVRawState *s = bs->opaque;
1460

    
1461
    if (fd_open(bs) < 0) {
1462
        return NULL;
1463
    }
1464
    return paio_submit(bs, s->fd, sector_num, NULL, nb_sectors,
1465
                       cb, opaque, QEMU_AIO_DISCARD|QEMU_AIO_BLKDEV);
1466
}
1467

    
1468
static int hdev_create(const char *filename, QEMUOptionParameter *options)
1469
{
1470
    int fd;
1471
    int ret = 0;
1472
    struct stat stat_buf;
1473
    int64_t total_size = 0;
1474

    
1475
    /* Read out options */
1476
    while (options && options->name) {
1477
        if (!strcmp(options->name, "size")) {
1478
            total_size = options->value.n / BDRV_SECTOR_SIZE;
1479
        }
1480
        options++;
1481
    }
1482

    
1483
    fd = qemu_open(filename, O_WRONLY | O_BINARY);
1484
    if (fd < 0)
1485
        return -errno;
1486

    
1487
    if (fstat(fd, &stat_buf) < 0)
1488
        ret = -errno;
1489
    else if (!S_ISBLK(stat_buf.st_mode) && !S_ISCHR(stat_buf.st_mode))
1490
        ret = -ENODEV;
1491
    else if (lseek(fd, 0, SEEK_END) < total_size * BDRV_SECTOR_SIZE)
1492
        ret = -ENOSPC;
1493

    
1494
    qemu_close(fd);
1495
    return ret;
1496
}
1497

    
1498
static int hdev_has_zero_init(BlockDriverState *bs)
1499
{
1500
    return 0;
1501
}
1502

    
1503
static BlockDriver bdrv_host_device = {
1504
    .format_name        = "host_device",
1505
    .protocol_name        = "host_device",
1506
    .instance_size      = sizeof(BDRVRawState),
1507
    .bdrv_probe_device  = hdev_probe_device,
1508
    .bdrv_file_open     = hdev_open,
1509
    .bdrv_close         = raw_close,
1510
    .bdrv_reopen_prepare = raw_reopen_prepare,
1511
    .bdrv_reopen_commit  = raw_reopen_commit,
1512
    .bdrv_reopen_abort   = raw_reopen_abort,
1513
    .bdrv_create        = hdev_create,
1514
    .create_options     = raw_create_options,
1515
    .bdrv_has_zero_init = hdev_has_zero_init,
1516

    
1517
    .bdrv_aio_readv        = raw_aio_readv,
1518
    .bdrv_aio_writev        = raw_aio_writev,
1519
    .bdrv_aio_flush        = raw_aio_flush,
1520
    .bdrv_aio_discard   = hdev_aio_discard,
1521

    
1522
    .bdrv_truncate      = raw_truncate,
1523
    .bdrv_getlength        = raw_getlength,
1524
    .bdrv_get_allocated_file_size
1525
                        = raw_get_allocated_file_size,
1526

    
1527
    /* generic scsi device */
1528
#ifdef __linux__
1529
    .bdrv_ioctl         = hdev_ioctl,
1530
    .bdrv_aio_ioctl     = hdev_aio_ioctl,
1531
#endif
1532
};
1533

    
1534
#ifdef __linux__
1535
static int floppy_open(BlockDriverState *bs, const char *filename,
1536
                       QDict *options, int flags)
1537
{
1538
    BDRVRawState *s = bs->opaque;
1539
    int ret;
1540

    
1541
    s->type = FTYPE_FD;
1542

    
1543
    /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
1544
    ret = raw_open_common(bs, filename, flags, O_NONBLOCK);
1545
    if (ret)
1546
        return ret;
1547

    
1548
    /* close fd so that we can reopen it as needed */
1549
    qemu_close(s->fd);
1550
    s->fd = -1;
1551
    s->fd_media_changed = 1;
1552

    
1553
    return 0;
1554
}
1555

    
1556
static int floppy_probe_device(const char *filename)
1557
{
1558
    int fd, ret;
1559
    int prio = 0;
1560
    struct floppy_struct fdparam;
1561
    struct stat st;
1562

    
1563
    if (strstart(filename, "/dev/fd", NULL) &&
1564
        !strstart(filename, "/dev/fdset/", NULL)) {
1565
        prio = 50;
1566
    }
1567

    
1568
    fd = qemu_open(filename, O_RDONLY | O_NONBLOCK);
1569
    if (fd < 0) {
1570
        goto out;
1571
    }
1572
    ret = fstat(fd, &st);
1573
    if (ret == -1 || !S_ISBLK(st.st_mode)) {
1574
        goto outc;
1575
    }
1576

    
1577
    /* Attempt to detect via a floppy specific ioctl */
1578
    ret = ioctl(fd, FDGETPRM, &fdparam);
1579
    if (ret >= 0)
1580
        prio = 100;
1581

    
1582
outc:
1583
    qemu_close(fd);
1584
out:
1585
    return prio;
1586
}
1587

    
1588

    
1589
static int floppy_is_inserted(BlockDriverState *bs)
1590
{
1591
    return fd_open(bs) >= 0;
1592
}
1593

    
1594
static int floppy_media_changed(BlockDriverState *bs)
1595
{
1596
    BDRVRawState *s = bs->opaque;
1597
    int ret;
1598

    
1599
    /*
1600
     * XXX: we do not have a true media changed indication.
1601
     * It does not work if the floppy is changed without trying to read it.
1602
     */
1603
    fd_open(bs);
1604
    ret = s->fd_media_changed;
1605
    s->fd_media_changed = 0;
1606
#ifdef DEBUG_FLOPPY
1607
    printf("Floppy changed=%d\n", ret);
1608
#endif
1609
    return ret;
1610
}
1611

    
1612
static void floppy_eject(BlockDriverState *bs, bool eject_flag)
1613
{
1614
    BDRVRawState *s = bs->opaque;
1615
    int fd;
1616

    
1617
    if (s->fd >= 0) {
1618
        qemu_close(s->fd);
1619
        s->fd = -1;
1620
    }
1621
    fd = qemu_open(bs->filename, s->open_flags | O_NONBLOCK);
1622
    if (fd >= 0) {
1623
        if (ioctl(fd, FDEJECT, 0) < 0)
1624
            perror("FDEJECT");
1625
        qemu_close(fd);
1626
    }
1627
}
1628

    
1629
static BlockDriver bdrv_host_floppy = {
1630
    .format_name        = "host_floppy",
1631
    .protocol_name      = "host_floppy",
1632
    .instance_size      = sizeof(BDRVRawState),
1633
    .bdrv_probe_device        = floppy_probe_device,
1634
    .bdrv_file_open     = floppy_open,
1635
    .bdrv_close         = raw_close,
1636
    .bdrv_reopen_prepare = raw_reopen_prepare,
1637
    .bdrv_reopen_commit  = raw_reopen_commit,
1638
    .bdrv_reopen_abort   = raw_reopen_abort,
1639
    .bdrv_create        = hdev_create,
1640
    .create_options     = raw_create_options,
1641
    .bdrv_has_zero_init = hdev_has_zero_init,
1642

    
1643
    .bdrv_aio_readv     = raw_aio_readv,
1644
    .bdrv_aio_writev    = raw_aio_writev,
1645
    .bdrv_aio_flush        = raw_aio_flush,
1646

    
1647
    .bdrv_truncate      = raw_truncate,
1648
    .bdrv_getlength        = raw_getlength,
1649
    .bdrv_get_allocated_file_size
1650
                        = raw_get_allocated_file_size,
1651

    
1652
    /* removable device support */
1653
    .bdrv_is_inserted   = floppy_is_inserted,
1654
    .bdrv_media_changed = floppy_media_changed,
1655
    .bdrv_eject         = floppy_eject,
1656
};
1657

    
1658
static int cdrom_open(BlockDriverState *bs, const char *filename,
1659
                      QDict *options, int flags)
1660
{
1661
    BDRVRawState *s = bs->opaque;
1662

    
1663
    s->type = FTYPE_CD;
1664

    
1665
    /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
1666
    return raw_open_common(bs, filename, flags, O_NONBLOCK);
1667
}
1668

    
1669
static int cdrom_probe_device(const char *filename)
1670
{
1671
    int fd, ret;
1672
    int prio = 0;
1673
    struct stat st;
1674

    
1675
    fd = qemu_open(filename, O_RDONLY | O_NONBLOCK);
1676
    if (fd < 0) {
1677
        goto out;
1678
    }
1679
    ret = fstat(fd, &st);
1680
    if (ret == -1 || !S_ISBLK(st.st_mode)) {
1681
        goto outc;
1682
    }
1683

    
1684
    /* Attempt to detect via a CDROM specific ioctl */
1685
    ret = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
1686
    if (ret >= 0)
1687
        prio = 100;
1688

    
1689
outc:
1690
    qemu_close(fd);
1691
out:
1692
    return prio;
1693
}
1694

    
1695
static int cdrom_is_inserted(BlockDriverState *bs)
1696
{
1697
    BDRVRawState *s = bs->opaque;
1698
    int ret;
1699

    
1700
    ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
1701
    if (ret == CDS_DISC_OK)
1702
        return 1;
1703
    return 0;
1704
}
1705

    
1706
static void cdrom_eject(BlockDriverState *bs, bool eject_flag)
1707
{
1708
    BDRVRawState *s = bs->opaque;
1709

    
1710
    if (eject_flag) {
1711
        if (ioctl(s->fd, CDROMEJECT, NULL) < 0)
1712
            perror("CDROMEJECT");
1713
    } else {
1714
        if (ioctl(s->fd, CDROMCLOSETRAY, NULL) < 0)
1715
            perror("CDROMEJECT");
1716
    }
1717
}
1718

    
1719
static void cdrom_lock_medium(BlockDriverState *bs, bool locked)
1720
{
1721
    BDRVRawState *s = bs->opaque;
1722

    
1723
    if (ioctl(s->fd, CDROM_LOCKDOOR, locked) < 0) {
1724
        /*
1725
         * Note: an error can happen if the distribution automatically
1726
         * mounts the CD-ROM
1727
         */
1728
        /* perror("CDROM_LOCKDOOR"); */
1729
    }
1730
}
1731

    
1732
static BlockDriver bdrv_host_cdrom = {
1733
    .format_name        = "host_cdrom",
1734
    .protocol_name      = "host_cdrom",
1735
    .instance_size      = sizeof(BDRVRawState),
1736
    .bdrv_probe_device        = cdrom_probe_device,
1737
    .bdrv_file_open     = cdrom_open,
1738
    .bdrv_close         = raw_close,
1739
    .bdrv_reopen_prepare = raw_reopen_prepare,
1740
    .bdrv_reopen_commit  = raw_reopen_commit,
1741
    .bdrv_reopen_abort   = raw_reopen_abort,
1742
    .bdrv_create        = hdev_create,
1743
    .create_options     = raw_create_options,
1744
    .bdrv_has_zero_init = hdev_has_zero_init,
1745

    
1746
    .bdrv_aio_readv     = raw_aio_readv,
1747
    .bdrv_aio_writev    = raw_aio_writev,
1748
    .bdrv_aio_flush        = raw_aio_flush,
1749

    
1750
    .bdrv_truncate      = raw_truncate,
1751
    .bdrv_getlength     = raw_getlength,
1752
    .bdrv_get_allocated_file_size
1753
                        = raw_get_allocated_file_size,
1754

    
1755
    /* removable device support */
1756
    .bdrv_is_inserted   = cdrom_is_inserted,
1757
    .bdrv_eject         = cdrom_eject,
1758
    .bdrv_lock_medium   = cdrom_lock_medium,
1759

    
1760
    /* generic scsi device */
1761
    .bdrv_ioctl         = hdev_ioctl,
1762
    .bdrv_aio_ioctl     = hdev_aio_ioctl,
1763
};
1764
#endif /* __linux__ */
1765

    
1766
#if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
1767
static int cdrom_open(BlockDriverState *bs, const char *filename,
1768
                      QDict *options, int flags)
1769
{
1770
    BDRVRawState *s = bs->opaque;
1771
    int ret;
1772

    
1773
    s->type = FTYPE_CD;
1774

    
1775
    ret = raw_open_common(bs, filename, flags, 0);
1776
    if (ret)
1777
        return ret;
1778

    
1779
    /* make sure the door isn't locked at this time */
1780
    ioctl(s->fd, CDIOCALLOW);
1781
    return 0;
1782
}
1783

    
1784
static int cdrom_probe_device(const char *filename)
1785
{
1786
    if (strstart(filename, "/dev/cd", NULL) ||
1787
            strstart(filename, "/dev/acd", NULL))
1788
        return 100;
1789
    return 0;
1790
}
1791

    
1792
static int cdrom_reopen(BlockDriverState *bs)
1793
{
1794
    BDRVRawState *s = bs->opaque;
1795
    int fd;
1796

    
1797
    /*
1798
     * Force reread of possibly changed/newly loaded disc,
1799
     * FreeBSD seems to not notice sometimes...
1800
     */
1801
    if (s->fd >= 0)
1802
        qemu_close(s->fd);
1803
    fd = qemu_open(bs->filename, s->open_flags, 0644);
1804
    if (fd < 0) {
1805
        s->fd = -1;
1806
        return -EIO;
1807
    }
1808
    s->fd = fd;
1809

    
1810
    /* make sure the door isn't locked at this time */
1811
    ioctl(s->fd, CDIOCALLOW);
1812
    return 0;
1813
}
1814

    
1815
static int cdrom_is_inserted(BlockDriverState *bs)
1816
{
1817
    return raw_getlength(bs) > 0;
1818
}
1819

    
1820
static void cdrom_eject(BlockDriverState *bs, bool eject_flag)
1821
{
1822
    BDRVRawState *s = bs->opaque;
1823

    
1824
    if (s->fd < 0)
1825
        return;
1826

    
1827
    (void) ioctl(s->fd, CDIOCALLOW);
1828

    
1829
    if (eject_flag) {
1830
        if (ioctl(s->fd, CDIOCEJECT) < 0)
1831
            perror("CDIOCEJECT");
1832
    } else {
1833
        if (ioctl(s->fd, CDIOCCLOSE) < 0)
1834
            perror("CDIOCCLOSE");
1835
    }
1836

    
1837
    cdrom_reopen(bs);
1838
}
1839

    
1840
static void cdrom_lock_medium(BlockDriverState *bs, bool locked)
1841
{
1842
    BDRVRawState *s = bs->opaque;
1843

    
1844
    if (s->fd < 0)
1845
        return;
1846
    if (ioctl(s->fd, (locked ? CDIOCPREVENT : CDIOCALLOW)) < 0) {
1847
        /*
1848
         * Note: an error can happen if the distribution automatically
1849
         * mounts the CD-ROM
1850
         */
1851
        /* perror("CDROM_LOCKDOOR"); */
1852
    }
1853
}
1854

    
1855
static BlockDriver bdrv_host_cdrom = {
1856
    .format_name        = "host_cdrom",
1857
    .protocol_name      = "host_cdrom",
1858
    .instance_size      = sizeof(BDRVRawState),
1859
    .bdrv_probe_device        = cdrom_probe_device,
1860
    .bdrv_file_open     = cdrom_open,
1861
    .bdrv_close         = raw_close,
1862
    .bdrv_reopen_prepare = raw_reopen_prepare,
1863
    .bdrv_reopen_commit  = raw_reopen_commit,
1864
    .bdrv_reopen_abort   = raw_reopen_abort,
1865
    .bdrv_create        = hdev_create,
1866
    .create_options     = raw_create_options,
1867
    .bdrv_has_zero_init = hdev_has_zero_init,
1868

    
1869
    .bdrv_aio_readv     = raw_aio_readv,
1870
    .bdrv_aio_writev    = raw_aio_writev,
1871
    .bdrv_aio_flush        = raw_aio_flush,
1872

    
1873
    .bdrv_truncate      = raw_truncate,
1874
    .bdrv_getlength     = raw_getlength,
1875
    .bdrv_get_allocated_file_size
1876
                        = raw_get_allocated_file_size,
1877

    
1878
    /* removable device support */
1879
    .bdrv_is_inserted   = cdrom_is_inserted,
1880
    .bdrv_eject         = cdrom_eject,
1881
    .bdrv_lock_medium   = cdrom_lock_medium,
1882
};
1883
#endif /* __FreeBSD__ */
1884

    
1885
#ifdef CONFIG_LINUX_AIO
1886
/**
1887
 * Return the file descriptor for Linux AIO
1888
 *
1889
 * This function is a layering violation and should be removed when it becomes
1890
 * possible to call the block layer outside the global mutex.  It allows the
1891
 * caller to hijack the file descriptor so I/O can be performed outside the
1892
 * block layer.
1893
 */
1894
int raw_get_aio_fd(BlockDriverState *bs)
1895
{
1896
    BDRVRawState *s;
1897

    
1898
    if (!bs->drv) {
1899
        return -ENOMEDIUM;
1900
    }
1901

    
1902
    if (bs->drv == bdrv_find_format("raw")) {
1903
        bs = bs->file;
1904
    }
1905

    
1906
    /* raw-posix has several protocols so just check for raw_aio_readv */
1907
    if (bs->drv->bdrv_aio_readv != raw_aio_readv) {
1908
        return -ENOTSUP;
1909
    }
1910

    
1911
    s = bs->opaque;
1912
    if (!s->use_aio) {
1913
        return -ENOTSUP;
1914
    }
1915
    return s->fd;
1916
}
1917
#endif /* CONFIG_LINUX_AIO */
1918

    
1919
static void bdrv_file_init(void)
1920
{
1921
    /*
1922
     * Register all the drivers.  Note that order is important, the driver
1923
     * registered last will get probed first.
1924
     */
1925
    bdrv_register(&bdrv_file);
1926
    bdrv_register(&bdrv_host_device);
1927
#ifdef __linux__
1928
    bdrv_register(&bdrv_host_floppy);
1929
    bdrv_register(&bdrv_host_cdrom);
1930
#endif
1931
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1932
    bdrv_register(&bdrv_host_cdrom);
1933
#endif
1934
}
1935

    
1936
block_init(bdrv_file_init);