Dynamically allocate AIO Completion Blocks.

git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@2098 c046a42c-6fe2-441c-8c8c-71466251a162
This commit is contained in:
pbrook 2006-08-07 02:38:06 +00:00
parent 51d6bae7a8
commit ce1a14dc0d
6 changed files with 486 additions and 551 deletions

View File

@ -522,7 +522,8 @@ static int qcow_write(BlockDriverState *bs, int64_t sector_num,
return 0; return 0;
} }
typedef struct { typedef struct QCowAIOCB {
BlockDriverAIOCB common;
int64_t sector_num; int64_t sector_num;
uint8_t *buf; uint8_t *buf;
int nb_sectors; int nb_sectors;
@ -530,223 +531,198 @@ typedef struct {
uint64_t cluster_offset; uint64_t cluster_offset;
uint8_t *cluster_data; uint8_t *cluster_data;
BlockDriverAIOCB *hd_aiocb; BlockDriverAIOCB *hd_aiocb;
BlockDriverAIOCB *backing_hd_aiocb;
} QCowAIOCB; } QCowAIOCB;
static void qcow_aio_delete(BlockDriverAIOCB *acb);
static int qcow_aio_new(BlockDriverAIOCB *acb)
{
BlockDriverState *bs = acb->bs;
BDRVQcowState *s = bs->opaque;
QCowAIOCB *acb1;
acb1 = qemu_mallocz(sizeof(QCowAIOCB));
if (!acb1)
return -1;
acb->opaque = acb1;
acb1->hd_aiocb = bdrv_aio_new(s->hd);
if (!acb1->hd_aiocb)
goto fail;
if (bs->backing_hd) {
acb1->backing_hd_aiocb = bdrv_aio_new(bs->backing_hd);
if (!acb1->backing_hd_aiocb)
goto fail;
}
return 0;
fail:
qcow_aio_delete(acb);
return -1;
}
static void qcow_aio_read_cb(void *opaque, int ret) static void qcow_aio_read_cb(void *opaque, int ret)
{ {
BlockDriverAIOCB *acb = opaque; QCowAIOCB *acb = opaque;
BlockDriverState *bs = acb->bs; BlockDriverState *bs = acb->common.bs;
BDRVQcowState *s = bs->opaque; BDRVQcowState *s = bs->opaque;
QCowAIOCB *acb1 = acb->opaque;
int index_in_cluster; int index_in_cluster;
acb->hd_aiocb = NULL;
if (ret < 0) { if (ret < 0) {
fail: fail:
acb->cb(acb->cb_opaque, ret); acb->common.cb(acb->common.opaque, ret);
qemu_aio_release(acb);
return; return;
} }
redo: redo:
/* post process the read buffer */ /* post process the read buffer */
if (!acb1->cluster_offset) { if (!acb->cluster_offset) {
/* nothing to do */ /* nothing to do */
} else if (acb1->cluster_offset & QCOW_OFLAG_COMPRESSED) { } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) {
/* nothing to do */ /* nothing to do */
} else { } else {
if (s->crypt_method) { if (s->crypt_method) {
encrypt_sectors(s, acb1->sector_num, acb1->buf, acb1->buf, encrypt_sectors(s, acb->sector_num, acb->buf, acb->buf,
acb1->n, 0, acb->n, 0,
&s->aes_decrypt_key); &s->aes_decrypt_key);
} }
} }
acb1->nb_sectors -= acb1->n; acb->nb_sectors -= acb->n;
acb1->sector_num += acb1->n; acb->sector_num += acb->n;
acb1->buf += acb1->n * 512; acb->buf += acb->n * 512;
if (acb1->nb_sectors == 0) { if (acb->nb_sectors == 0) {
/* request completed */ /* request completed */
acb->cb(acb->cb_opaque, 0); acb->common.cb(acb->common.opaque, 0);
qemu_aio_release(acb);
return; return;
} }
/* prepare next AIO request */ /* prepare next AIO request */
acb1->cluster_offset = get_cluster_offset(bs, acb->cluster_offset = get_cluster_offset(bs, acb->sector_num << 9,
acb1->sector_num << 9, 0, 0, 0, 0);
0, 0, 0, 0); index_in_cluster = acb->sector_num & (s->cluster_sectors - 1);
index_in_cluster = acb1->sector_num & (s->cluster_sectors - 1); acb->n = s->cluster_sectors - index_in_cluster;
acb1->n = s->cluster_sectors - index_in_cluster; if (acb->n > acb->nb_sectors)
if (acb1->n > acb1->nb_sectors) acb->n = acb->nb_sectors;
acb1->n = acb1->nb_sectors;
if (!acb1->cluster_offset) { if (!acb->cluster_offset) {
if (bs->backing_hd) { if (bs->backing_hd) {
/* read from the base image */ /* read from the base image */
ret = bdrv_aio_read(acb1->backing_hd_aiocb, acb1->sector_num, acb->hd_aiocb = bdrv_aio_read(bs->backing_hd,
acb1->buf, acb1->n, qcow_aio_read_cb, acb); acb->sector_num, acb->buf, acb->n, qcow_aio_read_cb, acb);
if (ret < 0) if (acb->hd_aiocb == NULL)
goto fail; goto fail;
} else { } else {
/* Note: in this case, no need to wait */ /* Note: in this case, no need to wait */
memset(acb1->buf, 0, 512 * acb1->n); memset(acb->buf, 0, 512 * acb->n);
goto redo; goto redo;
} }
} else if (acb1->cluster_offset & QCOW_OFLAG_COMPRESSED) { } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) {
/* add AIO support for compressed blocks ? */ /* add AIO support for compressed blocks ? */
if (decompress_cluster(s, acb1->cluster_offset) < 0) if (decompress_cluster(s, acb->cluster_offset) < 0)
goto fail; goto fail;
memcpy(acb1->buf, memcpy(acb->buf,
s->cluster_cache + index_in_cluster * 512, 512 * acb1->n); s->cluster_cache + index_in_cluster * 512, 512 * acb->n);
goto redo; goto redo;
} else { } else {
if ((acb1->cluster_offset & 511) != 0) { if ((acb->cluster_offset & 511) != 0) {
ret = -EIO; ret = -EIO;
goto fail; goto fail;
} }
ret = bdrv_aio_read(acb1->hd_aiocb, acb->hd_aiocb = bdrv_aio_read(s->hd,
(acb1->cluster_offset >> 9) + index_in_cluster, (acb->cluster_offset >> 9) + index_in_cluster,
acb1->buf, acb1->n, qcow_aio_read_cb, acb); acb->buf, acb->n, qcow_aio_read_cb, acb);
if (ret < 0) if (acb->hd_aiocb == NULL)
goto fail; goto fail;
} }
} }
static int qcow_aio_read(BlockDriverAIOCB *acb, int64_t sector_num, static BlockDriverAIOCB *qcow_aio_read(BlockDriverState *bs,
uint8_t *buf, int nb_sectors) int64_t sector_num, uint8_t *buf, int nb_sectors,
BlockDriverCompletionFunc *cb, void *opaque)
{ {
QCowAIOCB *acb1 = acb->opaque; QCowAIOCB *acb;
acb1->sector_num = sector_num; acb = qemu_aio_get(bs, cb, opaque);
acb1->buf = buf; if (!acb)
acb1->nb_sectors = nb_sectors; return NULL;
acb1->n = 0; acb->hd_aiocb = NULL;
acb1->cluster_offset = 0; acb->sector_num = sector_num;
acb->buf = buf;
acb->nb_sectors = nb_sectors;
acb->n = 0;
acb->cluster_offset = 0;
qcow_aio_read_cb(acb, 0); qcow_aio_read_cb(acb, 0);
return 0; return &acb->common;
} }
static void qcow_aio_write_cb(void *opaque, int ret) static void qcow_aio_write_cb(void *opaque, int ret)
{ {
BlockDriverAIOCB *acb = opaque; QCowAIOCB *acb = opaque;
BlockDriverState *bs = acb->bs; BlockDriverState *bs = acb->common.bs;
BDRVQcowState *s = bs->opaque; BDRVQcowState *s = bs->opaque;
QCowAIOCB *acb1 = acb->opaque;
int index_in_cluster; int index_in_cluster;
uint64_t cluster_offset; uint64_t cluster_offset;
const uint8_t *src_buf; const uint8_t *src_buf;
acb->hd_aiocb = NULL;
if (ret < 0) { if (ret < 0) {
fail: fail:
acb->cb(acb->cb_opaque, ret); acb->common.cb(acb->common.opaque, ret);
qemu_aio_release(acb);
return; return;
} }
acb1->nb_sectors -= acb1->n; acb->nb_sectors -= acb->n;
acb1->sector_num += acb1->n; acb->sector_num += acb->n;
acb1->buf += acb1->n * 512; acb->buf += acb->n * 512;
if (acb1->nb_sectors == 0) { if (acb->nb_sectors == 0) {
/* request completed */ /* request completed */
acb->cb(acb->cb_opaque, 0); acb->common.cb(acb->common.opaque, 0);
qemu_aio_release(acb);
return; return;
} }
index_in_cluster = acb1->sector_num & (s->cluster_sectors - 1); index_in_cluster = acb->sector_num & (s->cluster_sectors - 1);
acb1->n = s->cluster_sectors - index_in_cluster; acb->n = s->cluster_sectors - index_in_cluster;
if (acb1->n > acb1->nb_sectors) if (acb->n > acb->nb_sectors)
acb1->n = acb1->nb_sectors; acb->n = acb->nb_sectors;
cluster_offset = get_cluster_offset(bs, acb1->sector_num << 9, 1, 0, cluster_offset = get_cluster_offset(bs, acb->sector_num << 9, 1, 0,
index_in_cluster, index_in_cluster,
index_in_cluster + acb1->n); index_in_cluster + acb->n);
if (!cluster_offset || (cluster_offset & 511) != 0) { if (!cluster_offset || (cluster_offset & 511) != 0) {
ret = -EIO; ret = -EIO;
goto fail; goto fail;
} }
if (s->crypt_method) { if (s->crypt_method) {
if (!acb1->cluster_data) { if (!acb->cluster_data) {
acb1->cluster_data = qemu_mallocz(s->cluster_size); acb->cluster_data = qemu_mallocz(s->cluster_size);
if (!acb1->cluster_data) { if (!acb->cluster_data) {
ret = -ENOMEM; ret = -ENOMEM;
goto fail; goto fail;
} }
} }
encrypt_sectors(s, acb1->sector_num, acb1->cluster_data, acb1->buf, encrypt_sectors(s, acb->sector_num, acb->cluster_data, acb->buf,
acb1->n, 1, &s->aes_encrypt_key); acb->n, 1, &s->aes_encrypt_key);
src_buf = acb1->cluster_data; src_buf = acb->cluster_data;
} else { } else {
src_buf = acb1->buf; src_buf = acb->buf;
} }
ret = bdrv_aio_write(acb1->hd_aiocb, acb->hd_aiocb = bdrv_aio_write(s->hd,
(cluster_offset >> 9) + index_in_cluster, (cluster_offset >> 9) + index_in_cluster,
src_buf, acb1->n, src_buf, acb->n,
qcow_aio_write_cb, acb); qcow_aio_write_cb, acb);
if (ret < 0) if (acb->hd_aiocb == NULL)
goto fail; goto fail;
} }
static int qcow_aio_write(BlockDriverAIOCB *acb, int64_t sector_num, static BlockDriverAIOCB *qcow_aio_write(BlockDriverState *bs,
const uint8_t *buf, int nb_sectors) int64_t sector_num, const uint8_t *buf, int nb_sectors,
BlockDriverCompletionFunc *cb, void *opaque)
{ {
QCowAIOCB *acb1 = acb->opaque;
BlockDriverState *bs = acb->bs;
BDRVQcowState *s = bs->opaque; BDRVQcowState *s = bs->opaque;
QCowAIOCB *acb;
s->cluster_cache_offset = -1; /* disable compressed cache */ s->cluster_cache_offset = -1; /* disable compressed cache */
acb1->sector_num = sector_num; acb = qemu_aio_get(bs, cb, opaque);
acb1->buf = (uint8_t *)buf; if (!acb)
acb1->nb_sectors = nb_sectors; return NULL;
acb1->n = 0; acb->hd_aiocb = NULL;
acb->sector_num = sector_num;
acb->buf = (uint8_t *)buf;
acb->nb_sectors = nb_sectors;
acb->n = 0;
qcow_aio_write_cb(acb, 0); qcow_aio_write_cb(acb, 0);
return 0; return &acb->common;
} }
static void qcow_aio_cancel(BlockDriverAIOCB *acb) static void qcow_aio_cancel(BlockDriverAIOCB *blockacb)
{ {
QCowAIOCB *acb1 = acb->opaque; QCowAIOCB *acb = (QCowAIOCB *)blockacb;
if (acb1->hd_aiocb) if (acb->hd_aiocb)
bdrv_aio_cancel(acb1->hd_aiocb); bdrv_aio_cancel(acb->hd_aiocb);
if (acb1->backing_hd_aiocb) qemu_aio_release(acb);
bdrv_aio_cancel(acb1->backing_hd_aiocb);
}
static void qcow_aio_delete(BlockDriverAIOCB *acb)
{
QCowAIOCB *acb1 = acb->opaque;
if (acb1->hd_aiocb)
bdrv_aio_delete(acb1->hd_aiocb);
if (acb1->backing_hd_aiocb)
bdrv_aio_delete(acb1->backing_hd_aiocb);
qemu_free(acb1->cluster_data);
qemu_free(acb1);
} }
static void qcow_close(BlockDriverState *bs) static void qcow_close(BlockDriverState *bs)
@ -920,11 +896,10 @@ BlockDriver bdrv_qcow = {
qcow_set_key, qcow_set_key,
qcow_make_empty, qcow_make_empty,
.bdrv_aio_new = qcow_aio_new,
.bdrv_aio_read = qcow_aio_read, .bdrv_aio_read = qcow_aio_read,
.bdrv_aio_write = qcow_aio_write, .bdrv_aio_write = qcow_aio_write,
.bdrv_aio_cancel = qcow_aio_cancel, .bdrv_aio_cancel = qcow_aio_cancel,
.bdrv_aio_delete = qcow_aio_delete, .aiocb_size = sizeof(QCowAIOCB),
.bdrv_write_compressed = qcow_write_compressed, .bdrv_write_compressed = qcow_write_compressed,
.bdrv_get_info = qcow_get_info, .bdrv_get_info = qcow_get_info,
}; };

