vdi: Implement .bdrv_co_preadv() interface

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Reviewed-by: Fam Zheng <famz@redhat.com>
This commit is contained in:
Kevin Wolf 2016-04-25 16:22:39 +02:00
parent 3edf1e73d5
commit 0865bb6f04

View File

@ -557,47 +557,56 @@ static int64_t coroutine_fn vdi_co_get_block_status(BlockDriverState *bs,
return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | offset; return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | offset;
} }
static int vdi_co_read(BlockDriverState *bs, static int coroutine_fn
int64_t sector_num, uint8_t *buf, int nb_sectors) vdi_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
QEMUIOVector *qiov, int flags)
{ {
BDRVVdiState *s = bs->opaque; BDRVVdiState *s = bs->opaque;
QEMUIOVector local_qiov;
uint32_t bmap_entry; uint32_t bmap_entry;
uint32_t block_index; uint32_t block_index;
uint32_t sector_in_block; uint32_t offset_in_block;
uint32_t n_sectors; uint32_t n_bytes;
uint64_t bytes_done = 0;
int ret = 0; int ret = 0;
logout("\n"); logout("\n");
while (ret >= 0 && nb_sectors > 0) { qemu_iovec_init(&local_qiov, qiov->niov);
block_index = sector_num / s->block_sectors;
sector_in_block = sector_num % s->block_sectors;
n_sectors = s->block_sectors - sector_in_block;
if (n_sectors > nb_sectors) {
n_sectors = nb_sectors;
}
logout("will read %u sectors starting at sector %" PRIu64 "\n", while (ret >= 0 && bytes > 0) {
n_sectors, sector_num); block_index = offset / s->block_size;
offset_in_block = offset % s->block_size;
n_bytes = MIN(bytes, s->block_size - offset_in_block);
logout("will read %u bytes starting at offset %" PRIu64 "\n",
n_bytes, offset);
/* prepare next AIO request */ /* prepare next AIO request */
bmap_entry = le32_to_cpu(s->bmap[block_index]); bmap_entry = le32_to_cpu(s->bmap[block_index]);
if (!VDI_IS_ALLOCATED(bmap_entry)) { if (!VDI_IS_ALLOCATED(bmap_entry)) {
/* Block not allocated, return zeros, no need to wait. */ /* Block not allocated, return zeros, no need to wait. */
memset(buf, 0, n_sectors * SECTOR_SIZE); qemu_iovec_memset(qiov, bytes_done, 0, n_bytes);
ret = 0; ret = 0;
} else { } else {
uint64_t offset = s->header.offset_data / SECTOR_SIZE + uint64_t data_offset = s->header.offset_data +
(uint64_t)bmap_entry * s->block_sectors + (uint64_t)bmap_entry * s->block_size +
sector_in_block; offset_in_block;
ret = bdrv_read(bs->file->bs, offset, buf, n_sectors);
}
logout("%u sectors read\n", n_sectors);
nb_sectors -= n_sectors; qemu_iovec_reset(&local_qiov);
sector_num += n_sectors; qemu_iovec_concat(&local_qiov, qiov, bytes_done, n_bytes);
buf += n_sectors * SECTOR_SIZE;
ret = bdrv_co_preadv(bs->file->bs, data_offset, n_bytes,
&local_qiov, 0);
} }
logout("%u bytes read\n", n_bytes);
bytes -= n_bytes;
offset += n_bytes;
bytes_done += n_bytes;
}
qemu_iovec_destroy(&local_qiov);
return ret; return ret;
} }
@ -903,7 +912,7 @@ static BlockDriver bdrv_vdi = {
.bdrv_co_get_block_status = vdi_co_get_block_status, .bdrv_co_get_block_status = vdi_co_get_block_status,
.bdrv_make_empty = vdi_make_empty, .bdrv_make_empty = vdi_make_empty,
.bdrv_read = vdi_co_read, .bdrv_co_preadv = vdi_co_preadv,
#if defined(CONFIG_VDI_WRITE) #if defined(CONFIG_VDI_WRITE)
.bdrv_write = vdi_co_write, .bdrv_write = vdi_co_write,
#endif #endif