From patchwork Tue Feb 28 10:24:08 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Tokarev X-Patchwork-Id: 143408 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 9A195B6FA3 for ; Tue, 28 Feb 2012 22:14:46 +1100 (EST) Received: from localhost ([::1]:39994 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1S2L11-0005Zl-NB for incoming@patchwork.ozlabs.org; Tue, 28 Feb 2012 06:14:43 -0500 Received: from eggs.gnu.org ([208.118.235.92]:48793) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1S2L0q-0005YL-Ji for qemu-devel@nongnu.org; Tue, 28 Feb 2012 06:14:38 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1S2L0l-00053L-Dk for qemu-devel@nongnu.org; Tue, 28 Feb 2012 06:14:32 -0500 Received: from isrv.corpit.ru ([86.62.121.231]:40530) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1S2L0l-00052x-27 for qemu-devel@nongnu.org; Tue, 28 Feb 2012 06:14:27 -0500 Received: from gandalf.tls.msk.ru (mjt.vpn.tls.msk.ru [192.168.177.99]) by isrv.corpit.ru (Postfix) with ESMTP id D8B4BA1891; Tue, 28 Feb 2012 15:14:24 +0400 (MSK) Received: by gandalf.tls.msk.ru (Postfix, from userid 1000) id 2DC63195; Tue, 28 Feb 2012 15:14:24 +0400 (MSK) From: Michael Tokarev Date: Tue, 28 Feb 2012 14:24:08 +0400 To: Paolo Bonzini Message-Id: <20120228111424.2DC63195@gandalf.tls.msk.ru> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 86.62.121.231 Cc: mjt@tls.msk.ru, qemu-devel@nongnu.org Subject: [Qemu-devel] [PATCH v2] Consolidate reads and writes in nbd block device into one common routine X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This removes quite some duplicated code. v2 fixes a bug (uninitialized reply.error) and makes the loop more natural. Signed-off-By: Michael Tokarev --- block/nbd.c | 95 +++++++++++++++++++--------------------------------------- 1 files changed, 31 insertions(+), 64 deletions(-) diff --git a/block/nbd.c b/block/nbd.c index 161b299..a78e212 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -320,91 +320,58 @@ static int nbd_open(BlockDriverState *bs, const char* filename, int flags) return result; } -static int nbd_co_readv_1(BlockDriverState *bs, int64_t sector_num, - int nb_sectors, QEMUIOVector *qiov, - int offset) -{ - BDRVNBDState *s = bs->opaque; - struct nbd_request request; - struct nbd_reply reply; - - request.type = NBD_CMD_READ; - request.from = sector_num * 512; - request.len = nb_sectors * 512; - - nbd_coroutine_start(s, &request); - if (nbd_co_send_request(s, &request, NULL, 0) == -1) { - reply.error = errno; - } else { - nbd_co_receive_reply(s, &request, &reply, qiov->iov, offset); - } - nbd_coroutine_end(s, &request); - return -reply.error; - -} +/* qemu-nbd has a limit of slightly less than 1M per request. Try to + * remain aligned to 4K. */ +#define NBD_MAX_SECTORS 2040 -static int nbd_co_writev_1(BlockDriverState *bs, int64_t sector_num, - int nb_sectors, QEMUIOVector *qiov, - int offset) +static int nbd_co_rwv(BlockDriverState *bs, int64_t sector_num, + int nb_sectors, QEMUIOVector *qiov, int iswrite) { BDRVNBDState *s = bs->opaque; struct nbd_request request; struct nbd_reply reply; + int offset = 0; - request.type = NBD_CMD_WRITE; - if (!bdrv_enable_write_cache(bs) && (s->nbdflags & NBD_FLAG_SEND_FUA)) { + request.type = iswrite ? NBD_CMD_WRITE : NBD_CMD_READ; + if (iswrite && !bdrv_enable_write_cache(bs) && (s->nbdflags & NBD_FLAG_SEND_FUA)) { request.type |= NBD_CMD_FLAG_FUA; } + reply.error = 0; + + /* we split the request into pieces of at most NBD_MAX_SECTORS size + * and process them in a loop... */ + do { + request.from = sector_num * 512; + request.len = MIN(nb_sectors, NBD_MAX_SECTORS) * 512; + + nbd_coroutine_start(s, &request); + if (nbd_co_send_request(s, &request, iswrite ? qiov->iov : NULL, 0) == -1) { + reply.error = errno; + } else { + nbd_co_receive_reply(s, &request, &reply, iswrite ? NULL : qiov->iov, offset); + } + nbd_coroutine_end(s, &request); - request.from = sector_num * 512; - request.len = nb_sectors * 512; + offset += NBD_MAX_SECTORS * 512; + sector_num += NBD_MAX_SECTORS; + nb_sectors -= NBD_MAX_SECTORS; + + /* ..till we hit an error or there's nothing more to process */ + } while (reply.error == 0 && nb_sectors > 0); - nbd_coroutine_start(s, &request); - if (nbd_co_send_request(s, &request, qiov->iov, offset) == -1) { - reply.error = errno; - } else { - nbd_co_receive_reply(s, &request, &reply, NULL, 0); - } - nbd_coroutine_end(s, &request); return -reply.error; } -/* qemu-nbd has a limit of slightly less than 1M per request. Try to - * remain aligned to 4K. */ -#define NBD_MAX_SECTORS 2040 - static int nbd_co_readv(BlockDriverState *bs, int64_t sector_num, int nb_sectors, QEMUIOVector *qiov) { - int offset = 0; - int ret; - while (nb_sectors > NBD_MAX_SECTORS) { - ret = nbd_co_readv_1(bs, sector_num, NBD_MAX_SECTORS, qiov, offset); - if (ret < 0) { - return ret; - } - offset += NBD_MAX_SECTORS * 512; - sector_num += NBD_MAX_SECTORS; - nb_sectors -= NBD_MAX_SECTORS; - } - return nbd_co_readv_1(bs, sector_num, nb_sectors, qiov, offset); + return nbd_co_rwv(bs, sector_num, nb_sectors, qiov, 0); } static int nbd_co_writev(BlockDriverState *bs, int64_t sector_num, int nb_sectors, QEMUIOVector *qiov) { - int offset = 0; - int ret; - while (nb_sectors > NBD_MAX_SECTORS) { - ret = nbd_co_writev_1(bs, sector_num, NBD_MAX_SECTORS, qiov, offset); - if (ret < 0) { - return ret; - } - offset += NBD_MAX_SECTORS * 512; - sector_num += NBD_MAX_SECTORS; - nb_sectors -= NBD_MAX_SECTORS; - } - return nbd_co_writev_1(bs, sector_num, nb_sectors, qiov, offset); + return nbd_co_rwv(bs, sector_num, nb_sectors, qiov, 1); } static int nbd_co_flush(BlockDriverState *bs)