331
static int bdrv_is_whitelisted(BlockDriver *drv)
329
static int bdrv_is_whitelisted(BlockDriver *drv, bool read_only)
333
static const char *whitelist[] = {
334
CONFIG_BDRV_WHITELIST
331
static const char *whitelist_rw[] = {
332
CONFIG_BDRV_RW_WHITELIST
334
static const char *whitelist_ro[] = {
335
CONFIG_BDRV_RO_WHITELIST
339
if (!whitelist_rw[0] && !whitelist_ro[0]) {
339
340
return 1; /* no whitelist, anything goes */
341
for (p = whitelist; *p; p++) {
343
for (p = whitelist_rw; *p; p++) {
342
344
if (!strcmp(drv->format_name, *p)) {
349
for (p = whitelist_ro; *p; p++) {
350
if (!strcmp(drv->format_name, *p)) {
349
BlockDriver *bdrv_find_whitelisted_format(const char *format_name)
358
BlockDriver *bdrv_find_whitelisted_format(const char *format_name,
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;
355
365
typedef struct CreateCo {
801
814
/* Find the right block driver */
802
815
drvname = qdict_get_try_str(options, "driver");
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);
822
qerror_report(ERROR_CLASS_GENERIC_ERROR, "Unknown protocol");
809
825
qerror_report(ERROR_CLASS_GENERIC_ERROR,
810
826
"Must specify either driver or file");
1037
1054
extract_subqdict(options, &file_options, "file.");
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));
1045
1062
/* Find the right image format driver */
1063
drvname = qdict_get_try_str(options, "driver");
1065
drv = bdrv_find_whitelisted_format(drvname, !(flags & BDRV_O_RDWR));
1066
qdict_del(options, "driver");
1047
1070
ret = find_image_format(file, filename, &drv);
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,
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,
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);
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)
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);
2244
2258
/* Just like bdrv_read(), but with I/O throttling temporarily disabled */
2264
2278
int bdrv_write(BlockDriverState *bs, int64_t sector_num,
2265
2279
const uint8_t *buf, int nb_sectors)
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);
2270
2284
int bdrv_writev(BlockDriverState *bs, int64_t sector_num, QEMUIOVector *qiov)
2272
return bdrv_rwv_co(bs, sector_num, qiov, true);
2286
return bdrv_rwv_co(bs, sector_num, qiov, true, 0);
2289
int bdrv_write_zeroes(BlockDriverState *bs, int64_t sector_num, int nb_sectors)
2291
return bdrv_rw_co(bs, sector_num, NULL, nb_sectors, true,
2292
BDRV_REQ_ZERO_WRITE);
2275
2295
int bdrv_pread(BlockDriverState *bs, int64_t offset,
2620
2640
tracked_request_begin(&req, bs, sector_num, nb_sectors, true);
2622
if (flags & BDRV_REQ_ZERO_WRITE) {
2642
ret = notifier_with_return_list_notify(&bs->before_write_notifiers, &req);
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);
2625
2649
ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov);
3099
3135
return data.ret;
3102
BlockInfo *bdrv_query_info(BlockDriverState *bs)
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);
3110
if (bdrv_dev_has_removable_media(bs)) {
3111
info->has_tray_open = true;
3112
info->tray_open = bdrv_dev_is_tray_open(bs);
3115
if (bdrv_iostatus_is_enabled(bs)) {
3116
info->has_io_status = true;
3117
info->io_status = bs->iostatus;
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));
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);
3137
if (bs->backing_file[0]) {
3138
info->inserted->has_backing_file = true;
3139
info->inserted->backing_file = g_strdup(bs->backing_file);
3142
info->inserted->backing_file_depth = bdrv_get_backing_file_depth(bs);
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];
3162
BlockInfoList *qmp_query_block(Error **errp)
3164
BlockInfoList *head = NULL, **p_next = &head;
3165
BlockDriverState *bs;
3167
QTAILQ_FOREACH(bs, &bdrv_states, list) {
3168
BlockInfoList *info = g_malloc0(sizeof(*info));
3169
info->value = bdrv_query_info(bs);
3172
p_next = &info->next;
3178
BlockStats *bdrv_query_stats(const BlockDriverState *bs)
3182
s = g_malloc0(sizeof(*s));
3184
if (bs->device_name[0]) {
3185
s->has_device = true;
3186
s->device = g_strdup(bs->device_name);
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];
3201
s->has_parent = true;
3202
s->parent = bdrv_query_stats(bs->file);
3208
BlockStatsList *qmp_query_blockstats(Error **errp)
3210
BlockStatsList *head = NULL, **p_next = &head;
3211
BlockDriverState *bs;
3213
QTAILQ_FOREACH(bs, &bdrv_states, list) {
3214
BlockStatsList *info = g_malloc0(sizeof(*info));
3215
info->value = bdrv_query_stats(bs);
3218
p_next = &info->next;
3224
3138
const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
3226
3140
if (bs->backing_hd && bs->backing_hd->encrypted)
3359
/**************************************************************/
3360
/* handling of snapshots */
3362
int bdrv_can_snapshot(BlockDriverState *bs)
3364
BlockDriver *drv = bs->drv;
3365
if (!drv || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
3369
if (!drv->bdrv_snapshot_create) {
3370
if (bs->file != NULL) {
3371
return bdrv_can_snapshot(bs->file);
3379
3271
int bdrv_is_snapshot(BlockDriverState *bs)
3381
3273
return !!(bs->open_flags & BDRV_O_SNAPSHOT);
3384
BlockDriverState *bdrv_snapshots(void)
3386
BlockDriverState *bs;
3389
return bs_snapshots;
3393
while ((bs = bdrv_next(bs))) {
3394
if (bdrv_can_snapshot(bs)) {
3402
int bdrv_snapshot_create(BlockDriverState *bs,
3403
QEMUSnapshotInfo *sn_info)
3405
BlockDriver *drv = bs->drv;
3408
if (drv->bdrv_snapshot_create)
3409
return drv->bdrv_snapshot_create(bs, sn_info);
3411
return bdrv_snapshot_create(bs->file, sn_info);
3415
int bdrv_snapshot_goto(BlockDriverState *bs,
3416
const char *snapshot_id)
3418
BlockDriver *drv = bs->drv;
3423
if (drv->bdrv_snapshot_goto)
3424
return drv->bdrv_snapshot_goto(bs, snapshot_id);
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);
3431
bdrv_delete(bs->file);
3441
int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
3443
BlockDriver *drv = bs->drv;
3446
if (drv->bdrv_snapshot_delete)
3447
return drv->bdrv_snapshot_delete(bs, snapshot_id);
3449
return bdrv_snapshot_delete(bs->file, snapshot_id);
3453
int bdrv_snapshot_list(BlockDriverState *bs,
3454
QEMUSnapshotInfo **psn_info)
3456
BlockDriver *drv = bs->drv;
3459
if (drv->bdrv_snapshot_list)
3460
return drv->bdrv_snapshot_list(bs, psn_info);
3462
return bdrv_snapshot_list(bs->file, psn_info);
3466
int bdrv_snapshot_load_tmp(BlockDriverState *bs,
3467
const char *snapshot_name)
3469
BlockDriver *drv = bs->drv;
3473
if (!bs->read_only) {
3476
if (drv->bdrv_snapshot_load_tmp) {
3477
return drv->bdrv_snapshot_load_tmp(bs, snapshot_name);
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;
3577
#define NB_SUFFIXES 4
3579
char *get_human_readable_size(char *buf, int buf_size, int64_t size)
3581
static const char suffixes[NB_SUFFIXES] = "KMGT";
3586
snprintf(buf, buf_size, "%" PRId64, size);
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,
3595
} else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
3596
snprintf(buf, buf_size, "%" PRId64 "%c",
3597
((size + (base >> 1)) / base),
3607
char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
3609
char buf1[128], date_buf[128], clock_buf[128];
3615
snprintf(buf, buf_size,
3616
"%-10s%-20s%7s%20s%15s",
3617
"ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
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",
3627
(int)((secs / 60) % 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),
3640
3371
/**************************************************************/
3641
3372
/* async I/Os */