View File

@ -791,7 +791,8 @@ static int qcow_write(BlockDriverState *bs, int64_t sector_num,
return 0; return 0;
} }
typedef struct { typedef struct QCowAIOCB {
BlockDriverAIOCB common;
int64_t sector_num; int64_t sector_num;
uint8_t *buf; uint8_t *buf;
int nb_sectors; int nb_sectors;
@ -799,229 +800,211 @@ typedef struct {
uint64_t cluster_offset; uint64_t cluster_offset;
uint8_t *cluster_data; uint8_t *cluster_data;
BlockDriverAIOCB *hd_aiocb; BlockDriverAIOCB *hd_aiocb;
BlockDriverAIOCB *backing_hd_aiocb;
} QCowAIOCB; } QCowAIOCB;
static void qcow_aio_delete(BlockDriverAIOCB *acb);
static int qcow_aio_new(BlockDriverAIOCB *acb)
{
BlockDriverState *bs = acb->bs;
BDRVQcowState *s = bs->opaque;
QCowAIOCB *acb1;
acb1 = qemu_mallocz(sizeof(QCowAIOCB));
if (!acb1)
return -1;
acb->opaque = acb1;
acb1->hd_aiocb = bdrv_aio_new(s->hd);
if (!acb1->hd_aiocb)
goto fail;
if (bs->backing_hd) {
acb1->backing_hd_aiocb = bdrv_aio_new(bs->backing_hd);
if (!acb1->backing_hd_aiocb)
goto fail;
}
return 0;
fail:
qcow_aio_delete(acb);
return -1;
}
static void qcow_aio_read_cb(void *opaque, int ret) static void qcow_aio_read_cb(void *opaque, int ret)
{ {
BlockDriverAIOCB *acb = opaque; QCowAIOCB *acb = opaque;
BlockDriverState *bs = acb->bs; BlockDriverState *bs = acb->common.bs;
BDRVQcowState *s = bs->opaque; BDRVQcowState *s = bs->opaque;
QCowAIOCB *acb1 = acb->opaque;
int index_in_cluster, n1; int index_in_cluster, n1;
acb->hd_aiocb = NULL;
if (ret < 0) { if (ret < 0) {
fail: fail:
acb->cb(acb->cb_opaque, ret); acb->common.cb(acb->common.opaque, ret);
qemu_aio_release(acb);
return; return;
} }
redo: redo:
/* post process the read buffer */ /* post process the read buffer */
if (!acb1->cluster_offset) { if (!acb->cluster_offset) {
/* nothing to do */ /* nothing to do */
} else if (acb1->cluster_offset & QCOW_OFLAG_COMPRESSED) { } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) {
/* nothing to do */ /* nothing to do */
} else { } else {
if (s->crypt_method) { if (s->crypt_method) {
encrypt_sectors(s, acb1->sector_num, acb1->buf, acb1->buf, encrypt_sectors(s, acb->sector_num, acb->buf, acb->buf,
acb1->n, 0, acb->n, 0,
&s->aes_decrypt_key); &s->aes_decrypt_key);
} }
} }
acb1->nb_sectors -= acb1->n; acb->nb_sectors -= acb->n;
acb1->sector_num += acb1->n; acb->sector_num += acb->n;
acb1->buf += acb1->n * 512; acb->buf += acb->n * 512;
if (acb1->nb_sectors == 0) { if (acb->nb_sectors == 0) {
/* request completed */ /* request completed */
acb->cb(acb->cb_opaque, 0); acb->common.cb(acb->common.opaque, 0);
qemu_aio_release(acb);
return; return;
} }
/* prepare next AIO request */ /* prepare next AIO request */
acb1->cluster_offset = get_cluster_offset(bs, acb->cluster_offset = get_cluster_offset(bs, acb->sector_num << 9,
acb1->sector_num << 9, 0, 0, 0, 0);
0, 0, 0, 0); index_in_cluster = acb->sector_num & (s->cluster_sectors - 1);
index_in_cluster = acb1->sector_num & (s->cluster_sectors - 1); acb->n = s->cluster_sectors - index_in_cluster;
acb1->n = s->cluster_sectors - index_in_cluster; if (acb->n > acb->nb_sectors)
if (acb1->n > acb1->nb_sectors) acb->n = acb->nb_sectors;
acb1->n = acb1->nb_sectors;
if (!acb1->cluster_offset) { if (!acb->cluster_offset) {
if (bs->backing_hd) { if (bs->backing_hd) {
/* read from the base image */ /* read from the base image */
n1 = backing_read1(bs->backing_hd, acb1->sector_num, n1 = backing_read1(bs->backing_hd, acb->sector_num,
acb1->buf, acb1->n); acb->buf, acb->n);
if (n1 > 0) { if (n1 > 0) {
ret = bdrv_aio_read(acb1->backing_hd_aiocb, acb1->sector_num, acb->hd_aiocb = bdrv_aio_read(bs->backing_hd, acb->sector_num,
acb1->buf, n1, qcow_aio_read_cb, acb); acb->buf, acb->n, qcow_aio_read_cb, acb);
if (ret < 0) if (acb->hd_aiocb == NULL)
goto fail; goto fail;
} else { } else {
goto redo; goto redo;
} }
} else { } else {
/* Note: in this case, no need to wait */ /* Note: in this case, no need to wait */
memset(acb1->buf, 0, 512 * acb1->n); memset(acb->buf, 0, 512 * acb->n);
goto redo; goto redo;
} }
} else if (acb1->cluster_offset & QCOW_OFLAG_COMPRESSED) { } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) {
/* add AIO support for compressed blocks ? */ /* add AIO support for compressed blocks ? */
if (decompress_cluster(s, acb1->cluster_offset) < 0) if (decompress_cluster(s, acb->cluster_offset) < 0)
goto fail; goto fail;
memcpy(acb1->buf, memcpy(acb->buf,
s->cluster_cache + index_in_cluster * 512, 512 * acb1->n); s->cluster_cache + index_in_cluster * 512, 512 * acb->n);
goto redo; goto redo;
} else { } else {
if ((acb1->cluster_offset & 511) != 0) { if ((acb->cluster_offset & 511) != 0) {
ret = -EIO; ret = -EIO;
goto fail; goto fail;
} }
ret = bdrv_aio_read(acb1->hd_aiocb, acb->hd_aiocb = bdrv_aio_read(s->hd,
(acb1->cluster_offset >> 9) + index_in_cluster, (acb->cluster_offset >> 9) + index_in_cluster,
acb1->buf, acb1->n, qcow_aio_read_cb, acb); acb->buf, acb->n, qcow_aio_read_cb, acb);
if (ret < 0) if (acb->hd_aiocb == NULL)
goto fail; goto fail;
} }
} }
static int qcow_aio_read(BlockDriverAIOCB *acb, int64_t sector_num, static QCowAIOCB *qcow_aio_setup(BlockDriverState *bs,
uint8_t *buf, int nb_sectors) int64_t sector_num, uint8_t *buf, int nb_sectors,
BlockDriverCompletionFunc *cb, void *opaque)
{ {
QCowAIOCB *acb1 = acb->opaque; QCowAIOCB *acb;
acb1->sector_num = sector_num; acb = qemu_aio_get(bs, cb, opaque);
acb1->buf = buf; if (!acb)
acb1->nb_sectors = nb_sectors; return NULL;
acb1->n = 0; acb->hd_aiocb = NULL;
acb1->cluster_offset = 0; acb->sector_num = sector_num;
acb->buf = buf;
acb->nb_sectors = nb_sectors;
acb->n = 0;
acb->cluster_offset = 0;
return acb;
}
static BlockDriverAIOCB *qcow_aio_read(BlockDriverState *bs,
int64_t sector_num, uint8_t *buf, int nb_sectors,
BlockDriverCompletionFunc *cb, void *opaque)
{
QCowAIOCB *acb;
acb = qcow_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
if (!acb)
return NULL;
qcow_aio_read_cb(acb, 0); qcow_aio_read_cb(acb, 0);
return 0; return &acb->common;
} }
static void qcow_aio_write_cb(void *opaque, int ret) static void qcow_aio_write_cb(void *opaque, int ret)
{ {
BlockDriverAIOCB *acb = opaque; QCowAIOCB *acb = opaque;
BlockDriverState *bs = acb->bs; BlockDriverState *bs = acb->common.bs;
BDRVQcowState *s = bs->opaque; BDRVQcowState *s = bs->opaque;
QCowAIOCB *acb1 = acb->opaque;
int index_in_cluster; int index_in_cluster;
uint64_t cluster_offset; uint64_t cluster_offset;
const uint8_t *src_buf; const uint8_t *src_buf;
acb->hd_aiocb = NULL;
if (ret < 0) { if (ret < 0) {
fail: fail:
acb->cb(acb->cb_opaque, ret); acb->common.cb(acb->common.opaque, ret);
qemu_aio_release(acb);
return; return;
} }
acb1->nb_sectors -= acb1->n; acb->nb_sectors -= acb->n;
acb1->sector_num += acb1->n; acb->sector_num += acb->n;
acb1->buf += acb1->n * 512; acb->buf += acb->n * 512;
if (acb1->nb_sectors == 0) { if (acb->nb_sectors == 0) {
/* request completed */ /* request completed */
acb->cb(acb->cb_opaque, 0); acb->common.cb(acb->common.opaque, 0);
qemu_aio_release(acb);
return; return;
} }
index_in_cluster = acb1->sector_num & (s->cluster_sectors - 1); index_in_cluster = acb->sector_num & (s->cluster_sectors - 1);
acb1->n = s->cluster_sectors - index_in_cluster; acb->n = s->cluster_sectors - index_in_cluster;
if (acb1->n > acb1->nb_sectors) if (acb->n > acb->nb_sectors)
acb1->n = acb1->nb_sectors; acb->n = acb->nb_sectors;
cluster_offset = get_cluster_offset(bs, acb1->sector_num << 9, 1, 0, cluster_offset = get_cluster_offset(bs, acb->sector_num << 9, 1, 0,
index_in_cluster, index_in_cluster,
index_in_cluster + acb1->n); index_in_cluster + acb->n);
if (!cluster_offset || (cluster_offset & 511) != 0) { if (!cluster_offset || (cluster_offset & 511) != 0) {
ret = -EIO; ret = -EIO;
goto fail; goto fail;
} }
if (s->crypt_method) { if (s->crypt_method) {
if (!acb1->cluster_data) { if (!acb->cluster_data) {
acb1->cluster_data = qemu_mallocz(s->cluster_size); acb->cluster_data = qemu_mallocz(s->cluster_size);
if (!acb1->cluster_data) { if (!acb->cluster_data) {
ret = -ENOMEM; ret = -ENOMEM;
goto fail; goto fail;
} }
} }
encrypt_sectors(s, acb1->sector_num, acb1->cluster_data, acb1->buf, encrypt_sectors(s, acb->sector_num, acb->cluster_data, acb->buf,
acb1->n, 1, &s->aes_encrypt_key); acb->n, 1, &s->aes_encrypt_key);
src_buf = acb1->cluster_data; src_buf = acb->cluster_data;
} else { } else {
src_buf = acb1->buf; src_buf = acb->buf;
} }
ret = bdrv_aio_write(acb1->hd_aiocb, acb->hd_aiocb = bdrv_aio_write(s->hd,
(cluster_offset >> 9) + index_in_cluster, (cluster_offset >> 9) + index_in_cluster,
src_buf, acb1->n, src_buf, acb->n,
qcow_aio_write_cb, acb); qcow_aio_write_cb, acb);
if (ret < 0) if (acb->hd_aiocb == NULL)
goto fail; goto fail;
} }
static int qcow_aio_write(BlockDriverAIOCB *acb, int64_t sector_num, static BlockDriverAIOCB *qcow_aio_write(BlockDriverState *bs,
const uint8_t *buf, int nb_sectors) int64_t sector_num, const uint8_t *buf, int nb_sectors,
BlockDriverCompletionFunc *cb, void *opaque)
{ {
QCowAIOCB *acb1 = acb->opaque;
BlockDriverState *bs = acb->bs;
BDRVQcowState *s = bs->opaque; BDRVQcowState *s = bs->opaque;
QCowAIOCB *acb;
s->cluster_cache_offset = -1; /* disable compressed cache */ s->cluster_cache_offset = -1; /* disable compressed cache */
acb1->sector_num = sector_num; acb = qcow_aio_setup(bs, sector_num, (uint8_t*)buf, nb_sectors, cb, opaque);
acb1->buf = (uint8_t *)buf; if (!acb)
acb1->nb_sectors = nb_sectors; return NULL;
acb1->n = 0;
qcow_aio_write_cb(acb, 0); qcow_aio_write_cb(acb, 0);
return 0; return &acb->common;
} }
static void qcow_aio_cancel(BlockDriverAIOCB *acb) static void qcow_aio_cancel(BlockDriverAIOCB *blockacb)
{ {
QCowAIOCB *acb1 = acb->opaque; QCowAIOCB *acb = (QCowAIOCB *)blockacb;
if (acb1->hd_aiocb) if (acb->hd_aiocb)
bdrv_aio_cancel(acb1->hd_aiocb); bdrv_aio_cancel(acb->hd_aiocb);
if (acb1->backing_hd_aiocb) qemu_aio_release(acb);
bdrv_aio_cancel(acb1->backing_hd_aiocb);
}
static void qcow_aio_delete(BlockDriverAIOCB *acb)
{
QCowAIOCB *acb1 = acb->opaque;
if (acb1->hd_aiocb)
bdrv_aio_delete(acb1->hd_aiocb);
if (acb1->backing_hd_aiocb)
bdrv_aio_delete(acb1->backing_hd_aiocb);
qemu_free(acb1->cluster_data);
qemu_free(acb1);
} }
static void qcow_close(BlockDriverState *bs) static void qcow_close(BlockDriverState *bs)
@ -2249,11 +2232,10 @@ BlockDriver bdrv_qcow2 = {
qcow_set_key, qcow_set_key,
qcow_make_empty, qcow_make_empty,
.bdrv_aio_new = qcow_aio_new,
.bdrv_aio_read = qcow_aio_read, .bdrv_aio_read = qcow_aio_read,
.bdrv_aio_write = qcow_aio_write, .bdrv_aio_write = qcow_aio_write,
.bdrv_aio_cancel = qcow_aio_cancel, .bdrv_aio_cancel = qcow_aio_cancel,
.bdrv_aio_delete = qcow_aio_delete, .aiocb_size = sizeof(QCowAIOCB),
.bdrv_write_compressed = qcow_write_compressed, .bdrv_write_compressed = qcow_write_compressed,
.bdrv_snapshot_create = qcow_snapshot_create, .bdrv_snapshot_create = qcow_snapshot_create,

View File

@ -200,13 +200,13 @@ static int raw_pwrite(BlockDriverState *bs, int64_t offset,
/* Unix AOP using POSIX AIO */ /* Unix AOP using POSIX AIO */
typedef struct RawAIOCB { typedef struct RawAIOCB {
BlockDriverAIOCB common;
struct aiocb aiocb; struct aiocb aiocb;
int busy; /* only used for debugging */ struct RawAIOCB *next;
BlockDriverAIOCB *next;
} RawAIOCB; } RawAIOCB;
static int aio_sig_num = SIGUSR2; static int aio_sig_num = SIGUSR2;
static BlockDriverAIOCB *first_aio; /* AIO issued */ static RawAIOCB *first_aio; /* AIO issued */
static int aio_initialized = 0; static int aio_initialized = 0;
static void aio_signal_handler(int signum) static void aio_signal_handler(int signum)
@ -249,8 +249,7 @@ void qemu_aio_init(void)
void qemu_aio_poll(void) void qemu_aio_poll(void)
{ {
BlockDriverAIOCB *acb, **pacb; RawAIOCB *acb, **pacb;
RawAIOCB *acb1;
int ret; int ret;
for(;;) { for(;;) {
@ -259,17 +258,16 @@ void qemu_aio_poll(void)
acb = *pacb; acb = *pacb;
if (!acb) if (!acb)
goto the_end; goto the_end;
acb1 = acb->opaque; ret = aio_error(&acb->aiocb);
ret = aio_error(&acb1->aiocb);
if (ret == ECANCELED) { if (ret == ECANCELED) {
/* remove the request */ /* remove the request */
acb1->busy = 0; *pacb = acb->next;
*pacb = acb1->next; qemu_aio_release(acb);
} else if (ret != EINPROGRESS) { } else if (ret != EINPROGRESS) {
/* end of aio */ /* end of aio */
if (ret == 0) { if (ret == 0) {
ret = aio_return(&acb1->aiocb); ret = aio_return(&acb->aiocb);
if (ret == acb1->aiocb.aio_nbytes) if (ret == acb->aiocb.aio_nbytes)
ret = 0; ret = 0;
else else
ret = -1; ret = -1;
@ -277,13 +275,13 @@ void qemu_aio_poll(void)
ret = -ret; ret = -ret;
} }
/* remove the request */ /* remove the request */
acb1->busy = 0; *pacb = acb->next;
*pacb = acb1->next;
/* call the callback */ /* call the callback */
acb->cb(acb->cb_opaque, ret); acb->common.cb(acb->common.opaque, ret);
qemu_aio_release(acb);
break; break;
} else { } else {
pacb = &acb1->next; pacb = &acb->next;
} }
} }
} }
@ -324,70 +322,70 @@ void qemu_aio_wait_end(void)
sigprocmask(SIG_SETMASK, &wait_oset, NULL); sigprocmask(SIG_SETMASK, &wait_oset, NULL);
} }
static int raw_aio_new(BlockDriverAIOCB *acb) static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
int64_t sector_num, uint8_t *buf, int nb_sectors,
BlockDriverCompletionFunc *cb, void *opaque)
{ {
RawAIOCB *acb1; BDRVRawState *s = bs->opaque;
BDRVRawState *s = acb->bs->opaque; RawAIOCB *acb;
acb1 = qemu_mallocz(sizeof(RawAIOCB)); acb = qemu_aio_get(bs, cb, opaque);
if (!acb1) if (!acb)
return -1; return NULL;
acb->opaque = acb1; acb->aiocb.aio_fildes = s->fd;
acb1->aiocb.aio_fildes = s->fd; acb->aiocb.aio_sigevent.sigev_signo = aio_sig_num;
acb1->aiocb.aio_sigevent.sigev_signo = aio_sig_num; acb->aiocb.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
acb1->aiocb.aio_sigevent.sigev_notify = SIGEV_SIGNAL; acb->aiocb.aio_buf = buf;
return 0; acb->aiocb.aio_nbytes = nb_sectors * 512;
} acb->aiocb.aio_offset = sector_num * 512;
acb->next = first_aio;
static int raw_aio_read(BlockDriverAIOCB *acb, int64_t sector_num,
uint8_t *buf, int nb_sectors)
{
RawAIOCB *acb1 = acb->opaque;
assert(acb1->busy == 0);
acb1->busy = 1;
acb1->aiocb.aio_buf = buf;
acb1->aiocb.aio_nbytes = nb_sectors * 512;
acb1->aiocb.aio_offset = sector_num * 512;
acb1->next = first_aio;
first_aio = acb; first_aio = acb;
if (aio_read(&acb1->aiocb) < 0) { return acb;
acb1->busy = 0;
return -errno;
}
return 0;
} }
static int raw_aio_write(BlockDriverAIOCB *acb, int64_t sector_num, static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
const uint8_t *buf, int nb_sectors) int64_t sector_num, uint8_t *buf, int nb_sectors,
BlockDriverCompletionFunc *cb, void *opaque)
{ {
RawAIOCB *acb1 = acb->opaque; RawAIOCB *acb;
assert(acb1->busy == 0); acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
acb1->busy = 1; if (!acb)
acb1->aiocb.aio_buf = (uint8_t *)buf; return NULL;
acb1->aiocb.aio_nbytes = nb_sectors * 512; if (aio_read(&acb->aiocb) < 0) {
acb1->aiocb.aio_offset = sector_num * 512; qemu_aio_release(acb);
acb1->next = first_aio; return NULL;
first_aio = acb;
if (aio_write(&acb1->aiocb) < 0) {
acb1->busy = 0;
return -errno;
} }
return 0; return &acb->common;
} }
static void raw_aio_cancel(BlockDriverAIOCB *acb) static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
int64_t sector_num, const uint8_t *buf, int nb_sectors,
BlockDriverCompletionFunc *cb, void *opaque)
{
RawAIOCB *acb;
acb = raw_aio_setup(bs, sector_num, (uint8_t*)buf, nb_sectors, cb, opaque);
if (!acb)
return NULL;
if (aio_write(&acb->aiocb) < 0) {
qemu_aio_release(acb);
return NULL;
}
return &acb->common;
}
static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
{ {
RawAIOCB *acb1 = acb->opaque;
int ret; int ret;
BlockDriverAIOCB **pacb; RawAIOCB *acb = (RawAIOCB *)blockacb;
RawAIOCB **pacb;
ret = aio_cancel(acb1->aiocb.aio_fildes, &acb1->aiocb); ret = aio_cancel(acb->aiocb.aio_fildes, &acb->aiocb);
if (ret == AIO_NOTCANCELED) { if (ret == AIO_NOTCANCELED) {
/* fail safe: if the aio could not be canceled, we wait for /* fail safe: if the aio could not be canceled, we wait for
it */ it */
while (aio_error(&acb1->aiocb) == EINPROGRESS); while (aio_error(&acb->aiocb) == EINPROGRESS);
} }
/* remove the callback from the queue */ /* remove the callback from the queue */
@ -396,22 +394,14 @@ static void raw_aio_cancel(BlockDriverAIOCB *acb)
if (*pacb == NULL) { if (*pacb == NULL) {
break; break;
} else if (*pacb == acb) { } else if (*pacb == acb) {
acb1->busy = 0; *pacb = acb->next;
*pacb = acb1->next; qemu_aio_release(acb);
break; break;
} }
acb1 = (*pacb)->opaque; pacb = &acb->next;
pacb = &acb1->next;
} }
} }
static void raw_aio_delete(BlockDriverAIOCB *acb)
{
RawAIOCB *acb1 = acb->opaque;
raw_aio_cancel(acb);
qemu_free(acb1);
}
static void raw_close(BlockDriverState *bs) static void raw_close(BlockDriverState *bs)
{ {
BDRVRawState *s = bs->opaque; BDRVRawState *s = bs->opaque;
@ -508,11 +498,10 @@ BlockDriver bdrv_raw = {
raw_create, raw_create,
raw_flush, raw_flush,
.bdrv_aio_new = raw_aio_new,
.bdrv_aio_read = raw_aio_read, .bdrv_aio_read = raw_aio_read,
.bdrv_aio_write = raw_aio_write, .bdrv_aio_write = raw_aio_write,
.bdrv_aio_cancel = raw_aio_cancel, .bdrv_aio_cancel = raw_aio_cancel,
.bdrv_aio_delete = raw_aio_delete, .aiocb_size = sizeof(RawAIOCB),
.protocol_name = "file", .protocol_name = "file",
.bdrv_pread = raw_pread, .bdrv_pread = raw_pread,
.bdrv_pwrite = raw_pwrite, .bdrv_pwrite = raw_pwrite,
@ -530,6 +519,7 @@ typedef struct BDRVRawState {
} BDRVRawState; } BDRVRawState;
typedef struct RawAIOCB { typedef struct RawAIOCB {
BlockDriverAIOCB common;
HANDLE hEvent; HANDLE hEvent;
OVERLAPPED ov; OVERLAPPED ov;
int count; int count;
@ -574,6 +564,7 @@ static int raw_open(BlockDriverState *bs, const char *filename, int flags)
{ {
BDRVRawState *s = bs->opaque; BDRVRawState *s = bs->opaque;
int access_flags, create_flags; int access_flags, create_flags;
DWORD overlapped;
if ((flags & BDRV_O_ACCESS) == O_RDWR) { if ((flags & BDRV_O_ACCESS) == O_RDWR) {
access_flags = GENERIC_READ | GENERIC_WRITE; access_flags = GENERIC_READ | GENERIC_WRITE;
@ -585,9 +576,14 @@ static int raw_open(BlockDriverState *bs, const char *filename, int flags)
} else { } else {
create_flags = OPEN_EXISTING; create_flags = OPEN_EXISTING;
} }
#ifdef QEMU_TOOL
overlapped = 0;
#else
overlapped = FILE_FLAG_OVERLAPPED;
#endif
s->hfile = CreateFile(filename, access_flags, s->hfile = CreateFile(filename, access_flags,
FILE_SHARE_READ, NULL, FILE_SHARE_READ, NULL,
create_flags, FILE_FLAG_OVERLAPPED, 0); create_flags, overlapped, 0);
if (s->hfile == INVALID_HANDLE_VALUE) if (s->hfile == INVALID_HANDLE_VALUE)
return -1; return -1;
return 0; return 0;
@ -637,104 +633,107 @@ static int raw_pwrite(BlockDriverState *bs, int64_t offset,
return ret_count; return ret_count;
} }
static int raw_aio_new(BlockDriverAIOCB *acb)
{
RawAIOCB *acb1;
acb1 = qemu_mallocz(sizeof(RawAIOCB));
if (!acb1)
return -ENOMEM;
acb->opaque = acb1;
acb1->hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (!acb1->hEvent)
return -ENOMEM;
return 0;
}
#ifndef QEMU_TOOL #ifndef QEMU_TOOL
static void raw_aio_cb(void *opaque) static void raw_aio_cb(void *opaque)
{ {
BlockDriverAIOCB *acb = opaque; RawAIOCB *acb = opaque;
BlockDriverState *bs = acb->bs; BlockDriverState *bs = acb->common.bs;
BDRVRawState *s = bs->opaque; BDRVRawState *s = bs->opaque;
RawAIOCB *acb1 = acb->opaque;
DWORD ret_count; DWORD ret_count;
int ret; int ret;
ret = GetOverlappedResult(s->hfile, &acb1->ov, &ret_count, TRUE); ret = GetOverlappedResult(s->hfile, &acb->ov, &ret_count, TRUE);
if (!ret || ret_count != acb1->count) { if (!ret || ret_count != acb->count) {
acb->cb(acb->cb_opaque, -EIO); acb->common.cb(acb->common.opaque, -EIO);
} else { } else {
acb->cb(acb->cb_opaque, 0); acb->common.cb(acb->common.opaque, 0);
} }
} }
#endif #endif
static int raw_aio_read(BlockDriverAIOCB *acb, int64_t sector_num,
uint8_t *buf, int nb_sectors) static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
int64_t sector_num, uint8_t *buf, int nb_sectors,
BlockDriverCompletionFunc *cb, void *opaque)
{ {
BlockDriverState *bs = acb->bs; RawAIOCB *acb;
BDRVRawState *s = bs->opaque;
RawAIOCB *acb1 = acb->opaque;
int ret;
int64_t offset; int64_t offset;
memset(&acb1->ov, 0, sizeof(acb1->ov)); acb = qemu_aio_get(bs, cb, opaque);
if (acb->hEvent) {
acb->hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (!acb->hEvent) {
qemu_aio_release(acb);
return NULL;
}
}
memset(&acb->ov, 0, sizeof(acb->ov));
offset = sector_num * 512; offset = sector_num * 512;
acb1->ov.Offset = offset; acb->ov.Offset = offset;
acb1->ov.OffsetHigh = offset >> 32; acb->ov.OffsetHigh = offset >> 32;
acb1->ov.hEvent = acb1->hEvent; acb->ov.hEvent = acb->hEvent;
acb1->count = nb_sectors * 512; acb->count = nb_sectors * 512;
#ifndef QEMU_TOOL #ifndef QEMU_TOOL
qemu_add_wait_object(acb1->ov.hEvent, raw_aio_cb, acb); qemu_add_wait_object(acb->ov.hEvent, raw_aio_cb, acb);
#endif #endif
ret = ReadFile(s->hfile, buf, acb1->count, NULL, &acb1->ov); return acb;
if (!ret)
return -EIO;
return 0;
} }
static int raw_aio_write(BlockDriverAIOCB *acb, int64_t sector_num, static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
uint8_t *buf, int nb_sectors) int64_t sector_num, uint8_t *buf, int nb_sectors,
BlockDriverCompletionFunc *cb, void *opaque)
{ {
BlockDriverState *bs = acb->bs;
BDRVRawState *s = bs->opaque; BDRVRawState *s = bs->opaque;
RawAIOCB *acb1 = acb->opaque; RawAIOCB *acb;
int ret; int ret;
int64_t offset;
memset(&acb1->ov, 0, sizeof(acb1->ov)); acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
offset = sector_num * 512; if (!acb)
acb1->ov.Offset = offset; return NULL;
acb1->ov.OffsetHigh = offset >> 32; ret = ReadFile(s->hfile, buf, acb->count, NULL, &acb->ov);
acb1->ov.hEvent = acb1->hEvent; if (!ret) {
acb1->count = nb_sectors * 512; qemu_aio_release(acb);
#ifndef QEMU_TOOL return NULL;
qemu_add_wait_object(acb1->ov.hEvent, raw_aio_cb, acb); }
#ifdef QEMU_TOOL
qemu_aio_release(acb);
#endif #endif
ret = ReadFile(s->hfile, buf, acb1->count, NULL, &acb1->ov); return (BlockDriverAIOCB *)acb;
if (!ret)
return -EIO;
return 0;
} }
static void raw_aio_cancel(BlockDriverAIOCB *acb) static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
int64_t sector_num, uint8_t *buf, int nb_sectors,
BlockDriverCompletionFunc *cb, void *opaque)
{ {
BlockDriverState *bs = acb->bs;
BDRVRawState *s = bs->opaque; BDRVRawState *s = bs->opaque;
#ifndef QEMU_TOOL RawAIOCB *acb;
RawAIOCB *acb1 = acb->opaque; int ret;
qemu_del_wait_object(acb1->ov.hEvent, raw_aio_cb, acb); acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
if (!acb)
return NULL;
ret = WriteFile(s->hfile, buf, acb->count, NULL, &acb->ov);
if (!ret) {
qemu_aio_release(acb);
return NULL;
}
#ifdef QEMU_TOOL
qemu_aio_release(acb);
#endif #endif
return (BlockDriverAIOCB *)acb;
}
static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
{
#ifndef QEMU_TOOL
RawAIOCB *acb = (RawAIOCB *)blockacb;
BlockDriverState *bs = acb->common.bs;
BDRVRawState *s = bs->opaque;
qemu_del_wait_object(acb->ov.hEvent, raw_aio_cb, acb);
/* XXX: if more than one async I/O it is not correct */ /* XXX: if more than one async I/O it is not correct */
CancelIo(s->hfile); CancelIo(s->hfile);
} qemu_aio_release(acb);
#endif
static void raw_aio_delete(BlockDriverAIOCB *acb)
{
RawAIOCB *acb1 = acb->opaque;
raw_aio_cancel(acb);
CloseHandle(acb1->hEvent);
qemu_free(acb1);
} }
static void raw_flush(BlockDriverState *bs) static void raw_flush(BlockDriverState *bs)
@ -823,11 +822,10 @@ BlockDriver bdrv_raw = {
raw_flush, raw_flush,
#if 0 #if 0
.bdrv_aio_new = raw_aio_new,
.bdrv_aio_read = raw_aio_read, .bdrv_aio_read = raw_aio_read,
.bdrv_aio_write = raw_aio_write, .bdrv_aio_write = raw_aio_write,
.bdrv_aio_cancel = raw_aio_cancel, .bdrv_aio_cancel = raw_aio_cancel,
.bdrv_aio_delete = raw_aio_delete, .aiocb_size = sizeof(RawAIOCB);
#endif #endif
.protocol_name = "file", .protocol_name = "file",
.bdrv_pread = raw_pread, .bdrv_pread = raw_pread,

235
block.c
View File

@ -35,13 +35,13 @@
#define SECTOR_BITS 9 #define SECTOR_BITS 9
#define SECTOR_SIZE (1 << SECTOR_BITS) #define SECTOR_SIZE (1 << SECTOR_BITS)
static int bdrv_aio_new_em(BlockDriverAIOCB *acb); static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
static int bdrv_aio_read_em(BlockDriverAIOCB *acb, int64_t sector_num, int64_t sector_num, uint8_t *buf, int nb_sectors,
uint8_t *buf, int nb_sectors); BlockDriverCompletionFunc *cb, void *opaque);
static int bdrv_aio_write_em(BlockDriverAIOCB *acb, int64_t sector_num, static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
const uint8_t *buf, int nb_sectors); int64_t sector_num, const uint8_t *buf, int nb_sectors,
BlockDriverCompletionFunc *cb, void *opaque);
static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb); static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb);
static void bdrv_aio_delete_em(BlockDriverAIOCB *acb);
static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
uint8_t *buf, int nb_sectors); uint8_t *buf, int nb_sectors);
static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
@ -106,13 +106,11 @@ void path_combine(char *dest, int dest_size,
void bdrv_register(BlockDriver *bdrv) void bdrv_register(BlockDriver *bdrv)
{ {
if (!bdrv->bdrv_aio_new) { if (!bdrv->bdrv_aio_read) {
/* add AIO emulation layer */ /* add AIO emulation layer */
bdrv->bdrv_aio_new = bdrv_aio_new_em;
bdrv->bdrv_aio_read = bdrv_aio_read_em; bdrv->bdrv_aio_read = bdrv_aio_read_em;
bdrv->bdrv_aio_write = bdrv_aio_write_em; bdrv->bdrv_aio_write = bdrv_aio_write_em;
bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em; bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em;
bdrv->bdrv_aio_delete = bdrv_aio_delete_em;
} else if (!bdrv->bdrv_read && !bdrv->bdrv_pread) { } else if (!bdrv->bdrv_read && !bdrv->bdrv_pread) {
/* add synchronous IO emulation layer */ /* add synchronous IO emulation layer */
bdrv->bdrv_read = bdrv_read_em; bdrv->bdrv_read = bdrv_read_em;
@ -964,7 +962,9 @@ char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
"ID", "TAG", "VM SIZE", "DATE", "VM CLOCK"); "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
} else { } else {
ti = sn->date_sec; ti = sn->date_sec;
#ifndef _WIN32
localtime_r(&ti, &tm); localtime_r(&ti, &tm);
#endif
strftime(date_buf, sizeof(date_buf), strftime(date_buf, sizeof(date_buf),
"%Y-%m-%d %H:%M:%S", &tm); "%Y-%m-%d %H:%M:%S", &tm);
secs = sn->vm_clock_nsec / 1000000000; secs = sn->vm_clock_nsec / 1000000000;
@ -988,31 +988,14 @@ char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
/**************************************************************/ /**************************************************************/
/* async I/Os */ /* async I/Os */
BlockDriverAIOCB *bdrv_aio_new(BlockDriverState *bs) BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
uint8_t *buf, int nb_sectors,
BlockDriverCompletionFunc *cb, void *opaque)
{ {
BlockDriver *drv = bs->drv; BlockDriver *drv = bs->drv;
BlockDriverAIOCB *acb;
acb = qemu_mallocz(sizeof(BlockDriverAIOCB));
if (!acb)
return NULL;
acb->bs = bs;
if (drv->bdrv_aio_new(acb) < 0) {
qemu_free(acb);
return NULL;
}
return acb;
}
int bdrv_aio_read(BlockDriverAIOCB *acb, int64_t sector_num,
uint8_t *buf, int nb_sectors,
BlockDriverCompletionFunc *cb, void *opaque)
{
BlockDriverState *bs = acb->bs;
BlockDriver *drv = bs->drv;
if (!bs->inserted) if (!bs->inserted)
return -1; return NULL;
/* XXX: we assume that nb_sectors == 0 is suppored by the async read */ /* XXX: we assume that nb_sectors == 0 is suppored by the async read */
if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) { if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
@ -1022,141 +1005,114 @@ int bdrv_aio_read(BlockDriverAIOCB *acb, int64_t sector_num,
buf += 512; buf += 512;
} }
acb->cb = cb; return drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque);
acb->cb_opaque = opaque;
return drv->bdrv_aio_read(acb, sector_num, buf, nb_sectors);
} }
int bdrv_aio_write(BlockDriverAIOCB *acb, int64_t sector_num, BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
const uint8_t *buf, int nb_sectors, const uint8_t *buf, int nb_sectors,
BlockDriverCompletionFunc *cb, void *opaque) BlockDriverCompletionFunc *cb, void *opaque)
{ {
BlockDriverState *bs = acb->bs;
BlockDriver *drv = bs->drv; BlockDriver *drv = bs->drv;
if (!bs->inserted) if (!bs->inserted)
return -1; return NULL;
if (bs->read_only) if (bs->read_only)
return -1; return NULL;
if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) { if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
memcpy(bs->boot_sector_data, buf, 512); memcpy(bs->boot_sector_data, buf, 512);
} }
acb->cb = cb; return drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque);
acb->cb_opaque = opaque;
return drv->bdrv_aio_write(acb, sector_num, buf, nb_sectors);
} }
void bdrv_aio_cancel(BlockDriverAIOCB *acb) void bdrv_aio_cancel(BlockDriverAIOCB *acb)
{ {
BlockDriverState *bs = acb->bs; BlockDriver *drv = acb->bs->drv;
BlockDriver *drv = bs->drv;
drv->bdrv_aio_cancel(acb); drv->bdrv_aio_cancel(acb);
}
void bdrv_aio_delete(BlockDriverAIOCB *acb)
{
BlockDriverState *bs = acb->bs;
BlockDriver *drv = bs->drv;
drv->bdrv_aio_delete(acb);
qemu_free(acb);
} }
/**************************************************************/ /**************************************************************/
/* async block device emulation */ /* async block device emulation */
#ifdef QEMU_TOOL #ifdef QEMU_TOOL
static int bdrv_aio_new_em(BlockDriverAIOCB *acb) static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
{ int64_t sector_num, uint8_t *buf, int nb_sectors,
return 0; BlockDriverCompletionFunc *cb, void *opaque)
}
static int bdrv_aio_read_em(BlockDriverAIOCB *acb, int64_t sector_num,
uint8_t *buf, int nb_sectors)
{ {
int ret; int ret;
ret = bdrv_read(acb->bs, sector_num, buf, nb_sectors); ret = bdrv_read(bs, sector_num, buf, nb_sectors);
acb->cb(acb->cb_opaque, ret); cb(opaque, ret);
return 0; return NULL;
} }
static int bdrv_aio_write_em(BlockDriverAIOCB *acb, int64_t sector_num, static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
const uint8_t *buf, int nb_sectors) int64_t sector_num, const uint8_t *buf, int nb_sectors,
BlockDriverCompletionFunc *cb, void *opaque)
{ {
int ret; int ret;
ret = bdrv_write(acb->bs, sector_num, buf, nb_sectors); ret = bdrv_write(bs, sector_num, buf, nb_sectors);
acb->cb(acb->cb_opaque, ret); cb(opaque, ret);
return 0; return NULL;
} }
static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb) static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb)
{ {
} }
static void bdrv_aio_delete_em(BlockDriverAIOCB *acb)
{
}
#else #else
typedef struct BlockDriverAIOCBSync { typedef struct BlockDriverAIOCBSync {
BlockDriverAIOCB common;
QEMUBH *bh; QEMUBH *bh;
int ret; int ret;
} BlockDriverAIOCBSync; } BlockDriverAIOCBSync;
static BlockDriverAIOCBSync *free_acb = NULL;
static void bdrv_aio_bh_cb(void *opaque) static void bdrv_aio_bh_cb(void *opaque)
{ {
BlockDriverAIOCB *acb = opaque; BlockDriverAIOCBSync *acb = opaque;
BlockDriverAIOCBSync *acb1 = acb->opaque; acb->common.cb(acb->common.opaque, acb->ret);
acb->cb(acb->cb_opaque, acb1->ret); qemu_aio_release(acb);
} }
static int bdrv_aio_new_em(BlockDriverAIOCB *acb) static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
int64_t sector_num, uint8_t *buf, int nb_sectors,
BlockDriverCompletionFunc *cb, void *opaque)
{ {
BlockDriverAIOCBSync *acb1; BlockDriverAIOCBSync *acb;
acb1 = qemu_mallocz(sizeof(BlockDriverAIOCBSync));
if (!acb1)
return -1;
acb->opaque = acb1;
acb1->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
return 0;
}
static int bdrv_aio_read_em(BlockDriverAIOCB *acb, int64_t sector_num,
uint8_t *buf, int nb_sectors)
{
BlockDriverAIOCBSync *acb1 = acb->opaque;
int ret; int ret;
ret = bdrv_read(acb->bs, sector_num, buf, nb_sectors); acb = qemu_aio_get(bs, cb, opaque);
acb1->ret = ret; if (!acb->bh)
qemu_bh_schedule(acb1->bh); acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
return 0; ret = bdrv_read(bs, sector_num, buf, nb_sectors);
acb->ret = ret;
qemu_bh_schedule(acb->bh);
return &acb->common;
} }
static int bdrv_aio_write_em(BlockDriverAIOCB *acb, int64_t sector_num, static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
const uint8_t *buf, int nb_sectors) int64_t sector_num, const uint8_t *buf, int nb_sectors,
BlockDriverCompletionFunc *cb, void *opaque)
{ {
BlockDriverAIOCBSync *acb1 = acb->opaque; BlockDriverAIOCBSync *acb;
int ret; int ret;
ret = bdrv_write(acb->bs, sector_num, buf, nb_sectors); acb = qemu_aio_get(bs, cb, opaque);
acb1->ret = ret; if (!acb->bh)
qemu_bh_schedule(acb1->bh); acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
return 0; ret = bdrv_write(bs, sector_num, buf, nb_sectors);
acb->ret = ret;
qemu_bh_schedule(acb->bh);
return &acb->common;
} }
static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb) static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
{ {
BlockDriverAIOCBSync *acb1 = acb->opaque; BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
qemu_bh_cancel(acb1->bh); qemu_bh_cancel(acb->bh);
} qemu_aio_release(acb);
static void bdrv_aio_delete_em(BlockDriverAIOCB *acb)
{
BlockDriverAIOCBSync *acb1 = acb->opaque;
qemu_bh_delete(acb1->bh);
} }
#endif /* !QEMU_TOOL */ #endif /* !QEMU_TOOL */
@ -1173,20 +1129,16 @@ static void bdrv_rw_em_cb(void *opaque, int ret)
static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
uint8_t *buf, int nb_sectors) uint8_t *buf, int nb_sectors)
{ {
int async_ret, ret; int async_ret;
BlockDriverAIOCB *acb;
if (!bs->sync_aiocb) {
bs->sync_aiocb = bdrv_aio_new(bs);
if (!bs->sync_aiocb)
return -1;
}
async_ret = NOT_DONE; async_ret = NOT_DONE;
qemu_aio_wait_start(); qemu_aio_wait_start();
ret = bdrv_aio_read(bs->sync_aiocb, sector_num, buf, nb_sectors, acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors,
bdrv_rw_em_cb, &async_ret); bdrv_rw_em_cb, &async_ret);
if (ret < 0) { if (acb == NULL) {
qemu_aio_wait_end(); qemu_aio_wait_end();
return ret; return -1;
} }
while (async_ret == NOT_DONE) { while (async_ret == NOT_DONE) {
qemu_aio_wait(); qemu_aio_wait();
@ -1198,20 +1150,16 @@ static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
const uint8_t *buf, int nb_sectors) const uint8_t *buf, int nb_sectors)
{ {
int async_ret, ret; int async_ret;
BlockDriverAIOCB *acb;
if (!bs->sync_aiocb) {
bs->sync_aiocb = bdrv_aio_new(bs);
if (!bs->sync_aiocb)
return -1;
}
async_ret = NOT_DONE; async_ret = NOT_DONE;
qemu_aio_wait_start(); qemu_aio_wait_start();
ret = bdrv_aio_write(bs->sync_aiocb, sector_num, buf, nb_sectors, acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors,
bdrv_rw_em_cb, &async_ret); bdrv_rw_em_cb, &async_ret);
if (ret < 0) { if (acb == NULL) {
qemu_aio_wait_end(); qemu_aio_wait_end();
return ret; return -1;
} }
while (async_ret == NOT_DONE) { while (async_ret == NOT_DONE) {
qemu_aio_wait(); qemu_aio_wait();
@ -1235,3 +1183,32 @@ void bdrv_init(void)
bdrv_register(&bdrv_vvfat); bdrv_register(&bdrv_vvfat);
bdrv_register(&bdrv_qcow2); bdrv_register(&bdrv_qcow2);
} }
void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
void *opaque)
{
BlockDriver *drv;
BlockDriverAIOCB *acb;
drv = bs->drv;
if (drv->free_aiocb) {
acb = drv->free_aiocb;
drv->free_aiocb = acb->next;
} else {
acb = qemu_mallocz(drv->aiocb_size);
if (!acb)
return NULL;
}
acb->bs = bs;
acb->cb = cb;
acb->opaque = opaque;
return acb;
}
void qemu_aio_release(void *p)
{
BlockDriverAIOCB *acb = p;
BlockDriver *drv = acb->bs->drv;
acb->next = drv->free_aiocb;
drv->free_aiocb = acb;
}

View File

@ -42,13 +42,14 @@ struct BlockDriver {
int (*bdrv_set_key)(BlockDriverState *bs, const char *key); int (*bdrv_set_key)(BlockDriverState *bs, const char *key);
int (*bdrv_make_empty)(BlockDriverState *bs); int (*bdrv_make_empty)(BlockDriverState *bs);
/* aio */ /* aio */
int (*bdrv_aio_new)(BlockDriverAIOCB *acb); BlockDriverAIOCB *(*bdrv_aio_read)(BlockDriverState *bs,
int (*bdrv_aio_read)(BlockDriverAIOCB *acb, int64_t sector_num, int64_t sector_num, uint8_t *buf, int nb_sectors,
uint8_t *buf, int nb_sectors); BlockDriverCompletionFunc *cb, void *opaque);
int (*bdrv_aio_write)(BlockDriverAIOCB *acb, int64_t sector_num, BlockDriverAIOCB *(*bdrv_aio_write)(BlockDriverState *bs,
const uint8_t *buf, int nb_sectors); int64_t sector_num, const uint8_t *buf, int nb_sectors,
BlockDriverCompletionFunc *cb, void *opaque);
void (*bdrv_aio_cancel)(BlockDriverAIOCB *acb); void (*bdrv_aio_cancel)(BlockDriverAIOCB *acb);
void (*bdrv_aio_delete)(BlockDriverAIOCB *acb); int aiocb_size;
const char *protocol_name; const char *protocol_name;
int (*bdrv_pread)(BlockDriverState *bs, int64_t offset, int (*bdrv_pread)(BlockDriverState *bs, int64_t offset,
@ -69,6 +70,7 @@ struct BlockDriver {
QEMUSnapshotInfo **psn_info); QEMUSnapshotInfo **psn_info);
int (*bdrv_get_info)(BlockDriverState *bs, BlockDriverInfo *bdi); int (*bdrv_get_info)(BlockDriverState *bs, BlockDriverInfo *bdi);
BlockDriverAIOCB *free_aiocb;
struct BlockDriver *next; struct BlockDriver *next;
}; };
@ -96,9 +98,9 @@ struct BlockDriverState {
int is_temporary; int is_temporary;
BlockDriverState *backing_hd; BlockDriverState *backing_hd;
/* sync read/write emulation */ /* async read/write emulation */
BlockDriverAIOCB *sync_aiocb; void *sync_aiocb;
/* NOTE: the following infos are only hints for real hardware /* NOTE: the following infos are only hints for real hardware
drivers. They are not used by the block driver */ drivers. They are not used by the block driver */
@ -111,11 +113,14 @@ struct BlockDriverState {
struct BlockDriverAIOCB { struct BlockDriverAIOCB {
BlockDriverState *bs; BlockDriverState *bs;
BlockDriverCompletionFunc *cb; BlockDriverCompletionFunc *cb;
void *cb_opaque; void *opaque;
BlockDriverAIOCB *next;
void *opaque; /* driver opaque */
}; };
void get_tmp_filename(char *filename, int size); void get_tmp_filename(char *filename, int size);
void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
void *opaque);
void qemu_aio_release(void *p);
#endif /* BLOCK_INT_H */ #endif /* BLOCK_INT_H */

14
vl.h
View File

@ -569,15 +569,13 @@ void bdrv_set_boot_sector(BlockDriverState *bs, const uint8_t *data, int size);
typedef struct BlockDriverAIOCB BlockDriverAIOCB; typedef struct BlockDriverAIOCB BlockDriverAIOCB;
typedef void BlockDriverCompletionFunc(void *opaque, int ret); typedef void BlockDriverCompletionFunc(void *opaque, int ret);
BlockDriverAIOCB *bdrv_aio_new(BlockDriverState *bs); BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
int bdrv_aio_read(BlockDriverAIOCB *acb, int64_t sector_num, uint8_t *buf, int nb_sectors,
uint8_t *buf, int nb_sectors, BlockDriverCompletionFunc *cb, void *opaque);
BlockDriverCompletionFunc *cb, void *opaque); BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
int bdrv_aio_write(BlockDriverAIOCB *acb, int64_t sector_num, const uint8_t *buf, int nb_sectors,
const uint8_t *buf, int nb_sectors, BlockDriverCompletionFunc *cb, void *opaque);
BlockDriverCompletionFunc *cb, void *opaque);
void bdrv_aio_cancel(BlockDriverAIOCB *acb); void bdrv_aio_cancel(BlockDriverAIOCB *acb);
void bdrv_aio_delete(BlockDriverAIOCB *acb);
void qemu_aio_init(void); void qemu_aio_init(void);
void qemu_aio_poll(void); void qemu_aio_poll(void);