Statistics
| Branch: | Revision:

root / block.c @ abd7f68d

History | View | Annotate | Download (66.1 kB)

1
/*
2
 * QEMU System Emulator block driver
3
 *
4
 * Copyright (c) 2003 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 "config-host.h"
25
#include "qemu-common.h"
26
#include "monitor.h"
27
#include "block_int.h"
28
#include "module.h"
29
#include "qemu-objects.h"
30

    
31
#ifdef CONFIG_BSD
32
#include <sys/types.h>
33
#include <sys/stat.h>
34
#include <sys/ioctl.h>
35
#include <sys/queue.h>
36
#ifndef __DragonFly__
37
#include <sys/disk.h>
38
#endif
39
#endif
40

    
41
#ifdef _WIN32
42
#include <windows.h>
43
#endif
44

    
45
static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
46
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
47
        BlockDriverCompletionFunc *cb, void *opaque);
48
static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
49
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
50
        BlockDriverCompletionFunc *cb, void *opaque);
51
static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
52
        BlockDriverCompletionFunc *cb, void *opaque);
53
static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs,
54
        BlockDriverCompletionFunc *cb, void *opaque);
55
static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
56
                        uint8_t *buf, int nb_sectors);
57
static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
58
                         const uint8_t *buf, int nb_sectors);
59

    
60
static QTAILQ_HEAD(, BlockDriverState) bdrv_states =
61
    QTAILQ_HEAD_INITIALIZER(bdrv_states);
62

    
63
static QLIST_HEAD(, BlockDriver) bdrv_drivers =
64
    QLIST_HEAD_INITIALIZER(bdrv_drivers);
65

    
66
/* If non-zero, use only whitelisted block drivers */
67
static int use_bdrv_whitelist;
68

    
69
int path_is_absolute(const char *path)
70
{
71
    const char *p;
72
#ifdef _WIN32
73
    /* specific case for names like: "\\.\d:" */
74
    if (*path == '/' || *path == '\\')
75
        return 1;
76
#endif
77
    p = strchr(path, ':');
78
    if (p)
79
        p++;
80
    else
81
        p = path;
82
#ifdef _WIN32
83
    return (*p == '/' || *p == '\\');
84
#else
85
    return (*p == '/');
86
#endif
87
}
88

    
89
/* if filename is absolute, just copy it to dest. Otherwise, build a
90
   path to it by considering it is relative to base_path. URL are
91
   supported. */
92
void path_combine(char *dest, int dest_size,
93
                  const char *base_path,
94
                  const char *filename)
95
{
96
    const char *p, *p1;
97
    int len;
98

    
99
    if (dest_size <= 0)
100
        return;
101
    if (path_is_absolute(filename)) {
102
        pstrcpy(dest, dest_size, filename);
103
    } else {
104
        p = strchr(base_path, ':');
105
        if (p)
106
            p++;
107
        else
108
            p = base_path;
109
        p1 = strrchr(base_path, '/');
110
#ifdef _WIN32
111
        {
112
            const char *p2;
113
            p2 = strrchr(base_path, '\\');
114
            if (!p1 || p2 > p1)
115
                p1 = p2;
116
        }
117
#endif
118
        if (p1)
119
            p1++;
120
        else
121
            p1 = base_path;
122
        if (p1 > p)
123
            p = p1;
124
        len = p - base_path;
125
        if (len > dest_size - 1)
126
            len = dest_size - 1;
127
        memcpy(dest, base_path, len);
128
        dest[len] = '\0';
129
        pstrcat(dest, dest_size, filename);
130
    }
131
}
132

    
133
void bdrv_register(BlockDriver *bdrv)
134
{
135
    if (!bdrv->bdrv_aio_readv) {
136
        /* add AIO emulation layer */
137
        bdrv->bdrv_aio_readv = bdrv_aio_readv_em;
138
        bdrv->bdrv_aio_writev = bdrv_aio_writev_em;
139
    } else if (!bdrv->bdrv_read) {
140
        /* add synchronous IO emulation layer */
141
        bdrv->bdrv_read = bdrv_read_em;
142
        bdrv->bdrv_write = bdrv_write_em;
143
    }
144

    
145
    if (!bdrv->bdrv_aio_flush)
146
        bdrv->bdrv_aio_flush = bdrv_aio_flush_em;
147

    
148
    QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list);
149
}
150

    
151
/* create a new block device (by default it is empty) */
152
BlockDriverState *bdrv_new(const char *device_name)
153
{
154
    BlockDriverState *bs;
155

    
156
    bs = qemu_mallocz(sizeof(BlockDriverState));
157
    pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
158
    if (device_name[0] != '\0') {
159
        QTAILQ_INSERT_TAIL(&bdrv_states, bs, list);
160
    }
161
    return bs;
162
}
163

    
164
BlockDriver *bdrv_find_format(const char *format_name)
165
{
166
    BlockDriver *drv1;
167
    QLIST_FOREACH(drv1, &bdrv_drivers, list) {
168
        if (!strcmp(drv1->format_name, format_name)) {
169
            return drv1;
170
        }
171
    }
172
    return NULL;
173
}
174

    
175
static int bdrv_is_whitelisted(BlockDriver *drv)
176
{
177
    static const char *whitelist[] = {
178
        CONFIG_BDRV_WHITELIST
179
    };
180
    const char **p;
181

    
182
    if (!whitelist[0])
183
        return 1;               /* no whitelist, anything goes */
184

    
185
    for (p = whitelist; *p; p++) {
186
        if (!strcmp(drv->format_name, *p)) {
187
            return 1;
188
        }
189
    }
190
    return 0;
191
}
192

    
193
BlockDriver *bdrv_find_whitelisted_format(const char *format_name)
194
{
195
    BlockDriver *drv = bdrv_find_format(format_name);
196
    return drv && bdrv_is_whitelisted(drv) ? drv : NULL;
197
}
198

    
199
int bdrv_create(BlockDriver *drv, const char* filename,
200
    QEMUOptionParameter *options)
201
{
202
    if (!drv->bdrv_create)
203
        return -ENOTSUP;
204

    
205
    return drv->bdrv_create(filename, options);
206
}
207

    
208
int bdrv_create_file(const char* filename, QEMUOptionParameter *options)
209
{
210
    BlockDriver *drv;
211

    
212
    drv = bdrv_find_protocol(filename);
213
    if (drv == NULL) {
214
        drv = bdrv_find_format("file");
215
    }
216

    
217
    return bdrv_create(drv, filename, options);
218
}
219

    
220
#ifdef _WIN32
221
void get_tmp_filename(char *filename, int size)
222
{
223
    char temp_dir[MAX_PATH];
224

    
225
    GetTempPath(MAX_PATH, temp_dir);
226
    GetTempFileName(temp_dir, "qem", 0, filename);
227
}
228
#else
229
void get_tmp_filename(char *filename, int size)
230
{
231
    int fd;
232
    const char *tmpdir;
233
    /* XXX: race condition possible */
234
    tmpdir = getenv("TMPDIR");
235
    if (!tmpdir)
236
        tmpdir = "/tmp";
237
    snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
238
    fd = mkstemp(filename);
239
    close(fd);
240
}
241
#endif
242

    
243
#ifdef _WIN32
244
static int is_windows_drive_prefix(const char *filename)
245
{
246
    return (((filename[0] >= 'a' && filename[0] <= 'z') ||
247
             (filename[0] >= 'A' && filename[0] <= 'Z')) &&
248
            filename[1] == ':');
249
}
250

    
251
int is_windows_drive(const char *filename)
252
{
253
    if (is_windows_drive_prefix(filename) &&
254
        filename[2] == '\0')
255
        return 1;
256
    if (strstart(filename, "\\\\.\\", NULL) ||
257
        strstart(filename, "//./", NULL))
258
        return 1;
259
    return 0;
260
}
261
#endif
262

    
263
/*
264
 * Detect host devices. By convention, /dev/cdrom[N] is always
265
 * recognized as a host CDROM.
266
 */
267
static BlockDriver *find_hdev_driver(const char *filename)
268
{
269
    int score_max = 0, score;
270
    BlockDriver *drv = NULL, *d;
271

    
272
    QLIST_FOREACH(d, &bdrv_drivers, list) {
273
        if (d->bdrv_probe_device) {
274
            score = d->bdrv_probe_device(filename);
275
            if (score > score_max) {
276
                score_max = score;
277
                drv = d;
278
            }
279
        }
280
    }
281

    
282
    return drv;
283
}
284

    
285
BlockDriver *bdrv_find_protocol(const char *filename)
286
{
287
    BlockDriver *drv1;
288
    char protocol[128];
289
    int len;
290
    const char *p;
291
    int is_drive;
292

    
293
    /* TODO Drivers without bdrv_file_open must be specified explicitly */
294

    
295
#ifdef _WIN32
296
    is_drive = is_windows_drive(filename) ||
297
        is_windows_drive_prefix(filename);
298
#else
299
    is_drive = 0;
300
#endif
301
    p = strchr(filename, ':');
302
    if (!p || is_drive) {
303
        drv1 = find_hdev_driver(filename);
304
        if (!drv1) {
305
            drv1 = bdrv_find_format("file");
306
        }
307
        return drv1;
308
    }
309
    len = p - filename;
310
    if (len > sizeof(protocol) - 1)
311
        len = sizeof(protocol) - 1;
312
    memcpy(protocol, filename, len);
313
    protocol[len] = '\0';
314
    QLIST_FOREACH(drv1, &bdrv_drivers, list) {
315
        if (drv1->protocol_name &&
316
            !strcmp(drv1->protocol_name, protocol)) {
317
            return drv1;
318
        }
319
    }
320
    return NULL;
321
}
322

    
323
static BlockDriver *find_image_format(const char *filename)
324
{
325
    int ret, score, score_max;
326
    BlockDriver *drv1, *drv;
327
    uint8_t buf[2048];
328
    BlockDriverState *bs;
329

    
330
    ret = bdrv_file_open(&bs, filename, 0);
331
    if (ret < 0)
332
        return NULL;
333

    
334
    /* Return the raw BlockDriver * to scsi-generic devices or empty drives */
335
    if (bs->sg || !bdrv_is_inserted(bs)) {
336
        bdrv_delete(bs);
337
        return bdrv_find_format("raw");
338
    }
339

    
340
    ret = bdrv_pread(bs, 0, buf, sizeof(buf));
341
    bdrv_delete(bs);
342
    if (ret < 0) {
343
        return NULL;
344
    }
345

    
346
    score_max = 0;
347
    drv = NULL;
348
    QLIST_FOREACH(drv1, &bdrv_drivers, list) {
349
        if (drv1->bdrv_probe) {
350
            score = drv1->bdrv_probe(buf, ret, filename);
351
            if (score > score_max) {
352
                score_max = score;
353
                drv = drv1;
354
            }
355
        }
356
    }
357
    return drv;
358
}
359

    
360
/**
361
 * Set the current 'total_sectors' value
362
 */
363
static int refresh_total_sectors(BlockDriverState *bs, int64_t hint)
364
{
365
    BlockDriver *drv = bs->drv;
366

    
367
    /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */
368
    if (bs->sg)
369
        return 0;
370

    
371
    /* query actual device if possible, otherwise just trust the hint */
372
    if (drv->bdrv_getlength) {
373
        int64_t length = drv->bdrv_getlength(bs);
374
        if (length < 0) {
375
            return length;
376
        }
377
        hint = length >> BDRV_SECTOR_BITS;
378
    }
379

    
380
    bs->total_sectors = hint;
381
    return 0;
382
}
383

    
384
/*
385
 * Common part for opening disk images and files
386
 */
387
static int bdrv_open_common(BlockDriverState *bs, const char *filename,
388
    int flags, BlockDriver *drv)
389
{
390
    int ret, open_flags;
391

    
392
    assert(drv != NULL);
393

    
394
    bs->file = NULL;
395
    bs->total_sectors = 0;
396
    bs->is_temporary = 0;
397
    bs->encrypted = 0;
398
    bs->valid_key = 0;
399
    bs->open_flags = flags;
400
    /* buffer_alignment defaulted to 512, drivers can change this value */
401
    bs->buffer_alignment = 512;
402

    
403
    pstrcpy(bs->filename, sizeof(bs->filename), filename);
404

    
405
    if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) {
406
        return -ENOTSUP;
407
    }
408

    
409
    bs->drv = drv;
410
    bs->opaque = qemu_mallocz(drv->instance_size);
411

    
412
    /*
413
     * Yes, BDRV_O_NOCACHE aka O_DIRECT means we have to present a
414
     * write cache to the guest.  We do need the fdatasync to flush
415
     * out transactions for block allocations, and we maybe have a
416
     * volatile write cache in our backing device to deal with.
417
     */
418
    if (flags & (BDRV_O_CACHE_WB|BDRV_O_NOCACHE))
419
        bs->enable_write_cache = 1;
420

    
421
    /*
422
     * Clear flags that are internal to the block layer before opening the
423
     * image.
424
     */
425
    open_flags = flags & ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
426

    
427
    /*
428
     * Snapshots should be writeable.
429
     */
430
    if (bs->is_temporary) {
431
        open_flags |= BDRV_O_RDWR;
432
    }
433

    
434
    /* Open the image, either directly or using a protocol */
435
    if (drv->bdrv_file_open) {
436
        ret = drv->bdrv_file_open(bs, filename, open_flags);
437
    } else {
438
        ret = bdrv_file_open(&bs->file, filename, open_flags);
439
        if (ret >= 0) {
440
            ret = drv->bdrv_open(bs, open_flags);
441
        }
442
    }
443

    
444
    if (ret < 0) {
445
        goto free_and_fail;
446
    }
447

    
448
    bs->keep_read_only = bs->read_only = !(open_flags & BDRV_O_RDWR);
449

    
450
    ret = refresh_total_sectors(bs, bs->total_sectors);
451
    if (ret < 0) {
452
        goto free_and_fail;
453
    }
454

    
455
#ifndef _WIN32
456
    if (bs->is_temporary) {
457
        unlink(filename);
458
    }
459
#endif
460
    return 0;
461

    
462
free_and_fail:
463
    if (bs->file) {
464
        bdrv_delete(bs->file);
465
        bs->file = NULL;
466
    }
467
    qemu_free(bs->opaque);
468
    bs->opaque = NULL;
469
    bs->drv = NULL;
470
    return ret;
471
}
472

    
473
/*
474
 * Opens a file using a protocol (file, host_device, nbd, ...)
475
 */
476
int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
477
{
478
    BlockDriverState *bs;
479
    BlockDriver *drv;
480
    int ret;
481

    
482
    drv = bdrv_find_protocol(filename);
483
    if (!drv) {
484
        return -ENOENT;
485
    }
486

    
487
    bs = bdrv_new("");
488
    ret = bdrv_open_common(bs, filename, flags, drv);
489
    if (ret < 0) {
490
        bdrv_delete(bs);
491
        return ret;
492
    }
493
    bs->growable = 1;
494
    *pbs = bs;
495
    return 0;
496
}
497

    
498
/*
499
 * Opens a disk image (raw, qcow2, vmdk, ...)
500
 */
501
int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
502
              BlockDriver *drv)
503
{
504
    int ret;
505

    
506
    if (flags & BDRV_O_SNAPSHOT) {
507
        BlockDriverState *bs1;
508
        int64_t total_size;
509
        int is_protocol = 0;
510
        BlockDriver *bdrv_qcow2;
511
        QEMUOptionParameter *options;
512
        char tmp_filename[PATH_MAX];
513
        char backing_filename[PATH_MAX];
514

    
515
        /* if snapshot, we create a temporary backing file and open it
516
           instead of opening 'filename' directly */
517

    
518
        /* if there is a backing file, use it */
519
        bs1 = bdrv_new("");
520
        ret = bdrv_open(bs1, filename, 0, drv);
521
        if (ret < 0) {
522
            bdrv_delete(bs1);
523
            return ret;
524
        }
525
        total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK;
526

    
527
        if (bs1->drv && bs1->drv->protocol_name)
528
            is_protocol = 1;
529

    
530
        bdrv_delete(bs1);
531

    
532
        get_tmp_filename(tmp_filename, sizeof(tmp_filename));
533

    
534
        /* Real path is meaningless for protocols */
535
        if (is_protocol)
536
            snprintf(backing_filename, sizeof(backing_filename),
537
                     "%s", filename);
538
        else if (!realpath(filename, backing_filename))
539
            return -errno;
540

    
541
        bdrv_qcow2 = bdrv_find_format("qcow2");
542
        options = parse_option_parameters("", bdrv_qcow2->create_options, NULL);
543

    
544
        set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size);
545
        set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename);
546
        if (drv) {
547
            set_option_parameter(options, BLOCK_OPT_BACKING_FMT,
548
                drv->format_name);
549
        }
550

    
551
        ret = bdrv_create(bdrv_qcow2, tmp_filename, options);
552
        free_option_parameters(options);
553
        if (ret < 0) {
554
            return ret;
555
        }
556

    
557
        filename = tmp_filename;
558
        drv = bdrv_qcow2;
559
        bs->is_temporary = 1;
560
    }
561

    
562
    /* Find the right image format driver */
563
    if (!drv) {
564
        drv = find_image_format(filename);
565
    }
566

    
567
    if (!drv) {
568
        ret = -ENOENT;
569
        goto unlink_and_fail;
570
    }
571

    
572
    /* Open the image */
573
    ret = bdrv_open_common(bs, filename, flags, drv);
574
    if (ret < 0) {
575
        goto unlink_and_fail;
576
    }
577

    
578
    /* If there is a backing file, use it */
579
    if ((flags & BDRV_O_NO_BACKING) == 0 && bs->backing_file[0] != '\0') {
580
        char backing_filename[PATH_MAX];
581
        int back_flags;
582
        BlockDriver *back_drv = NULL;
583

    
584
        bs->backing_hd = bdrv_new("");
585
        path_combine(backing_filename, sizeof(backing_filename),
586
                     filename, bs->backing_file);
587
        if (bs->backing_format[0] != '\0')
588
            back_drv = bdrv_find_format(bs->backing_format);
589

    
590
        /* backing files always opened read-only */
591
        back_flags =
592
            flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
593

    
594
        ret = bdrv_open(bs->backing_hd, backing_filename, back_flags, back_drv);
595
        if (ret < 0) {
596
            bdrv_close(bs);
597
            return ret;
598
        }
599
        if (bs->is_temporary) {
600
            bs->backing_hd->keep_read_only = !(flags & BDRV_O_RDWR);
601
        } else {
602
            /* base image inherits from "parent" */
603
            bs->backing_hd->keep_read_only = bs->keep_read_only;
604
        }
605
    }
606

    
607
    if (!bdrv_key_required(bs)) {
608
        /* call the change callback */
609
        bs->media_changed = 1;
610
        if (bs->change_cb)
611
            bs->change_cb(bs->change_opaque);
612
    }
613

    
614
    return 0;
615

    
616
unlink_and_fail:
617
    if (bs->is_temporary) {
618
        unlink(filename);
619
    }
620
    return ret;
621
}
622

    
623
void bdrv_close(BlockDriverState *bs)
624
{
625
    if (bs->drv) {
626
        if (bs->backing_hd) {
627
            bdrv_delete(bs->backing_hd);
628
            bs->backing_hd = NULL;
629
        }
630
        bs->drv->bdrv_close(bs);
631
        qemu_free(bs->opaque);
632
#ifdef _WIN32
633
        if (bs->is_temporary) {
634
            unlink(bs->filename);
635
        }
636
#endif
637
        bs->opaque = NULL;
638
        bs->drv = NULL;
639

    
640
        if (bs->file != NULL) {
641
            bdrv_close(bs->file);
642
        }
643

    
644
        /* call the change callback */
645
        bs->media_changed = 1;
646
        if (bs->change_cb)
647
            bs->change_cb(bs->change_opaque);
648
    }
649
}
650

    
651
void bdrv_close_all(void)
652
{
653
    BlockDriverState *bs;
654

    
655
    QTAILQ_FOREACH(bs, &bdrv_states, list) {
656
        bdrv_close(bs);
657
    }
658
}
659

    
660
void bdrv_delete(BlockDriverState *bs)
661
{
662
    /* remove from list, if necessary */
663
    if (bs->device_name[0] != '\0') {
664
        QTAILQ_REMOVE(&bdrv_states, bs, list);
665
    }
666

    
667
    bdrv_close(bs);
668
    if (bs->file != NULL) {
669
        bdrv_delete(bs->file);
670
    }
671

    
672
    qemu_free(bs);
673
}
674

    
675
/*
676
 * Run consistency checks on an image
677
 *
678
 * Returns the number of errors or -errno when an internal error occurs
679
 */
