Statistics
| Branch: | Revision:

root / block.c @ de189a1b

History | View | Annotate | Download (69.6 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
/* The device to use for VM snapshots */
67
static BlockDriverState *bs_snapshots;
68

    
69
/* If non-zero, use only whitelisted block drivers */
70
static int use_bdrv_whitelist;
71

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

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

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

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

    
148
    if (!bdrv->bdrv_aio_flush)
149
        bdrv->bdrv_aio_flush = bdrv_aio_flush_em;
150

    
151
    QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list);
152
}
153

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

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

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

    
178
static int bdrv_is_whitelisted(BlockDriver *drv)
179
{
180
    static const char *whitelist[] = {
181
        CONFIG_BDRV_WHITELIST
182
    };
183
    const char **p;
184

    
185
    if (!whitelist[0])
186
        return 1;               /* no whitelist, anything goes */
187

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

    
196
BlockDriver *bdrv_find_whitelisted_format(const char *format_name)
197
{
198
    BlockDriver *drv = bdrv_find_format(format_name);
199
    return drv && bdrv_is_whitelisted(drv) ? drv : NULL;
200
}
201

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

    
208
    return drv->bdrv_create(filename, options);
209
}
210

    
211
int bdrv_create_file(const char* filename, QEMUOptionParameter *options)
212
{
213
    BlockDriver *drv;
214

    
215
    drv = bdrv_find_protocol(filename);
216
    if (drv == NULL) {
217
        drv = bdrv_find_format("file");
218
    }
219

    
220
    return bdrv_create(drv, filename, options);
221
}
222

    
223
#ifdef _WIN32
224
void get_tmp_filename(char *filename, int size)
225
{
226
    char temp_dir[MAX_PATH];
227

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

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

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

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

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

    
285
    return drv;
286
}
287

    
288
BlockDriver *bdrv_find_protocol(const char *filename)
289
{
290
    BlockDriver *drv1;
291
    char protocol[128];
292
    int len;
293
    const char *p;
294

    
295
    /* TODO Drivers without bdrv_file_open must be specified explicitly */
296

    
297
    /*
298
     * XXX(hch): we really should not let host device detection
299
     * override an explicit protocol specification, but moving this
300
     * later breaks access to device names with colons in them.
301
     * Thanks to the brain-dead persistent naming schemes on udev-
302
     * based Linux systems those actually are quite common.
303
     */
304
    drv1 = find_hdev_driver(filename);
305
    if (drv1) {
306
        return drv1;
307
    }
308

    
309
#ifdef _WIN32
310
     if (is_windows_drive(filename) ||
311
         is_windows_drive_prefix(filename))
312
         return bdrv_find_format("file");
313
#endif
314

    
315
    p = strchr(filename, ':');
316
    if (!p) {
317
        return bdrv_find_format("file");
318
    }
319
    len = p - filename;
320
    if (len > sizeof(protocol) - 1)
321
        len = sizeof(protocol) - 1;
322
    memcpy(protocol, filename, len);
323
    protocol[len] = '\0';
324
    QLIST_FOREACH(drv1, &bdrv_drivers, list) {
325
        if (drv1->protocol_name &&
326
            !strcmp(drv1->protocol_name, protocol)) {
327
            return drv1;
328
        }
329
    }
330
    return NULL;
331
}
332

    
333
static BlockDriver *find_image_format(const char *filename)
334
{
335
    int ret, score, score_max;
336
    BlockDriver *drv1, *drv;
337
    uint8_t buf[2048];
338
    BlockDriverState *bs;
339

    
340
    ret = bdrv_file_open(&bs, filename, 0);
341
    if (ret < 0)
342
        return NULL;
343

    
344
    /* Return the raw BlockDriver * to scsi-generic devices or empty drives */
345
    if (bs->sg || !bdrv_is_inserted(bs)) {
346
        bdrv_delete(bs);
347
        return bdrv_find_format("raw");
348
    }
349

    
350
    ret = bdrv_pread(bs, 0, buf, sizeof(buf));
351
    bdrv_delete(bs);
352
    if (ret < 0) {
353
        return NULL;
354
    }
355

    
356
    score_max = 0;
357
    drv = NULL;
358
    QLIST_FOREACH(drv1, &bdrv_drivers, list) {
359
        if (drv1->bdrv_probe) {
360
            score = drv1->bdrv_probe(buf, ret, filename);
361
            if (score > score_max) {
362
                score_max = score;
363
                drv = drv1;
364
            }
365
        }
366
    }
367
    return drv;
368
}
369

    
370
/**
371
 * Set the current 'total_sectors' value
372
 */
373
static int refresh_total_sectors(BlockDriverState *bs, int64_t hint)
374
{
375
    BlockDriver *drv = bs->drv;
376

    
377
    /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */
378
    if (bs->sg)
379
        return 0;
380

    
381
    /* query actual device if possible, otherwise just trust the hint */
382
    if (drv->bdrv_getlength) {
383
        int64_t length = drv->bdrv_getlength(bs);
384
        if (length < 0) {
385
            return length;
386
        }
387
        hint = length >> BDRV_SECTOR_BITS;
388
    }
389

    
390
    bs->total_sectors = hint;
391
    return 0;
392
}
393

    
394
/*
395
 * Common part for opening disk images and files
396
 */
397
static int bdrv_open_common(BlockDriverState *bs, const char *filename,
398
    int flags, BlockDriver *drv)
399
{
400
    int ret, open_flags;
401

    
402
    assert(drv != NULL);
403

    
404
    bs->file = NULL;
405
    bs->total_sectors = 0;
406
    bs->encrypted = 0;
407
    bs->valid_key = 0;
408
    bs->open_flags = flags;
409
    /* buffer_alignment defaulted to 512, drivers can change this value */
410
    bs->buffer_alignment = 512;
411

    
412
    pstrcpy(bs->filename, sizeof(bs->filename), filename);
413

    
414
    if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) {
415
        return -ENOTSUP;
416
    }
417

    
418
    bs->drv = drv;
419
    bs->opaque = qemu_mallocz(drv->instance_size);
420

    
421
    /*
422
     * Yes, BDRV_O_NOCACHE aka O_DIRECT means we have to present a
423
     * write cache to the guest.  We do need the fdatasync to flush
424
     * out transactions for block allocations, and we maybe have a
425
     * volatile write cache in our backing device to deal with.
426
     */
427
    if (flags & (BDRV_O_CACHE_WB|BDRV_O_NOCACHE))
428
        bs->enable_write_cache = 1;
429

    
430
    /*
431
     * Clear flags that are internal to the block layer before opening the
432
     * image.
433
     */
434
    open_flags = flags & ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
435

    
436
    /*
437
     * Snapshots should be writeable.
438
     */
439
    if (bs->is_temporary) {
440
        open_flags |= BDRV_O_RDWR;
441
    }
442

    
443
    /* Open the image, either directly or using a protocol */
444
    if (drv->bdrv_file_open) {
445
        ret = drv->bdrv_file_open(bs, filename, open_flags);
446
    } else {
447
        ret = bdrv_file_open(&bs->file, filename, open_flags);
448
        if (ret >= 0) {
449
            ret = drv->bdrv_open(bs, open_flags);
450
        }
451
    }
452

    
453
    if (ret < 0) {
454
        goto free_and_fail;
455
    }
456

    
457
    bs->keep_read_only = bs->read_only = !(open_flags & BDRV_O_RDWR);
458

    
459
    ret = refresh_total_sectors(bs, bs->total_sectors);
460
    if (ret < 0) {
461
        goto free_and_fail;
462
    }
463

    
464
#ifndef _WIN32
465
    if (bs->is_temporary) {
466
        unlink(filename);
467
    }
468
#endif
469
    return 0;
470

    
471
free_and_fail:
472
    if (bs->file) {
473
        bdrv_delete(bs->file);
474
        bs->file = NULL;
475
    }
476
    qemu_free(bs->opaque);
477
    bs->opaque = NULL;
478
    bs->drv = NULL;
479
    return ret;
480
}
481

    
482
/*
483
 * Opens a file using a protocol (file, host_device, nbd, ...)
484
 */
485
int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
486
{
487
    BlockDriverState *bs;
488
    BlockDriver *drv;
489
    int ret;
490

    
491
    drv = bdrv_find_protocol(filename);
492
    if (!drv) {
493
        return -ENOENT;
494
    }
495

    
496
    bs = bdrv_new("");
497
    ret = bdrv_open_common(bs, filename, flags, drv);
498
    if (ret < 0) {
499
        bdrv_delete(bs);
500
        return ret;
501
    }
502
    bs->growable = 1;
503
    *pbs = bs;
504
    return 0;
505
}
506

    
507
/*
508
 * Opens a disk image (raw, qcow2, vmdk, ...)
509
 */
510
int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
511
              BlockDriver *drv)
512
{
513
    int ret;
514

    
515
    if (flags & BDRV_O_SNAPSHOT) {
516
        BlockDriverState *bs1;
517
        int64_t total_size;
518
        int is_protocol = 0;
519
        BlockDriver *bdrv_qcow2;
520
        QEMUOptionParameter *options;
521
        char tmp_filename[PATH_MAX];
522
        char backing_filename[PATH_MAX];
523

    
524
        /* if snapshot, we create a temporary backing file and open it
525
           instead of opening 'filename' directly */
526

    
527
        /* if there is a backing file, use it */
528
        bs1 = bdrv_new("");
529
        ret = bdrv_open(bs1, filename, 0, drv);
530
        if (ret < 0) {
531
            bdrv_delete(bs1);
532
            return ret;
533
        }
534
        total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK;
535

    
536
        if (bs1->drv && bs1->drv->protocol_name)
537
            is_protocol = 1;
538

    
539
        bdrv_delete(bs1);
540

    
541
        get_tmp_filename(tmp_filename, sizeof(tmp_filename));
542

    
543
        /* Real path is meaningless for protocols */
544
        if (is_protocol)
545
            snprintf(backing_filename, sizeof(backing_filename),
546
                     "%s", filename);
547
        else if (!realpath(filename, backing_filename))
548
            return -errno;
549

    
550
        bdrv_qcow2 = bdrv_find_format("qcow2");
551
        options = parse_option_parameters("", bdrv_qcow2->create_options, NULL);
552

    
553
        set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size);
554
        set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename);
