~ubuntu-branches/ubuntu/trusty/qemu/trusty

« back to all changes in this revision

Viewing changes to block.c

  • Committer: Package Import Robot
  • Author(s): Serge Hallyn
  • Date: 2013-10-22 22:47:07 UTC
  • mfrom: (1.8.3) (10.1.42 sid)
  • Revision ID: package-import@ubuntu.com-20131022224707-1lya34fw3k3f24tv
Tags: 1.6.0+dfsg-2ubuntu1
* Merge 1.6.0~rc0+dfsg-2exp from debian experimental.  Remaining changes:
  - debian/control
    * update maintainer
    * remove libiscsi, usb-redir, vde, vnc-jpeg, and libssh2-1-dev
      from build-deps
    * enable rbd
    * add qemu-system and qemu-common B/R to qemu-keymaps
    * add D:udev, R:qemu, R:qemu-common and B:qemu-common to
      qemu-system-common
    * qemu-system-arm, qemu-system-ppc, qemu-system-sparc:
      - add qemu-kvm to Provides
      - add qemu-common, qemu-kvm, kvm to B/R
      - remove openbios-sparc from qemu-system-sparc D
      - drop openbios-ppc and openhackware Depends to Suggests (for now)
    * qemu-system-x86:
      - add qemu-common to Breaks/Replaces.
      - add cpu-checker to Recommends.
    * qemu-user: add B/R:qemu-kvm
    * qemu-kvm:
      - add armhf armel powerpc sparc to Architecture
      - C/R/P: qemu-kvm-spice
    * add qemu-common package
    * drop qemu-slof which is not packaged in ubuntu
  - add qemu-system-common.links for tap ifup/down scripts and OVMF link.
  - qemu-system-x86.links:
    * remove pxe rom links which are in kvm-ipxe
    * add symlink for kvm.1 manpage
  - debian/rules
    * add kvm-spice symlink to qemu-kvm
    * call dh_installmodules for qemu-system-x86
    * update dh_installinit to install upstart script
    * run dh_installman (Closes: #709241) (cherrypicked from 1.5.0+dfsg-2)
  - Add qemu-utils.links for kvm-* symlinks.
  - Add qemu-system-x86.qemu-kvm.upstart and .default
  - Add qemu-system-x86.modprobe to set nesting=1
  - Add qemu-system-common.preinst to add kvm group
  - qemu-system-common.postinst: remove bad group acl if there, then have
    udev relabel /dev/kvm.
  - New linaro patches from qemu-linaro rebasing branch
  - Dropped patches:
    * xen-simplify-xen_enabled.patch
    * sparc-linux-user-fix-missing-symbols-in-.rel-.rela.plt-sections.patch
    * main_loop-do-not-set-nonblocking-if-xen_enabled.patch
    * xen_machine_pv-do-not-create-a-dummy-CPU-in-machine-.patch
    * virtio-rng-fix-crash
  - Kept patches:
    * expose_vms_qemu64cpu.patch - updated
    * linaro arm patches from qemu-linaro rebasing branch
  - New patches:
    * fix-pci-add: change CONFIG variable in ifdef to make sure that
      pci_add is defined.
* Add linaro patches
* Add experimental mach-virt patches for arm virtualization.
* qemu-system-common.install: add debian/tmp/usr/lib to install the
  qemu-bridge-helper

Show diffs side-by-side

added added

removed removed

Lines of Context:
99
99
static QLIST_HEAD(, BlockDriver) bdrv_drivers =
100
100
    QLIST_HEAD_INITIALIZER(bdrv_drivers);
101
101
 
102
 
/* The device to use for VM snapshots */
103
 
static BlockDriverState *bs_snapshots;
104
 
 
105
102
/* If non-zero, use only whitelisted block drivers */
106
103
static int use_bdrv_whitelist;
107
104
 
130
127
{
131
128
    bs->io_limits_enabled = false;
132
129
 
133
 
    while (qemu_co_queue_next(&bs->throttled_reqs));
 
130
    do {} while (qemu_co_enter_next(&bs->throttled_reqs));
134
131
 
135
132
    if (bs->block_timer) {
136
133
        qemu_del_timer(bs->block_timer);
146
143
{
147
144
    BlockDriverState *bs = opaque;
148
145
 
149
 
    qemu_co_queue_next(&bs->throttled_reqs);
 
146
    qemu_co_enter_next(&bs->throttled_reqs);
150
147
}
151
148
 
152
149
void bdrv_io_limits_enable(BlockDriverState *bs)
308
305
    }
309
306
    bdrv_iostatus_disable(bs);
310
307
    notifier_list_init(&bs->close_notifiers);
 
308
    notifier_with_return_list_init(&bs->before_write_notifiers);
311
309
 
312
310
    return bs;
313
311
}
328
326
    return NULL;
329
327
}
330
328
 
331
 
static int bdrv_is_whitelisted(BlockDriver *drv)
 