680
int bdrv_check(BlockDriverState *bs)
681
{
682
    if (bs->drv->bdrv_check == NULL) {
683
        return -ENOTSUP;
684
    }
685

    
686
    return bs->drv->bdrv_check(bs);
687
}
688

    
689
/* commit COW file into the raw image */
690
int bdrv_commit(BlockDriverState *bs)
691
{
692
    BlockDriver *drv = bs->drv;
693
    int64_t i, total_sectors;
694
    int n, j, ro, open_flags;
695
    int ret = 0, rw_ret = 0;
696
    unsigned char sector[BDRV_SECTOR_SIZE];
697
    char filename[1024];
698
    BlockDriverState *bs_rw, *bs_ro;
699

    
700
    if (!drv)
701
        return -ENOMEDIUM;
702
    
703
    if (!bs->backing_hd) {
704
        return -ENOTSUP;
705
    }
706

    
707
    if (bs->backing_hd->keep_read_only) {
708
        return -EACCES;
709
    }
710
    
711
    ro = bs->backing_hd->read_only;
712
    strncpy(filename, bs->backing_hd->filename, sizeof(filename));
713
    open_flags =  bs->backing_hd->open_flags;
714

    
715
    if (ro) {
716
        /* re-open as RW */
717
        bdrv_delete(bs->backing_hd);
718
        bs->backing_hd = NULL;
719
        bs_rw = bdrv_new("");
720
        rw_ret = bdrv_open(bs_rw, filename, open_flags | BDRV_O_RDWR, drv);
721
        if (rw_ret < 0) {
722
            bdrv_delete(bs_rw);
723
            /* try to re-open read-only */
724
            bs_ro = bdrv_new("");
725
            ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, drv);
726
            if (ret < 0) {
727
                bdrv_delete(bs_ro);
728
                /* drive not functional anymore */
729
                bs->drv = NULL;
730
                return ret;
731
            }
732
            bs->backing_hd = bs_ro;
733
            return rw_ret;
734
        }
735
        bs->backing_hd = bs_rw;
736
    }
737

    
738
    total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
739
    for (i = 0; i < total_sectors;) {
740
        if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
741
            for(j = 0; j < n; j++) {
742
                if (bdrv_read(bs, i, sector, 1) != 0) {
743
                    ret = -EIO;
744
                    goto ro_cleanup;
745
                }
746

    
747
                if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
748
                    ret = -EIO;
749
                    goto ro_cleanup;
750
                }
751
                i++;
752
            }
753
        } else {
754
            i += n;
755
        }
756
    }
757

    
758
    if (drv->bdrv_make_empty) {
759
        ret = drv->bdrv_make_empty(bs);
760
        bdrv_flush(bs);
761
    }
762

    
763
    /*
764
     * Make sure all data we wrote to the backing device is actually
765
     * stable on disk.
766
     */
767
    if (bs->backing_hd)
768
        bdrv_flush(bs->backing_hd);
769

    
770
ro_cleanup:
771

    
772
    if (ro) {
773
        /* re-open as RO */
774
        bdrv_delete(bs->backing_hd);
775
        bs->backing_hd = NULL;
776
        bs_ro = bdrv_new("");
777
        ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, drv);
778
        if (ret < 0) {
779
            bdrv_delete(bs_ro);
780
            /* drive not functional anymore */
781
            bs->drv = NULL;
782
            return ret;
783
        }
784
        bs->backing_hd = bs_ro;
785
        bs->backing_hd->keep_read_only = 0;
786
    }
787

    
788
    return ret;
789
}
790

    
791
/*
792
 * Return values:
793
 * 0        - success
794
 * -EINVAL  - backing format specified, but no file
795
 * -ENOSPC  - can't update the backing file because no space is left in the
796
 *            image file header
797
 * -ENOTSUP - format driver doesn't support changing the backing file
798
 */
799
int bdrv_change_backing_file(BlockDriverState *bs,
800
    const char *backing_file, const char *backing_fmt)
801
{
802
    BlockDriver *drv = bs->drv;
803

    
804
    if (drv->bdrv_change_backing_file != NULL) {
805
        return drv->bdrv_change_backing_file(bs, backing_file, backing_fmt);
806
    } else {
807
        return -ENOTSUP;
808
    }
809
}
810

    
811
static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
812
                                   size_t size)
813
{
814
    int64_t len;
815

    
816
    if (!bdrv_is_inserted(bs))
817
        return -ENOMEDIUM;
818

    
819
    if (bs->growable)
820
        return 0;
821

    
822
    len = bdrv_getlength(bs);
823

    
824
    if (offset < 0)
825
        return -EIO;
826

    
827
    if ((offset > len) || (len - offset < size))
828
        return -EIO;
829

    
830
    return 0;
831
}
832

    
833
static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
834
                              int nb_sectors)
835
{
836
    return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE,
837
                                   nb_sectors * BDRV_SECTOR_SIZE);
838
}
839

    
840
/* return < 0 if error. See bdrv_write() for the return codes */
841
int bdrv_read(BlockDriverState *bs, int64_t sector_num,
842
              uint8_t *buf, int nb_sectors)
843
{
844
    BlockDriver *drv = bs->drv;
845

    
846
    if (!drv)
847
        return -ENOMEDIUM;
848
    if (bdrv_check_request(bs, sector_num, nb_sectors))
849
        return -EIO;
850

    
851
    return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
852
}
853

    
854
static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num,
855
                             int nb_sectors, int dirty)
856
{
857
    int64_t start, end;
858
    unsigned long val, idx, bit;
859

    
860
    start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK;
861
    end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
862

    
863
    for (; start <= end; start++) {
864
        idx = start / (sizeof(unsigned long) * 8);
865
        bit = start % (sizeof(unsigned long) * 8);
866
        val = bs->dirty_bitmap[idx];
867
        if (dirty) {
868
            if (!(val & (1 << bit))) {
869
                bs->dirty_count++;
870
                val |= 1 << bit;
871
            }
872
        } else {
873
            if (val & (1 << bit)) {
874
                bs->dirty_count--;
875
                val &= ~(1 << bit);
876
            }
877
        }
878
        bs->dirty_bitmap[idx] = val;
879
    }
880
}
881

    
882
/* Return < 0 if error. Important errors are:
883
  -EIO         generic I/O error (may happen for all errors)
884
  -ENOMEDIUM   No media inserted.
885
  -EINVAL      Invalid sector number or nb_sectors
886
  -EACCES      Trying to write a read-only device
887
*/
888
int bdrv_write(BlockDriverState *bs, int64_t sector_num,
889
               const uint8_t *buf, int nb_sectors)
890
{
891
    BlockDriver *drv = bs->drv;
892
    if (!bs->drv)
893
        return -ENOMEDIUM;
894
    if (bs->read_only)
895
        return -EACCES;
896
    if (bdrv_check_request(bs, sector_num, nb_sectors))
897
        return -EIO;
898

    
899
    if (bs->dirty_bitmap) {
900
        set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
901
    }
902

    
903
    if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
904
        bs->wr_highest_sector = sector_num + nb_sectors - 1;
905
    }
906

    
907
    return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
908
}
909

    
910
int bdrv_pread(BlockDriverState *bs, int64_t offset,
911
               void *buf, int count1)
912
{
913
    uint8_t tmp_buf[BDRV_SECTOR_SIZE];
914
    int len, nb_sectors, count;
915
    int64_t sector_num;
916
    int ret;
917

    
918
    count = count1;
919
    /* first read to align to sector start */
920
    len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
921
    if (len > count)
922
        len = count;
923
    sector_num = offset >> BDRV_SECTOR_BITS;
924
    if (len > 0) {
925
        if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
926
            return ret;
927
        memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len);
928
        count -= len;
929
        if (count == 0)
930
            return count1;
931
        sector_num++;
932
        buf += len;
933
    }
934

    
935
    /* read the sectors "in place" */
936
    nb_sectors = count >> BDRV_SECTOR_BITS;
937
    if (nb_sectors > 0) {
938
        if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0)
939
            return ret;
940
        sector_num += nb_sectors;
941
        len = nb_sectors << BDRV_SECTOR_BITS;
942
        buf += len;
943
        count -= len;
944
    }
945

    
946
    /* add data from the last sector */
947
    if (count > 0) {
948
        if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
949
            return ret;
950
        memcpy(buf, tmp_buf, count);
951
    }
952
    return count1;
953
}
954

    
955
int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
956
                const void *buf, int count1)
957
{
958
    uint8_t tmp_buf[BDRV_SECTOR_SIZE];
959
    int len, nb_sectors, count;
960
    int64_t sector_num;
961
    int ret;
962

    
963
    count = count1;
964
    /* first write to align to sector start */
965
    len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
966
    if (len > count)
967
        len = count;
968
    sector_num = offset >> BDRV_SECTOR_BITS;
969
    if (len > 0) {
970
        if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
971
            return ret;
972
        memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len);
973
        if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
974
            return ret;
975
        count -= len;
976
        if (count == 0)
977
            return count1;
978
        sector_num++;
979
        buf += len;
980
    }
981

    
982
    /* write the sectors "in place" */
983
    nb_sectors = count >> BDRV_SECTOR_BITS;
984
    if (nb_sectors > 0) {
985
        if ((ret = bdrv_write(bs, sector_num, buf, nb_sectors)) < 0)
986
            return ret;
987
        sector_num += nb_sectors;
988
        len = nb_sectors << BDRV_SECTOR_BITS;
989
        buf += len;
990
        count -= len;
991
    }
992

    
993
    /* add data from the last sector */
994
    if (count > 0) {
995
        if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
996
            return ret;
997
        memcpy(tmp_buf, buf, count);
998
        if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
999
            return ret;
1000
    }
1001
    return count1;
1002
}
1003

    
1004
/**
1005
 * Truncate file to 'offset' bytes (needed only for file protocols)
1006
 */
1007
int bdrv_truncate(BlockDriverState *bs, int64_t offset)
1008
{
1009
    BlockDriver *drv = bs->drv;
1010
    int ret;
1011
    if (!drv)
1012
        return -ENOMEDIUM;
1013
    if (!drv->bdrv_truncate)
1014
        return -ENOTSUP;
1015
    if (bs->read_only)
1016
        return -EACCES;
1017
    ret = drv->bdrv_truncate(bs, offset);
1018
    if (ret == 0) {
1019
        ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
1020
    }
1021
    return ret;
1022
}
1023

    
1024
/**
1025
 * Length of a file in bytes. Return < 0 if error or unknown.
1026
 */
1027
int64_t bdrv_getlength(BlockDriverState *bs)
1028
{
1029
    BlockDriver *drv = bs->drv;
1030
    if (!drv)
1031
        return -ENOMEDIUM;
1032

    
1033
    /* Fixed size devices use the total_sectors value for speed instead of
1034
       issuing a length query (like lseek) on each call.  Also, legacy block
1035
       drivers don't provide a bdrv_getlength function and must use
1036
       total_sectors. */
1037
    if (!bs->growable || !drv->bdrv_getlength) {
1038
        return bs->total_sectors * BDRV_SECTOR_SIZE;
1039
    }
1040
    return drv->bdrv_getlength(bs);
1041
}
1042

    
1043
/* return 0 as number of sectors if no device present or error */
1044
void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
1045
{
1046
    int64_t length;
1047
    length = bdrv_getlength(bs);
1048
    if (length < 0)
1049
        length = 0;
1050
    else
1051
        length = length >> BDRV_SECTOR_BITS;
1052
    *nb_sectors_ptr = length;
1053
}
1054

    
1055
struct partition {
1056
        uint8_t boot_ind;           /* 0x80 - active */
1057
        uint8_t head;               /* starting head */
1058
        uint8_t sector;             /* starting sector */
1059
        uint8_t cyl;                /* starting cylinder */
1060
        uint8_t sys_ind;            /* What partition type */
1061
        uint8_t end_head;           /* end head */
1062
        uint8_t end_sector;         /* end sector */
1063
        uint8_t end_cyl;            /* end cylinder */
1064
        uint32_t start_sect;        /* starting sector counting from 0 */
1065
        uint32_t nr_sects;          /* nr of sectors in partition */
1066
} __attribute__((packed));
1067

    
1068
/* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
1069
static int guess_disk_lchs(BlockDriverState *bs,
1070
                           int *pcylinders, int *pheads, int *psectors)
1071
{
1072
    uint8_t buf[BDRV_SECTOR_SIZE];
1073
    int ret, i, heads, sectors, cylinders;
1074
    struct partition *p;
1075
    uint32_t nr_sects;
1076
    uint64_t nb_sectors;
1077

    
1078
    bdrv_get_geometry(bs, &nb_sectors);
1079

    
1080
    ret = bdrv_read(bs, 0, buf, 1);
1081
    if (ret < 0)
1082
        return -1;
1083
    /* test msdos magic */
1084
    if (buf[510] != 0x55 || buf[511] != 0xaa)
1085
        return -1;
1086
    for(i = 0; i < 4; i++) {
1087
        p = ((struct partition *)(buf + 0x1be)) + i;
1088
        nr_sects = le32_to_cpu(p->nr_sects);
1089
        if (nr_sects && p->end_head) {
1090
            /* We make the assumption that the partition terminates on
1091
               a cylinder boundary */
1092
            heads = p->end_head + 1;
1093
            sectors = p->end_sector & 63;
1094
            if (sectors == 0)
1095
                continue;
1096
            cylinders = nb_sectors / (heads * sectors);
1097
            if (cylinders < 1 || cylinders > 16383)
1098
                continue;
1099
            *pheads = heads;
1100
            *psectors = sectors;
1101
            *pcylinders = cylinders;
1102
#if 0
1103
            printf("guessed geometry: LCHS=%d %d %d\n",
1104
                   cylinders, heads, sectors);
1105
#endif
1106
            return 0;
1107
        }
1108
    }
1109
    return -1;
1110
}
1111

    
1112
void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
1113
{
1114
    int translation, lba_detected = 0;
1115
    int cylinders, heads, secs;
1116
    uint64_t nb_sectors;
1117

    
1118
    /* if a geometry hint is available, use it */
1119
    bdrv_get_geometry(bs, &nb_sectors);
1120
    bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
1121
    translation = bdrv_get_translation_hint(bs);
1122
    if (cylinders != 0) {
1123
        *pcyls = cylinders;
1124
        *pheads = heads;
1125
        *psecs = secs;
1126
    } else {
1127
        if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
1128
            if (heads > 16) {
1129
                /* if heads > 16, it means that a BIOS LBA
1130
                   translation was active, so the default
1131
                   hardware geometry is OK */
1132
                lba_detected = 1;
1133
                goto default_geometry;
1134
            } else {
1135
                *pcyls = cylinders;
1136
                *pheads = heads;
1137
                *psecs = secs;
1138
                /* disable any translation to be in sync with
1139
                   the logical geometry */
1140
                if (translation == BIOS_ATA_TRANSLATION_AUTO) {
1141
                    bdrv_set_translation_hint(bs,
1142
                                              BIOS_ATA_TRANSLATION_NONE);
1143
                }
1144
            }
1145
        } else {
1146
        default_geometry:
1147
            /* if no geometry, use a standard physical disk geometry */
1148
            cylinders = nb_sectors / (16 * 63);
1149

    
1150
            if (cylinders > 16383)
1151
                cylinders = 16383;
1152
            else if (cylinders < 2)
1153
                cylinders = 2;
1154
            *pcyls = cylinders;
1155
            *pheads = 16;
1156
            *psecs = 63;
1157
            if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
1158
                if ((*pcyls * *pheads) <= 131072) {
1159
                    bdrv_set_translation_hint(bs,
1160
                                              BIOS_ATA_TRANSLATION_LARGE);
1161
                } else {
1162
                    bdrv_set_translation_hint(bs,
1163
                                              BIOS_ATA_TRANSLATION_LBA);
1164
                }
1165
            }
1166
        }
1167
        bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
1168
    }
1169
}
1170

    
1171
void bdrv_set_geometry_hint(BlockDriverState *bs,
1172
                            int cyls, int heads, int secs)
1173
{
1174
    bs->cyls = cyls;
1175
    bs->heads = heads;
1176
    bs->secs = secs;
1177
}
1178

    
1179
void bdrv_set_type_hint(BlockDriverState *bs, int type)
1180
{
1181
    bs->type = type;
1182
    bs->removable = ((type == BDRV_TYPE_CDROM ||
1183
                      type == BDRV_TYPE_FLOPPY));
1184
}
1185

    
1186
void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
1187
{
1188
    bs->translation = translation;
1189
}
1190

    
1191
void bdrv_get_geometry_hint(BlockDriverState *bs,
1192
                            int *pcyls, int *pheads, int *psecs)
1193
{
1194
    *pcyls = bs->cyls;
1195
    *pheads = bs->heads;
1196
    *psecs = bs->secs;
1197
}
1198

    
1199
int bdrv_get_type_hint(BlockDriverState *bs)
1200
{
1201
    return bs->type;
1202
}
1203

    
1204
int bdrv_get_translation_hint(BlockDriverState *bs)
1205
{
1206
    return bs->translation;
1207
}
1208

    
1209
void bdrv_set_on_error(BlockDriverState *bs, BlockErrorAction on_read_error,
1210
                       BlockErrorAction on_write_error)
1211
{
1212
    bs->on_read_error = on_read_error;
1213
    bs->on_write_error = on_write_error;
1214
}
1215

    
1216
BlockErrorAction bdrv_get_on_error(BlockDriverState *bs, int is_read)
1217
{
1218
    return is_read ? bs->on_read_error : bs->on_write_error;
1219
}
1220

    
1221
int bdrv_is_removable(BlockDriverState *bs)
1222
{
1223
    return bs->removable;
1224
}
1225

    
1226
int bdrv_is_read_only(BlockDriverState *bs)
1227
{
1228
    return bs->read_only;
1229
}
1230

    
1231
int bdrv_is_sg(BlockDriverState *bs)
1232
{
1233
    return bs->sg;
1234
}
1235

    
1236
int bdrv_enable_write_cache(BlockDriverState *bs)
1237
{
1238
    return bs->enable_write_cache;
1239
}
1240

    
1241
/* XXX: no longer used */
1242
void bdrv_set_change_cb(BlockDriverState *bs,
1243
                        void (*change_cb)(void *opaque), void *opaque)
1244
{
1245
    bs->change_cb = change_cb;
1246
    bs->change_opaque = opaque;
1247
}
1248

    
1249
int bdrv_is_encrypted(BlockDriverState *bs)
1250
{
1251
    if (bs->backing_hd && bs->backing_hd->encrypted)
1252
        return 1;
1253
    return bs->encrypted;
1254
}
1255

    
1256
int bdrv_key_required(BlockDriverState *bs)
1257
{
1258
    BlockDriverState *backing_hd = bs->backing_hd;
1259

    
1260
    if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
1261
        return 1;
1262
    return (bs->encrypted && !bs->valid_key);
1263
}
1264

    
1265
int bdrv_set_key(BlockDriverState *bs, const char *key)
1266
{
1267
    int ret;
1268
    if (bs->backing_hd && bs->backing_hd->encrypted) {
1269
        ret = bdrv_set_key(bs->backing_hd, key);
1270
        if (ret < 0)
1271
            return ret;
1272
        if (!bs->encrypted)
1273
            return 0;
1274
    }
1275
    if (!bs->encrypted) {
1276
        return -EINVAL;
1277
    } else if (!bs->drv || !bs->drv->bdrv_set_key) {
1278
        return -ENOMEDIUM;
1279
    }
1280
    ret = bs->drv->bdrv_set_key(bs, key);
1281
    if (ret < 0) {
1282
        bs->valid_key = 0;
1283
    } else if (!bs->valid_key) {
1284
        bs->valid_key = 1;
1285
        /* call the change callback now, we skipped it on open */
1286
        bs->media_changed = 1;
1287
        if (bs->change_cb)
1288
            bs->change_cb(bs->change_opaque);
1289
    }
1290
    return ret;
1291
}
1292

    
1293
void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
1294
{
1295
    if (!bs->drv) {
1296
        buf[0] = '\0';
1297
    } else {
1298
        pstrcpy(buf, buf_size, bs->drv->format_name);
1299
    }
1300
}
1301

    
1302
void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
1303
                         void *opaque)
1304
{
1305
    BlockDriver *drv;
1306

    
1307
    QLIST_FOREACH(drv, &bdrv_drivers, list) {
1308
        it(opaque, drv->format_name);
1309
    }
1310
}
1311

    
1312
BlockDriverState *bdrv_find(const char *name)
1313
{
1314
    BlockDriverState *bs;
1315

    
1316
    QTAILQ_FOREACH(bs, &bdrv_states, list) {
1317
        if (!strcmp(name, bs->device_name)) {
1318
            return bs;
1319
        }
1320
    }
1321
    return NULL;
1322
}
1323

    
1324
void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
1325
{
1326
    BlockDriverState *bs;
1327

    
1328
    QTAILQ_FOREACH(bs, &bdrv_states, list) {
1329
        it(opaque, bs);
1330
    }
1331
}
1332

    
1333
const char *bdrv_get_device_name(BlockDriverState *bs)
1334
{
1335
    return bs->device_name;
1336
}
1337

    
1338
void bdrv_flush(BlockDriverState *bs)
1339
{
1340
    if (bs->open_flags & BDRV_O_NO_FLUSH) {
1341
        return;
1342
    }
1343

    
1344
    if (bs->drv && bs->drv->bdrv_flush)
1345
        bs->drv->bdrv_flush(bs);
1346
}
1347

    
1348
void bdrv_flush_all(void)
1349
{
1350
    BlockDriverState *bs;
1351

    
1352
    QTAILQ_FOREACH(bs, &bdrv_states, list) {
1353
        if (bs->drv && !bdrv_is_read_only(bs) &&
1354
            (!bdrv_is_removable(bs) || bdrv_is_inserted(bs))) {
1355
            bdrv_flush(bs);
1356
        }
1357
    }
1358
}
1359

    
1360
int bdrv_has_zero_init(BlockDriverState *bs)
1361
{
1362
    assert(bs->drv);
1363

    
1364
    if (bs->drv->no_zero_init) {
1365
        return 0;
1366
    } else if (bs->file) {
1367
        return bdrv_has_zero_init(bs->file);
1368
    }
1369

    
1370
    return 1;
1371
}
1372

    
1373
/*
1374
 * Returns true iff the specified sector is present in the disk image. Drivers
1375
 * not implementing the functionality are assumed to not support backing files,
1376
 * hence all their sectors are reported as allocated.
1377
 *
1378
 * 'pnum' is set to the number of sectors (including and immediately following
1379
 * the specified sector) that are known to be in the same
1380
 * allocated/unallocated state.
1381
 *
1382
 * 'nb_sectors' is the max value 'pnum' should be set to.
1383
 */
1384
int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1385
        int *pnum)
1386
{
1387
    int64_t n;
1388
    if (!bs->drv->bdrv_is_allocated) {
1389
        if (sector_num >= bs->total_sectors) {
1390
            *pnum = 0;
1391
            return 0;
1392
        }
1393
        n = bs->total_sectors - sector_num;
1394
        *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1395
        return 1;
1396
    }
1397
    return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1398
}
1399

    
1400
void bdrv_mon_event(const BlockDriverState *bdrv,
1401
                    BlockMonEventAction action, int is_read)
1402
{
1403
    QObject *data;
1404
    const char *action_str;
1405

    
1406
    switch (action) {
1407
    case BDRV_ACTION_REPORT:
1408
        action_str = "report";
1409
        break;
1410
    case BDRV_ACTION_IGNORE:
1411
        action_str = "ignore";
1412
        break;
1413
    case BDRV_ACTION_STOP:
1414
        action_str = "stop";
1415
        break;
1416
    default:
1417
        abort();
1418
    }
1419

    
1420
    data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }",
1421
                              bdrv->device_name,
1422
                              action_str,
1423
                              is_read ? "read" : "write");
1424
    monitor_protocol_event(QEVENT_BLOCK_IO_ERROR, data);
1425

    
1426
    qobject_decref(data);
1427
}
1428

    
1429
static void bdrv_print_dict(QObject *obj, void *opaque)
1430
{
1431
    QDict *bs_dict;
1432
    Monitor *mon = opaque;
1433

    
1434
    bs_dict = qobject_to_qdict(obj);
1435

    
1436
    monitor_printf(mon, "%s: type=%s removable=%d",
1437
                        qdict_get_str(bs_dict, "device"),
1438
                        qdict_get_str(bs_dict, "type"),
1439
                        qdict_get_bool(bs_dict, "removable"));
1440

    
1441
    if (qdict_get_bool(bs_dict, "removable")) {
1442
        monitor_printf(mon, " locked=%d", qdict_get_bool(bs_dict, "locked"));
1443
    }
1444

    
1445
    if (qdict_haskey(bs_dict, "inserted")) {
1446
        QDict *qdict = qobject_to_qdict(qdict_get(bs_dict, "inserted"));
1447

    
1448
        monitor_printf(mon, " file=");
1449
        monitor_print_filename(mon, qdict_get_str(qdict, "file"));
1450
        if (qdict_haskey(qdict, "backing_file")) {
1451
            monitor_printf(mon, " backing_file=");
1452
            monitor_print_filename(mon, qdict_get_str(qdict, "backing_file"));
1453
        }
1454
        monitor_printf(mon, " ro=%d drv=%s encrypted=%d",
1455
                            qdict_get_bool(qdict, "ro"),
1456
                            qdict_get_str(qdict, "drv"),
1457
                            qdict_get_bool(qdict, "encrypted"));
1458
    } else {
1459
        monitor_printf(mon, " [not inserted]");
1460
    }
1461

    
1462
    monitor_printf(mon, "\n");
1463
}
1464

    
1465
void bdrv_info_print(Monitor *mon, const QObject *data)
1466
{
1467
    qlist_iter(qobject_to_qlist(data), bdrv_print_dict, mon);
1468
}
1469

    
1470
void bdrv_info(Monitor *mon, QObject **ret_data)
1471
{
1472
    QList *bs_list;
1473
    BlockDriverState *bs;
1474

    
1475
    bs_list = qlist_new();
1476

    
1477
    QTAILQ_FOREACH(bs, &bdrv_states, list) {
1478
        QObject *bs_obj;
1479
        const char *type = "unknown";
1480

    
1481
        switch(bs->type) {
1482
        case BDRV_TYPE_HD:
1483
            type = "hd";
1484
            break;
1485
        case BDRV_TYPE_CDROM:
1486
            type = "cdrom";
1487
            break;
1488
        case BDRV_TYPE_FLOPPY:
1489
            type = "floppy";
1490
            break;
1491
        }
1492

    
1493
        bs_obj = qobject_from_jsonf("{ 'device': %s, 'type': %s, "
1494
                                    "'removable': %i, 'locked': %i }",
1495
                                    bs->device_name, type, bs->removable,
1496
                                    bs->locked);
1497

    
1498
        if (bs->drv) {
1499
            QObject *obj;
1500
            QDict *bs_dict = qobject_to_qdict(bs_obj);
1501

    
1502
            obj = qobject_from_jsonf("{ 'file': %s, 'ro': %i, 'drv': %s, "
1503
                                     "'encrypted': %i }",
1504
                                     bs->filename, bs->read_only,
1505
                                     bs->drv->format_name,
1506
                                     bdrv_is_encrypted(bs));
1507
            if (bs->backing_file[0] != '\0') {
1508
                QDict *qdict = qobject_to_qdict(obj);
1509
                qdict_put(qdict, "backing_file",
1510
                          qstring_from_str(bs->backing_file));
1511
            }
1512

    
1513
            qdict_put_obj(bs_dict, "inserted", obj);
1514
        }
1515
        qlist_append_obj(bs_list, bs_obj);
1516
    }
1517

    
1518
    *ret_data = QOBJECT(bs_list);
1519
}
1520

    
1521
static void bdrv_stats_iter(QObject *data, void *opaque)
1522
{
1523
    QDict *qdict;
1524
    Monitor *mon = opaque;
1525

    
1526
    qdict = qobject_to_qdict(data);
1527
    monitor_printf(mon, "%s:", qdict_get_str(qdict, "device"));
1528

    
1529
    qdict = qobject_to_qdict(qdict_get(qdict, "stats"));
1530
    monitor_printf(mon, " rd_bytes=%" PRId64
1531
                        " wr_bytes=%" PRId64
1532
                        " rd_operations=%" PRId64
1533
                        " wr_operations=%" PRId64
1534
                        "\n",
1535
                        qdict_get_int(qdict, "rd_bytes"),
1536
                        qdict_get_int(qdict, "wr_bytes"),
1537
                        qdict_get_int(qdict, "rd_operations"),
1538
                        qdict_get_int(qdict, "wr_operations"));
1539
}
1540

    
1541
void bdrv_stats_print(Monitor *mon, const QObject *data)
1542
{
1543
    qlist_iter(qobject_to_qlist(data), bdrv_stats_iter, mon);
1544
}
1545

    
1546
static QObject* bdrv_info_stats_bs(BlockDriverState *bs)
1547
{
1548
    QObject *res;
1549
    QDict *dict;
1550

    
1551
    res = qobject_from_jsonf("{ 'stats': {"
1552
                             "'rd_bytes': %" PRId64 ","
1553
                             "'wr_bytes': %" PRId64 ","
1554
                             "'rd_operations': %" PRId64 ","
1555
                             "'wr_operations': %" PRId64 ","
1556
                             "'wr_highest_offset': %" PRId64
1557
                             "} }",
1558
                             bs->rd_bytes, bs->wr_bytes,
1559
                             bs->rd_ops, bs->wr_ops,
1560
                             bs->wr_highest_sector * (long)BDRV_SECTOR_SIZE);
1561
    dict  = qobject_to_qdict(res);
1562

    
1563
    if (*bs->device_name) {
1564
        qdict_put(dict, "device", qstring_from_str(bs->device_name));
1565
    }
1566

    
1567
    if (bs->file) {
1568
        QObject *parent = bdrv_info_stats_bs(bs->file);
1569
        qdict_put_obj(dict, "parent", parent);
1570
    }
1571

    
1572
    return res;
1573
}
1574

    
1575
void bdrv_info_stats(Monitor *mon, QObject **ret_data)
1576
{
1577
    QObject *obj;
1578
    QList *devices;
1579
    BlockDriverState *bs;
1580

    
1581
    devices = qlist_new();
1582

    
1583
    QTAILQ_FOREACH(bs, &bdrv_states, list) {
1584
        obj = bdrv_info_stats_bs(bs);
1585
        qlist_append_obj(devices, obj);
1586
    }
1587

    
1588
    *ret_data = QOBJECT(devices);
1589
}
1590

    
1591
const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1592
{
1593
    if (bs->backing_hd && bs->backing_hd->encrypted)
1594
        return bs->backing_file;
1595
    else if (bs->encrypted)
1596
        return bs->filename;
1597
    else
1598
        return NULL;
1599
}
1600

    
1601
void bdrv_get_backing_filename(BlockDriverState *bs,
1602
                               char *filename, int filename_size)
1603
{
1604
    if (!bs->backing_file) {
1605
        pstrcpy(filename, filename_size, "");
1606
    } else {
1607
        pstrcpy(filename, filename_size, bs->backing_file);
1608
    }
1609
}
1610

    
1611
int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
1612
                          const uint8_t *buf, int nb_sectors)
1613
{
1614
    BlockDriver *drv = bs->drv;
1615
    if (!drv)
1616
        return -ENOMEDIUM;
1617
    if (!drv->bdrv_write_compressed)
1618
        return -ENOTSUP;
1619
    if (bdrv_check_request(bs, sector_num, nb_sectors))
1620
        return -EIO;
1621

    
1622
    if (bs->dirty_bitmap) {
1623
        set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
1624
    }
1625

    
1626
    return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1627
}
1628

    
1629
int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1630
{
1631
    BlockDriver *drv = bs->drv;
1632
    if (!drv)
1633
        return -ENOMEDIUM;
1634
    if (!drv->bdrv_get_info)
1635
        return -ENOTSUP;
1636
    memset(bdi, 0, sizeof(*bdi));
1637
    return drv->bdrv_get_info(bs, bdi);
1638
}
1639

    
1640
int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
1641
                      int64_t pos, int size)
1642
{
1643
    BlockDriver *drv = bs->drv;
1644
    if (!drv)
1645
        return -ENOMEDIUM;
1646
    if (drv->bdrv_save_vmstate)
1647
        return drv->bdrv_save_vmstate(bs, buf, pos, size);
1648
    if (bs->file)
1649
        return bdrv_save_vmstate(bs->file, buf, pos, size);
1650
    return -ENOTSUP;
1651
}
1652

    
1653
int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
1654
                      int64_t pos, int size)
1655
{
1656
    BlockDriver *drv = bs->drv;
1657
    if (!drv)
1658
        return -ENOMEDIUM;
1659
    if (drv->bdrv_load_vmstate)
1660
        return drv->bdrv_load_vmstate(bs, buf, pos, size);
1661
    if (bs->file)
1662
        return bdrv_load_vmstate(bs->file, buf, pos, size);
1663
    return -ENOTSUP;
1664
}
1665

    
1666
void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event)
1667
{
1668
    BlockDriver *drv = bs->drv;
1669

    
1670
    if (!drv || !drv->bdrv_debug_event) {
1671
        return;
1672
    }
1673

    
1674
    return drv->bdrv_debug_event(bs, event);
1675

    
1676
}
1677

    
1678
/**************************************************************/
1679
/* handling of snapshots */
1680

    
1681
int bdrv_can_snapshot(BlockDriverState *bs)
1682
{
1683
    BlockDriver *drv = bs->drv;
1684
    if (!drv || bdrv_is_removable(bs) || bdrv_is_read_only(bs)) {
1685
        return 0;
1686
    }
1687

    
1688
    if (!drv->bdrv_snapshot_create) {
1689
        if (bs->file != NULL) {
1690
            return bdrv_can_snapshot(bs->file);
1691
        }
1692
        return 0;
1693
    }
1694

    
1695
    return 1;
1696
}
1697

    
1698
int bdrv_snapshot_create(BlockDriverState *bs,
1699
                         QEMUSnapshotInfo *sn_info)
1700
{
1701
    BlockDriver *drv = bs->drv;
1702
    if (!drv)
1703
        return -ENOMEDIUM;
1704
    if (drv->bdrv_snapshot_create)
1705
        return drv->bdrv_snapshot_create(bs, sn_info);
1706
    if (bs->file)
1707
        return bdrv_snapshot_create(bs->file, sn_info);
1708
    return -ENOTSUP;
1709
}
1710

    
1711
int bdrv_snapshot_goto(BlockDriverState *bs,
1712
                       const char *snapshot_id)
1713
{
1714
    BlockDriver *drv = bs->drv;
1715
    int ret, open_ret;
1716

    
1717
    if (!drv)
1718
        return -ENOMEDIUM;
1719
    if (drv->bdrv_snapshot_goto)
1720
        return drv->bdrv_snapshot_goto(bs, snapshot_id);
1721

    
1722
    if (bs->file) {
1723
        drv->bdrv_close(bs);
1724
        ret = bdrv_snapshot_goto(bs->file, snapshot_id);
1725
        open_ret = drv->bdrv_open(bs, bs->open_flags);
1726
        if (open_ret < 0) {
1727
            bdrv_delete(bs->file);
1728
            bs->drv = NULL;
1729
            return open_ret;
1730
        }
1731
        return ret;
1732
    }
1733

    
1734
    return -ENOTSUP;
1735
}
1736

    
1737
int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1738
{
1739
    BlockDriver *drv = bs->drv;
1740
    if (!drv)
1741
        return -ENOMEDIUM;
1742
    if (drv->bdrv_snapshot_delete)
1743
        return drv->bdrv_snapshot_delete(bs, snapshot_id);
1744
    if (bs->file)
1745
        return bdrv_snapshot_delete(bs->file, snapshot_id);
1746
    return -ENOTSUP;
1747
}
1748

    
1749
int bdrv_snapshot_list(BlockDriverState *bs,
1750
                       QEMUSnapshotInfo **psn_info)
1751
{
1752
    BlockDriver *drv = bs->drv;
1753
    if (!drv)
1754
        return -ENOMEDIUM;
1755
    if (drv->bdrv_snapshot_list)
1756
        return drv->bdrv_snapshot_list(bs, psn_info);
1757
    if (bs->file)
1758
        return bdrv_snapshot_list(bs->file, psn_info);
1759
    return -ENOTSUP;
1760
}
1761

    
1762
#define NB_SUFFIXES 4
1763

    
1764
char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1765
{
1766
    static const char suffixes[NB_SUFFIXES] = "KMGT";
1767
    int64_t base;
1768
    int i;
1769

    
1770
    if (size <= 999) {
1771
        snprintf(buf, buf_size, "%" PRId64, size);
1772
    } else {
1773
        base = 1024;
1774
        for(i = 0; i < NB_SUFFIXES; i++) {
1775
            if (size < (10 * base)) {
1776
                snprintf(buf, buf_size, "%0.1f%c",
1777
                         (double)size / base,
1778
                         suffixes[i]);
1779
                break;
1780
            } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
1781
                snprintf(buf, buf_size, "%" PRId64 "%c",
1782
                         ((size + (base >> 1)) / base),
1783
                         suffixes[i]);
1784
                break;
1785
            }
1786
            base = base * 1024;
1787
        }
1788
    }
1789
    return buf;
1790
}
1791

    
1792
char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1793
{
1794
    char buf1[128], date_buf[128], clock_buf[128];
1795
#ifdef _WIN32
1796
    struct tm *ptm;
1797
#else
1798
    struct tm tm;
1799
#endif
1800
    time_t ti;
1801
    int64_t secs;
1802

    
1803
    if (!sn) {
1804
        snprintf(buf, buf_size,
1805
                 "%-10s%-20s%7s%20s%15s",
1806
                 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1807
    } else {
1808
        ti = sn->date_sec;
1809
#ifdef _WIN32
1810
        ptm = localtime(&ti);
1811
        strftime(date_buf, sizeof(date_buf),
1812
                 "%Y-%m-%d %H:%M:%S", ptm);
1813
#else
1814
        localtime_r(&ti, &tm);
1815
        strftime(date_buf, sizeof(date_buf),
1816
                 "%Y-%m-%d %H:%M:%S", &tm);
1817
#endif
1818
        secs = sn->vm_clock_nsec / 1000000000;
1819
        snprintf(clock_buf, sizeof(clock_buf),
1820
                 "%02d:%02d:%02d.%03d",
1821
                 (int)(secs / 3600),
1822
                 (int)((secs / 60) % 60),
1823
                 (int)(secs % 60),
1824
                 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1825
        snprintf(buf, buf_size,
1826
                 "%-10s%-20s%7s%20s%15s",
1827
                 sn->id_str, sn->name,
1828
                 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1829
                 date_buf,
1830
                 clock_buf);
1831
    }
1832
    return buf;
1833
}
1834

    
1835

    
1836
/**************************************************************/
1837
/* async I/Os */
1838

    
1839
BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
1840
                                 QEMUIOVector *qiov, int nb_sectors,
1841
                                 BlockDriverCompletionFunc *cb, void *opaque)
1842
{
1843
    BlockDriver *drv = bs->drv;
1844
    BlockDriverAIOCB *ret;
1845

    
1846
    if (!drv)
1847
        return NULL;
1848
    if (bdrv_check_request(bs, sector_num, nb_sectors))
1849
        return NULL;
1850

    
1851
    ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors,
1852
                              cb, opaque);
1853

    
1854
    if (ret) {
1855
        /* Update stats even though technically transfer has not happened. */
1856
        bs->rd_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE;
1857
        bs->rd_ops ++;
1858
    }
1859

    
1860
    return ret;
1861
}
1862

    
1863
BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
1864
                                  QEMUIOVector *qiov, int nb_sectors,
1865
                                  BlockDriverCompletionFunc *cb, void *opaque)
1866
{
1867
    BlockDriver *drv = bs->drv;
1868
    BlockDriverAIOCB *ret;
1869

    
1870
    if (!drv)
1871
        return NULL;
1872
    if (bs->read_only)
1873
        return NULL;
1874
    if (bdrv_check_request(bs, sector_num, nb_sectors))
1875
        return NULL;
1876

    
1877
    if (bs->dirty_bitmap) {
1878
        set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
1879
    }
1880

    
1881
    ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors,
1882
                               cb, opaque);
1883

    
1884
    if (ret) {
1885
        /* Update stats even though technically transfer has not happened. */
1886
        bs->wr_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE;
1887
        bs->wr_ops ++;
1888
        if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
1889
            bs->wr_highest_sector = sector_num + nb_sectors - 1;
1890
        }
1891
    }
1892

    
1893
    return ret;
1894
}
1895

    
1896

    
1897
typedef struct MultiwriteCB {
1898
    int error;
1899
    int num_requests;
1900
    int num_callbacks;
1901
    struct {
1902
        BlockDriverCompletionFunc *cb;
1903
        void *opaque;
1904
        QEMUIOVector *free_qiov;
1905
        void *free_buf;
1906
    } callbacks[];
1907
} MultiwriteCB;
1908

    
1909
static void multiwrite_user_cb(MultiwriteCB *mcb)
1910
{
1911
    int i;
1912

    
1913
    for (i = 0; i < mcb->num_callbacks; i++) {
1914
        mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error);
1915
        if (mcb->callbacks[i].free_qiov) {
1916
            qemu_iovec_destroy(mcb->callbacks[i].free_qiov);
1917
        }
1918
        qemu_free(mcb->callbacks[i].free_qiov);
1919
        qemu_vfree(mcb->callbacks[i].free_buf);
1920
    }
1921
}
1922

    
1923
static void multiwrite_cb(void *opaque, int ret)
1924
{
1925
    MultiwriteCB *mcb = opaque;
1926

    
1927
    if (ret < 0 && !mcb->error) {
1928
        mcb->error = ret;
1929
        multiwrite_user_cb(mcb);
1930
    }
1931

    
1932
    mcb->num_requests--;
1933
    if (mcb->num_requests == 0) {
1934
        if (mcb->error == 0) {
1935
            multiwrite_user_cb(mcb);
1936
        }
1937
        qemu_free(mcb);
1938
    }
1939
}
1940

    
1941
static int multiwrite_req_compare(const void *a, const void *b)
1942
{
1943
    const BlockRequest *req1 = a, *req2 = b;
1944

    
1945
    /*
1946
     * Note that we can't simply subtract req2->sector from req1->sector
1947
     * here as that could overflow the return value.
1948
     */
1949
    if (req1->sector > req2->sector) {
1950
        return 1;
1951
    } else if (req1->sector < req2->sector) {
1952
        return -1;
1953
    } else {
1954
        return 0;
1955
    }
1956
}
1957

    
1958
/*
1959
 * Takes a bunch of requests and tries to merge them. Returns the number of
1960
 * requests that remain after merging.
1961
 */
1962
static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs,
1963
    int num_reqs, MultiwriteCB *mcb)
1964
{
1965
    int i, outidx;
1966

    
1967
    // Sort requests by start sector
1968
    qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare);
1969

    
1970
    // Check if adjacent requests touch the same clusters. If so, combine them,
1971
    // filling up gaps with zero sectors.
1972
    outidx = 0;
1973
    for (i = 1; i < num_reqs; i++) {
1974
        int merge = 0;
1975
        int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors;
1976

    
1977
        // This handles the cases that are valid for all block drivers, namely
1978
        // exactly sequential writes and overlapping writes.
1979
        if (reqs[i].sector <= oldreq_last) {
1980
            merge = 1;
1981
        }
1982

    
1983
        // The block driver may decide that it makes sense to combine requests
1984
        // even if there is a gap of some sectors between them. In this case,
1985
        // the gap is filled with zeros (therefore only applicable for yet
1986
        // unused space in format like qcow2).
1987
        if (!merge && bs->drv->bdrv_merge_requests) {
1988
            merge = bs->drv->bdrv_merge_requests(bs, &reqs[outidx], &reqs[i]);
1989
        }
1990

    
1991
        if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) {
1992
            merge = 0;
1993
        }
1994

    
1995
        if (merge) {
1996
            size_t size;
1997
            QEMUIOVector *qiov = qemu_mallocz(sizeof(*qiov));
1998
            qemu_iovec_init(qiov,
1999
                reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1);
2000

    
2001
            // Add the first request to the merged one. If the requests are
2002
            // overlapping, drop the last sectors of the first request.
2003
            size = (reqs[i].sector - reqs[outidx].sector) << 9;
2004
            qemu_iovec_concat(qiov, reqs[outidx].qiov, size);
2005

    
2006
            // We might need to add some zeros between the two requests
2007
            if (reqs[i].sector > oldreq_last) {
2008
                size_t zero_bytes = (reqs[i].sector - oldreq_last) << 9;
2009
                uint8_t *buf = qemu_blockalign(bs, zero_bytes);
2010
                memset(buf, 0, zero_bytes);
2011
                qemu_iovec_add(qiov, buf, zero_bytes);
2012
                mcb->callbacks[i].free_buf = buf;
2013
            }
2014

    
2015
            // Add the second request
2016
            qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size);
2017

    
2018
            reqs[outidx].nb_sectors = qiov->size >> 9;
2019
            reqs[outidx].qiov = qiov;
2020

    
2021
            mcb->callbacks[i].free_qiov = reqs[outidx].qiov;
2022
        } else {
2023
            outidx++;
2024
            reqs[outidx].sector     = reqs[i].sector;
2025
            reqs[outidx].nb_sectors = reqs[i].nb_sectors;
2026
            reqs[outidx].qiov       = reqs[i].qiov;
2027
        }
2028
    }
2029

    
2030
    return outidx + 1;
2031
}
2032

    
2033
/*
2034
 * Submit multiple AIO write requests at once.
2035
 *
2036
 * On success, the function returns 0 and all requests in the reqs array have
2037
 * been submitted. In error case this function returns -1, and any of the
2038
 * requests may or may not be submitted yet. In particular, this means that the
2039
 * callback will be called for some of the requests, for others it won't. The
2040
 * caller must check the error field of the BlockRequest to wait for the right
2041
 * callbacks (if error != 0, no callback will be called).
2042
 *
2043
 * The implementation may modify the contents of the reqs array, e.g. to merge
2044
 * requests. However, the fields opaque and error are left unmodified as they
2045
 * are used to signal failure for a single request to the caller.
2046
 */
2047
int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs)
2048
{
2049
    BlockDriverAIOCB *acb;
2050
    MultiwriteCB *mcb;
2051
    int i;
2052

    
2053
    if (num_reqs == 0) {
2054
        return 0;
2055
    }
2056

    
2057
    // Create MultiwriteCB structure
2058
    mcb = qemu_mallocz(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks));
2059
    mcb->num_requests = 0;
2060
    mcb->num_callbacks = num_reqs;
2061

    
2062
    for (i = 0; i < num_reqs; i++) {
2063
        mcb->callbacks[i].cb = reqs[i].cb;
2064
        mcb->callbacks[i].opaque = reqs[i].opaque;
2065
    }
2066

    
2067
    // Check for mergable requests
2068
    num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb);
2069

    
2070
    // Run the aio requests
2071
    for (i = 0; i < num_reqs; i++) {
2072
        acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov,
2073
            reqs[i].nb_sectors, multiwrite_cb, mcb);
2074

    
2075
        if (acb == NULL) {
2076
            // We can only fail the whole thing if no request has been
2077
            // submitted yet. Otherwise we'll wait for the submitted AIOs to
2078
            // complete and report the error in the callback.
2079
            if (mcb->num_requests == 0) {
2080
                reqs[i].error = -EIO;
2081
                goto fail;
2082
            } else {
2083
                mcb->num_requests++;
2084
                multiwrite_cb(mcb, -EIO);
2085
                break;
2086
            }
2087
        } else {
2088
            mcb->num_requests++;
2089
        }
2090
    }
2091

    
2092
    return 0;
2093

    
2094
fail:
2095
    qemu_free(mcb);
2096
    return -1;
2097
}
2098

    
2099
BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs,
2100
        BlockDriverCompletionFunc *cb, void *opaque)
2101
{
2102
    BlockDriver *drv = bs->drv;
2103

    
2104
    if (bs->open_flags & BDRV_O_NO_FLUSH) {
2105
        return bdrv_aio_noop_em(bs, cb, opaque);
2106
    }
2107

    
2108
    if (!drv)
2109
        return NULL;
2110
    return drv->bdrv_aio_flush(bs, cb, opaque);
2111
}
2112

    
2113
void bdrv_aio_cancel(BlockDriverAIOCB *acb)
2114
{
2115
    acb->pool->cancel(acb);
2116
}
2117

    
2118

    
2119
/**************************************************************/
2120
/* async block device emulation */
2121

    
2122
typedef struct BlockDriverAIOCBSync {
2123
    BlockDriverAIOCB common;
2124
    QEMUBH *bh;
2125
    int ret;
2126
    /* vector translation state */
2127
    QEMUIOVector *qiov;
2128
    uint8_t *bounce;
2129
    int is_write;
2130
} BlockDriverAIOCBSync;
2131

    
2132
static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
2133
{
2134
    BlockDriverAIOCBSync *acb =
2135
        container_of(blockacb, BlockDriverAIOCBSync, common);
2136
    qemu_bh_delete(acb->bh);
2137
    acb->bh = NULL;
2138
    qemu_aio_release(acb);
2139
}
2140

    
2141
static AIOPool bdrv_em_aio_pool = {
2142
    .aiocb_size         = sizeof(BlockDriverAIOCBSync),
2143
    .cancel             = bdrv_aio_cancel_em,
2144
};
2145

    
2146
static void bdrv_aio_bh_cb(void *opaque)
2147
{
2148
    BlockDriverAIOCBSync *acb = opaque;
2149

    
2150
    if (!acb->is_write)
2151
        qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size);
2152
    qemu_vfree(acb->bounce);
2153
    acb->common.cb(acb->common.opaque, acb->ret);
2154
    qemu_bh_delete(acb->bh);
2155
    acb->bh = NULL;
2156
    qemu_aio_release(acb);
2157
}
2158

    
2159
static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
2160
                                            int64_t sector_num,
2161
                                            QEMUIOVector *qiov,
2162
                                            int nb_sectors,
2163
                                            BlockDriverCompletionFunc *cb,
2164
                                            void *opaque,
2165
                                            int is_write)
2166

    
2167
{
2168
    BlockDriverAIOCBSync *acb;
2169

    
2170
    acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2171
    acb->is_write = is_write;
2172
    acb->qiov = qiov;
2173
    acb->bounce = qemu_blockalign(bs, qiov->size);
2174

    
2175
    if (!acb->bh)
2176
        acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2177

    
2178
    if (is_write) {
2179
        qemu_iovec_to_buffer(acb->qiov, acb->bounce);
2180
        acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
2181
    } else {
2182
        acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors);
2183
    }
2184

    
2185
    qemu_bh_schedule(acb->bh);
2186

    
2187
    return &acb->common;
2188
}
2189

    
2190
static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
2191
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
2192
        BlockDriverCompletionFunc *cb, void *opaque)
2193
{
2194
    return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
2195
}
2196

    
2197
static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
2198
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
2199
        BlockDriverCompletionFunc *cb, void *opaque)
2200
{
2201
    return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
2202
}
2203

    
2204
static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
2205
        BlockDriverCompletionFunc *cb, void *opaque)
2206
{
2207
    BlockDriverAIOCBSync *acb;
2208

    
2209
    acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2210
    acb->is_write = 1; /* don't bounce in the completion hadler */
2211
    acb->qiov = NULL;
2212
    acb->bounce = NULL;
2213
    acb->ret = 0;
2214

    
2215
    if (!acb->bh)
2216
        acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2217

    
2218
    bdrv_flush(bs);
2219
    qemu_bh_schedule(acb->bh);
2220
    return &acb->common;
2221
}
2222

    
2223
static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs,
2224
        BlockDriverCompletionFunc *cb, void *opaque)
2225
{
2226
    BlockDriverAIOCBSync *acb;
2227

    
2228
    acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2229
    acb->is_write = 1; /* don't bounce in the completion handler */
2230
    acb->qiov = NULL;
2231
    acb->bounce = NULL;
2232
    acb->ret = 0;
2233

    
2234
    if (!acb->bh) {
2235
        acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2236
    }
2237

    
2238
    qemu_bh_schedule(acb->bh);
2239
    return &acb->common;
2240
}
2241

    
2242
/**************************************************************/
2243
/* sync block device emulation */
2244

    
2245
static void bdrv_rw_em_cb(void *opaque, int ret)
2246
{
2247
    *(int *)opaque = ret;
2248
}
2249

    
2250
#define NOT_DONE 0x7fffffff
2251

    
2252
static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
2253
                        uint8_t *buf, int nb_sectors)
2254
{
2255
    int async_ret;
2256
    BlockDriverAIOCB *acb;
2257
    struct iovec iov;
2258
    QEMUIOVector qiov;
2259

    
2260
    async_context_push();
2261

    
2262
    async_ret = NOT_DONE;
2263
    iov.iov_base = (void *)buf;
2264
    iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE;
2265
    qemu_iovec_init_external(&qiov, &iov, 1);
2266
    acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors,
2267
        bdrv_rw_em_cb, &async_ret);
2268
    if (acb == NULL) {
2269
        async_ret = -1;
2270
        goto fail;
2271
    }
2272

    
2273
    while (async_ret == NOT_DONE) {
2274
        qemu_aio_wait();
2275
    }
2276

    
2277

    
2278
fail:
2279
    async_context_pop();
2280
    return async_ret;
2281
}
2282

    
2283
static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
2284
                         const uint8_t *buf, int nb_sectors)
2285
{
2286
    int async_ret;
2287
    BlockDriverAIOCB *acb;
2288
    struct iovec iov;
2289
    QEMUIOVector qiov;
2290

    
2291
    async_context_push();
2292

    
2293
    async_ret = NOT_DONE;
2294
    iov.iov_base = (void *)buf;
2295
    iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE;
2296
    qemu_iovec_init_external(&qiov, &iov, 1);
2297
    acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors,
2298
        bdrv_rw_em_cb, &async_ret);
2299
    if (acb == NULL) {
2300
        async_ret = -1;
2301
        goto fail;
2302
    }
2303
    while (async_ret == NOT_DONE) {
2304
        qemu_aio_wait();
2305
    }
2306

    
2307
fail:
2308
    async_context_pop();
2309
    return async_ret;
2310
}
2311

    
2312
void bdrv_init(void)
2313
{
2314
    module_call_init(MODULE_INIT_BLOCK);
2315
}
2316

    
2317
void bdrv_init_with_whitelist(void)
2318
{
2319
    use_bdrv_whitelist = 1;
2320
    bdrv_init();
2321
}
2322

    
2323
void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs,
2324
                   BlockDriverCompletionFunc *cb, void *opaque)
2325
{
2326
    BlockDriverAIOCB *acb;
2327

    
2328
    if (pool->free_aiocb) {
2329
        acb = pool->free_aiocb;
2330
        pool->free_aiocb = acb->next;
2331
    } else {
2332
        acb = qemu_mallocz(pool->aiocb_size);
2333
        acb->pool = pool;
2334
    }
2335
    acb->bs = bs;
2336
    acb->cb = cb;
2337
    acb->opaque = opaque;
2338
    return acb;
2339
}
2340

    
2341
void qemu_aio_release(void *p)
2342
{
2343
    BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
2344
    AIOPool *pool = acb->pool;
2345
    acb->next = pool->free_aiocb;
2346
    pool->free_aiocb = acb;
2347
}
2348

    
2349
/**************************************************************/
2350
/* removable device support */
2351

    
2352
/**
2353
 * Return TRUE if the media is present
2354
 */
2355
int bdrv_is_inserted(BlockDriverState *bs)
2356
{
2357
    BlockDriver *drv = bs->drv;
2358
    int ret;
2359
    if (!drv)
2360
        return 0;
2361
    if (!drv->bdrv_is_inserted)
2362
        return 1;
2363
    ret = drv->bdrv_is_inserted(bs);
2364
    return ret;
2365
}
2366

    
2367
/**
2368
 * Return TRUE if the media changed since the last call to this
2369
 * function. It is currently only used for floppy disks
2370
 */
2371
int bdrv_media_changed(BlockDriverState *bs)
2372
{
2373
    BlockDriver *drv = bs->drv;
2374
    int ret;
2375

    
2376
    if (!drv || !drv->bdrv_media_changed)
2377
        ret = -ENOTSUP;
2378
    else
2379
        ret = drv->bdrv_media_changed(bs);
2380
    if (ret == -ENOTSUP)
2381
        ret = bs->media_changed;
2382
    bs->media_changed = 0;
2383
    return ret;
2384
}
2385

    
2386
/**
2387
 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
2388
 */
2389
int bdrv_eject(BlockDriverState *bs, int eject_flag)
2390
{
2391
    BlockDriver *drv = bs->drv;
2392
    int ret;
2393

    
2394
    if (bs->locked) {
2395
        return -EBUSY;
2396
    }
2397

    
2398
    if (!drv || !drv->bdrv_eject) {
2399
        ret = -ENOTSUP;
2400
    } else {
2401
        ret = drv->bdrv_eject(bs, eject_flag);
2402
    }
2403
    if (ret == -ENOTSUP) {
2404
        if (eject_flag)
2405
            bdrv_close(bs);
2406
        ret = 0;
2407
    }
2408

    
2409
    return ret;
2410
}
2411

    
2412
int bdrv_is_locked(BlockDriverState *bs)
2413
{
2414
    return bs->locked;
2415
}
2416

    
2417
/**
2418
 * Lock or unlock the media (if it is locked, the user won't be able
2419
 * to eject it manually).
2420
 */
2421
void bdrv_set_locked(BlockDriverState *bs, int locked)
2422
{
2423
    BlockDriver *drv = bs->drv;
2424

    
2425
    bs->locked = locked;
2426
    if (drv && drv->bdrv_set_locked) {
2427
        drv->bdrv_set_locked(bs, locked);
2428
    }
2429
}
2430

    
2431
/* needed for generic scsi interface */
2432

    
2433
int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
2434
{
2435
    BlockDriver *drv = bs->drv;
2436

    
2437
    if (drv && drv->bdrv_ioctl)
2438
        return drv->bdrv_ioctl(bs, req, buf);
2439
    return -ENOTSUP;
2440
}
2441

    
2442
BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
2443
        unsigned long int req, void *buf,
2444
        BlockDriverCompletionFunc *cb, void *opaque)
2445
{
2446
    BlockDriver *drv = bs->drv;
2447

    
2448
    if (drv && drv->bdrv_aio_ioctl)
2449
        return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
2450
    return NULL;
2451
}
2452

    
2453

    
2454

    
2455
void *qemu_blockalign(BlockDriverState *bs, size_t size)
2456
{
2457
    return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size);
2458
}
2459

    
2460
void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable)
2461
{
2462
    int64_t bitmap_size;
2463

    
2464
    bs->dirty_count = 0;
2465
    if (enable) {
2466
        if (!bs->dirty_bitmap) {
2467
            bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) +
2468
                    BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
2469
            bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
2470

    
2471
            bs->dirty_bitmap = qemu_mallocz(bitmap_size);
2472
        }
2473
    } else {
2474
        if (bs->dirty_bitmap) {
2475
            qemu_free(bs->dirty_bitmap);
2476
            bs->dirty_bitmap = NULL;
2477
        }
2478
    }
2479
}
2480

    
2481
int bdrv_get_dirty(BlockDriverState *bs, int64_t sector)
2482
{
2483
    int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
2484

    
2485
    if (bs->dirty_bitmap &&
2486
        (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bs)) {
2487
        return bs->dirty_bitmap[chunk / (sizeof(unsigned long) * 8)] &
2488
            (1 << (chunk % (sizeof(unsigned long) * 8)));
2489
    } else {
2490
        return 0;
2491
    }
2492
}
2493

    
2494
void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector,
2495
                      int nr_sectors)
2496
{
2497
    set_dirty_bitmap(bs, cur_sector, nr_sectors, 0);
2498
}
2499

    
2500
int64_t bdrv_get_dirty_count(BlockDriverState *bs)
2501
{
2502
    return bs->dirty_count;
2503
}