From: Kevin Wolf <kwolf@redhat.com>
To: qemu-devel@nongnu.org
Cc: kwolf@redhat.com, stefanha@gmail.com
Subject: [Qemu-devel] [RFC PATCH v3 1/4] block: Implement bdrv_aio_pwrite
Date: Tue, 30 Nov 2010 13:48:49 +0100 [thread overview]
Message-ID: <1291121332-10588-2-git-send-email-kwolf@redhat.com> (raw)
In-Reply-To: <1291121332-10588-1-git-send-email-kwolf@redhat.com>
This implements an asynchronous version of bdrv_pwrite.
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
block.c | 167 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
block.h | 2 +
2 files changed, 169 insertions(+), 0 deletions(-)
diff --git a/block.c b/block.c
index 63effd8..f10066e 100644
--- a/block.c
+++ b/block.c
@@ -2106,6 +2106,173 @@ BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
return ret;
}
+typedef struct PwriteAIOCB {
+ BlockDriverAIOCB common;
+ int state;
+ int64_t offset;
+ size_t bytes;
+ uint8_t* buf;
+ uint8_t* tmp_buf;
+ struct iovec iov;
+ QEMUIOVector qiov;
+} PwriteAIOCB;
+
+static void pwrite_aio_cancel(BlockDriverAIOCB *blockacb)
+{
+ qemu_aio_flush();
+}
+
+static AIOPool blkqueue_aio_pool = {
+ .aiocb_size = sizeof(PwriteAIOCB),
+ .cancel = pwrite_aio_cancel,
+};
+
+static void bdrv_aio_pwrite_cb(void *opaque, int ret)
+{
+ PwriteAIOCB *acb = opaque;
+ BlockDriverAIOCB *tmp_acb;
+ int64_t sector_num;
+
+ if (ret < 0) {
+ goto done;
+ }
+
+ sector_num = acb->offset >> BDRV_SECTOR_BITS;
+
+ switch (acb->state) {
+ case 0: {
+ /* Read first sector if needed */
+ int len;
+
+ len = (BDRV_SECTOR_SIZE - acb->offset) & (BDRV_SECTOR_SIZE - 1);
+
+ if (len > 0) {
+ acb->state = 1;
+ acb->tmp_buf = qemu_blockalign(acb->common.bs, BDRV_SECTOR_SIZE);
+ acb->iov.iov_base = acb->tmp_buf;
+ acb->iov.iov_len = BDRV_SECTOR_SIZE;
+ qemu_iovec_init_external(&acb->qiov, &acb->iov, 1);
+ tmp_acb = bdrv_aio_readv(acb->common.bs, sector_num, &acb->qiov, 1,
+ bdrv_aio_pwrite_cb, acb);
+ if (tmp_acb == NULL) {
+ bdrv_aio_pwrite_cb(acb, -EIO);
+ }
+ } else {
+ acb->state = 2;
+ bdrv_aio_pwrite_cb(acb, 0);
+ }
+ break;
+ }
+
+ case 1: {
+ /* Modify first cluster and write it back */
+ int len;
+
+ len = (BDRV_SECTOR_SIZE - acb->offset) & (BDRV_SECTOR_SIZE - 1);
+ if (len > acb->bytes) {
+ len = acb->bytes;
+ }
+
+ memcpy(acb->tmp_buf + (acb->offset & (BDRV_SECTOR_SIZE - 1)),
+ acb->buf, len);
+
+ acb->state = 2;
+ acb->offset += len;
+ acb->buf += len;
+ acb->bytes -= len;
+
+ tmp_acb = bdrv_aio_writev(acb->common.bs, sector_num, &acb->qiov, 1,
+ bdrv_aio_pwrite_cb, acb);
+ if (tmp_acb == NULL) {
+ bdrv_aio_pwrite_cb(acb, -EIO);
+ }
+ break;
+ }
+
+ case 2: {
+ /* Write the sectors "in place" */
+ int nb_sectors = acb->bytes >> BDRV_SECTOR_BITS;
+
+ acb->state = 3;
+ if (nb_sectors > 0) {
+ int len = nb_sectors << BDRV_SECTOR_BITS;
+
+ acb->iov.iov_base = acb->buf;
+ acb->iov.iov_len = len;
+ qemu_iovec_init_external(&acb->qiov, &acb->iov, 1);
+
+ acb->offset += len;
+ acb->buf += len;
+ acb->bytes -= len;
+
+ tmp_acb = bdrv_aio_writev(acb->common.bs, sector_num, &acb->qiov,
+ nb_sectors, bdrv_aio_pwrite_cb, acb);
+ if (tmp_acb == NULL) {
+ bdrv_aio_pwrite_cb(acb, -EIO);
+ }
+ } else {
+ bdrv_aio_pwrite_cb(acb, 0);
+ }
+ break;
+ }
+
+ case 3: {
+ /* Read last sector if needed */
+ if (acb->bytes == 0) {
+ goto done;
+ }
+
+ acb->state = 4;
+ acb->iov.iov_base = acb->tmp_buf;
+ acb->iov.iov_len = BDRV_SECTOR_SIZE;
+ qemu_iovec_init_external(&acb->qiov, &acb->iov, 1);
+ tmp_acb = bdrv_aio_readv(acb->common.bs, sector_num, &acb->qiov, 1,
+ bdrv_aio_pwrite_cb, acb);
+ if (tmp_acb == NULL) {
+ bdrv_aio_pwrite_cb(acb, -EIO);
+ }
+ break;
+ }
+
+ case 4:
+ /* Modify and write last sector */
+ acb->state = 5;
+ memcpy(acb->tmp_buf, acb->buf, acb->bytes);
+ tmp_acb = bdrv_aio_writev(acb->common.bs, sector_num, &acb->qiov, 1,
+ bdrv_aio_pwrite_cb, acb);
+ if (tmp_acb == NULL) {
+ bdrv_aio_pwrite_cb(acb, -EIO);
+ }
+ break;
+
+ case 5:
+ goto done;
+ }
+ return;
+
+done:
+ qemu_free(acb->tmp_buf);
+ acb->common.cb(acb->common.opaque, ret);
+ qemu_aio_release(acb);
+}
+
+BlockDriverAIOCB *bdrv_aio_pwrite(BlockDriverState *bs, int64_t offset,
+ void* buf, size_t bytes, BlockDriverCompletionFunc *cb, void *opaque)
+{
+ PwriteAIOCB *acb;
+
+ acb = qemu_aio_get(&blkqueue_aio_pool, bs, cb, opaque);
+ acb->state = 0;
+ acb->offset = offset;
+ acb->buf = buf;
+ acb->bytes = bytes;
+ acb->tmp_buf = NULL;
+
+ bdrv_aio_pwrite_cb(acb, 0);
+
+ return &acb->common;
+}
+
typedef struct MultiwriteCB {
int error;
diff --git a/block.h b/block.h
index 78ecfac..c6e4d90 100644
--- a/block.h
+++ b/block.h
@@ -116,6 +116,8 @@ BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
QEMUIOVector *iov, int nb_sectors,
BlockDriverCompletionFunc *cb, void *opaque);
+BlockDriverAIOCB *bdrv_aio_pwrite(BlockDriverState *bs, int64_t offset, void* buf,
+ size_t bytes, BlockDriverCompletionFunc *cb, void *opaque);
BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs,
BlockDriverCompletionFunc *cb, void *opaque);
void bdrv_aio_cancel(BlockDriverAIOCB *acb);
--
1.7.2.3
next prev parent reply other threads:[~2010-12-01 4:58 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-11-30 12:48 [Qemu-devel] [RFC PATCH v3 0/4] block-queue: Delay and batch metadata writes Kevin Wolf
2010-11-30 12:48 ` Kevin Wolf [this message]
2010-12-02 12:07 ` [Qemu-devel] Re: [RFC PATCH v3 1/4] block: Implement bdrv_aio_pwrite Stefan Hajnoczi
2010-12-02 12:30 ` Kevin Wolf
2010-12-02 13:04 ` Stefan Hajnoczi
2010-11-30 12:48 ` [Qemu-devel] [RFC PATCH v3 2/4] Add block-queue Kevin Wolf
2010-12-03 9:44 ` [Qemu-devel] " Stefan Hajnoczi
2010-11-30 12:48 ` [Qemu-devel] [RFC PATCH v3 3/4] Test cases for block-queue Kevin Wolf
2010-11-30 12:48 ` [Qemu-devel] [RFC PATCH v3 4/4] qcow2: Preliminary block-queue support Kevin Wolf
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1291121332-10588-2-git-send-email-kwolf@redhat.com \
--to=kwolf@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=stefanha@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.