329
static int bdrv_is_whitelisted(BlockDriver *drv, bool read_only)
332
330
{
333
 
    static const char *whitelist[] = {
334
 
        CONFIG_BDRV_WHITELIST
 
331
    static const char *whitelist_rw[] = {
 
332
        CONFIG_BDRV_RW_WHITELIST
 
333
    };
 
334
    static const char *whitelist_ro[] = {
 
335
        CONFIG_BDRV_RO_WHITELIST
335
336
    };
336
337
    const char **p;
337
338
 
338
 
    if (!whitelist[0])
 
339
    if (!whitelist_rw[0] && !whitelist_ro[0]) {
339
340
        return 1;               /* no whitelist, anything goes */
 
341
    }
340
342
 
341
 
    for (p = whitelist; *p; p++) {
 
343
    for (p = whitelist_rw; *p; p++) {
342
344
        if (!strcmp(drv->format_name, *p)) {
343
345
            return 1;
344
346
        }
345
347
    }
 
348
    if (read_only) {
 
349
        for (p = whitelist_ro; *p; p++) {
 
350
            if (!strcmp(drv->format_name, *p)) {
 
351
                return 1;
 
352
            }
 
353
        }
 
354
    }
346
355
    return 0;
347
356
}
348
357
 
349
 
BlockDriver *bdrv_find_whitelisted_format(const char *format_name)
 
358
BlockDriver *bdrv_find_whitelisted_format(const char *format_name,
 
359
                                          bool read_only)
350
360
{
351
361
    BlockDriver *drv = bdrv_find_format(format_name);
352
 
    return drv && bdrv_is_whitelisted(drv) ? drv : NULL;
 
362
    return drv && bdrv_is_whitelisted(drv, read_only) ? drv : NULL;
353
363
}
354
364
 
355
365
typedef struct CreateCo {
407
417
{
408
418
    BlockDriver *drv;
409
419
 
410
 
    drv = bdrv_find_protocol(filename);
 
420
    drv = bdrv_find_protocol(filename, true);
411
421
    if (drv == NULL) {
412
422
        return -ENOENT;
413
423
    }
472
482
    return drv;
473
483
}
474
484
 
475
 
BlockDriver *bdrv_find_protocol(const char *filename)
 
485
BlockDriver *bdrv_find_protocol(const char *filename,
 
486
                                bool allow_protocol_prefix)
476
487
{
477
488
    BlockDriver *drv1;
478
489
    char protocol[128];
493
504
        return drv1;
494
505
    }
495
506
 
496
 
    if (!path_has_protocol(filename)) {
 
507
    if (!path_has_protocol(filename) || !allow_protocol_prefix) {
497
508
        return bdrv_find_format("file");
498
509
    }
 
510
 
499
511
    p = strchr(filename, ':');
500
512
    assert(p != NULL);
501
513
    len = p - filename;
684
696
 
685
697
    trace_bdrv_open_common(bs, filename ?: "", flags, drv->format_name);
686
698
 
687
 
    if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) {
688
 
        return -ENOTSUP;
689
 
    }
690
 
 
691
699
    /* bdrv_open() with directly using a protocol as drv. This layer is already
692
700
     * opened, so assign it to bs (while file becomes a closed BlockDriverState)
693
701
     * and return immediately. */
698
706
 
699
707
    bs->open_flags = flags;
700
708
    bs->buffer_alignment = 512;
 
709
    open_flags = bdrv_open_flags(bs, flags);
 
710
    bs->read_only = !(open_flags & BDRV_O_RDWR);
 
711
 
 
712
    if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv, bs->read_only)) {
 
713
        return -ENOTSUP;
 
714
    }
701
715
 
702
716
    assert(bs->copy_on_read == 0); /* bdrv_new() and bdrv_close() make it so */
703
 
    if ((flags & BDRV_O_RDWR) && (flags & BDRV_O_COPY_ON_READ)) {
 
717
    if (!bs->read_only && (flags & BDRV_O_COPY_ON_READ)) {
704
718
        bdrv_enable_copy_on_read(bs);
705
719
    }
706
720
 
714
728
    bs->opaque = g_malloc0(drv->instance_size);
715
729
 
716
730
    bs->enable_write_cache = !!(flags & BDRV_O_CACHE_WB);
717
 
    open_flags = bdrv_open_flags(bs, flags);
718
 
 
719
 
    bs->read_only = !(open_flags & BDRV_O_RDWR);
720
731
 
721
732
    /* Open the image, either directly or using a protocol */
722
733
    if (drv->bdrv_file_open) {
775
786
    BlockDriverState *bs;
776
787
    BlockDriver *drv;
777
788
    const char *drvname;
 
789
    bool allow_protocol_prefix = false;
778
790
    int ret;
779
791
 
780
792
    /* NULL means an empty set of options */
791
803
        filename = qdict_get_try_str(options, "filename");
792
804
    } else if (filename && !qdict_haskey(options, "filename")) {
793
805
        qdict_put(options, "filename", qstring_from_str(filename));
 
806
        allow_protocol_prefix = true;
794
807
    } else {
795
808
        qerror_report(ERROR_CLASS_GENERIC_ERROR, "Can't specify 'file' and "
796
809
                      "'filename' options at the same time");
801
814
    /* Find the right block driver */
802
815
    drvname = qdict_get_try_str(options, "driver");
803
816
    if (drvname) {
804
 
        drv = bdrv_find_whitelisted_format(drvname);
 
817
        drv = bdrv_find_whitelisted_format(drvname, !(flags & BDRV_O_RDWR));
805
818
        qdict_del(options, "driver");
806
819
    } else if (filename) {
807
 
        drv = bdrv_find_protocol(filename);
 
820
        drv = bdrv_find_protocol(filename, allow_protocol_prefix);
 
821
        if (!drv) {
 
822
            qerror_report(ERROR_CLASS_GENERIC_ERROR, "Unknown protocol");
 
823
        }
808
824
    } else {
809
825
        qerror_report(ERROR_CLASS_GENERIC_ERROR,
810
826
                      "Must specify either driver or file");
954
970
    char tmp_filename[PATH_MAX + 1];
955
971
    BlockDriverState *file = NULL;
956
972
    QDict *file_options = NULL;
 
973
    const char *drvname;
957
974
 
958
975
    /* NULL means an empty set of options */
959
976
    if (options == NULL) {
1037
1054
    extract_subqdict(options, &file_options, "file.");
1038
1055
 
1039
1056
    ret = bdrv_file_open(&file, filename, file_options,
1040
 
                         bdrv_open_flags(bs, flags));
 
1057
                         bdrv_open_flags(bs, flags | BDRV_O_UNMAP));
1041
1058
    if (ret < 0) {
1042
1059
        goto fail;
1043
1060
    }
1044
1061
 
1045
1062
    /* Find the right image format driver */
 
1063
    drvname = qdict_get_try_str(options, "driver");
 
1064
    if (drvname) {
 
1065
        drv = bdrv_find_whitelisted_format(drvname, !(flags & BDRV_O_RDWR));
 
1066
        qdict_del(options, "driver");
 
1067
    }
 
1068
 
1046
1069
    if (!drv) {
1047
1070
        ret = find_image_format(file, filename, &drv);
1048
1071
    }
1283
1306
            if (local_err != NULL) {
1284
1307
                error_propagate(errp, local_err);
1285
1308
            } else {
1286
 
                error_set(errp, QERR_OPEN_FILE_FAILED,
1287
 
                          reopen_state->bs->filename);
 
1309
                error_setg(errp, "failed while preparing to reopen image '%s'",
 
1310
                           reopen_state->bs->filename);
1288
1311
            }
1289
1312
            goto error;
1290
1313
        }
1349
1372
 
1350
1373
void bdrv_close(BlockDriverState *bs)
1351
1374
{
1352
 
    bdrv_flush(bs);
1353
1375
    if (bs->job) {
1354
1376
        block_job_cancel_sync(bs->job);
1355
1377
    }
1356
 
    bdrv_drain_all();
 
1378
    bdrv_drain_all(); /* complete I/O */
 
1379
    bdrv_flush(bs);
 
1380
    bdrv_drain_all(); /* in case flush left pending I/O */
1357
1381
    notifier_list_notify(&bs->close_notifiers, bs);
1358
1382
 
1359
1383
    if (bs->drv) {
1360
 
        if (bs == bs_snapshots) {
1361
 
            bs_snapshots = NULL;
1362
 
        }
1363
1384
        if (bs->backing_hd) {
1364
1385
            bdrv_delete(bs->backing_hd);
1365
1386
            bs->backing_hd = NULL;
1431
1452
         * a busy wait.
1432
1453
         */
1433
1454
        QTAILQ_FOREACH(bs, &bdrv_states, list) {
1434
 
            if (!qemu_co_queue_empty(&bs->throttled_reqs)) {
1435
 
                qemu_co_queue_restart_all(&bs->throttled_reqs);
 
1455
            while (qemu_co_enter_next(&bs->throttled_reqs)) {
1436
1456
                busy = true;
1437
1457
            }
1438
1458
        }
1586
1606
    assert(!bs->job);
1587
1607
    assert(!bs->in_use);
1588
1608
 
 
1609
    bdrv_close(bs);
 
1610
 
1589
1611
    /* remove from list, if necessary */
1590
1612
    bdrv_make_anon(bs);
1591
1613
 
1592
 
    bdrv_close(bs);
1593
 
 
1594
 
    assert(bs != bs_snapshots);
1595
1614
    g_free(bs);
1596
1615
}
1597
1616
 
1635
1654
{
1636
1655
    bs->dev_ops = ops;
1637
1656
    bs->dev_opaque = opaque;
1638
 
    if (bdrv_dev_has_removable_media(bs) && bs == bs_snapshots) {
1639
 
        bs_snapshots = NULL;
1640
 
    }
1641
1657
}
1642
1658
 
1643
1659
void bdrv_emit_qmp_error_event(const BlockDriverState *bdrv,
1787
1803
    buf = g_malloc(COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE);
1788
1804
 
1789
1805
    for (sector = 0; sector < total_sectors; sector += n) {
1790
 
        if (bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n)) {
1791
 
 
 
1806
        ret = bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n);
 
1807
        if (ret < 0) {
 
1808
            goto ro_cleanup;
 
1809
        }
 
1810
        if (ret) {
1792
1811
            if (bdrv_read(bs, sector, buf, n) != 0) {
1793
1812
                ret = -EIO;
1794
1813
                goto ro_cleanup;
1839
1858
    return 0;
1840
1859
}
1841
1860
 
1842
 
struct BdrvTrackedRequest {
1843
 
    BlockDriverState *bs;
1844
 
    int64_t sector_num;
1845
 
    int nb_sectors;
1846
 
    bool is_write;
1847
 
    QLIST_ENTRY(BdrvTrackedRequest) list;
1848
 
    Coroutine *co; /* owner, used for deadlock detection */
1849
 
    CoQueue wait_queue; /* coroutines blocked on this request */
1850
 
};
1851
 
 
1852
1861
/**
1853
1862
 * Remove an active request from the tracked requests list
1854
1863
 *
2162
2171
    QEMUIOVector *qiov;
2163
2172
    bool is_write;
2164
2173
    int ret;
 
2174
    BdrvRequestFlags flags;
2165
2175
} RwCo;
2166
2176
 
2167
2177
static void coroutine_fn bdrv_rw_co_entry(void *opaque)
2170
2180
 
2171
2181
    if (!rwco->is_write) {
2172
2182
        rwco->ret = bdrv_co_do_readv(rwco->bs, rwco->sector_num,
2173
 
                                     rwco->nb_sectors, rwco->qiov, 0);
 
2183
                                     rwco->nb_sectors, rwco->qiov,
 
2184
                                     rwco->flags);
2174
2185
    } else {
2175
2186
        rwco->ret = bdrv_co_do_writev(rwco->bs, rwco->sector_num,
2176
 
                                      rwco->nb_sectors, rwco->qiov, 0);
 
2187
                                      rwco->nb_sectors, rwco->qiov,
 
2188
                                      rwco->flags);
2177
2189
    }
2178
2190
}
2179
2191
 
2181
2193
 * Process a vectored synchronous request using coroutines
2182
2194
 */
2183
2195
static int bdrv_rwv_co(BlockDriverState *bs, int64_t sector_num,
2184
 
                       QEMUIOVector *qiov, bool is_write)
 
2196
                       QEMUIOVector *qiov, bool is_write,
 
2197
                       BdrvRequestFlags flags)
2185
2198
{
2186
2199
    Coroutine *co;
2187
2200
    RwCo rwco = {
2191
2204
        .qiov = qiov,
2192
2205
        .is_write = is_write,
2193
2206
        .ret = NOT_DONE,
 
2207
        .flags = flags,
2194
2208
    };
2195
2209
    assert((qiov->size & (BDRV_SECTOR_SIZE - 1)) == 0);
2196
2210
 
2222
2236
 * Process a synchronous request using coroutines
2223
2237
 */
2224
2238
static int bdrv_rw_co(BlockDriverState *bs, int64_t sector_num, uint8_t *buf,
2225
 
                      int nb_sectors, bool is_write)
 
2239
                      int nb_sectors, bool is_write, BdrvRequestFlags flags)
2226
2240
{
2227
2241
    QEMUIOVector qiov;
2228
2242
    struct iovec iov = {
2231
2245
    };
2232
2246
 
2233
2247
    qemu_iovec_init_external(&qiov, &iov, 1);
2234
 
    return bdrv_rwv_co(bs, sector_num, &qiov, is_write);
 
2248
    return bdrv_rwv_co(bs, sector_num, &qiov, is_write, flags);
2235
2249
}
2236
2250
 
2237
2251
/* return < 0 if error. See bdrv_write() for the return codes */
2238
2252
int bdrv_read(BlockDriverState *bs, int64_t sector_num,
2239
2253
              uint8_t *buf, int nb_sectors)
2240
2254
{
2241
 
    return bdrv_rw_co(bs, sector_num, buf, nb_sectors, false);
 
2255
    return bdrv_rw_co(bs, sector_num, buf, nb_sectors, false, 0);
2242
2256
}
2243
2257
 
2244
2258
/* Just like bdrv_read(), but with I/O throttling temporarily disabled */
2250
2264
 
2251
2265
    enabled = bs->io_limits_enabled;
2252
2266
    bs->io_limits_enabled = false;
2253
 
    ret = bdrv_read(bs, 0, buf, 1);
 
2267
    ret = bdrv_read(bs, sector_num, buf, nb_sectors);
2254
2268
    bs->io_limits_enabled = enabled;
2255
2269
    return ret;
2256
2270
}
2264
2278
int bdrv_write(BlockDriverState *bs, int64_t sector_num,
2265
2279
               const uint8_t *buf, int nb_sectors)
2266
2280
{
2267
 
    return bdrv_rw_co(bs, sector_num, (uint8_t *)buf, nb_sectors, true);
 
2281
    return bdrv_rw_co(bs, sector_num, (uint8_t *)buf, nb_sectors, true, 0);
2268
2282
}
2269
2283
 
2270
2284
int bdrv_writev(BlockDriverState *bs, int64_t sector_num, QEMUIOVector *qiov)
2271
2285
{
2272
 
    return bdrv_rwv_co(bs, sector_num, qiov, true);
 
2286
    return bdrv_rwv_co(bs, sector_num, qiov, true, 0);
 
2287
}
 
2288
 
 
2289
int bdrv_write_zeroes(BlockDriverState *bs, int64_t sector_num, int nb_sectors)
 
2290
{
 
2291
    return bdrv_rw_co(bs, sector_num, NULL, nb_sectors, true,
 
2292
                      BDRV_REQ_ZERO_WRITE);
2273
2293
}
2274
2294
 
2275
2295
int bdrv_pread(BlockDriverState *bs, int64_t offset,
2619
2639
 
2620
2640
    tracked_request_begin(&req, bs, sector_num, nb_sectors, true);
2621
2641
 
2622
 
    if (flags & BDRV_REQ_ZERO_WRITE) {
 
2642
    ret = notifier_with_return_list_notify(&bs->before_write_notifiers, &req);
 
2643
 
 
2644
    if (ret < 0) {
 
2645
        /* Do nothing, write notifier decided to fail this request */
 
2646
    } else if (flags & BDRV_REQ_ZERO_WRITE) {
2623
2647
        ret = bdrv_co_do_write_zeroes(bs, sector_num, nb_sectors);
2624
2648
    } else {
2625
2649
        ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov);
2906
2930
    return bs->open_flags;
2907
2931
}
2908
2932
 
2909
 
void bdrv_flush_all(void)
 
2933
int bdrv_flush_all(void)
2910
2934
{
2911
2935
    BlockDriverState *bs;
 
2936
    int result = 0;
2912
2937
 
2913
2938
    QTAILQ_FOREACH(bs, &bdrv_states, list) {
2914
 
        bdrv_flush(bs);
 
2939
        int ret = bdrv_flush(bs);
 
2940
        if (ret < 0 && !result) {
 
2941
            result = ret;
 
2942
        }
2915
2943
    }
 
2944
 
 
2945
    return result;
 
2946
}
 
2947
 
 
2948
int bdrv_has_zero_init_1(BlockDriverState *bs)
 
2949
{
 
2950
    return 1;
2916
2951
}
2917
2952
 
2918
2953
int bdrv_has_zero_init(BlockDriverState *bs)
2923
2958
        return bs->drv->bdrv_has_zero_init(bs);
2924
2959
    }
2925
2960
 
2926
 
    return 1;
 
2961
    /* safe default */
 
2962
    return 0;
2927
2963
}
2928
2964
 
2929
2965
typedef struct BdrvCoIsAllocatedData {
3099
3135
    return data.ret;
3100
3136
}
3101
3137
 
3102
 
BlockInfo *bdrv_query_info(BlockDriverState *bs)
3103
 
{
3104
 
    BlockInfo *info = g_malloc0(sizeof(*info));
3105
 
    info->device = g_strdup(bs->device_name);
3106
 
    info->type = g_strdup("unknown");
3107
 
    info->locked = bdrv_dev_is_medium_locked(bs);
3108
 
    info->removable = bdrv_dev_has_removable_media(bs);
3109
 
 
3110
 
    if (bdrv_dev_has_removable_media(bs)) {
3111
 
        info->has_tray_open = true;
3112
 
        info->tray_open = bdrv_dev_is_tray_open(bs);
3113
 
    }
3114
 
 
3115
 
    if (bdrv_iostatus_is_enabled(bs)) {
3116
 
        info->has_io_status = true;
3117
 
        info->io_status = bs->iostatus;
3118
 
    }
3119
 
 
3120
 
    if (bs->dirty_bitmap) {
3121
 
        info->has_dirty = true;
3122
 
        info->dirty = g_malloc0(sizeof(*info->dirty));
3123
 
        info->dirty->count = bdrv_get_dirty_count(bs) * BDRV_SECTOR_SIZE;
3124
 
        info->dirty->granularity =
3125
 
            ((int64_t) BDRV_SECTOR_SIZE << hbitmap_granularity(bs->dirty_bitmap));
3126
 
    }
3127
 
 
3128
 
    if (bs->drv) {
3129
 
        info->has_inserted = true;
3130
 
        info->inserted = g_malloc0(sizeof(*info->inserted));
3131
 
        info->inserted->file = g_strdup(bs->filename);
3132
 
        info->inserted->ro = bs->read_only;
3133
 
        info->inserted->drv = g_strdup(bs->drv->format_name);
3134
 
        info->inserted->encrypted = bs->encrypted;
3135
 
        info->inserted->encryption_key_missing = bdrv_key_required(bs);
3136
 
 
3137
 
        if (bs->backing_file[0]) {
3138
 
            info->inserted->has_backing_file = true;
3139
 
            info->inserted->backing_file = g_strdup(bs->backing_file);
3140
 
        }
3141
 
 
3142
 
        info->inserted->backing_file_depth = bdrv_get_backing_file_depth(bs);
3143
 
 
3144
 
        if (bs->io_limits_enabled) {
3145
 
            info->inserted->bps =
3146
 
                           bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL];
3147
 
            info->inserted->bps_rd =
3148
 
                           bs->io_limits.bps[BLOCK_IO_LIMIT_READ];
3149
 
            info->inserted->bps_wr =
3150
 
                           bs->io_limits.bps[BLOCK_IO_LIMIT_WRITE];
3151
 
            info->inserted->iops =
3152
 
                           bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL];
3153
 
            info->inserted->iops_rd =
3154
 
                           bs->io_limits.iops[BLOCK_IO_LIMIT_READ];
3155
 
            info->inserted->iops_wr =
3156
 
                           bs->io_limits.iops[BLOCK_IO_LIMIT_WRITE];
3157
 
        }
3158
 
    }
3159
 
    return info;
3160
 
}
3161
 
 
3162
 
BlockInfoList *qmp_query_block(Error **errp)
3163
 
{
3164
 
    BlockInfoList *head = NULL, **p_next = &head;
3165
 
    BlockDriverState *bs;
3166
 
 
3167
 
    QTAILQ_FOREACH(bs, &bdrv_states, list) {
3168
 
        BlockInfoList *info = g_malloc0(sizeof(*info));
3169
 
        info->value = bdrv_query_info(bs);
3170
 
 
3171
 
        *p_next = info;
3172
 
        p_next = &info->next;
3173
 
    }
3174
 
 
3175
 
    return head;
3176
 
}
3177
 
 
3178
 
BlockStats *bdrv_query_stats(const BlockDriverState *bs)
3179
 
{
3180
 
    BlockStats *s;
3181
 
 
3182
 
    s = g_malloc0(sizeof(*s));
3183
 
 
3184
 
    if (bs->device_name[0]) {
3185
 
        s->has_device = true;
3186
 
        s->device = g_strdup(bs->device_name);
3187
 
    }
3188
 
 
3189
 
    s->stats = g_malloc0(sizeof(*s->stats));
3190
 
    s->stats->rd_bytes = bs->nr_bytes[BDRV_ACCT_READ];
3191
 
    s->stats->wr_bytes = bs->nr_bytes[BDRV_ACCT_WRITE];
3192
 
    s->stats->rd_operations = bs->nr_ops[BDRV_ACCT_READ];
3193
 
    s->stats->wr_operations = bs->nr_ops[BDRV_ACCT_WRITE];
3194
 
    s->stats->wr_highest_offset = bs->wr_highest_sector * BDRV_SECTOR_SIZE;
3195
 
    s->stats->flush_operations = bs->nr_ops[BDRV_ACCT_FLUSH];
3196
 
    s->stats->wr_total_time_ns = bs->total_time_ns[BDRV_ACCT_WRITE];
3197
 
    s->stats->rd_total_time_ns = bs->total_time_ns[BDRV_ACCT_READ];
3198
 
    s->stats->flush_total_time_ns = bs->total_time_ns[BDRV_ACCT_FLUSH];
3199
 
 
3200
 
    if (bs->file) {
3201
 
        s->has_parent = true;
3202
 
        s->parent = bdrv_query_stats(bs->file);
3203
 
    }
3204
 
 
3205
 
    return s;
3206
 
}
3207
 
 
3208
 
BlockStatsList *qmp_query_blockstats(Error **errp)
3209
 
{
3210
 
    BlockStatsList *head = NULL, **p_next = &head;
3211
 
    BlockDriverState *bs;
3212
 
 
3213
 
    QTAILQ_FOREACH(bs, &bdrv_states, list) {
3214
 
        BlockStatsList *info = g_malloc0(sizeof(*info));
3215
 
        info->value = bdrv_query_stats(bs);
3216
 
 
3217
 
        *p_next = info;
3218
 
        p_next = &info->next;
3219
 
    }
3220
 
 
3221
 
    return head;
3222
 
}
3223
 
 
3224
3138
const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
3225
3139
{
3226
3140
    if (bs->backing_hd && bs->backing_hd->encrypted)
3307
3221
 
3308
3222
void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event)
3309
3223
{
3310
 
    BlockDriver *drv = bs->drv;
3311
 
 
3312
 
    if (!drv || !drv->bdrv_debug_event) {
 
3224
    if (!bs || !bs->drv || !bs->drv->bdrv_debug_event) {
3313
3225
        return;
3314
3226
    }
3315
3227
 
3316
 
    drv->bdrv_debug_event(bs, event);
 
3228
    bs->drv->bdrv_debug_event(bs, event);
3317
3229
}
3318
3230
 
3319
3231
int bdrv_debug_breakpoint(BlockDriverState *bs, const char *event,
3356
3268
    return false;
3357
3269
}
3358
3270
 
3359
 
/**************************************************************/
3360
 
/* handling of snapshots */
3361
 
 
3362
 
int bdrv_can_snapshot(BlockDriverState *bs)
3363
 
{
3364
 
    BlockDriver *drv = bs->drv;
3365
 
    if (!drv || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
3366
 
        return 0;
3367
 
    }
3368
 
 
3369
 
    if (!drv->bdrv_snapshot_create) {
3370
 
        if (bs->file != NULL) {
3371
 
            return bdrv_can_snapshot(bs->file);
3372
 
        }
3373
 
        return 0;
3374
 
    }
3375
 
 
3376
 
    return 1;
3377
 
}
3378
 
 
3379
3271
int bdrv_is_snapshot(BlockDriverState *bs)
3380
3272
{
3381
3273
    return !!(bs->open_flags & BDRV_O_SNAPSHOT);
3382
3274
}
3383
3275
 
3384
 
BlockDriverState *bdrv_snapshots(void)
3385
 
{
3386
 
    BlockDriverState *bs;
3387
 
 
3388
 
    if (bs_snapshots) {
3389
 
        return bs_snapshots;
3390
 
    }
3391
 
 
3392
 
    bs = NULL;
3393
 
    while ((bs = bdrv_next(bs))) {
3394
 
        if (bdrv_can_snapshot(bs)) {
3395
 
            bs_snapshots = bs;
3396
 
            return bs;
3397
 
        }
3398
 
    }
3399
 
    return NULL;
3400
 
}
3401
 
 
3402
 
int bdrv_snapshot_create(BlockDriverState *bs,
3403
 
                         QEMUSnapshotInfo *sn_info)
3404
 
{
3405
 
    BlockDriver *drv = bs->drv;
3406
 
    if (!drv)
3407
 
        return -ENOMEDIUM;
3408
 
    if (drv->bdrv_snapshot_create)
3409
 
        return drv->bdrv_snapshot_create(bs, sn_info);
3410
 
    if (bs->file)
3411
 
        return bdrv_snapshot_create(bs->file, sn_info);
3412
 
    return -ENOTSUP;
3413
 
}
3414
 
 
3415
 
int bdrv_snapshot_goto(BlockDriverState *bs,
3416
 
                       const char *snapshot_id)
3417
 
{
3418
 
    BlockDriver *drv = bs->drv;
3419
 
    int ret, open_ret;
3420
 
 
3421
 
    if (!drv)
3422
 
        return -ENOMEDIUM;
3423
 
    if (drv->bdrv_snapshot_goto)
3424
 
        return drv->bdrv_snapshot_goto(bs, snapshot_id);
3425
 
 
3426
 
    if (bs->file) {
3427
 
        drv->bdrv_close(bs);
3428
 
        ret = bdrv_snapshot_goto(bs->file, snapshot_id);
3429
 
        open_ret = drv->bdrv_open(bs, NULL, bs->open_flags);
3430
 
        if (open_ret < 0) {
3431
 
            bdrv_delete(bs->file);
3432
 
            bs->drv = NULL;
3433
 
            return open_ret;
3434
 
        }
3435
 
        return ret;
3436
 
    }
3437
 
 
3438
 
    return -ENOTSUP;
3439
 
}
3440
 
 
3441
 
int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
3442
 
{
3443
 
    BlockDriver *drv = bs->drv;
3444
 
    if (!drv)
3445
 
        return -ENOMEDIUM;
3446
 
    if (drv->bdrv_snapshot_delete)
3447
 
        return drv->bdrv_snapshot_delete(bs, snapshot_id);
3448
 
    if (bs->file)
3449
 
        return bdrv_snapshot_delete(bs->file, snapshot_id);
3450
 
    return -ENOTSUP;
3451
 
}
3452
 
 
3453
 
int bdrv_snapshot_list(BlockDriverState *bs,
3454
 
                       QEMUSnapshotInfo **psn_info)
3455
 
{
3456
 
    BlockDriver *drv = bs->drv;
3457
 
    if (!drv)
3458
 
        return -ENOMEDIUM;
3459
 
    if (drv->bdrv_snapshot_list)
3460
 
        return drv->bdrv_snapshot_list(bs, psn_info);
3461
 
    if (bs->file)
3462
 
        return bdrv_snapshot_list(bs->file, psn_info);
3463
 
    return -ENOTSUP;
3464
 
}
3465
 
 
3466
 
int bdrv_snapshot_load_tmp(BlockDriverState *bs,
3467
 
        const char *snapshot_name)
3468
 
{
3469
 
    BlockDriver *drv = bs->drv;
3470
 
    if (!drv) {
3471
 
        return -ENOMEDIUM;
3472
 
    }
3473
 
    if (!bs->read_only) {
3474
 
        return -EINVAL;
3475
 
    }
3476
 
    if (drv->bdrv_snapshot_load_tmp) {
3477
 
        return drv->bdrv_snapshot_load_tmp(bs, snapshot_name);
3478
 
    }
3479
 
    return -ENOTSUP;
3480
 
}
3481
 
 
3482
3276
/* backing_file can either be relative, or absolute, or a protocol.  If it is
3483
3277
 * relative, it must be relative to the chain.  So, passing in bs->filename
3484
3278
 * from a BDS as backing_file should not be done, as that may be relative to
3574
3368
    return curr_bs;
3575
3369
}
3576
3370
 
3577
 
#define NB_SUFFIXES 4
3578
 
 
3579
 
char *get_human_readable_size(char *buf, int buf_size, int64_t size)
3580
 
{
3581
 
    static const char suffixes[NB_SUFFIXES] = "KMGT";
3582
 
    int64_t base;
3583
 
    int i;
3584
 
 
3585
 
    if (size <= 999) {
3586
 
        snprintf(buf, buf_size, "%" PRId64, size);
3587
 
    } else {
3588
 
        base = 1024;
3589
 
        for(i = 0; i < NB_SUFFIXES; i++) {
3590
 
            if (size < (10 * base)) {
3591
 
                snprintf(buf, buf_size, "%0.1f%c",
3592
 
                         (double)size / base,
3593
 
                         suffixes[i]);
3594
 
                break;
3595
 
            } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
3596
 
                snprintf(buf, buf_size, "%" PRId64 "%c",
3597
 
                         ((size + (base >> 1)) / base),
3598
 
                         suffixes[i]);
3599
 
                break;
3600
 
            }
3601
 
            base = base * 1024;
3602
 
        }
3603
 
    }
3604
 
    return buf;
3605
 
}
3606
 
 
3607
 
char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
3608
 
{
3609
 
    char buf1[128], date_buf[128], clock_buf[128];
3610
 
    struct tm tm;
3611
 
    time_t ti;
3612
 
    int64_t secs;
3613
 
 
3614
 
    if (!sn) {
3615
 
        snprintf(buf, buf_size,
3616
 
                 "%-10s%-20s%7s%20s%15s",
3617
 
                 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
3618
 
    } else {
3619
 
        ti = sn->date_sec;
3620
 
        localtime_r(&ti, &tm);
3621
 
        strftime(date_buf, sizeof(date_buf),
3622
 
                 "%Y-%m-%d %H:%M:%S", &tm);
3623
 
        secs = sn->vm_clock_nsec / 1000000000;
3624
 
        snprintf(clock_buf, sizeof(clock_buf),
3625
 
                 "%02d:%02d:%02d.%03d",
3626
 
                 (int)(secs / 3600),
3627
 
                 (int)((secs / 60) % 60),
3628
 
                 (int)(secs % 60),
3629
 
                 (int)((sn->vm_clock_nsec / 1000000) % 1000));
3630
 
        snprintf(buf, buf_size,
3631
 
                 "%-10s%-20s%7s%20s%15s",
3632
 
                 sn->id_str, sn->name,
3633
 
                 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
3634
 
                 date_buf,
3635
 
                 clock_buf);
3636
 
    }
3637
 
    return buf;
3638
 
}
3639
 
 
3640
3371
/**************************************************************/
3641
3372
/* async I/Os */
3642
3373
 
4326
4057
    }
4327
4058
 
4328
4059
    /* Write back cached data to the OS even with cache=unsafe */
 
4060
    BLKDBG_EVENT(bs->file, BLKDBG_FLUSH_TO_OS);
4329
4061
    if (bs->drv->bdrv_co_flush_to_os) {
4330
4062
        ret = bs->drv->bdrv_co_flush_to_os(bs);
4331
4063
        if (ret < 0) {
4338
4070
        goto flush_parent;
4339
4071
    }
4340
4072
 
 
4073
    BLKDBG_EVENT(bs->file, BLKDBG_FLUSH_TO_DISK);
4341
4074
    if (bs->drv->bdrv_co_flush_to_disk) {
4342
4075
        ret = bs->drv->bdrv_co_flush_to_disk(bs);
4343
4076
    } else if (bs->drv->bdrv_aio_flush) {
4752
4485
        return;
4753
4486
    }
4754
4487
 
4755
 
    proto_drv = bdrv_find_protocol(filename);
 
4488
    proto_drv = bdrv_find_protocol(filename, true);
4756
4489
    if (!proto_drv) {
4757
4490
        error_setg(errp, "Unknown protocol '%s'", filename);
4758
4491
        return;
4883
4616
    /* Currently BlockDriverState always uses the main loop AioContext */
4884
4617
    return qemu_get_aio_context();
4885
4618
}
 
4619
 
 
4620
void bdrv_add_before_write_notifier(BlockDriverState *bs,
 
4621
                                    NotifierWithReturn *notifier)
 
4622
{
 
4623
    notifier_with_return_list_add(&bs->before_write_notifiers, notifier);
 
4624
}