From patchwork Mon Jun 20 23:39:25 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Blake X-Patchwork-Id: 9189029 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 9407C6075F for ; Mon, 20 Jun 2016 23:40:31 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 836ED27E63 for ; Mon, 20 Jun 2016 23:40:31 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7827927EE2; Mon, 20 Jun 2016 23:40:31 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 1815527E63 for ; Mon, 20 Jun 2016 23:40:31 +0000 (UTC) Received: from localhost ([::1]:47084 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bF8nu-0006fX-3L for patchwork-qemu-devel@patchwork.kernel.org; Mon, 20 Jun 2016 19:40:30 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38080) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bF8nC-0006SZ-ND for qemu-devel@nongnu.org; Mon, 20 Jun 2016 19:39:56 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bF8n1-0002Tr-Vy for qemu-devel@nongnu.org; Mon, 20 Jun 2016 19:39:45 -0400 Received: from resqmta-po-08v.sys.comcast.net ([2001:558:fe16:19:96:114:154:167]:55142) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bF8n1-0002Tf-RD for qemu-devel@nongnu.org; Mon, 20 Jun 2016 19:39:35 -0400 Received: from resomta-po-10v.sys.comcast.net ([96.114.154.234]) by resqmta-po-08v.sys.comcast.net with SMTP id F8mUbeS2sB1Y8F8n1bXJHa; Mon, 20 Jun 2016 23:39:35 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=comcast.net; s=q20140121; t=1466465975; bh=X1YdS3Ir2EIO7xBE+y9HgWc4dNVH3mRYJU4a+yIQ6YY=; h=Received:Received:From:To:Subject:Date:Message-Id; b=s2CRT1w2Vrl25Y3bZPO8mV7gLS1vtr2DWFVWxvnudcWVd0vzQEgEiK+qVtBoHQX8O ILUGkX3p9aSoum4mJo2OdZkjj/gUuxN/mYu7PwYDnSlURa2nQ3oh8yEmOVFTmF574A XHNL1SDYtl0NuguZjwmh8waPgKg+FRRkOUsGlRKG4eBQVFRlvT1eK/vMwwNtBGH1GX a9RgHJ5NeOlbDcwZiNMnSDw2Y5N+Nrhdmbp84JNTBGWcc4YeKQh59ooEBAtVn2BGF9 PSyAb+SZXHzXKVLMFjoj7rRe03NsgV9bKyrCJYJnK02gjQV+nqhrLvVQxD9A33AF3U oyreAhzMzt0eg== Received: from red.redhat.com ([24.10.254.122]) by resomta-po-10v.sys.comcast.net with comcast id 9BfW1t0022fD5rL01BfaZl; Mon, 20 Jun 2016 23:39:35 +0000 From: Eric Blake To: qemu-devel@nongnu.org Date: Mon, 20 Jun 2016 17:39:25 -0600 Message-Id: <1466465969-25315-2-git-send-email-eblake@redhat.com> X-Mailer: git-send-email 2.5.5 In-Reply-To: <1466465969-25315-1-git-send-email-eblake@redhat.com> References: <1466465969-25315-1-git-send-email-eblake@redhat.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2001:558:fe16:19:96:114:154:167 Subject: [Qemu-devel] [PATCH 1/5] block: Fragment reads to max transfer length X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, Fam Zheng , Stefan Hajnoczi , qemu-block@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Drivers should be able to rely on the block layer honoring the max transfer length, rather than needing to return -EINVAL (iscsi) or manually fragment things (nbd). This patch adds the fragmentation in the block layer, after requests have been aligned (fragmenting before alignment would lead to multiple unaligned requests, rather than just the head and tail). Signed-off-by: Eric Blake --- block/io.c | 53 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 20 deletions(-) diff --git a/block/io.c b/block/io.c index 4e19868..a1443e3 100644 --- a/block/io.c +++ b/block/io.c @@ -971,8 +971,8 @@ err: /* * Forwards an already correctly aligned request to the BlockDriver. This - * handles copy on read and zeroing after EOF; any other features must be - * implemented by the caller. + * handles copy on read, zeroing after EOF, and fragmentation of large + * reads; any other features must be implemented by the caller. */ static int coroutine_fn bdrv_aligned_preadv(BlockDriverState *bs, BdrvTrackedRequest *req, int64_t offset, unsigned int bytes, @@ -980,12 +980,16 @@ static int coroutine_fn bdrv_aligned_preadv(BlockDriverState *bs, { int64_t total_bytes, max_bytes; int ret; + uint64_t bytes_remaining = bytes; + int max_transfer; assert(is_power_of_2(align)); assert((offset & (align - 1)) == 0); assert((bytes & (align - 1)) == 0); assert(!qiov || bytes == qiov->size); assert((bs->open_flags & BDRV_O_NO_IO) == 0); + max_transfer = QEMU_ALIGN_DOWN(MIN_NON_ZERO(bs->bl.max_transfer, INT_MAX), + align); /* TODO: We would need a per-BDS .supported_read_flags and * potential fallback support, if we ever implement any read flags @@ -1024,7 +1028,7 @@ static int coroutine_fn bdrv_aligned_preadv(BlockDriverState *bs, } } - /* Forward the request to the BlockDriver */ + /* Forward the request to the BlockDriver, possibly fragmenting it */ total_bytes = bdrv_getlength(bs); if (total_bytes < 0) { ret = total_bytes; @@ -1032,26 +1036,35 @@ static int coroutine_fn bdrv_aligned_preadv(BlockDriverState *bs, } max_bytes = ROUND_UP(MAX(0, total_bytes - offset), align); - if (bytes <= max_bytes) { + if (bytes <= max_bytes && bytes <= max_transfer) { ret = bdrv_driver_preadv(bs, offset, bytes, qiov, 0); - } else if (max_bytes > 0) { - QEMUIOVector local_qiov; - - qemu_iovec_init(&local_qiov, qiov->niov); - qemu_iovec_concat(&local_qiov, qiov, 0, max_bytes); - - ret = bdrv_driver_preadv(bs, offset, max_bytes, &local_qiov, 0); - - qemu_iovec_destroy(&local_qiov); - } else { - ret = 0; + goto out; } - /* Reading beyond end of file is supposed to produce zeroes */ - if (ret == 0 && total_bytes < offset + bytes) { - uint64_t zero_offset = MAX(0, total_bytes - offset); - uint64_t zero_bytes = offset + bytes - zero_offset; - qemu_iovec_memset(qiov, zero_offset, 0, zero_bytes); + while (bytes_remaining) { + int num; + + if (max_bytes) { + QEMUIOVector local_qiov; + + num = MIN(bytes_remaining, MIN(max_bytes, max_transfer)); + assert(num); + qemu_iovec_init(&local_qiov, qiov->niov); + qemu_iovec_concat(&local_qiov, qiov, bytes - bytes_remaining, num); + + ret = bdrv_driver_preadv(bs, offset + bytes - bytes_remaining, + num, &local_qiov, 0); + max_bytes -= num; + qemu_iovec_destroy(&local_qiov); + if (ret < 0) { + break; + } + } else { + num = bytes_remaining; + qemu_iovec_memset(qiov, bytes - bytes_remaining, 0, + bytes_remaining); + } + bytes_remaining -= num; } out: