From patchwork Tue Nov 30 12:48:49 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kevin Wolf X-Patchwork-Id: 73819 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id DD2B0B6EF1 for ; Thu, 2 Dec 2010 00:33:21 +1100 (EST) Received: from localhost ([127.0.0.1]:49132 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PNmoB-0007AN-3s for incoming@patchwork.ozlabs.org; Wed, 01 Dec 2010 08:33:19 -0500 Received: from [140.186.70.92] (port=36403 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PNelS-0002Mm-6f for qemu-devel@nongnu.org; Tue, 30 Nov 2010 23:58:20 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PNPd2-0004x7-Pv for qemu-devel@nongnu.org; Tue, 30 Nov 2010 07:48:21 -0500 Received: from mx1.redhat.com ([209.132.183.28]:2754) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PNPd2-0004P9-FV for qemu-devel@nongnu.org; Tue, 30 Nov 2010 07:48:16 -0500 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id oAUClq9S003968 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Tue, 30 Nov 2010 07:47:53 -0500 Received: from dhcp-5-188.str.redhat.com (dhcp-5-175.str.redhat.com [10.32.5.175]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id oAUCln0M014757; Tue, 30 Nov 2010 07:47:51 -0500 From: Kevin Wolf To: qemu-devel@nongnu.org Date: Tue, 30 Nov 2010 13:48:49 +0100 Message-Id: <1291121332-10588-2-git-send-email-kwolf@redhat.com> In-Reply-To: <1291121332-10588-1-git-send-email-kwolf@redhat.com> References: <1291121332-10588-1-git-send-email-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. Cc: kwolf@redhat.com, stefanha@gmail.com Subject: [Qemu-devel] [RFC PATCH v3 1/4] block: Implement bdrv_aio_pwrite X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This implements an asynchronous version of bdrv_pwrite. Signed-off-by: Kevin Wolf --- 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);