#include "qapi/qmp/qerror.h"
#include "sysemu/block-backend.h"
-static const AIOCBInfo qed_aiocb_info = {
- .aiocb_size = sizeof(QEDAIOCB),
-};
-
static int bdrv_qed_probe(const uint8_t *buf, int buf_size,
const char *filename)
{
* This function only updates known header fields in-place and does not affect
* extra data after the QED header.
*/
-static int qed_write_header(BDRVQEDState *s)
+static int coroutine_fn qed_write_header(BDRVQEDState *s)
{
/* We must write full sectors for O_DIRECT but cannot necessarily generate
* the data following the header if an unrecognized compat feature is
};
qemu_iovec_init_external(&qiov, &iov, 1);
- ret = bdrv_preadv(s->bs->file, 0, &qiov);
+ ret = bdrv_co_preadv(s->bs->file, 0, qiov.size, &qiov, 0);
if (ret < 0) {
goto out;
}
/* Update header */
qed_header_cpu_to_le(&s->header, (QEDHeader *) buf);
- ret = bdrv_pwritev(s->bs->file, 0, &qiov);
+ ret = bdrv_co_pwritev(s->bs->file, 0, qiov.size, &qiov, 0);
if (ret < 0) {
goto out;
}
return l2_table;
}
-static void qed_aio_next_io(QEDAIOCB *acb);
-
-static void qed_aio_start_io(QEDAIOCB *acb)
-{
- qed_aio_next_io(acb);
-}
-
static void qed_plug_allocating_write_reqs(BDRVQEDState *s)
{
assert(!s->allocating_write_reqs_plugged);
static void qed_unplug_allocating_write_reqs(BDRVQEDState *s)
{
- QEDAIOCB *acb;
-
assert(s->allocating_write_reqs_plugged);
s->allocating_write_reqs_plugged = false;
-
- acb = QSIMPLEQ_FIRST(&s->allocating_write_reqs);
- if (acb) {
- qed_aio_start_io(acb);
- }
+ qemu_co_enter_next(&s->allocating_write_reqs);
}
-static void qed_clear_need_check(void *opaque, int ret)
+static void coroutine_fn qed_need_check_timer_entry(void *opaque)
{
BDRVQEDState *s = opaque;
+ int ret;
- if (ret) {
+ /* The timer should only fire when allocating writes have drained */
+ assert(!s->allocating_acb);
+
+ trace_qed_need_check_timer_cb(s);
+
+ qed_acquire(s);
+ qed_plug_allocating_write_reqs(s);
+
+ /* Ensure writes are on disk before clearing flag */
+ ret = bdrv_co_flush(s->bs->file->bs);
+ qed_release(s);
+ if (ret < 0) {
qed_unplug_allocating_write_reqs(s);
return;
}
qed_unplug_allocating_write_reqs(s);
- ret = bdrv_flush(s->bs);
+ ret = bdrv_co_flush(s->bs);
(void) ret;
}
static void qed_need_check_timer_cb(void *opaque)
{
- BDRVQEDState *s = opaque;
-
- /* The timer should only fire when allocating writes have drained */
- assert(!QSIMPLEQ_FIRST(&s->allocating_write_reqs));
-
- trace_qed_need_check_timer_cb(s);
-
- qed_acquire(s);
- qed_plug_allocating_write_reqs(s);
-
- /* Ensure writes are on disk before clearing flag */
- bdrv_aio_flush(s->bs->file->bs, qed_clear_need_check, s);
- qed_release(s);
+ Coroutine *co = qemu_coroutine_create(qed_need_check_timer_entry, opaque);
+ qemu_coroutine_enter(co);
}
void qed_acquire(BDRVQEDState *s)
int ret;
s->bs = bs;
- QSIMPLEQ_INIT(&s->allocating_write_reqs);
+ qemu_co_queue_init(&s->allocating_write_reqs);
ret = bdrv_pread(bs->file, 0, &le_header, sizeof(le_header));
if (ret < 0) {
static BDRVQEDState *acb_to_s(QEDAIOCB *acb)
{
- return acb->common.bs->opaque;
+ return acb->bs->opaque;
}
/**
* This function reads qiov->size bytes starting at pos from the backing file.
* If there is no backing file then zeroes are read.
*/
-static int qed_read_backing_file(BDRVQEDState *s, uint64_t pos,
- QEMUIOVector *qiov,
- QEMUIOVector **backing_qiov)
+static int coroutine_fn qed_read_backing_file(BDRVQEDState *s, uint64_t pos,
+ QEMUIOVector *qiov,
+ QEMUIOVector **backing_qiov)
{
uint64_t backing_length = 0;
size_t size;
qemu_iovec_concat(*backing_qiov, qiov, 0, size);
BLKDBG_EVENT(s->bs->file, BLKDBG_READ_BACKING_AIO);
- ret = bdrv_preadv(s->bs->backing, pos, *backing_qiov);
+ ret = bdrv_co_preadv(s->bs->backing, pos, size, *backing_qiov, 0);
if (ret < 0) {
return ret;
}
* @len: Number of bytes
* @offset: Byte offset in image file
*/
-static int qed_copy_from_backing_file(BDRVQEDState *s, uint64_t pos,
- uint64_t len, uint64_t offset)
+static int coroutine_fn qed_copy_from_backing_file(BDRVQEDState *s,
+ uint64_t pos, uint64_t len,
+ uint64_t offset)
{
QEMUIOVector qiov;
QEMUIOVector *backing_qiov = NULL;
}
BLKDBG_EVENT(s->bs->file, BLKDBG_COW_WRITE);
- ret = bdrv_pwritev(s->bs->file, offset, &qiov);
+ ret = bdrv_co_pwritev(s->bs->file, offset, qiov.size, &qiov, 0);
if (ret < 0) {
goto out;
}
* The cluster offset may be an allocated byte offset in the image file, the
* zero cluster marker, or the unallocated cluster marker.
*/
-static void qed_update_l2_table(BDRVQEDState *s, QEDTable *table, int index,
- unsigned int n, uint64_t cluster)
+static void coroutine_fn qed_update_l2_table(BDRVQEDState *s, QEDTable *table,
+ int index, unsigned int n,
+ uint64_t cluster)
{
int i;
for (i = index; i < index + n; i++) {
}
}
-static void qed_aio_complete_bh(void *opaque)
-{
- QEDAIOCB *acb = opaque;
- BDRVQEDState *s = acb_to_s(acb);
- BlockCompletionFunc *cb = acb->common.cb;
- void *user_opaque = acb->common.opaque;
- int ret = acb->bh_ret;
-
- qemu_aio_unref(acb);
-
- /* Invoke callback */
- qed_acquire(s);
- cb(user_opaque, ret);
- qed_release(s);
-}
-
-static void qed_resume_alloc_bh(void *opaque)
-{
- qed_aio_start_io(opaque);
-}
-
-static void qed_aio_complete(QEDAIOCB *acb, int ret)
+static void coroutine_fn qed_aio_complete(QEDAIOCB *acb)
{
BDRVQEDState *s = acb_to_s(acb);
- trace_qed_aio_complete(s, acb, ret);
-
/* Free resources */
qemu_iovec_destroy(&acb->cur_qiov);
qed_unref_l2_cache_entry(acb->request.l2_table);
acb->qiov->iov[0].iov_base = NULL;
}
- /* Arrange for a bh to invoke the completion function */
- acb->bh_ret = ret;
- aio_bh_schedule_oneshot(bdrv_get_aio_context(acb->common.bs),
- qed_aio_complete_bh, acb);
-
/* Start next allocating write request waiting behind this one. Note that
* requests enqueue themselves when they first hit an unallocated cluster
* but they wait until the entire request is finished before waking up the
* next request in the queue. This ensures that we don't cycle through
* requests multiple times but rather finish one at a time completely.
*/
- if (acb == QSIMPLEQ_FIRST(&s->allocating_write_reqs)) {
- QEDAIOCB *next_acb;
- QSIMPLEQ_REMOVE_HEAD(&s->allocating_write_reqs, next);
- next_acb = QSIMPLEQ_FIRST(&s->allocating_write_reqs);
- if (next_acb) {
- aio_bh_schedule_oneshot(bdrv_get_aio_context(acb->common.bs),
- qed_resume_alloc_bh, next_acb);
+ if (acb == s->allocating_acb) {
+ s->allocating_acb = NULL;
+ if (!qemu_co_queue_empty(&s->allocating_write_reqs)) {
+ qemu_co_enter_next(&s->allocating_write_reqs);
} else if (s->header.features & QED_F_NEED_CHECK) {
qed_start_need_check_timer(s);
}
/**
* Update L1 table with new L2 table offset and write it out
*/
-static int qed_aio_write_l1_update(QEDAIOCB *acb)
+static int coroutine_fn qed_aio_write_l1_update(QEDAIOCB *acb)
{
BDRVQEDState *s = acb_to_s(acb);
CachedL2Table *l2_table = acb->request.l2_table;
/**
* Update L2 table with new cluster offsets and write them out
*/
-static int qed_aio_write_l2_update(QEDAIOCB *acb, uint64_t offset)
+static int coroutine_fn qed_aio_write_l2_update(QEDAIOCB *acb, uint64_t offset)
{
BDRVQEDState *s = acb_to_s(acb);
bool need_alloc = acb->find_cluster_ret == QED_CLUSTER_L1;
/**
* Write data to the image file
*/
-static int qed_aio_write_main(QEDAIOCB *acb)
+static int coroutine_fn qed_aio_write_main(QEDAIOCB *acb)
{
BDRVQEDState *s = acb_to_s(acb);
uint64_t offset = acb->cur_cluster +
trace_qed_aio_write_main(s, acb, 0, offset, acb->cur_qiov.size);
BLKDBG_EVENT(s->bs->file, BLKDBG_WRITE_AIO);
- ret = bdrv_pwritev(s->bs->file, offset, &acb->cur_qiov);
+ ret = bdrv_co_pwritev(s->bs->file, offset, acb->cur_qiov.size,
+ &acb->cur_qiov, 0);
if (ret < 0) {
return ret;
}
* region. The solution is to flush after writing a new data
* cluster and before updating the L2 table.
*/
- ret = bdrv_flush(s->bs->file->bs);
+ ret = bdrv_co_flush(s->bs->file->bs);
if (ret < 0) {
return ret;
}
/**
* Populate untouched regions of new data cluster
*/
-static int qed_aio_write_cow(QEDAIOCB *acb)
+static int coroutine_fn qed_aio_write_cow(QEDAIOCB *acb)
{
BDRVQEDState *s = acb_to_s(acb);
uint64_t start, len, offset;
*
* This path is taken when writing to previously unallocated clusters.
*/
-static int qed_aio_write_alloc(QEDAIOCB *acb, size_t len)
+static int coroutine_fn qed_aio_write_alloc(QEDAIOCB *acb, size_t len)
{
BDRVQEDState *s = acb_to_s(acb);
int ret;
/* Cancel timer when the first allocating request comes in */
- if (QSIMPLEQ_EMPTY(&s->allocating_write_reqs)) {
+ if (s->allocating_acb == NULL) {
qed_cancel_need_check_timer(s);
}
/* Freeze this request if another allocating write is in progress */
- if (acb != QSIMPLEQ_FIRST(&s->allocating_write_reqs)) {
- QSIMPLEQ_INSERT_TAIL(&s->allocating_write_reqs, acb, next);
- }
- if (acb != QSIMPLEQ_FIRST(&s->allocating_write_reqs) ||
- s->allocating_write_reqs_plugged) {
- return -EINPROGRESS; /* wait for existing request to finish */
+ if (s->allocating_acb != acb || s->allocating_write_reqs_plugged) {
+ if (s->allocating_acb != NULL) {
+ qemu_co_queue_wait(&s->allocating_write_reqs, NULL);
+ assert(s->allocating_acb == NULL);
+ }
+ s->allocating_acb = acb;
+ return -EAGAIN; /* start over with looking up table entries */
}
acb->cur_nclusters = qed_bytes_to_clusters(s,
*
* This path is taken when writing to already allocated clusters.
*/
-static int qed_aio_write_inplace(QEDAIOCB *acb, uint64_t offset, size_t len)
+static int coroutine_fn qed_aio_write_inplace(QEDAIOCB *acb, uint64_t offset,
+ size_t len)
{
/* Allocate buffer for zero writes */
if (acb->flags & QED_AIOCB_ZERO) {
struct iovec *iov = acb->qiov->iov;
if (!iov->iov_base) {
- iov->iov_base = qemu_try_blockalign(acb->common.bs, iov->iov_len);
+ iov->iov_base = qemu_try_blockalign(acb->bs, iov->iov_len);
if (iov->iov_base == NULL) {
return -ENOMEM;
}
* @offset: Cluster offset in bytes
* @len: Length in bytes
*/
-static int qed_aio_write_data(void *opaque, int ret,
- uint64_t offset, size_t len)
+static int coroutine_fn qed_aio_write_data(void *opaque, int ret,
+ uint64_t offset, size_t len)
{
QEDAIOCB *acb = opaque;
* @offset: Cluster offset in bytes
* @len: Length in bytes
*/
-static int qed_aio_read_data(void *opaque, int ret, uint64_t offset, size_t len)
+static int coroutine_fn qed_aio_read_data(void *opaque, int ret,
+ uint64_t offset, size_t len)
{
QEDAIOCB *acb = opaque;
BDRVQEDState *s = acb_to_s(acb);
- BlockDriverState *bs = acb->common.bs;
+ BlockDriverState *bs = acb->bs;
/* Adjust offset into cluster */
offset += qed_offset_into_cluster(s, acb->cur_pos);
}
BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO);
- ret = bdrv_preadv(bs->file, offset, &acb->cur_qiov);
+ ret = bdrv_co_preadv(bs->file, offset, acb->cur_qiov.size,
+ &acb->cur_qiov, 0);
if (ret < 0) {
return ret;
}
/**
* Begin next I/O or complete the request
*/
-static void qed_aio_next_io(QEDAIOCB *acb)
+static int coroutine_fn qed_aio_next_io(QEDAIOCB *acb)
{
BDRVQEDState *s = acb_to_s(acb);
uint64_t offset;
size_t len;
int ret;
- trace_qed_aio_next_io(s, acb, 0, acb->cur_pos + acb->cur_qiov.size);
+ while (1) {
+ trace_qed_aio_next_io(s, acb, 0, acb->cur_pos + acb->cur_qiov.size);
- if (acb->backing_qiov) {
- qemu_iovec_destroy(acb->backing_qiov);
- g_free(acb->backing_qiov);
- acb->backing_qiov = NULL;
- }
+ if (acb->backing_qiov) {
+ qemu_iovec_destroy(acb->backing_qiov);
+ g_free(acb->backing_qiov);
+ acb->backing_qiov = NULL;
+ }
- acb->qiov_offset += acb->cur_qiov.size;
- acb->cur_pos += acb->cur_qiov.size;
- qemu_iovec_reset(&acb->cur_qiov);
+ acb->qiov_offset += acb->cur_qiov.size;
+ acb->cur_pos += acb->cur_qiov.size;
+ qemu_iovec_reset(&acb->cur_qiov);
- /* Complete request */
- if (acb->cur_pos >= acb->end_pos) {
- qed_aio_complete(acb, 0);
- return;
- }
+ /* Complete request */
+ if (acb->cur_pos >= acb->end_pos) {
+ ret = 0;
+ break;
+ }
- /* Find next cluster and start I/O */
- len = acb->end_pos - acb->cur_pos;
- ret = qed_find_cluster(s, &acb->request, acb->cur_pos, &len, &offset);
- if (ret < 0) {
- qed_aio_complete(acb, ret);
- return;
- }
+ /* Find next cluster and start I/O */
+ len = acb->end_pos - acb->cur_pos;
+ ret = qed_find_cluster(s, &acb->request, acb->cur_pos, &len, &offset);
+ if (ret < 0) {
+ break;
+ }
- if (acb->flags & QED_AIOCB_WRITE) {
- ret = qed_aio_write_data(acb, ret, offset, len);
- } else {
- ret = qed_aio_read_data(acb, ret, offset, len);
- }
+ if (acb->flags & QED_AIOCB_WRITE) {
+ ret = qed_aio_write_data(acb, ret, offset, len);
+ } else {
+ ret = qed_aio_read_data(acb, ret, offset, len);
+ }
- if (ret < 0) {
- if (ret != -EINPROGRESS) {
- qed_aio_complete(acb, ret);
+ if (ret < 0 && ret != -EAGAIN) {
+ break;
}
- return;
}
- qed_aio_next_io(acb);
-}
-static BlockAIOCB *qed_aio_setup(BlockDriverState *bs,
- int64_t sector_num,
- QEMUIOVector *qiov, int nb_sectors,
- BlockCompletionFunc *cb,
- void *opaque, int flags)
-{
- QEDAIOCB *acb = qemu_aio_get(&qed_aiocb_info, bs, cb, opaque);
+ trace_qed_aio_complete(s, acb, ret);
+ qed_aio_complete(acb);
+ return ret;
+}
- trace_qed_aio_setup(bs->opaque, acb, sector_num, nb_sectors,
- opaque, flags);
+static int coroutine_fn qed_co_request(BlockDriverState *bs, int64_t sector_num,
+ QEMUIOVector *qiov, int nb_sectors,
+ int flags)
+{
+ QEDAIOCB acb = {
+ .bs = bs,
+ .cur_pos = (uint64_t) sector_num * BDRV_SECTOR_SIZE,
+ .end_pos = (sector_num + nb_sectors) * BDRV_SECTOR_SIZE,
+ .qiov = qiov,
+ .flags = flags,
+ };
+ qemu_iovec_init(&acb.cur_qiov, qiov->niov);
- acb->flags = flags;
- acb->qiov = qiov;
- acb->qiov_offset = 0;
- acb->cur_pos = (uint64_t)sector_num * BDRV_SECTOR_SIZE;
- acb->end_pos = acb->cur_pos + nb_sectors * BDRV_SECTOR_SIZE;
- acb->backing_qiov = NULL;
- acb->request.l2_table = NULL;
- qemu_iovec_init(&acb->cur_qiov, qiov->niov);
+ trace_qed_aio_setup(bs->opaque, &acb, sector_num, nb_sectors, NULL, flags);
/* Start request */
- qed_aio_start_io(acb);
- return &acb->common;
+ return qed_aio_next_io(&acb);
}
-static BlockAIOCB *bdrv_qed_aio_readv(BlockDriverState *bs,
- int64_t sector_num,
- QEMUIOVector *qiov, int nb_sectors,
- BlockCompletionFunc *cb,
- void *opaque)
+static int coroutine_fn bdrv_qed_co_readv(BlockDriverState *bs,
+ int64_t sector_num, int nb_sectors,
+ QEMUIOVector *qiov)
{
- return qed_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
+ return qed_co_request(bs, sector_num, qiov, nb_sectors, 0);
}
-static BlockAIOCB *bdrv_qed_aio_writev(BlockDriverState *bs,
- int64_t sector_num,
- QEMUIOVector *qiov, int nb_sectors,
- BlockCompletionFunc *cb,
- void *opaque)
+static int coroutine_fn bdrv_qed_co_writev(BlockDriverState *bs,
+ int64_t sector_num, int nb_sectors,
+ QEMUIOVector *qiov)
{
- return qed_aio_setup(bs, sector_num, qiov, nb_sectors, cb,
- opaque, QED_AIOCB_WRITE);
-}
-
-typedef struct {
- Coroutine *co;
- int ret;
- bool done;
-} QEDWriteZeroesCB;
-
-static void coroutine_fn qed_co_pwrite_zeroes_cb(void *opaque, int ret)
-{
- QEDWriteZeroesCB *cb = opaque;
-
- cb->done = true;
- cb->ret = ret;
- if (cb->co) {
- aio_co_wake(cb->co);
- }
+ return qed_co_request(bs, sector_num, qiov, nb_sectors, QED_AIOCB_WRITE);
}
static int coroutine_fn bdrv_qed_co_pwrite_zeroes(BlockDriverState *bs,
int64_t offset,
- int count,
+ int bytes,
BdrvRequestFlags flags)
{
- BlockAIOCB *blockacb;
BDRVQEDState *s = bs->opaque;
- QEDWriteZeroesCB cb = { .done = false };
QEMUIOVector qiov;
struct iovec iov;
/* Fall back if the request is not aligned */
if (qed_offset_into_cluster(s, offset) ||
- qed_offset_into_cluster(s, count)) {
+ qed_offset_into_cluster(s, bytes)) {
return -ENOTSUP;
}
* then it will be allocated during request processing.
*/
iov.iov_base = NULL;
- iov.iov_len = count;
+ iov.iov_len = bytes;
qemu_iovec_init_external(&qiov, &iov, 1);
- blockacb = qed_aio_setup(bs, offset >> BDRV_SECTOR_BITS, &qiov,
- count >> BDRV_SECTOR_BITS,
- qed_co_pwrite_zeroes_cb, &cb,
- QED_AIOCB_WRITE | QED_AIOCB_ZERO);
- if (!blockacb) {
- return -EIO;
- }
- if (!cb.done) {
- cb.co = qemu_coroutine_self();
- qemu_coroutine_yield();
- }
- assert(cb.done);
- return cb.ret;
+ return qed_co_request(bs, offset >> BDRV_SECTOR_BITS, &qiov,
+ bytes >> BDRV_SECTOR_BITS,
+ QED_AIOCB_WRITE | QED_AIOCB_ZERO);
}
static int bdrv_qed_truncate(BlockDriverState *bs, int64_t offset, Error **errp)
.bdrv_create = bdrv_qed_create,
.bdrv_has_zero_init = bdrv_has_zero_init_1,
.bdrv_co_get_block_status = bdrv_qed_co_get_block_status,
- .bdrv_aio_readv = bdrv_qed_aio_readv,
- .bdrv_aio_writev = bdrv_qed_aio_writev,
+ .bdrv_co_readv = bdrv_qed_co_readv,
+ .bdrv_co_writev = bdrv_qed_co_writev,
.bdrv_co_pwrite_zeroes = bdrv_qed_co_pwrite_zeroes,
.bdrv_truncate = bdrv_qed_truncate,
.bdrv_getlength = bdrv_qed_getlength,