555
        if (drv) {
556
            set_option_parameter(options, BLOCK_OPT_BACKING_FMT,
557
                drv->format_name);
558
        }
559

    
560
        ret = bdrv_create(bdrv_qcow2, tmp_filename, options);
561
        free_option_parameters(options);
562
        if (ret < 0) {
563
            return ret;
564
        }
565

    
566
        filename = tmp_filename;
567
        drv = bdrv_qcow2;
568
        bs->is_temporary = 1;
569
    }
570

    
571
    /* Find the right image format driver */
572
    if (!drv) {
573
        drv = find_image_format(filename);
574
    }
575

    
576
    if (!drv) {
577
        ret = -ENOENT;
578
        goto unlink_and_fail;
579
    }
580

    
581
    /* Open the image */
582
    ret = bdrv_open_common(bs, filename, flags, drv);
583
    if (ret < 0) {
584
        goto unlink_and_fail;
585
    }
586

    
587
    /* If there is a backing file, use it */
588
    if ((flags & BDRV_O_NO_BACKING) == 0 && bs->backing_file[0] != '\0') {
589
        char backing_filename[PATH_MAX];
590
        int back_flags;
591
        BlockDriver *back_drv = NULL;
592

    
593
        bs->backing_hd = bdrv_new("");
594
        path_combine(backing_filename, sizeof(backing_filename),
595
                     filename, bs->backing_file);
596
        if (bs->backing_format[0] != '\0')
597
            back_drv = bdrv_find_format(bs->backing_format);
598

    
599
        /* backing files always opened read-only */
600
        back_flags =
601
            flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
602

    
603
        ret = bdrv_open(bs->backing_hd, backing_filename, back_flags, back_drv);
604
        if (ret < 0) {
605
            bdrv_close(bs);
606
            return ret;
607
        }
608
        if (bs->is_temporary) {
609
            bs->backing_hd->keep_read_only = !(flags & BDRV_O_RDWR);
610
        } else {
611
            /* base image inherits from "parent" */
612
            bs->backing_hd->keep_read_only = bs->keep_read_only;
613
        }
614
    }
615

    
616
    if (!bdrv_key_required(bs)) {
617
        /* call the change callback */
618
        bs->media_changed = 1;
619
        if (bs->change_cb)
620
            bs->change_cb(bs->change_opaque);
621
    }
622

    
623
    return 0;
624

    
625
unlink_and_fail:
626
    if (bs->is_temporary) {
627
        unlink(filename);
628
    }
629
    return ret;
630
}
631

    
632
void bdrv_close(BlockDriverState *bs)
633
{
634
    if (bs->drv) {
635
        if (bs == bs_snapshots) {
636
            bs_snapshots = NULL;
637
        }
638
        if (bs->backing_hd) {
639
            bdrv_delete(bs->backing_hd);
640
            bs->backing_hd = NULL;
641
        }
642
        bs->drv->bdrv_close(bs);
643
        qemu_free(bs->opaque);
644
#ifdef _WIN32
645
        if (bs->is_temporary) {
646
            unlink(bs->filename);
647
        }
648
#endif
649
        bs->opaque = NULL;
650
        bs->drv = NULL;
651

    
652
        if (bs->file != NULL) {
653
            bdrv_close(bs->file);
654
        }
655

    
656
        /* call the change callback */
657
        bs->media_changed = 1;
658
        if (bs->change_cb)
659
            bs->change_cb(bs->change_opaque);
660
    }
661
}
662

    
663
void bdrv_close_all(void)
664
{
665
    BlockDriverState *bs;
666

    
667
    QTAILQ_FOREACH(bs, &bdrv_states, list) {
668
        bdrv_close(bs);
669
    }
670
}
671

    
672
void bdrv_delete(BlockDriverState *bs)
673
{
674
    assert(!bs->peer);
675

    
676
    /* remove from list, if necessary */
677
    if (bs->device_name[0] != '\0') {
678
        QTAILQ_REMOVE(&bdrv_states, bs, list);
679
    }
680

    
681
    bdrv_close(bs);
682
    if (bs->file != NULL) {
683
        bdrv_delete(bs->file);
684
    }
685

    
686
    assert(bs != bs_snapshots);
687
    qemu_free(bs);
688
}
689

    
690
int bdrv_attach(BlockDriverState *bs, DeviceState *qdev)
691
{
692
    if (bs->peer) {
693
        return -EBUSY;
694
    }
695
    bs->peer = qdev;
696
    return 0;
697
}
698

    
699
void bdrv_detach(BlockDriverState *bs, DeviceState *qdev)
700
{
701
    assert(bs->peer == qdev);
702
    bs->peer = NULL;
703
}
704

    
705
DeviceState *bdrv_get_attached(BlockDriverState *bs)
706
{
707
    return bs->peer;
708
}
709

    
710
/*
711
 * Run consistency checks on an image
712
 *
713
 * Returns the number of errors or -errno when an internal error occurs
714
 */
715
int bdrv_check(BlockDriverState *bs)
716
{
717
    if (bs->drv->bdrv_check == NULL) {
718
        return -ENOTSUP;
719
    }
720

    
721
    return bs->drv->bdrv_check(bs);
722
}
723

    
724
/* commit COW file into the raw image */
725
int bdrv_commit(BlockDriverState *bs)
726
{
727
    BlockDriver *drv = bs->drv;
728
    int64_t i, total_sectors;
729
    int n, j, ro, open_flags;
730
    int ret = 0, rw_ret = 0;
731
    unsigned char sector[BDRV_SECTOR_SIZE];
732
    char filename[1024];
733
    BlockDriverState *bs_rw, *bs_ro;
734

    
735
    if (!drv)
736
        return -ENOMEDIUM;
737
    
738
    if (!bs->backing_hd) {
739
        return -ENOTSUP;
740
    }
741

    
742
    if (bs->backing_hd->keep_read_only) {
743
        return -EACCES;
744
    }
745
    
746
    ro = bs->backing_hd->read_only;
747
    strncpy(filename, bs->backing_hd->filename, sizeof(filename));
748
    open_flags =  bs->backing_hd->open_flags;
749

    
750
    if (ro) {
751
        /* re-open as RW */
752
        bdrv_delete(bs->backing_hd);
753
        bs->backing_hd = NULL;
754
        bs_rw = bdrv_new("");
755
        rw_ret = bdrv_open(bs_rw, filename, open_flags | BDRV_O_RDWR, drv);
756
        if (rw_ret < 0) {
757
            bdrv_delete(bs_rw);
758
            /* try to re-open read-only */
759
            bs_ro = bdrv_new("");
760
            ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, drv);
761
            if (ret < 0) {
762
                bdrv_delete(bs_ro);
763
                /* drive not functional anymore */
764
                bs->drv = NULL;
765
                return ret;
766
            }
767
            bs->backing_hd = bs_ro;
768
            return rw_ret;
769
        }
770
        bs->backing_hd = bs_rw;
771
    }
772

    
773
    total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
774
    for (i = 0; i < total_sectors;) {
775
        if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
776
            for(j = 0; j < n; j++) {
777
                if (bdrv_read(bs, i, sector, 1) != 0) {
778
                    ret = -EIO;
779
                    goto ro_cleanup;
780
                }
781

    
782
                if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
783
                    ret = -EIO;
784
                    goto ro_cleanup;
785
                }
786
                i++;
787
            }
788
        } else {
789
            i += n;
790
        }
791
    }
792

    
793
    if (drv->bdrv_make_empty) {
794
        ret = drv->bdrv_make_empty(bs);
795
        bdrv_flush(bs);
796
    }
797

    
798
    /*
799
     * Make sure all data we wrote to the backing device is actually
800
     * stable on disk.
801
     */
802
    if (bs->backing_hd)
803
        bdrv_flush(bs->backing_hd);
804

    
805
ro_cleanup:
806

    
807
    if (ro) {
808
        /* re-open as RO */
809
        bdrv_delete(bs->backing_hd);
810
        bs->backing_hd = NULL;
811
        bs_ro = bdrv_new("");
812
        ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, drv);
813
        if (ret < 0) {
814
            bdrv_delete(bs_ro);
815
            /* drive not functional anymore */
816
            bs->drv = NULL;
817
            return ret;
818
        }
819
        bs->backing_hd = bs_ro;
820
        bs->backing_hd->keep_read_only = 0;
821
    }
822

    
823
    return ret;
824
}
825

    
826
void bdrv_commit_all(void)
827
{
828
    BlockDriverState *bs;
829

    
830
    QTAILQ_FOREACH(bs, &bdrv_states, list) {
831
        bdrv_commit(bs);
832
    }
833
}
834

    
835
/*
836
 * Return values:
837
 * 0        - success
838
 * -EINVAL  - backing format specified, but no file
839
 * -ENOSPC  - can't update the backing file because no space is left in the
840
 *            image file header
841
 * -ENOTSUP - format driver doesn't support changing the backing file
842
 */
843
int bdrv_change_backing_file(BlockDriverState *bs,
844
    const char *backing_file, const char *backing_fmt)
845
{
846
    BlockDriver *drv = bs->drv;
847

    
848
    if (drv->bdrv_change_backing_file != NULL) {
849
        return drv->bdrv_change_backing_file(bs, backing_file, backing_fmt);
850
    } else {
851
        return -ENOTSUP;
852
    }
853
}
854

    
855
static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
856
                                   size_t size)
857
{
858
    int64_t len;
859

    
860
    if (!bdrv_is_inserted(bs))
861
        return -ENOMEDIUM;
862

    
863
    if (bs->growable)
864
        return 0;
865

    
866
    len = bdrv_getlength(bs);
867

    
868
    if (offset < 0)
869
        return -EIO;
870

    
871
    if ((offset > len) || (len - offset < size))
872
        return -EIO;
873

    
874
    return 0;
875
}
876

    
877
static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
878
                              int nb_sectors)
879
{
880
    return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE,
881
                                   nb_sectors * BDRV_SECTOR_SIZE);
882
}
883

    
884
/* return < 0 if error. See bdrv_write() for the return codes */
885
int bdrv_read(BlockDriverState *bs, int64_t sector_num,
886
              uint8_t *buf, int nb_sectors)
887
{
888
    BlockDriver *drv = bs->drv;
889

    
890
    if (!drv)
891
        return -ENOMEDIUM;
892
    if (bdrv_check_request(bs, sector_num, nb_sectors))
893
        return -EIO;
894

    
895
    return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
896
}
897

    
898
static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num,
899
                             int nb_sectors, int dirty)
900
{
901
    int64_t start, end;
902
    unsigned long val, idx, bit;
903

    
904
    start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK;
905
    end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
906

    
907
    for (; start <= end; start++) {
908
        idx = start / (sizeof(unsigned long) * 8);
909
        bit = start % (sizeof(unsigned long) * 8);
910
        val = bs->dirty_bitmap[idx];
911
        if (dirty) {
912
            if (!(val & (1 << bit))) {
913
                bs->dirty_count++;
914
                val |= 1 << bit;
915
            }
916
        } else {
917
            if (val & (1 << bit)) {
918
                bs->dirty_count--;
919
                val &= ~(1 << bit);
920
            }
921
        }
922
        bs->dirty_bitmap[idx] = val;
923
    }
924
}
925

    
926
/* Return < 0 if error. Important errors are:
927
  -EIO         generic I/O error (may happen for all errors)
928
  -ENOMEDIUM   No media inserted.
929
  -EINVAL      Invalid sector number or nb_sectors
930
  -EACCES      Trying to write a read-only device
931
*/
932
int bdrv_write(BlockDriverState *bs, int64_t sector_num,
933
               const uint8_t *buf, int nb_sectors)
934
{
935
    BlockDriver *drv = bs->drv;
936
    if (!bs->drv)
937
        return -ENOMEDIUM;
938
    if (bs->read_only)
939
        return -EACCES;
940
    if (bdrv_check_request(bs, sector_num, nb_sectors))
941
        return -EIO;
942

    
943
    if (bs->dirty_bitmap) {
944
        set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
945
    }
946

    
947
    if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
948
        bs->wr_highest_sector = sector_num + nb_sectors - 1;
949
    }
950

    
951
    return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
952
}
953

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

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

    
979
    /* read the sectors "in place" */
980
    nb_sectors = count >> BDRV_SECTOR_BITS;
981
    if (nb_sectors > 0) {
982
        if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0)
983
            return ret;
984
        sector_num += nb_sectors;
985
        len = nb_sectors << BDRV_SECTOR_BITS;
986
        buf += len;
987
        count -= len;
988
    }
989

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

    
999
int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
1000
                const void *buf, int count1)
1001
{
1002
    uint8_t tmp_buf[BDRV_SECTOR_SIZE];
1003
    int len, nb_sectors, count;
1004
    int64_t sector_num;
1005
    int ret;
1006

    
1007
    count = count1;
1008
    /* first write to align to sector start */
1009
    len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
1010
    if (len > count)
1011
        len = count;
1012
    sector_num = offset >> BDRV_SECTOR_BITS;
1013
    if (len > 0) {
1014
        if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1015
            return ret;
1016
        memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len);
1017
        if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
1018
            return ret;
1019
        count -= len;
1020
        if (count == 0)
1021
            return count1;
1022
        sector_num++;
1023
        buf += len;
1024
    }
1025

    
1026
    /* write the sectors "in place" */
1027
    nb_sectors = count >> BDRV_SECTOR_BITS;
1028
    if (nb_sectors > 0) {
1029
        if ((ret = bdrv_write(bs, sector_num, buf, nb_sectors)) < 0)
1030
            return ret;
1031
        sector_num += nb_sectors;
1032
        len = nb_sectors << BDRV_SECTOR_BITS;
1033
        buf += len;
1034
        count -= len;
1035
    }
1036

    
1037
    /* add data from the last sector */
1038
    if (count > 0) {
1039
        if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1040
            return ret;
1041
        memcpy(tmp_buf, buf, count);
1042
        if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
1043
            return ret;
1044
    }
1045
    return count1;
1046
}
1047

    
1048
/*
1049
 * Writes to the file and ensures that no writes are reordered across this
1050
 * request (acts as a barrier)
1051
 *
1052
 * Returns 0 on success, -errno in error cases.
1053
 */
1054
int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset,
1055
    const void *buf, int count)
1056
{
1057
    int ret;
1058

    
1059
    ret = bdrv_pwrite(bs, offset, buf, count);
1060
    if (ret < 0) {
1061
        return ret;
1062
    }
1063

    
1064
    /* No flush needed for cache=writethrough, it uses O_DSYNC */
1065
    if ((bs->open_flags & BDRV_O_CACHE_MASK) != 0) {
1066
        bdrv_flush(bs);
1067
    }
1068

    
1069
    return 0;
1070
}
1071

    
1072
/*
1073
 * Writes to the file and ensures that no writes are reordered across this
1074
 * request (acts as a barrier)
1075
 *
1076
 * Returns 0 on success, -errno in error cases.
1077
 */
1078
int bdrv_write_sync(BlockDriverState *bs, int64_t sector_num,
1079
    const uint8_t *buf, int nb_sectors)
1080
{
1081
    return bdrv_pwrite_sync(bs, BDRV_SECTOR_SIZE * sector_num,
1082
        buf, BDRV_SECTOR_SIZE * nb_sectors);
1083
}
1084

    
1085
/**
1086
 * Truncate file to 'offset' bytes (needed only for file protocols)
1087
 */
1088
int bdrv_truncate(BlockDriverState *bs, int64_t offset)
1089
{
1090
    BlockDriver *drv = bs->drv;
1091
    int ret;
1092
    if (!drv)
1093
        return -ENOMEDIUM;
1094
    if (!drv->bdrv_truncate)
1095
        return -ENOTSUP;
1096
    if (bs->read_only)
1097
        return -EACCES;
1098
    ret = drv->bdrv_truncate(bs, offset);
1099
    if (ret == 0) {
1100
        ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
1101
    }
1102
    return ret;
1103
}
1104

    
1105
/**
1106
 * Length of a file in bytes. Return < 0 if error or unknown.
1107
 */
1108
int64_t bdrv_getlength(BlockDriverState *bs)
1109
{
1110
    BlockDriver *drv = bs->drv;
1111
    if (!drv)
1112
        return -ENOMEDIUM;
1113

    
1114
    /* Fixed size devices use the total_sectors value for speed instead of
1115
       issuing a length query (like lseek) on each call.  Also, legacy block
1116
       drivers don't provide a bdrv_getlength function and must use
1117
       total_sectors. */
1118
    if (!bs->growable || !drv->bdrv_getlength) {
1119
        return bs->total_sectors * BDRV_SECTOR_SIZE;
1120
    }
1121
    return drv->bdrv_getlength(bs);
1122
}
1123

    
1124
/* return 0 as number of sectors if no device present or error */
1125
void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
1126
{
1127
    int64_t length;
1128
    length = bdrv_getlength(bs);
1129
    if (length < 0)
1130
        length = 0;
1131
    else
1132
        length = length >> BDRV_SECTOR_BITS;
1133
    *nb_sectors_ptr = length;
1134
}
1135

    
1136
struct partition {
1137
        uint8_t boot_ind;           /* 0x80 - active */
1138
        uint8_t head;               /* starting head */
1139
        uint8_t sector;             /* starting sector */
1140
        uint8_t cyl;                /* starting cylinder */
1141
        uint8_t sys_ind;            /* What partition type */
1142
        uint8_t end_head;           /* end head */
1143
        uint8_t end_sector;         /* end sector */
1144
        uint8_t end_cyl;            /* end cylinder */
1145
        uint32_t start_sect;        /* starting sector counting from 0 */
1146
        uint32_t nr_sects;          /* nr of sectors in partition */
1147
} __attribute__((packed));
1148

    
1149
/* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
1150
static int guess_disk_lchs(BlockDriverState *bs,
1151
                           int *pcylinders, int *pheads, int *psectors)
1152
{
1153
    uint8_t buf[BDRV_SECTOR_SIZE];
1154
    int ret, i, heads, sectors, cylinders;
1155
    struct partition *p;
1156
    uint32_t nr_sects;
1157
    uint64_t nb_sectors;
1158

    
1159
    bdrv_get_geometry(bs, &nb_sectors);
1160

    
1161
    ret = bdrv_read(bs, 0, buf, 1);
1162
    if (ret < 0)
1163
        return -1;
1164
    /* test msdos magic */
1165
    if (buf[510] != 0x55 || buf[511] != 0xaa)
1166
        return -1;
1167
    for(i = 0; i < 4; i++) {
1168
        p = ((struct partition *)(buf + 0x1be)) + i;
1169
        nr_sects = le32_to_cpu(p->nr_sects);
1170
        if (nr_sects && p->end_head) {
1171
            /* We make the assumption that the partition terminates on
1172
               a cylinder boundary */
1173
            heads = p->end_head + 1;
1174
            sectors = p->end_sector & 63;
1175
            if (sectors == 0)
1176
                continue;
1177
            cylinders = nb_sectors / (heads * sectors);
1178
            if (cylinders < 1 || cylinders > 16383)
1179
                continue;
1180
            *pheads = heads;
1181
            *psectors = sectors;
1182
            *pcylinders = cylinders;
1183
#if 0
1184
            printf("guessed geometry: LCHS=%d %d %d\n",
1185
                   cylinders, heads, sectors);
1186
#endif
1187
            return 0;
1188
        }
1189
    }
1190
    return -1;
1191
}
1192

    
1193
void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
1194
{
1195
    int translation, lba_detected = 0;
1196
    int cylinders, heads, secs;
1197
    uint64_t nb_sectors;
1198

    
1199
    /* if a geometry hint is available, use it */
1200
    bdrv_get_geometry(bs, &nb_sectors);
1201
    bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
1202
    translation = bdrv_get_translation_hint(bs);
1203
    if (cylinders != 0) {
1204
        *pcyls = cylinders;
1205
        *pheads = heads;
1206
        *psecs = secs;
1207
    } else {
1208
        if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
1209
            if (heads > 16) {
1210
                /* if heads > 16, it means that a BIOS LBA
1211
                   translation was active, so the default
1212
                   hardware geometry is OK */
1213
                lba_detected = 1;
1214
                goto default_geometry;
1215
            } else {
1216
                *pcyls = cylinders;
1217
                *pheads = heads;
1218
                *psecs = secs;
1219
                /* disable any translation to be in sync with
1220
                   the logical geometry */
1221
                if (translation == BIOS_ATA_TRANSLATION_AUTO) {
1222
                    bdrv_set_translation_hint(bs,
1223
                                              BIOS_ATA_TRANSLATION_NONE);
1224
                }
1225
            }
1226
        } else {
1227
        default_geometry:
1228
            /* if no geometry, use a standard physical disk geometry */
1229
            cylinders = nb_sectors / (16 * 63);
1230

    
1231
            if (cylinders > 16383)
1232
                cylinders = 16383;
1233
            else if (cylinders < 2)
1234
                cylinders = 2;
1235
            *pcyls = cylinders;
1236
            *pheads = 16;
1237
            *psecs = 63;
1238
            if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
1239
                if ((*pcyls * *pheads) <= 131072) {
1240
                    bdrv_set_translation_hint(bs,
1241
                                              BIOS_ATA_TRANSLATION_LARGE);
1242
                } else {
1243
                    bdrv_set_translation_hint(bs,
1244
                                              BIOS_ATA_TRANSLATION_LBA);
1245
                }
1246
            }
1247
        }
1248
        bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
1249
    }
1250
}
1251

    
1252
void bdrv_set_geometry_hint(BlockDriverState *bs,
1253
                            int cyls, int heads, int secs)
1254
{
1255
    bs->cyls = cyls;
1256
    bs->heads = heads;
1257
    bs->secs = secs;
1258
}
1259

    
1260
void bdrv_set_type_hint(BlockDriverState *bs, int type)
1261
{
1262
    bs->type = type;
1263
    bs->removable = ((type == BDRV_TYPE_CDROM ||
1264
                      type == BDRV_TYPE_FLOPPY));
1265
}
1266

    
1267
void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
1268
{
1269
    bs->translation = translation;
1270
}
1271

    
1272
void bdrv_get_geometry_hint(BlockDriverState *bs,
1273
                            int *pcyls, int *pheads, int *psecs)
1274
{
1275
    *pcyls = bs->cyls;
1276
    *pheads = bs->heads;
1277
    *psecs = bs->secs;
1278
}
1279

    
1280
int bdrv_get_type_hint(BlockDriverState *bs)
1281
{
1282
    return bs->type;
1283
}
1284

    
1285
int bdrv_get_translation_hint(BlockDriverState *bs)
1286
{
1287
    return bs->translation;
1288
}
1289

    
1290
void bdrv_set_on_error(BlockDriverState *bs, BlockErrorAction on_read_error,
1291
                       BlockErrorAction on_write_error)
1292
{
1293
    bs->on_read_error = on_read_error;
1294
    bs->on_write_error = on_write_error;
1295
}
1296

    
1297
BlockErrorAction bdrv_get_on_error(BlockDriverState *bs, int is_read)
1298
{
1299
    return is_read ? bs->on_read_error : bs->on_write_error;
1300
}
1301

    
1302
void bdrv_set_removable(BlockDriverState *bs, int removable)
1303
{
1304
    bs->removable = removable;
1305
    if (removable && bs == bs_snapshots) {
1306
        bs_snapshots = NULL;
1307
    }
1308
}
1309

    
1310
int bdrv_is_removable(BlockDriverState *bs)
1311
{
1312
    return bs->removable;
1313
}
1314

    
1315
int bdrv_is_read_only(BlockDriverState *bs)
1316
{
1317
    return bs->read_only;
1318
}
1319

    
1320
int bdrv_is_sg(BlockDriverState *bs)
1321
{
1322
    return bs->sg;
1323
}
1324

    
1325
int bdrv_enable_write_cache(BlockDriverState *bs)
1326
{
1327
    return bs->enable_write_cache;
1328
}
1329

    
1330
/* XXX: no longer used */
1331
void bdrv_set_change_cb(BlockDriverState *bs,
1332
                        void (*change_cb)(void *opaque), void *opaque)
1333
{
1334
    bs->change_cb = change_cb;
1335
    bs->change_opaque = opaque;
1336
}
1337

    
1338
int bdrv_is_encrypted(BlockDriverState *bs)
1339
{
1340
    if (bs->backing_hd && bs->backing_hd->encrypted)
1341
        return 1;
1342
    return bs->encrypted;
1343
}
1344

    
1345
int bdrv_key_required(BlockDriverState *bs)
1346
{
1347
    BlockDriverState *backing_hd = bs->backing_hd;
1348

    
1349
    if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
1350
        return 1;
1351
    return (bs->encrypted && !bs->valid_key);
1352
}
1353

    
1354
int bdrv_set_key(BlockDriverState *bs, const char *key)
1355
{
1356
    int ret;
1357
    if (bs->backing_hd && bs->backing_hd->encrypted) {
1358
        ret = bdrv_set_key(bs->backing_hd, key);
1359
        if (ret < 0)
1360
            return ret;
1361
        if (!bs->encrypted)
1362
            return 0;
1363
    }
1364
    if (!bs->encrypted) {
1365
        return -EINVAL;
1366
    } else if (!bs->drv || !bs->drv->bdrv_set_key) {
1367
        return -ENOMEDIUM;
1368
    }
1369
    ret = bs->drv->bdrv_set_key(bs, key);
1370
    if (ret < 0) {
1371
        bs->valid_key = 0;
1372
    } else if (!bs->valid_key) {
1373
        bs->valid_key = 1;
1374
        /* call the change callback now, we skipped it on open */
1375
        bs->media_changed = 1;
1376
        if (bs->change_cb)
1377
            bs->change_cb(bs->change_opaque);
1378
    }
1379
    return ret;
1380
}
1381

    
1382
void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
1383
{
1384
    if (!bs->drv) {
1385
        buf[0] = '\0';
1386
    } else {
1387
        pstrcpy(buf, buf_size, bs->drv->format_name);
1388
    }
1389
}
1390

    
1391
void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
1392
                         void *opaque)
1393
{
1394
    BlockDriver *drv;
1395

    
1396
    QLIST_FOREACH(drv, &bdrv_drivers, list) {
1397
        it(opaque, drv->format_name);
1398
    }
1399
}
1400

    
1401
BlockDriverState *bdrv_find(const char *name)
1402
{
1403
    BlockDriverState *bs;
1404

    
1405
    QTAILQ_FOREACH(bs, &bdrv_states, list) {
1406
        if (!strcmp(name, bs->device_name)) {
1407
            return bs;
1408
        }
1409
    }
1410
    return NULL;
1411
}
1412

    
1413
BlockDriverState *bdrv_next(BlockDriverState *bs)
1414
{
1415
    if (!bs) {
1416
        return QTAILQ_FIRST(&bdrv_states);
1417
    }
1418
    return QTAILQ_NEXT(bs, list);
1419
}
1420

    
1421
void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
1422
{
1423
    BlockDriverState *bs;
1424

    
1425
    QTAILQ_FOREACH(bs, &bdrv_states, list) {
1426
        it(opaque, bs);
1427
    }
1428
}
1429

    
1430
const char *bdrv_get_device_name(BlockDriverState *bs)
1431
{
1432
    return bs->device_name;
1433
}
1434

    
1435
void bdrv_flush(BlockDriverState *bs)
1436
{
1437
    if (bs->open_flags & BDRV_O_NO_FLUSH) {
1438
        return;
1439
    }
1440

    
1441
    if (bs->drv && bs->drv->bdrv_flush)
1442
        bs->drv->bdrv_flush(bs);
1443
}
1444

    
1445
void bdrv_flush_all(void)
1446
{
1447
    BlockDriverState *bs;
1448

    
1449
    QTAILQ_FOREACH(bs, &bdrv_states, list) {
1450
        if (bs->drv && !bdrv_is_read_only(bs) &&
1451
            (!bdrv_is_removable(bs) || bdrv_is_inserted(bs))) {
1452
            bdrv_flush(bs);
1453
        }
1454
    }
1455
}
1456

    
1457
int bdrv_has_zero_init(BlockDriverState *bs)
1458
{
1459
    assert(bs->drv);
1460

    
1461
    if (bs->drv->no_zero_init) {
1462
        return 0;
1463
    } else if (bs->file) {
1464
        return bdrv_has_zero_init(bs->file);
1465
    }
1466

    
1467
    return 1;
1468
}
1469

    
1470
/*
1471
 * Returns true iff the specified sector is present in the disk image. Drivers
1472
 * not implementing the functionality are assumed to not support backing files,
1473
 * hence all their sectors are reported as allocated.
1474
 *
1475
 * 'pnum' is set to the number of sectors (including and immediately following
1476
 * the specified sector) that are known to be in the same
1477
 * allocated/unallocated state.
1478
 *
1479
 * 'nb_sectors' is the max value 'pnum' should be set to.
1480
 */
1481
int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1482
        int *pnum)
1483
{
1484
    int64_t n;
1485
    if (!bs->drv->bdrv_is_allocated) {
1486
        if (sector_num >= bs->total_sectors) {
1487
            *pnum = 0;
1488
            return 0;
1489
        }
1490
        n = bs->total_sectors - sector_num;
1491
        *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1492
        return 1;
1493
    }
1494
    return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1495
}
1496

    
1497
void bdrv_mon_event(const BlockDriverState *bdrv,
1498
                    BlockMonEventAction action, int is_read)
1499
{
1500
    QObject *data;
1501
    const char *action_str;
1502

    
1503
    switch (action) {
1504
    case BDRV_ACTION_REPORT:
1505
        action_str = "report";
1506
        break;
1507
    case BDRV_ACTION_IGNORE:
1508
        action_str = "ignore";
1509
        break;
1510
    case BDRV_ACTION_STOP:
1511
        action_str = "stop";
1512
        break;
1513
    default:
1514
        abort();
1515
    }
1516

    
1517
    data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }",
1518
                              bdrv->device_name,
1519
                              action_str,
1520
                              is_read ? "read" : "write");
1521
    monitor_protocol_event(QEVENT_BLOCK_IO_ERROR, data);
1522

    
1523
    qobject_decref(data);
1524
}
1525

    
1526
static void bdrv_print_dict(QObject *obj, void *opaque)
1527
{
1528
    QDict *bs_dict;
1529
    Monitor *mon = opaque;
1530

    
1531
    bs_dict = qobject_to_qdict(obj);
1532

    
1533
    monitor_printf(mon, "%s: type=%s removable=%d",
1534
                        qdict_get_str(bs_dict, "device"),
1535
                        qdict_get_str(bs_dict, "type"),
1536
                        qdict_get_bool(bs_dict, "removable"));
1537

    
1538
    if (qdict_get_bool(bs_dict, "removable")) {
1539
        monitor_printf(mon, " locked=%d", qdict_get_bool(bs_dict, "locked"));
1540
    }
1541

    
1542
    if (qdict_haskey(bs_dict, "inserted")) {
1543
        QDict *qdict = qobject_to_qdict(qdict_get(bs_dict, "inserted"));
1544

    
1545
        monitor_printf(mon, " file=");
1546
        monitor_print_filename(mon, qdict_get_str(qdict, "file"));
1547
        if (qdict_haskey(qdict, "backing_file")) {
1548
            monitor_printf(mon, " backing_file=");
1549
            monitor_print_filename(mon, qdict_get_str(qdict, "backing_file"));
1550
        }
1551
        monitor_printf(mon, " ro=%d drv=%s encrypted=%d",
1552
                            qdict_get_bool(qdict, "ro"),
1553
                            qdict_get_str(qdict, "drv"),
1554
                            qdict_get_bool(qdict, "encrypted"));
1555
    } else {
1556
        monitor_printf(mon, " [not inserted]");
1557
    }
1558

    
1559
    monitor_printf(mon, "\n");
1560
}
1561

    
1562
void bdrv_info_print(Monitor *mon, const QObject *data)
1563
{
1564
    qlist_iter(qobject_to_qlist(data), bdrv_print_dict, mon);
1565
}
1566

    
1567
void bdrv_info(Monitor *mon, QObject **ret_data)
1568
{
1569
    QList *bs_list;
1570
    BlockDriverState *bs;
1571

    
1572
    bs_list = qlist_new();
1573

    
1574
    QTAILQ_FOREACH(bs, &bdrv_states, list) {
1575
        QObject *bs_obj;
1576
        const char *type = "unknown";
1577

    
1578
        switch(bs->type) {
1579
        case BDRV_TYPE_HD:
1580
            type = "hd";
1581
            break;
1582
        case BDRV_TYPE_CDROM:
1583
            type = "cdrom";
1584
            break;
1585
        case BDRV_TYPE_FLOPPY:
1586
            type = "floppy";
1587
            break;
1588
        }
1589

    
1590
        bs_obj = qobject_from_jsonf("{ 'device': %s, 'type': %s, "
1591
                                    "'removable': %i, 'locked': %i }",
1592
                                    bs->device_name, type, bs->removable,
1593
                                    bs->locked);
1594

    
1595
        if (bs->drv) {
1596
            QObject *obj;
1597
            QDict *bs_dict = qobject_to_qdict(bs_obj);
1598

    
1599
            obj = qobject_from_jsonf("{ 'file': %s, 'ro': %i, 'drv': %s, "
1600
                                     "'encrypted': %i }",
1601
                                     bs->filename, bs->read_only,
1602
                                     bs->drv->format_name,
1603
                                     bdrv_is_encrypted(bs));
1604
            if (bs->backing_file[0] != '\0') {
1605
                QDict *qdict = qobject_to_qdict(obj);
1606
                qdict_put(qdict, "backing_file",
1607
                          qstring_from_str(bs->backing_file));
1608
            }
1609

    
1610
            qdict_put_obj(bs_dict, "inserted", obj);
1611
        }
1612
        qlist_append_obj(bs_list, bs_obj);
1613
    }
1614

    
1615
    *ret_data = QOBJECT(bs_list);
1616
}
1617

    
1618
static void bdrv_stats_iter(QObject *data, void *opaque)
1619
{
1620
    QDict *qdict;
1621
    Monitor *mon = opaque;
1622

    
1623
    qdict = qobject_to_qdict(data);
1624
    monitor_printf(mon, "%s:", qdict_get_str(qdict, "device"));
1625

    
1626
    qdict = qobject_to_qdict(qdict_get(qdict, "stats"));
1627
    monitor_printf(mon, " rd_bytes=%" PRId64
1628
                        " wr_bytes=%" PRId64
1629
                        " rd_operations=%" PRId64
1630
                        " wr_operations=%" PRId64
1631
                        "\n",
1632
                        qdict_get_int(qdict, "rd_bytes"),
1633
                        qdict_get_int(qdict, "wr_bytes"),
1634
                        qdict_get_int(qdict, "rd_operations"),
1635
                        qdict_get_int(qdict, "wr_operations"));
1636
}
1637

    
1638
void bdrv_stats_print(Monitor *mon, const QObject *data)
1639
{
1640
    qlist_iter(qobject_to_qlist(data), bdrv_stats_iter, mon);
1641
}
1642

    
1643
static QObject* bdrv_info_stats_bs(BlockDriverState *bs)
1644
{
1645
    QObject *res;
1646
    QDict *dict;
1647

    
1648
    res = qobject_from_jsonf("{ 'stats': {"
1649
                             "'rd_bytes': %" PRId64 ","
1650
                             "'wr_bytes': %" PRId64 ","
1651
                             "'rd_operations': %" PRId64 ","
1652
                             "'wr_operations': %" PRId64 ","
1653
                             "'wr_highest_offset': %" PRId64
1654
                             "} }",
1655
                             bs->rd_bytes, bs->wr_bytes,
1656
                             bs->rd_ops, bs->wr_ops,
1657
                             bs->wr_highest_sector *
1658
                             (uint64_t)BDRV_SECTOR_SIZE);
1659
    dict  = qobject_to_qdict(res);
1660

    
1661
    if (*bs->device_name) {
1662
        qdict_put(dict, "device", qstring_from_str(bs->device_name));
1663
    }
1664

    
1665
    if (bs->file) {
1666
        QObject *parent = bdrv_info_stats_bs(bs->file);
1667
        qdict_put_obj(dict, "parent", parent);
1668
    }
1669

    
1670
    return res;
1671
}
1672

    
1673
void bdrv_info_stats(Monitor *mon, QObject **ret_data)
1674
{
1675
    QObject *obj;
1676
    QList *devices;
1677
    BlockDriverState *bs;
1678

    
1679
    devices = qlist_new();
1680

    
1681
    QTAILQ_FOREACH(bs, &bdrv_states, list) {
1682
        obj = bdrv_info_stats_bs(bs);
1683
        qlist_append_obj(devices, obj);
1684
    }
1685

    
1686
    *ret_data = QOBJECT(devices);
1687
}
1688

    
1689
const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1690
{
1691
    if (bs->backing_hd && bs->backing_hd->encrypted)
1692
        return bs->backing_file;
1693
    else if (bs->encrypted)
1694
        return bs->filename;
1695
    else
1696
        return NULL;
1697
}
1698

    
1699
void bdrv_get_backing_filename(BlockDriverState *bs,
1700
                               char *filename, int filename_size)
1701
{
1702
    if (!bs->backing_file) {
1703
        pstrcpy(filename, filename_size, "");
1704
    } else {
1705
        pstrcpy(filename, filename_size, bs->backing_file);
1706
    }
1707
}
1708

    
1709
int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
1710
                          const uint8_t *buf, int nb_sectors)
1711
{
1712
    BlockDriver *drv = bs->drv;
1713
    if (!drv)
1714
        return -ENOMEDIUM;
1715
    if (!drv->bdrv_write_compressed)
1716
        return -ENOTSUP;
1717
    if (bdrv_check_request(bs, sector_num, nb_sectors))
1718
        return -EIO;
1719

    
1720
    if (bs->dirty_bitmap) {
1721
        set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
1722
    }
1723

    
1724
    return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1725
}
1726

    
1727
int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1728
{
1729
    BlockDriver *drv = bs->drv;
1730
    if (!drv)
1731
        return -ENOMEDIUM;
1732
    if (!drv->bdrv_get_info)
1733
        return -ENOTSUP;
1734
    memset(bdi, 0, sizeof(*bdi));
1735
    return drv->bdrv_get_info(bs, bdi);
1736
}
1737

    
1738
int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
1739
                      int64_t pos, int size)
1740
{
1741
    BlockDriver *drv = bs->drv;
1742
    if (!drv)
1743
        return -ENOMEDIUM;
1744
    if (drv->bdrv_save_vmstate)
1745
        return drv->bdrv_save_vmstate(bs, buf, pos, size);
1746
    if (bs->file)
1747
        return bdrv_save_vmstate(bs->file, buf, pos, size);
1748
    return -ENOTSUP;
1749
}
1750

    
1751
int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
1752
                      int64_t pos, int size)
1753
{
1754
    BlockDriver *drv = bs->drv;
1755
    if (!drv)
1756
        return -ENOMEDIUM;
1757
    if (drv->bdrv_load_vmstate)
1758
        return drv->bdrv_load_vmstate(bs, buf, pos, size);
1759
    if (bs->file)
1760
        return bdrv_load_vmstate(bs->file, buf, pos, size);
1761
    return -ENOTSUP;
1762
}
1763

    
1764
void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event)
1765
{
1766
    BlockDriver *drv = bs->drv;
1767

    
1768
    if (!drv || !drv->bdrv_debug_event) {
1769
        return;
1770
    }
1771

    
1772
    return drv->bdrv_debug_event(bs, event);
1773

    
1774
}
1775

    
1776
/**************************************************************/
1777
/* handling of snapshots */
1778

    
1779
int bdrv_can_snapshot(BlockDriverState *bs)
1780
{
1781
    BlockDriver *drv = bs->drv;
1782
    if (!drv || bdrv_is_removable(bs) || bdrv_is_read_only(bs)) {
1783
        return 0;
1784
    }
1785

    
1786
    if (!drv->bdrv_snapshot_create) {
1787
        if (bs->file != NULL) {
1788
            return bdrv_can_snapshot(bs->file);
1789
        }
1790
        return 0;
1791
    }
1792

    
1793
    return 1;
1794
}
1795

    
1796
BlockDriverState *bdrv_snapshots(void)
1797
{
1798
    BlockDriverState *bs;
1799

    
1800
    if (bs_snapshots) {
1801
        return bs_snapshots;
1802
    }
1803

    
1804
    bs = NULL;
1805
    while ((bs = bdrv_next(bs))) {
1806
        if (bdrv_can_snapshot(bs)) {
1807
            bs_snapshots = bs;
1808
            return bs;
1809
        }
1810
    }
1811
    return NULL;
1812
}
1813

    
1814
int bdrv_snapshot_create(BlockDriverState *bs,
1815
                         QEMUSnapshotInfo *sn_info)
1816
{
1817
    BlockDriver *drv = bs->drv;
1818
    if (!drv)
1819
        return -ENOMEDIUM;
1820
    if (drv->bdrv_snapshot_create)
1821
        return drv->bdrv_snapshot_create(bs, sn_info);
1822
    if (bs->file)
1823
        return bdrv_snapshot_create(bs->file, sn_info);
1824
    return -ENOTSUP;
1825
}
1826

    
1827
int bdrv_snapshot_goto(BlockDriverState *bs,
1828
                       const char *snapshot_id)
1829
{
1830
    BlockDriver *drv = bs->drv;
1831
    int ret, open_ret;
1832

    
1833
    if (!drv)
1834
        return -ENOMEDIUM;
1835
    if (drv->bdrv_snapshot_goto)
1836
        return drv->bdrv_snapshot_goto(bs, snapshot_id);
1837

    
1838
    if (bs->file) {
1839
        drv->bdrv_close(bs);
1840
        ret = bdrv_snapshot_goto(bs->file, snapshot_id);
1841
        open_ret = drv->bdrv_open(bs, bs->open_flags);
1842
        if (open_ret < 0) {
1843
            bdrv_delete(bs->file);
1844
            bs->drv = NULL;
1845
            return open_ret;
1846
        }
1847
        return ret;
1848
    }
1849

    
1850
    return -ENOTSUP;
1851
}
1852

    
1853
int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1854
{
1855
    BlockDriver *drv = bs->drv;
1856
    if (!drv)
1857
        return -ENOMEDIUM;
1858
    if (drv->bdrv_snapshot_delete)
1859
        return drv->bdrv_snapshot_delete(bs, snapshot_id);
1860
    if (bs->file)
1861
        return bdrv_snapshot_delete(bs->file, snapshot_id);
1862
    return -ENOTSUP;
1863
}
1864

    
1865
int bdrv_snapshot_list(BlockDriverState *bs,
1866
                       QEMUSnapshotInfo **psn_info)
1867
{
1868
    BlockDriver *drv = bs->drv;
1869
    if (!drv)
1870
        return -ENOMEDIUM;
1871
    if (drv->bdrv_snapshot_list)
1872
        return drv->bdrv_snapshot_list(bs, psn_info);
1873
    if (bs->file)
1874
        return bdrv_snapshot_list(bs->file, psn_info);
1875
    return -ENOTSUP;
1876
}
1877

    
1878
#define NB_SUFFIXES 4
1879

    
1880
char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1881
{
1882
    static const char suffixes[NB_SUFFIXES] = "KMGT";
1883
    int64_t base;
1884
    int i;
1885

    
1886
    if (size <= 999) {
1887
        snprintf(buf, buf_size, "%" PRId64, size);
1888
    } else {
1889
        base = 1024;
1890
        for(i = 0; i < NB_SUFFIXES; i++) {
1891
            if (size < (10 * base)) {
1892
                snprintf(buf, buf_size, "%0.1f%c",
1893
                         (double)size / base,
1894
                         suffixes[i]);
1895
                break;
1896
            } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
1897
                snprintf(buf, buf_size, "%" PRId64 "%c",
1898
                         ((size + (base >> 1)) / base),
1899
                         suffixes[i]);
1900
                break;
1901
            }
1902
            base = base * 1024;
1903
        }
1904
    }
1905
    return buf;
1906
}
1907

    
1908
char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1909
{
1910
    char buf1[128], date_buf[128], clock_buf[128];
1911
#ifdef _WIN32
1912
    struct tm *ptm;
1913
#else
1914
    struct tm tm;
1915
#endif
1916
    time_t ti;
1917
    int64_t secs;
1918

    
1919
    if (!sn) {
1920
        snprintf(buf, buf_size,
1921
                 "%-10s%-20s%7s%20s%15s",
1922
                 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1923
    } else {
1924
        ti = sn->date_sec;
1925
#ifdef _WIN32
1926
        ptm = localtime(&ti);
1927
        strftime(date_buf, sizeof(date_buf),
1928
                 "%Y-%m-%d %H:%M:%S", ptm);
1929
#else
1930
        localtime_r(&ti, &tm);
1931
        strftime(date_buf, sizeof(date_buf),
1932
                 "%Y-%m-%d %H:%M:%S", &tm);
1933
#endif
1934
        secs = sn->vm_clock_nsec / 1000000000;
1935
        snprintf(clock_buf, sizeof(clock_buf),
1936
                 "%02d:%02d:%02d.%03d",
1937
                 (int)(secs / 3600),
1938
                 (int)((secs / 60) % 60),
1939
                 (int)(secs % 60),
1940
                 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1941
        snprintf(buf, buf_size,
1942
                 "%-10s%-20s%7s%20s%15s",
1943
                 sn->id_str, sn->name,
1944
                 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1945
                 date_buf,
1946
                 clock_buf);
1947
    }
1948
    return buf;
1949
}
1950

    
1951

    
1952
/**************************************************************/
1953
/* async I/Os */
1954

    
1955
BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
1956
                                 QEMUIOVector *qiov, int nb_sectors,
1957
                                 BlockDriverCompletionFunc *cb, void *opaque)
1958
{
1959
    BlockDriver *drv = bs->drv;
1960
    BlockDriverAIOCB *ret;
1961

    
1962
    if (!drv)
1963
        return NULL;
1964
    if (bdrv_check_request(bs, sector_num, nb_sectors))
1965
        return NULL;
1966

    
1967
    ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors,
1968
                              cb, opaque);
1969

    
1970
    if (ret) {
1971
        /* Update stats even though technically transfer has not happened. */
1972
        bs->rd_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE;
1973
        bs->rd_ops ++;
1974
    }
1975

    
1976
    return ret;
1977
}
1978

    
1979
BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
1980
                                  QEMUIOVector *qiov, int nb_sectors,
1981
                                  BlockDriverCompletionFunc *cb, void *opaque)
1982
{
1983
    BlockDriver *drv = bs->drv;
1984
    BlockDriverAIOCB *ret;
1985

    
1986
    if (!drv)
1987
        return NULL;
1988
    if (bs->read_only)
1989
        return NULL;
1990
    if (bdrv_check_request(bs, sector_num, nb_sectors))
1991
        return NULL;
1992

    
1993
    if (bs->dirty_bitmap) {
1994
        set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
1995
    }
1996

    
1997
    ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors,
1998
                               cb, opaque);
1999

    
2000
    if (ret) {
2001
        /* Update stats even though technically transfer has not happened. */
2002
        bs->wr_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE;
2003
        bs->wr_ops ++;
2004
        if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
2005
            bs->wr_highest_sector = sector_num + nb_sectors - 1;
2006
        }
2007
    }
2008

    
2009
    return ret;
2010
}
2011

    
2012

    
2013
typedef struct MultiwriteCB {
2014
    int error;
2015
    int num_requests;
2016
    int num_callbacks;
2017
    struct {
2018
        BlockDriverCompletionFunc *cb;
2019
        void *opaque;
2020
        QEMUIOVector *free_qiov;
2021
        void *free_buf;
2022
    } callbacks[];
2023
} MultiwriteCB;
2024

    
2025
static void multiwrite_user_cb(MultiwriteCB *mcb)
2026
{
2027
    int i;
2028

    
2029
    for (i = 0; i < mcb->num_callbacks; i++) {
2030
        mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error);
2031
        if (mcb->callbacks[i].free_qiov) {
2032
            qemu_iovec_destroy(mcb->callbacks[i].free_qiov);
2033
        }
2034
        qemu_free(mcb->callbacks[i].free_qiov);
2035
        qemu_vfree(mcb->callbacks[i].free_buf);
2036
    }
2037
}
2038

    
2039
static void multiwrite_cb(void *opaque, int ret)
2040
{
2041
    MultiwriteCB *mcb = opaque;
2042

    
2043
    if (ret < 0 && !mcb->error) {
2044
        mcb->error = ret;
2045
    }
2046

    
2047
    mcb->num_requests--;
2048
    if (mcb->num_requests == 0) {
2049
        multiwrite_user_cb(mcb);
2050
        qemu_free(mcb);
2051
    }
2052
}
2053

    
2054
static int multiwrite_req_compare(const void *a, const void *b)
2055
{
2056
    const BlockRequest *req1 = a, *req2 = b;
2057

    
2058
    /*
2059
     * Note that we can't simply subtract req2->sector from req1->sector
2060
     * here as that could overflow the return value.
2061
     */
2062
    if (req1->sector > req2->sector) {
2063
        return 1;
2064
    } else if (req1->sector < req2->sector) {
2065
        return -1;
2066
    } else {
2067
        return 0;
2068
    }
2069
}
2070

    
2071
/*
2072
 * Takes a bunch of requests and tries to merge them. Returns the number of
2073
 * requests that remain after merging.
2074
 */
2075
static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs,
2076
    int num_reqs, MultiwriteCB *mcb)
2077
{
2078
    int i, outidx;
2079

    
2080
    // Sort requests by start sector
2081
    qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare);
2082

    
2083
    // Check if adjacent requests touch the same clusters. If so, combine them,
2084
    // filling up gaps with zero sectors.
2085
    outidx = 0;
2086
    for (i = 1; i < num_reqs; i++) {
2087
        int merge = 0;
2088
        int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors;
2089

    
2090
        // This handles the cases that are valid for all block drivers, namely
2091
        // exactly sequential writes and overlapping writes.
2092
        if (reqs[i].sector <= oldreq_last) {
2093
            merge = 1;
2094
        }
2095

    
2096
        // The block driver may decide that it makes sense to combine requests
2097
        // even if there is a gap of some sectors between them. In this case,
2098
        // the gap is filled with zeros (therefore only applicable for yet
2099
        // unused space in format like qcow2).
2100
        if (!merge && bs->drv->bdrv_merge_requests) {
2101
            merge = bs->drv->bdrv_merge_requests(bs, &reqs[outidx], &reqs[i]);
2102
        }
2103

    
2104
        if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) {
2105
            merge = 0;
2106
        }
2107

    
2108
        if (merge) {
2109
            size_t size;
2110
            QEMUIOVector *qiov = qemu_mallocz(sizeof(*qiov));
2111
            qemu_iovec_init(qiov,
2112
                reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1);
2113

    
2114
            // Add the first request to the merged one. If the requests are
2115
            // overlapping, drop the last sectors of the first request.
2116
            size = (reqs[i].sector - reqs[outidx].sector) << 9;
2117
            qemu_iovec_concat(qiov, reqs[outidx].qiov, size);
2118

    
2119
            // We might need to add some zeros between the two requests
2120
            if (reqs[i].sector > oldreq_last) {
2121
                size_t zero_bytes = (reqs[i].sector - oldreq_last) << 9;
2122
                uint8_t *buf = qemu_blockalign(bs, zero_bytes);
2123
                memset(buf, 0, zero_bytes);
2124
                qemu_iovec_add(qiov, buf, zero_bytes);
2125
                mcb->callbacks[i].free_buf = buf;
2126
            }
2127

    
2128
            // Add the second request
2129
            qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size);
2130

    
2131
            reqs[outidx].nb_sectors = qiov->size >> 9;
2132
            reqs[outidx].qiov = qiov;
2133

    
2134
            mcb->callbacks[i].free_qiov = reqs[outidx].qiov;
2135
        } else {
2136
            outidx++;
2137
            reqs[outidx].sector     = reqs[i].sector;
2138
            reqs[outidx].nb_sectors = reqs[i].nb_sectors;
2139
            reqs[outidx].qiov       = reqs[i].qiov;
2140
        }
2141
    }
2142

    
2143
    return outidx + 1;
2144
}
2145

    
2146
/*
2147
 * Submit multiple AIO write requests at once.
2148
 *
2149
 * On success, the function returns 0 and all requests in the reqs array have
2150
 * been submitted. In error case this function returns -1, and any of the
2151
 * requests may or may not be submitted yet. In particular, this means that the
2152
 * callback will be called for some of the requests, for others it won't. The
2153
 * caller must check the error field of the BlockRequest to wait for the right
2154
 * callbacks (if error != 0, no callback will be called).
2155
 *
2156
 * The implementation may modify the contents of the reqs array, e.g. to merge
2157
 * requests. However, the fields opaque and error are left unmodified as they
2158
 * are used to signal failure for a single request to the caller.
2159
 */
2160
int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs)
2161
{
2162
    BlockDriverAIOCB *acb;
2163
    MultiwriteCB *mcb;
2164
    int i;
2165

    
2166
    if (num_reqs == 0) {
2167
        return 0;
2168
    }
2169

    
2170
    // Create MultiwriteCB structure
2171
    mcb = qemu_mallocz(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks));
2172
    mcb->num_requests = 0;
2173
    mcb->num_callbacks = num_reqs;
2174

    
2175
    for (i = 0; i < num_reqs; i++) {
2176
        mcb->callbacks[i].cb = reqs[i].cb;
2177
        mcb->callbacks[i].opaque = reqs[i].opaque;
2178
    }
2179

    
2180
    // Check for mergable requests
2181
    num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb);
2182

    
2183
    /*
2184
     * Run the aio requests. As soon as one request can't be submitted
2185
     * successfully, fail all requests that are not yet submitted (we must
2186
     * return failure for all requests anyway)
2187
     *
2188
     * num_requests cannot be set to the right value immediately: If
2189
     * bdrv_aio_writev fails for some request, num_requests would be too high
2190
     * and therefore multiwrite_cb() would never recognize the multiwrite
2191
     * request as completed. We also cannot use the loop variable i to set it
2192
     * when the first request fails because the callback may already have been
2193
     * called for previously submitted requests. Thus, num_requests must be
2194
     * incremented for each request that is submitted.
2195
     *
2196
     * The problem that callbacks may be called early also means that we need
2197
     * to take care that num_requests doesn't become 0 before all requests are
2198
     * submitted - multiwrite_cb() would consider the multiwrite request
2199
     * completed. A dummy request that is "completed" by a manual call to
2200
     * multiwrite_cb() takes care of this.
2201
     */
2202
    mcb->num_requests = 1;
2203

    
2204
    for (i = 0; i < num_reqs; i++) {
2205
        mcb->num_requests++;
2206
        acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov,
2207
            reqs[i].nb_sectors, multiwrite_cb, mcb);
2208

    
2209
        if (acb == NULL) {
2210
            // We can only fail the whole thing if no request has been
2211
            // submitted yet. Otherwise we'll wait for the submitted AIOs to
2212
            // complete and report the error in the callback.
2213
            if (i == 0) {
2214
                goto fail;
2215
            } else {
2216
                multiwrite_cb(mcb, -EIO);
2217
                break;
2218
            }
2219
        }
2220
    }
2221

    
2222
    /* Complete the dummy request */
2223
    multiwrite_cb(mcb, 0);
2224

    
2225
    return 0;
2226

    
2227
fail:
2228
    for (i = 0; i < mcb->num_callbacks; i++) {
2229
        reqs[i].error = -EIO;
2230
    }
2231
    qemu_free(mcb);
2232
    return -1;
2233
}
2234

    
2235
BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs,
2236
        BlockDriverCompletionFunc *cb, void *opaque)
2237
{
2238
    BlockDriver *drv = bs->drv;
2239

    
2240
    if (bs->open_flags & BDRV_O_NO_FLUSH) {
2241
        return bdrv_aio_noop_em(bs, cb, opaque);
2242
    }
2243

    
2244
    if (!drv)
2245
        return NULL;
2246
    return drv->bdrv_aio_flush(bs, cb, opaque);
2247
}
2248

    
2249
void bdrv_aio_cancel(BlockDriverAIOCB *acb)
2250
{
2251
    acb->pool->cancel(acb);
2252
}
2253

    
2254

    
2255
/**************************************************************/
2256
/* async block device emulation */
2257

    
2258
typedef struct BlockDriverAIOCBSync {
2259
    BlockDriverAIOCB common;
2260
    QEMUBH *bh;
2261
    int ret;
2262
    /* vector translation state */
2263
    QEMUIOVector *qiov;
2264
    uint8_t *bounce;
2265
    int is_write;
2266
} BlockDriverAIOCBSync;
2267

    
2268
static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
2269
{
2270
    BlockDriverAIOCBSync *acb =
2271
        container_of(blockacb, BlockDriverAIOCBSync, common);
2272
    qemu_bh_delete(acb->bh);
2273
    acb->bh = NULL;
2274
    qemu_aio_release(acb);
2275
}
2276

    
2277
static AIOPool bdrv_em_aio_pool = {
2278
    .aiocb_size         = sizeof(BlockDriverAIOCBSync),
2279
    .cancel             = bdrv_aio_cancel_em,
2280
};
2281

    
2282
static void bdrv_aio_bh_cb(void *opaque)
2283
{
2284
    BlockDriverAIOCBSync *acb = opaque;
2285

    
2286
    if (!acb->is_write)
2287
        qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size);
2288
    qemu_vfree(acb->bounce);
2289
    acb->common.cb(acb->common.opaque, acb->ret);
2290
    qemu_bh_delete(acb->bh);
2291
    acb->bh = NULL;
2292
    qemu_aio_release(acb);
2293
}
2294

    
2295
static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
2296
                                            int64_t sector_num,
2297
                                            QEMUIOVector *qiov,
2298
                                            int nb_sectors,
2299
                                            BlockDriverCompletionFunc *cb,
2300
                                            void *opaque,
2301
                                            int is_write)
2302

    
2303
{
2304
    BlockDriverAIOCBSync *acb;
2305

    
2306
    acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2307
    acb->is_write = is_write;
2308
    acb->qiov = qiov;
2309
    acb->bounce = qemu_blockalign(bs, qiov->size);
2310

    
2311
    if (!acb->bh)
2312
        acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2313

    
2314
    if (is_write) {
2315
        qemu_iovec_to_buffer(acb->qiov, acb->bounce);
2316
        acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
2317
    } else {
2318
        acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors);
2319
    }
2320

    
2321
    qemu_bh_schedule(acb->bh);
2322

    
2323
    return &acb->common;
2324
}
2325

    
2326
static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
2327
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
2328
        BlockDriverCompletionFunc *cb, void *opaque)
2329
{
2330
    return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
2331
}
2332

    
2333
static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
2334
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
2335
        BlockDriverCompletionFunc *cb, void *opaque)
2336
{
2337
    return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
2338
}
2339

    
2340
static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
2341
        BlockDriverCompletionFunc *cb, void *opaque)
2342
{
2343
    BlockDriverAIOCBSync *acb;
2344

    
2345
    acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2346
    acb->is_write = 1; /* don't bounce in the completion hadler */
2347
    acb->qiov = NULL;
2348
    acb->bounce = NULL;
2349
    acb->ret = 0;
2350

    
2351
    if (!acb->bh)
2352
        acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2353

    
2354
    bdrv_flush(bs);
2355
    qemu_bh_schedule(acb->bh);
2356
    return &acb->common;
2357
}
2358

    
2359
static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs,
2360
        BlockDriverCompletionFunc *cb, void *opaque)
2361
{
2362
    BlockDriverAIOCBSync *acb;
2363

    
2364
    acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2365
    acb->is_write = 1; /* don't bounce in the completion handler */
2366
    acb->qiov = NULL;
2367
    acb->bounce = NULL;
2368
    acb->ret = 0;
2369

    
2370
    if (!acb->bh) {
2371
        acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2372
    }
2373

    
2374
    qemu_bh_schedule(acb->bh);
2375
    return &acb->common;
2376
}
2377

    
2378
/**************************************************************/
2379
/* sync block device emulation */
2380

    
2381
static void bdrv_rw_em_cb(void *opaque, int ret)
2382
{
2383
    *(int *)opaque = ret;
2384
}
2385

    
2386
#define NOT_DONE 0x7fffffff
2387

    
2388
static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
2389
                        uint8_t *buf, int nb_sectors)
2390
{
2391
    int async_ret;
2392
    BlockDriverAIOCB *acb;
2393
    struct iovec iov;
2394
    QEMUIOVector qiov;
2395

    
2396
    async_context_push();
2397

    
2398
    async_ret = NOT_DONE;
2399
    iov.iov_base = (void *)buf;
2400
    iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE;
2401
    qemu_iovec_init_external(&qiov, &iov, 1);
2402
    acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors,
2403
        bdrv_rw_em_cb, &async_ret);
2404
    if (acb == NULL) {
2405
        async_ret = -1;
2406
        goto fail;
2407
    }
2408

    
2409
    while (async_ret == NOT_DONE) {
2410
        qemu_aio_wait();
2411
    }
2412

    
2413

    
2414
fail:
2415
    async_context_pop();
2416
    return async_ret;
2417
}
2418

    
2419
static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
2420
                         const uint8_t *buf, int nb_sectors)
2421
{
2422
    int async_ret;
2423
    BlockDriverAIOCB *acb;
2424
    struct iovec iov;
2425
    QEMUIOVector qiov;
2426

    
2427
    async_context_push();
2428

    
2429
    async_ret = NOT_DONE;
2430
    iov.iov_base = (void *)buf;
2431
    iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE;
2432
    qemu_iovec_init_external(&qiov, &iov, 1);
2433
    acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors,
2434
        bdrv_rw_em_cb, &async_ret);
2435
    if (acb == NULL) {
2436
        async_ret = -1;
2437
        goto fail;
2438
    }
2439
    while (async_ret == NOT_DONE) {
2440
        qemu_aio_wait();
2441
    }
2442

    
2443
fail:
2444
    async_context_pop();
2445
    return async_ret;
2446
}
2447

    
2448
void bdrv_init(void)
2449
{
2450
    module_call_init(MODULE_INIT_BLOCK);
2451
}
2452

    
2453
void bdrv_init_with_whitelist(void)
2454
{
2455
    use_bdrv_whitelist = 1;
2456
    bdrv_init();
2457
}
2458

    
2459
void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs,
2460
                   BlockDriverCompletionFunc *cb, void *opaque)
2461
{
2462
    BlockDriverAIOCB *acb;
2463

    
2464
    if (pool->free_aiocb) {
2465
        acb = pool->free_aiocb;
2466
        pool->free_aiocb = acb->next;
2467
    } else {
2468
        acb = qemu_mallocz(pool->aiocb_size);
2469
        acb->pool = pool;
2470
    }
2471
    acb->bs = bs;
2472
    acb->cb = cb;
2473
    acb->opaque = opaque;
2474
    return acb;
2475
}
2476

    
2477
void qemu_aio_release(void *p)
2478
{
2479
    BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
2480
    AIOPool *pool = acb->pool;
2481
    acb->next = pool->free_aiocb;
2482
    pool->free_aiocb = acb;
2483
}
2484

    
2485
/**************************************************************/
2486
/* removable device support */
2487

    
2488
/**
2489
 * Return TRUE if the media is present
2490
 */
2491
int bdrv_is_inserted(BlockDriverState *bs)
2492
{
2493
    BlockDriver *drv = bs->drv;
2494
    int ret;
2495
    if (!drv)
2496
        return 0;
2497
    if (!drv->bdrv_is_inserted)
2498
        return 1;
2499
    ret = drv->bdrv_is_inserted(bs);
2500
    return ret;
2501
}
2502

    
2503
/**
2504
 * Return TRUE if the media changed since the last call to this
2505
 * function. It is currently only used for floppy disks
2506
 */
2507
int bdrv_media_changed(BlockDriverState *bs)
2508
{
2509
    BlockDriver *drv = bs->drv;
2510
    int ret;
2511

    
2512
    if (!drv || !drv->bdrv_media_changed)
2513
        ret = -ENOTSUP;
2514
    else
2515
        ret = drv->bdrv_media_changed(bs);
2516
    if (ret == -ENOTSUP)
2517
        ret = bs->media_changed;
2518
    bs->media_changed = 0;
2519
    return ret;
2520
}
2521

    
2522
/**
2523
 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
2524
 */
2525
int bdrv_eject(BlockDriverState *bs, int eject_flag)
2526
{
2527
    BlockDriver *drv = bs->drv;
2528
    int ret;
2529

    
2530
    if (bs->locked) {
2531
        return -EBUSY;
2532
    }
2533

    
2534
    if (!drv || !drv->bdrv_eject) {
2535
        ret = -ENOTSUP;
2536
    } else {
2537
        ret = drv->bdrv_eject(bs, eject_flag);
2538
    }
2539
    if (ret == -ENOTSUP) {
2540
        if (eject_flag)
2541
            bdrv_close(bs);
2542
        ret = 0;
2543
    }
2544

    
2545
    return ret;
2546
}
2547

    
2548
int bdrv_is_locked(BlockDriverState *bs)
2549
{
2550
    return bs->locked;
2551
}
2552

    
2553
/**
2554
 * Lock or unlock the media (if it is locked, the user won't be able
2555
 * to eject it manually).
2556
 */
2557
void bdrv_set_locked(BlockDriverState *bs, int locked)
2558
{
2559
    BlockDriver *drv = bs->drv;
2560

    
2561
    bs->locked = locked;
2562
    if (drv && drv->bdrv_set_locked) {
2563
        drv->bdrv_set_locked(bs, locked);
2564
    }
2565
}
2566

    
2567
/* needed for generic scsi interface */
2568

    
2569
int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
2570
{
2571
    BlockDriver *drv = bs->drv;
2572

    
2573
    if (drv && drv->bdrv_ioctl)
2574
        return drv->bdrv_ioctl(bs, req, buf);
2575
    return -ENOTSUP;
2576
}
2577

    
2578
BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
2579
        unsigned long int req, void *buf,
2580
        BlockDriverCompletionFunc *cb, void *opaque)
2581
{
2582
    BlockDriver *drv = bs->drv;
2583

    
2584
    if (drv && drv->bdrv_aio_ioctl)
2585
        return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
2586
    return NULL;
2587
}
2588

    
2589

    
2590

    
2591
void *qemu_blockalign(BlockDriverState *bs, size_t size)
2592
{
2593
    return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size);
2594
}
2595

    
2596
void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable)
2597
{
2598
    int64_t bitmap_size;
2599

    
2600
    bs->dirty_count = 0;
2601
    if (enable) {
2602
        if (!bs->dirty_bitmap) {
2603
            bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) +
2604
                    BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
2605
            bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
2606

    
2607
            bs->dirty_bitmap = qemu_mallocz(bitmap_size);
2608
        }
2609
    } else {
2610
        if (bs->dirty_bitmap) {
2611
            qemu_free(bs->dirty_bitmap);
2612
            bs->dirty_bitmap = NULL;
2613
        }
2614
    }
2615
}
2616

    
2617
int bdrv_get_dirty(BlockDriverState *bs, int64_t sector)
2618
{
2619
    int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
2620

    
2621
    if (bs->dirty_bitmap &&
2622
        (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bs)) {
2623
        return bs->dirty_bitmap[chunk / (sizeof(unsigned long) * 8)] &
2624
            (1 << (chunk % (sizeof(unsigned long) * 8)));
2625
    } else {
2626
        return 0;
2627
    }
2628
}
2629

    
2630
void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector,
2631
                      int nr_sectors)
2632
{
2633
    set_dirty_bitmap(bs, cur_sector, nr_sectors, 0);
2634
}
2635

    
2636
int64_t bdrv_get_dirty_count(BlockDriverState *bs)
2637
{
2638
    return bs->dirty_count;
2639
}